Skip to content

Commit

Permalink
fix(trie): prevent save root hash twice
Browse files Browse the repository at this point in the history
  • Loading branch information
jyc228 committed Jan 17, 2024
1 parent 17c2eed commit 77a3b33
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
4 changes: 2 additions & 2 deletions trie/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,10 @@ func TestMerkleTreeIterator(t *testing.T) {

t.Run("zk merkle tree", func(t *testing.T) {
tree, db := makeMerkleTreeWithData(testdata1)
expected := db.Len() - 1
expected := db.Len()
it, _ := tree.NodeIterator(nil)
count, leafCount := testIterator(t, db, it)
if db.Len()-1 != 0 {
if db.Len() != 0 {
t.Errorf("db is not empty. remain size %d", db.Len())
}
if expected != count {
Expand Down
3 changes: 2 additions & 1 deletion trie/triedb/hashdb/zktrie_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/VictoriaMetrics/fastcache"
zktrie "github.com/kroma-network/zktrie/trie"
zkt "github.com/kroma-network/zktrie/types"
"github.com/syndtr/goleveldb/leveldb"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -47,7 +48,7 @@ func NewZk(diskdb ethdb.Database, config *Config) *ZktrieDatabase {
func (db *ZktrieDatabase) Scheme() string { return rawdb.HashScheme }

func (db *ZktrieDatabase) Initialized(genesisRoot common.Hash) bool {
return rawdb.HasLegacyTrieNode(db.diskdb, genesisRoot)
return rawdb.HasLegacyTrieNode(db.diskdb, common.BytesToHash(zkt.ReverseByteOrder(genesisRoot[:])))
}

func (db *ZktrieDatabase) Size() (common.StorageSize, common.StorageSize) {
Expand Down
7 changes: 3 additions & 4 deletions trie/zk_merkle_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
zktrie "github.com/kroma-network/zktrie/trie"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/trie/trienode"
Expand Down Expand Up @@ -40,13 +39,13 @@ func (z *ZkMerkleTrie) NodeIterator(startKey []byte) (NodeIterator, error) {
}

func (z *ZkMerkleTrie) Commit(_ bool) (common.Hash, *trienode.NodeSet, error) {
if root := z.RootNode().Hash(); root != nil {
return common.BytesToHash(root.Bytes()), nil, nil
}
err := z.ComputeAllNodeHash(func(node zk.TreeNode) error { return z.db.Put(node.Hash()[:], node.CanonicalValue()) })
if err != nil {
log.Error("Failed to commit zk merkle trie", "err", err)
}
// There is a bug where root node is saved twice.
// It is because of the bottom rawdb.WriteLegacyTrieNode, and we will remove it after checking if it can be removed.
rawdb.WriteLegacyTrieNode(z.db.diskdb, common.BytesToHash(z.RootNode().Hash().Bytes()), z.RootNode().CanonicalValue())
// Since NodeSet relies directly on mpt, we can't create a NodeSet.
// Of course, we might be able to force-fit it by implementing the node interface.
// However, NodeSet has been improved in geth, it could be improved to return a NodeSet when a commit is applied.
Expand Down
7 changes: 0 additions & 7 deletions trie/zk_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
zkt "github.com/kroma-network/zktrie/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/poseidon"
"github.com/ethereum/go-ethereum/ethdb"
Expand Down Expand Up @@ -171,12 +170,6 @@ func (t *ZkTrie) GetKey(kHashBytes []byte) []byte {
func (t *ZkTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error) {
// in current implementation, every update of trie already writes into database
// so Commit does nothing
node, err := t.Tree().GetNode(t.Tree().Root())
if err != nil {
return types.GetEmptyRootHash(true), nil, err
}

rawdb.WriteLegacyTrieNode(t.db.diskdb, t.Hash(), node.Value())
return t.Hash(), nil, nil
}

Expand Down

0 comments on commit 77a3b33

Please sign in to comment.