feat: upgrade to zktrie 0.8.4 and fix some issues (#820)

* feat(zktrie): bump to `v0.8.4`

* fix(zktrie): copy trie properly for concurrent access (#747)

* fix `ZkTrie` `Commit`

* some fixes

---------

Co-authored-by: Ömer Faruk Irmak <omerfirmak@gmail.com>
This commit is contained in:
HAOYUatHZ 2024-06-13 19:38:53 +08:00 committed by GitHub
parent 1f58af2d5b
commit 1ca3b74ec1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 24 additions and 33 deletions

2
go.mod
View file

@ -57,7 +57,7 @@ require (
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7 github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7
github.com/rs/cors v1.7.0 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/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/status-im/keycard-go v0.2.0 github.com/status-im/keycard-go v0.2.0
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4

4
go.sum
View file

@ -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/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/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/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.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7IgRE=
github.com/scroll-tech/zktrie v0.6.0/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk= 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/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 h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=

View file

@ -162,15 +162,10 @@ func (t *ZkTrie) GetKey(kHashBytes []byte) []byte {
// //
// Committing flushes nodes from memory. Subsequent Get calls will load nodes // Committing flushes nodes from memory. Subsequent Get calls will load nodes
// from the database. // 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) { func (t *ZkTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
// in current implmentation, every update of trie already writes into database if err := t.ZkTrie.Commit(); err != nil {
// so Commmit does nothing return common.Hash{}, nil, err
}
return t.Hash(), nil, nil return t.Hash(), nil, nil
} }

View file

@ -80,7 +80,11 @@ func TestSMTOneElementProof(t *testing.T) {
if proof.Len() != 2 { if proof.Len() != 2 {
t.Errorf("prover %d: proof should have 1+1 element (including the magic kv)", i) 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 { if err != nil {
t.Fatalf("prover %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) 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) { func TestSMTProof(t *testing.T) {
mt, vals := randomZktrie(t, 500) 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 i, prover := range makeSMTProvers(mt) {
for _, kv := range vals { for _, kv := range vals {
proof := prover(kv.k) proof := prover(kv.k)
@ -112,7 +118,9 @@ func TestSMTProof(t *testing.T) {
func TestSMTBadProof(t *testing.T) { func TestSMTBadProof(t *testing.T) {
mt, vals := randomZktrie(t, 500) 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 i, prover := range makeSMTProvers(mt) {
for _, kv := range vals { for _, kv := range vals {
proof := prover(kv.k) proof := prover(kv.k)
@ -158,7 +166,11 @@ func TestSMTMissingKeyProof(t *testing.T) {
if proof.Len() != 2 { if proof.Len() != 2 {
t.Errorf("test %d: proof should have 2 element (with magic kv)", i) 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 { if err != nil {
t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof)
} }

View file

@ -129,8 +129,7 @@ func TestZkTrieConcurrency(t *testing.T) {
threads := runtime.NumCPU() threads := runtime.NumCPU()
tries := make([]*ZkTrie, threads) tries := make([]*ZkTrie, threads)
for i := 0; i < threads; i++ { for i := 0; i < threads; i++ {
cpy := *trie tries[i] = trie.Copy()
tries[i] = &cpy
} }
// Start a batch of goroutines interactng with the trie // Start a batch of goroutines interactng with the trie
pend := new(sync.WaitGroup) pend := new(sync.WaitGroup)

View file

@ -10,21 +10,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "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 { type ProofTracer struct {
*ZkTrie *ZkTrie
deletionTracer map[zkt.Hash]struct{} deletionTracer map[zkt.Hash]struct{}
@ -95,7 +80,7 @@ func (t *ProofTracer) GetDeletionProofs() ([][]byte, error) {
siblingHash = n.ChildL siblingHash = n.ChildL
} }
if siblingHash != nil { if siblingHash != nil {
sibling, err := t.TryGetNode(siblingHash) sibling, err := t.ZkTrie.Tree().GetNode(siblingHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }