Skip to content

Commit

Permalink
fix(trie): modify to prevent errors when updating with non-existent k…
Browse files Browse the repository at this point in the history
…eys (#96)
  • Loading branch information
jyc228 authored Apr 11, 2024
1 parent 305d159 commit 95927ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions trie/merkle_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ type MerkleStateTrie interface {
Hash() common.Hash
GetNode(path []byte) ([]byte, int, error)
MustGet(key []byte) []byte
GetAccount(address common.Address) (*types.StateAccount, error)
GetAccountByHash(addrHash common.Hash) (*types.StateAccount, error)
UpdateAccount(address common.Address, account *types.StateAccount) error
DeleteAccount(address common.Address) error
MustUpdate(key, value []byte)
GetStorage(addr common.Address, key []byte) ([]byte, error)
DeleteStorage(address common.Address, key []byte) error
MustDelete(key []byte)
MustNodeIterator(start []byte) NodeIterator
Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
Expand Down
15 changes: 13 additions & 2 deletions trie/zk_merkle_state_trie.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package trie

import (
"errors"

"github.com/kroma-network/zktrie/trie"
zkt "github.com/kroma-network/zktrie/types"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -92,8 +95,16 @@ func (z *ZkMerkleStateTrie) UpdateContractCode(_ common.Address, _ common.Hash,
return nil
}

func (z *ZkMerkleStateTrie) DeleteStorage(_ common.Address, key []byte) error { return z.Delete(key) }
func (z *ZkMerkleStateTrie) DeleteAccount(address common.Address) error { return z.Delete(address[:]) }
func (z *ZkMerkleStateTrie) DeleteStorage(_ common.Address, key []byte) error { return z.delete(key) }
func (z *ZkMerkleStateTrie) DeleteAccount(address common.Address) error { return z.delete(address[:]) }

func (z *ZkMerkleStateTrie) delete(key []byte) error {
if err := z.Delete(key); err != nil && errors.Is(err, trie.ErrKeyNotFound) {
return nil
} else {
return err
}
}

func (z *ZkMerkleStateTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
return z.prove(common.ReverseBytes(key), proofDb, func(node zk.TreeNode) error {
Expand Down
19 changes: 19 additions & 0 deletions trie/zk_merkle_trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ import (
"github.com/ethereum/go-ethereum/trie/zk"
)

func TestZkTrieNoExistKey(t *testing.T) {
testMerkleStateTrieNoExistKey(t, newEmptyZkTrie())
testMerkleStateTrieNoExistKey(t, NewEmptyZkMerkleStateTrie(NewZkDatabase(rawdb.NewMemoryDatabase())))
}

func testMerkleStateTrieNoExistKey(t *testing.T, trie MerkleStateTrie) {
address := common.HexToAddress("0xffffffffffffffffffffffffffffffffffffffff")
if acc, err := trie.GetAccount(address); acc != nil || err != nil {
t.Errorf("GetAccount return acc : %v, err : %v", acc, err)
}
assert.NoError(t, trie.DeleteAccount(address))

storageKey := common.LeftPadBytes([]byte{1}, 20)
if val, err := trie.GetStorage(address, storageKey); len(val) > 0 || err != nil {
t.Errorf("GetStorage return acc : %v, err : %v", val, err)
}
assert.NoError(t, trie.DeleteStorage(address, storageKey))
}

func TestProve(t *testing.T) {
t.Run("without preimage", func(t *testing.T) {
scrolldb, kromadb := NewZkDatabase(rawdb.NewMemoryDatabase()), NewZkDatabase(rawdb.NewMemoryDatabase())
Expand Down

0 comments on commit 95927ab

Please sign in to comment.