Skip to content

Commit

Permalink
Fix hyper search tests
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Díaz <[email protected]>
  • Loading branch information
aalda and gdiazlo committed Feb 25, 2019
1 parent ab1d01d commit ad84405
Show file tree
Hide file tree
Showing 16 changed files with 1,246 additions and 443 deletions.
31 changes: 23 additions & 8 deletions balloon/hyper2/navigation/audit.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package navigation

import (
"github.com/bbva/qed/balloon/navigator"
"github.com/bbva/qed/hashing"
)

type AuditPath []hashing.Digest
type AuditPath map[string]hashing.Digest

func NewAuditPath() AuditPath {
return make(AuditPath, 0)
func (p AuditPath) Get(pos navigator.Position) (hashing.Digest, bool) {
digest, ok := p[pos.StringId()]
return digest, ok
}

func (p AuditPath) Get(index int) (hashing.Digest, bool) {
if index >= len(p) {
return nil, false
}
return p[index], true
func NewAuditPath() AuditPath {
return make(AuditPath, 0)
}
5 changes: 4 additions & 1 deletion balloon/hyper2/navigation/position.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -58,7 +61,7 @@ func (p Position) String() string {
}

func (p Position) StringId() string {
return fmt.Sprintf("%x|%d", p.Index, p.Height)
return fmt.Sprintf("%#x|%d", p.Index, p.Height)
}

func (p Position) Left() Position {
Expand Down
3 changes: 3 additions & 0 deletions balloon/hyper2/navigation/test_util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
16 changes: 16 additions & 0 deletions balloon/hyper2/proof.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hyper2

import (
Expand Down
18 changes: 17 additions & 1 deletion balloon/hyper2/pruning2/batch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pruning2

import (
Expand Down Expand Up @@ -35,7 +51,7 @@ func (b BatchNode) String() string {
}

func (b BatchNode) HasLeafAt(i int8) bool {
return b.Batch[i][b.nodeSize] == byte(1)
return len(b.Batch[i]) > 0 && b.Batch[i][b.nodeSize] == byte(1)
}

func (b BatchNode) AddHashAt(i int8, value []byte) {
Expand Down
69 changes: 38 additions & 31 deletions balloon/hyper2/pruning2/insert.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pruning2

import (
Expand Down Expand Up @@ -50,11 +66,9 @@ func PruneToInsert(index []byte, value []byte, cacheHeightLimit uint16, batches
var traverse, traverseThroughCache, traverseAfterCache TraverseBatch

traverse = func(pos navigation.Position, leaves Leaves, batch *BatchNode, iBatch int8, ops *OperationsStack) {

if batch == nil {
batch = batches.Load(pos)
}

if pos.Height > cacheHeightLimit {
traverseThroughCache(pos, leaves, batch, iBatch, ops)
} else {
Expand Down Expand Up @@ -82,19 +96,16 @@ func PruneToInsert(index []byte, value []byte, cacheHeightLimit uint16, batches

// on an internal node of the subtree

// we found a node in our path
if batch.HasElementAt(iBatch) {
// we found a shortcut leaf in our path
if batch.HasLeafAt(iBatch) {
// push down leaf
key, value := batch.GetLeafKVAt(iBatch)
leaves = leaves.InsertSorted(Leaf{key, value})
batch.ResetElementAt(iBatch)
batch.ResetElementAt(2*iBatch + 1)
batch.ResetElementAt(2*iBatch + 2)
traverseThroughCache(pos, leaves, batch, iBatch, ops)
return
}
// we found a node in our path and it is a shortcut leaf
if batch.HasLeafAt(iBatch) {
// push down leaf
key, value := batch.GetLeafKVAt(iBatch)
leaves = leaves.InsertSorted(Leaf{key, value})
batch.ResetElementAt(iBatch)
batch.ResetElementAt(2*iBatch + 1)
batch.ResetElementAt(2*iBatch + 2)
traverseThroughCache(pos, leaves, batch, iBatch, ops)
return
}

// on an internal node with more than one leaf
Expand Down Expand Up @@ -148,9 +159,8 @@ func PruneToInsert(index []byte, value []byte, cacheHeightLimit uint16, batches
ops.Push(updateBatchNode(pos, iBatch, batch))
return
}
// with only one leaf to insert -> add a new shortcut leaf or continue traversing
// with only one leaf to insert -> continue traversing
if batch.HasElementAt(iBatch) {
// continue traversing
traverse(pos, leaves, nil, 0, ops)
ops.Push(updateBatchNode(pos, iBatch, batch))
return
Expand All @@ -175,25 +185,22 @@ func PruneToInsert(index []byte, value []byte, cacheHeightLimit uint16, batches
leafHash(pos, leaves[0].Value),
updateBatchShortcut(pos, iBatch, batch, leaves[0].Index, leaves[0].Value),
)
if pos.Height%4 == 0 { // at the root or at a leaf of the subtree
if pos.Height%4 == 0 { // at the root or at a leaf of the subtree (not necessary to check iBatch)
ops.Push(mutateBatch(pos, batch))
}
return
}

// we found a node in our path
if batch.HasElementAt(iBatch) {
// we found a shortcut leaf in our path
if batch.HasLeafAt(iBatch) {
// push down leaf
key, value := batch.GetLeafKVAt(iBatch)
leaves = leaves.InsertSorted(Leaf{key, value})
batch.ResetElementAt(iBatch)
batch.ResetElementAt(2*iBatch + 1)
batch.ResetElementAt(2*iBatch + 2)
traverseAfterCache(pos, leaves, batch, iBatch, ops)
return
}
// we found a node in our path and itis a shortcut leaf
if batch.HasLeafAt(iBatch) {
// push down leaf
key, value := batch.GetLeafKVAt(iBatch)
leaves = leaves.InsertSorted(Leaf{key, value})
batch.ResetElementAt(iBatch)
batch.ResetElementAt(2*iBatch + 1)
batch.ResetElementAt(2*iBatch + 2)
traverseAfterCache(pos, leaves, batch, iBatch, ops)
return
}
}

Expand Down
Loading

0 comments on commit ad84405

Please sign in to comment.