Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Implement removal of orphan nodes #37

Merged
2 changes: 1 addition & 1 deletion deepsubtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (dsmst *DeepSparseMerkleSubTree) AddBranch(proof SparseMerkleProof, key []b
}

if valueHash != nil {
if err := dsmst.values.Set(valueKey(key, valueHash), value); err != nil {
if err := dsmst.values.Set(dsmst.SparseMerkleTree.th.path(key), value); err != nil {
roysc marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
Expand Down
18 changes: 6 additions & 12 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ func (smt *SparseMerkleTree) GetForRoot(key []byte, root []byte) ([]byte, error)
return nil, err
} else if smt.th.isLeaf(currentData) {
// We've reached the end. Is this the actual leaf?
p, valueHash := smt.th.parseLeaf(currentData)
p, _ := smt.th.parseLeaf(currentData)
if !bytes.Equal(path, p) {
// Nope. Therefore the key is actually empty.
return defaultValue, nil
}
// Otherwise, yes. Return the value.
value, err := smt.values.Get(valueKey(key, valueHash))
value, err := smt.values.Get(path)
if err != nil {
return nil, err
}
Expand All @@ -114,12 +114,7 @@ func (smt *SparseMerkleTree) GetForRoot(key []byte, root []byte) ([]byte, error)
// The following lines of code should only be reached if the path is 256
// nodes high, which should be very unlikely if the underlying hash function
// is collision-resistant.
currentData, err := smt.nodes.Get(currentHash)
if err != nil {
return nil, err
}
_, valueHash := smt.th.parseLeaf(currentData)
value, err := smt.values.Get(valueKey(key, valueHash))
value, err := smt.values.Get(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -169,8 +164,7 @@ func (smt *SparseMerkleTree) UpdateForRoot(key []byte, value []byte, root []byte
// This key is already empty; return the old root.
return root, nil
}
_, valueHash := smt.th.parseLeaf(oldLeafData)
if err := smt.values.Delete(valueKey(key, valueHash)); err != nil {
if err := smt.values.Delete(path); err != nil {
return nil, err
}

Expand Down Expand Up @@ -297,7 +291,7 @@ func (smt *SparseMerkleTree) updateWithSideNodes(key, path []byte, value []byte,
if err := smt.nodes.Delete(pathNodes[0]); err != nil {
return nil, err
}
if err := smt.values.Delete(valueKey(key, oldValueHash)); err != nil {
if err := smt.values.Delete(path); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -340,7 +334,7 @@ func (smt *SparseMerkleTree) updateWithSideNodes(key, path []byte, value []byte,
}
currentData = currentHash
}
if err := smt.values.Set(valueKey(key, valueHash), value); err != nil {
if err := smt.values.Set(path, value); err != nil {
return nil, err
}

Expand Down
6 changes: 0 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,3 @@ func reverseByteSlices(sideNodes [][]byte) [][]byte {

return sideNodes
}

func valueKey(key, root []byte) []byte {
ret := make([]byte, len(root), len(root)+len(key))
copy(ret, root)
return append(ret, key...)
}