From 9336306d46ecc730955772d4263ba26b8872039e Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Thu, 6 Jun 2024 07:31:45 +0800 Subject: [PATCH 1/4] feat(zktrie): bump to `v0.8.4` --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 67b7d8211d53..e867627b40df 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,7 @@ require ( github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7 github.com/rs/cors v1.7.0 - github.com/scroll-tech/zktrie v0.6.0 + github.com/scroll-tech/zktrie v0.8.4 github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible github.com/status-im/keycard-go v0.2.0 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 4a8641f70a96..d82a5c8590d5 100644 --- a/go.sum +++ b/go.sum @@ -543,8 +543,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZKJ48R1S7H23Ji7oFO5Bw= -github.com/scroll-tech/zktrie v0.6.0 h1:xLrMAO31Yo2BiPg1jtYKzcjpEFnXy8acbB7iIsyshPs= -github.com/scroll-tech/zktrie v0.6.0/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk= +github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE= +github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= From 3e49e68887ccc168b0414c87d71321432b262b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Irmak?= Date: Mon, 13 May 2024 15:57:14 +0300 Subject: [PATCH 2/4] fix(zktrie): copy trie properly for concurrent access (#747) --- trie/zk_trie_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/trie/zk_trie_test.go b/trie/zk_trie_test.go index 7675df013f2d..254793365ec4 100644 --- a/trie/zk_trie_test.go +++ b/trie/zk_trie_test.go @@ -129,8 +129,7 @@ func TestZkTrieConcurrency(t *testing.T) { threads := runtime.NumCPU() tries := make([]*ZkTrie, threads) for i := 0; i < threads; i++ { - cpy := *trie - tries[i] = &cpy + tries[i] = trie.Copy() } // Start a batch of goroutines interactng with the trie pend := new(sync.WaitGroup) From 4ed089266f605541f588d81ec6ec78837455a1e2 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Tue, 11 Jun 2024 22:17:16 +0800 Subject: [PATCH 3/4] fix `ZkTrie` `Commit` --- trie/zk_trie.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/trie/zk_trie.go b/trie/zk_trie.go index 4c56dcdef5e9..a66f315ca73d 100644 --- a/trie/zk_trie.go +++ b/trie/zk_trie.go @@ -162,15 +162,10 @@ func (t *ZkTrie) GetKey(kHashBytes []byte) []byte { // // Committing flushes nodes from memory. Subsequent Get calls will load nodes // from the database. -// -// func (t *ZkTrie) Commit(LeafCallback) (common.Hash, int, error) { -// // in current implmentation, every update of trie already writes into database -// // so Commmit does nothing -// return t.Hash(), 0, nil -// } func (t *ZkTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) { - // in current implmentation, every update of trie already writes into database - // so Commmit does nothing + if err := t.ZkTrie.Commit(); err != nil { + return common.Hash{}, nil, err + } return t.Hash(), nil, nil } From fe77a2e14c122d7089286e8b4f415f8ecc3e396a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Irmak?= Date: Wed, 24 Apr 2024 06:31:56 +0300 Subject: [PATCH 4/4] some fixes --- trie/zk_trie_proof_test.go | 20 ++++++++++++++++---- trie/zktrie_deletionproof.go | 17 +---------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/trie/zk_trie_proof_test.go b/trie/zk_trie_proof_test.go index c11b4a8d21c1..1313624bcbac 100644 --- a/trie/zk_trie_proof_test.go +++ b/trie/zk_trie_proof_test.go @@ -80,7 +80,11 @@ func TestSMTOneElementProof(t *testing.T) { if proof.Len() != 2 { t.Errorf("prover %d: proof should have 1+1 element (including the magic kv)", i) } - val, err := VerifyProof(common.BytesToHash(mt.Root().Bytes()), keyBytes, proof) + + root, err := mt.Root() + assert.NoError(t, err) + + val, err := VerifyProof(common.BytesToHash(root.Bytes()), keyBytes, proof) if err != nil { t.Fatalf("prover %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) } @@ -92,7 +96,9 @@ func TestSMTOneElementProof(t *testing.T) { func TestSMTProof(t *testing.T) { mt, vals := randomZktrie(t, 500) - root := mt.Tree().Root() + root, err := mt.Tree().Root() + assert.NoError(t, err) + for i, prover := range makeSMTProvers(mt) { for _, kv := range vals { proof := prover(kv.k) @@ -112,7 +118,9 @@ func TestSMTProof(t *testing.T) { func TestSMTBadProof(t *testing.T) { mt, vals := randomZktrie(t, 500) - root := mt.Tree().Root() + root, err := mt.Tree().Root() + assert.NoError(t, err) + for i, prover := range makeSMTProvers(mt) { for _, kv := range vals { proof := prover(kv.k) @@ -158,7 +166,11 @@ func TestSMTMissingKeyProof(t *testing.T) { if proof.Len() != 2 { t.Errorf("test %d: proof should have 2 element (with magic kv)", i) } - val, err := VerifyProof(common.BytesToHash(mt.Root().Bytes()), keyBytes, proof) + + root, err := mt.Root() + assert.NoError(t, err) + + val, err := VerifyProof(common.BytesToHash(root.Bytes()), keyBytes, proof) if err != nil { t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) } diff --git a/trie/zktrie_deletionproof.go b/trie/zktrie_deletionproof.go index 5a024152db56..b2a472c4eb0f 100644 --- a/trie/zktrie_deletionproof.go +++ b/trie/zktrie_deletionproof.go @@ -10,21 +10,6 @@ import ( "github.com/ethereum/go-ethereum/ethdb" ) -// Pick Node from its hash directly from database, notice it has different -// interface with the function of same name in `trie` -func (t *ZkTrie) TryGetNode(nodeHash *zkt.Hash) (*zktrie.Node, error) { - if bytes.Equal(nodeHash[:], zkt.HashZero[:]) { - return zktrie.NewEmptyNode(), nil - } - nBytes, err := t.db.Get(nodeHash[:]) - if err == zktrie.ErrKeyNotFound { - return nil, zktrie.ErrKeyNotFound - } else if err != nil { - return nil, err - } - return zktrie.NewNodeFromBytes(nBytes) -} - type ProofTracer struct { *ZkTrie deletionTracer map[zkt.Hash]struct{} @@ -95,7 +80,7 @@ func (t *ProofTracer) GetDeletionProofs() ([][]byte, error) { siblingHash = n.ChildL } if siblingHash != nil { - sibling, err := t.TryGetNode(siblingHash) + sibling, err := t.ZkTrie.Tree().GetNode(siblingHash) if err != nil { return nil, err }