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

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

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
11 changes: 3 additions & 8 deletions trie/zk_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 16 additions & 4 deletions trie/zk_trie_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions trie/zk_trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 1 addition & 16 deletions trie/zktrie_deletionproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down
Loading