diff --git a/balloon/hyper2/pruning/batch.go b/balloon/hyper2/pruning/batch.go index 43f6c76d4..10f0e758c 100644 --- a/balloon/hyper2/pruning/batch.go +++ b/balloon/hyper2/pruning/batch.go @@ -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") } diff --git a/balloon/hyper2/pruning/visitor.go b/balloon/hyper2/pruning/visitor.go index b8f5643bc..5513e807b 100644 --- a/balloon/hyper2/pruning/visitor.go +++ b/balloon/hyper2/pruning/visitor.go @@ -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 } @@ -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 } diff --git a/balloon/hyper2/tree.go b/balloon/hyper2/tree.go index 5566f48e2..91e6865a3 100644 --- a/balloon/hyper2/tree.go +++ b/balloon/hyper2/tree.go @@ -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, @@ -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 { @@ -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 +} diff --git a/balloon/hyper2/tree_test.go b/balloon/hyper2/tree_test.go index 5b1f26203..bfc829160 100644 --- a/balloon/hyper2/tree_test.go +++ b/balloon/hyper2/tree_test.go @@ -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) }