Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nullify the leaf node instead of removing #24

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ test:

# Linter
lint:
golangci-lint --config .golangci.yml run
golangci-lint --config .golangci.yml run

fix-lint:
# Fix linter
golangci-lint --config .golangci.yml run --fix
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)
}
}
31 changes: 31 additions & 0 deletions merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,37 @@ func (mt *MerkleTree) rmAndUpload(ctx context.Context, path []bool, kHash *Hash,
return err
}
}

//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
Loading