Skip to content

Commit

Permalink
Fix version bytes length
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 3855655 commit 3bf8fae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion balloon/hyper2/pruning/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewBatchNode(nodeSize int, batch [][]byte) *BatchNode {
func (b BatchNode) String() string {
var strs []string
for i, n := range b.Batch {
strs = append(strs, fmt.Sprintf("[%d - %x]", i, n))
strs = append(strs, fmt.Sprintf("[%d - %#x]", i, n))
}
return strings.Join(strs, "\n")
}
Expand Down
13 changes: 11 additions & 2 deletions balloon/hyper2/pruning/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ func (v InsertVisitor) Result() []*storage.Mutation {

func (v *InsertVisitor) VisitShortcutLeafOp(op ShortcutLeafOp) hashing.Digest {
hash := v.hasher.Salted(op.Position().Bytes(), op.Value)
// fmt.Printf("Shortcut hash(%v): %x\n", op.Position(), hash)
op.Batch.AddLeafAt(op.Idx, hash, op.Key, op.Value)
return hash
}

func (v *InsertVisitor) VisitLeafOp(op LeafOp) hashing.Digest {
hash := op.Operation.Accept(v)
// fmt.Printf("Leaf hash(%v): %x\n", op.Position(), hash)
op.Batch.AddHashAt(op.Idx, hash)
return hash
}
Expand All @@ -43,27 +45,34 @@ func (v *InsertVisitor) VisitInnerHashOp(op InnerHashOp) hashing.Digest {
leftHash := op.Left.Accept(v)
rightHash := op.Right.Accept(v)
hash := v.hasher.Salted(op.Position().Bytes(), leftHash, rightHash)
// fmt.Printf("Inner hash(%v): %x, %x => %x\n", op.Position(), leftHash, rightHash, hash)
op.Batch.AddHashAt(op.Idx, hash)
return hash
}

func (v *InsertVisitor) VisitGetDefaultOp(op GetDefaultOp) hashing.Digest {
return v.defaultHashes[op.Position().Height]
hash := v.defaultHashes[op.Position().Height]
// fmt.Printf("Default hash(%v): %x\n", op.Position(), hash)
return hash
}

func (v *InsertVisitor) VisitUseProvidedOp(op UseProvidedOp) hashing.Digest {
return op.Batch.GetElementAt(op.Idx)
hash := op.Batch.GetElementAt(op.Idx)
// fmt.Printf("Provided hash(%v): %x\n", op.Position(), hash)
return hash
}

func (v *InsertVisitor) VisitPutBatchOp(op PutBatchOp) hashing.Digest {
hash := op.Operation.Accept(v)
v.cache.Put(op.Position().Bytes(), op.Batch.Serialize())
// fmt.Printf("Put cache hash(%v) [%d]: %x\n", op.Position(), op.Batch.Serialize(), hash)
return hash
}

func (v *InsertVisitor) VisitMutateBatchOp(op MutateBatchOp) hashing.Digest {
hash := op.Operation.Accept(v)
v.mutations = append(v.mutations, storage.NewMutation(storage.HyperCachePrefix, op.Position().Bytes(), op.Batch.Serialize()))
// fmt.Printf("Mutate hash(%v) [%d]: %x\n", op.Position(), op.Batch.Serialize(), hash)
return hash
}

Expand Down
10 changes: 9 additions & 1 deletion balloon/hyper2/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewHyperTree(hasherF func() hashing.Hasher, store storage.Store, cache cach

hasher := hasherF()
numBits := hasher.Len()
cacheHeightLimit := numBits - 24 // TODO change this!!!
cacheHeightLimit := numBits - min(24, numBits/8*4)

tree := &HyperTree{
store: store,
Expand Down Expand Up @@ -58,6 +58,7 @@ func (t *HyperTree) Add(eventDigest hashing.Digest, version uint64) (hashing.Dig

// build a visitable pruned tree and then visit it to generate the root hash
versionAsBytes := util.Uint64AsPaddedBytes(version, len(eventDigest))
versionAsBytes = versionAsBytes[len(versionAsBytes)-len(eventDigest):]
visitor := pruning.NewInsertVisitor(t.hasher, t.cache, t.defaultHashes)
op, err := pruning.PruneToInsert(eventDigest, versionAsBytes, t.cacheHeightLimit, t.batchLoader)
if err != nil {
Expand All @@ -68,3 +69,10 @@ func (t *HyperTree) Add(eventDigest hashing.Digest, version uint64) (hashing.Dig

return rh, visitor.Result(), nil
}

func min(x, y uint16) uint16 {
if x < y {
return x
}
return y
}
6 changes: 3 additions & 3 deletions balloon/hyper2/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func TestAdd(t *testing.T) {
tree := NewHyperTree(hashing.NewFakeXorHasher, store, cache.NewSimpleCache(10))

for i, c := range testCases {
index := uint64(i)
rootHash, mutations, err := tree.Add(c.eventDigest, index)
require.NoErrorf(t, err, "This should not fail for index %d", i)
version := uint64(i)
rootHash, mutations, err := tree.Add(c.eventDigest, version)
require.NoErrorf(t, err, "This should not fail for version %d", i)
tree.store.Mutate(mutations)
assert.Equalf(t, c.expectedRootHash, rootHash, "Incorrect root hash for index %d", i)
}
Expand Down

0 comments on commit 3bf8fae

Please sign in to comment.