Skip to content

Commit

Permalink
nullify the leaf node instead of removing
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-korotya committed Apr 16, 2024
1 parent d140a68 commit 64c7858
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
4 changes: 1 addition & 3 deletions db/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
)

func TestMemoryStorageInterface(t *testing.T) {
var db merkletree.Storage //nolint:gosimple

db = NewMemoryStorage()
db := NewMemoryStorage()
require.NotNil(t, db)
}

Expand Down
24 changes: 24 additions & 0 deletions db/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func TestAll(t *testing.T, sb StorageBuilder) {
t.Run("TestTypesMarshalers", func(t *testing.T) {
TestTypesMarshalers(t, sb.NewStorage(t))
})
t.Run("TestRemoveLeafNearMiddleNode", func(t *testing.T) {
TestRemoveLeafNearMiddleNode(t, sb.NewStorage(t))
})
}

// TestReturnKnownErrIfNotExists checks that the implementation of the
Expand Down Expand Up @@ -927,3 +930,24 @@ func TestTypesMarshalers(t *testing.T, sto merkletree.Storage) {
assert.Nil(t, err)
assert.Equal(t, cpp, cpp2)
}

func TestRemoveLeafNearMiddleNode(t *testing.T, sto merkletree.Storage) {
ctx := context.Background()
mt, err := merkletree.NewMerkleTree(ctx, sto, 140)
require.Nil(t, err)

values := []*big.Int{big.NewInt(7), big.NewInt(1), big.NewInt(5)}

for _, v := range values {
err = mt.Add(ctx, v, v)
require.NoError(t, err)
}

for _, v := range values {
err = mt.Delete(ctx, v)
require.NoError(t, err)
existProof, _, err := mt.GenerateProof(ctx, v, mt.Root())
require.NoError(t, err)
require.False(t, existProof.Existence)
}
}
33 changes: 33 additions & 0 deletions merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,39 @@ func (mt *MerkleTree) rmAndUpload(ctx context.Context, path []bool, kHash *Hash,
return err
}
}

/*

Check failure on line 562 in merkletree.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)

Check failure on line 562 in merkletree.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)
When deleting a leaf node that is on the same level as middleNode,
need to nullify the leaf node instead of removing it from the tree.
*/
nearestSibling, err := mt.db.Get(ctx, toUpload[:])
if err != nil {
return err
}
if nearestSibling.Type == NodeTypeMiddle {
var newNode *Node
if path[len(siblings)-1] {
newNode = NewNodeMiddle(toUpload, &HashZero)
} else {
newNode = NewNodeMiddle(&HashZero, toUpload)
}
_, err = mt.addNode(ctx, newNode)
if err != nil {
return err
}
newRootKey, err := mt.recalculatePathUntilRoot(path, newNode,
siblings[:len(siblings)-1])
if err != nil {
return err
}
mt.rootKey = newRootKey
err = mt.db.SetRoot(ctx, mt.rootKey)
if err != nil {
return err
}
return nil
}

for i := len(siblings) - 2; i >= 0; i-- {
if !bytes.Equal(siblings[i][:], HashZero[:]) {
var newNode *Node
Expand Down

0 comments on commit 64c7858

Please sign in to comment.