forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_test.go
86 lines (72 loc) · 2.28 KB
/
tree_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package merkletree
import (
"context"
"fmt"
"testing"
"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/test/contracts/bin/EmitLog2"
"github.com/0xPolygonHermez/zkevm-node/test/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestGetCode(t *testing.T) {
ctx := context.Background()
zkProverURI := testutils.GetEnv("ZKPROVER_URI", "localhost")
cfg := Config{URI: fmt.Sprintf("%s:50061", zkProverURI)}
c, _, _ := NewMTDBServiceClient(ctx, cfg)
sTree := NewStateTree(c)
type testCase struct {
name string
addr common.Address
root []byte
expectedResult []byte
expectedError error
setup func(*testing.T, *testCase, *StateTree)
}
testCases := []testCase{
{
name: "get existent code successfully",
addr: common.HexToAddress("0x1"),
root: common.HexToHash("0x0").Bytes(),
expectedResult: hex.DecodeBig(EmitLog2.EmitLog2Bin).Bytes(),
expectedError: nil,
setup: func(t *testing.T, tc *testCase, sTree *StateTree) {
txID := uuid.NewString()
err := sTree.StartBlock(ctx, common.Hash(tc.root), txID)
require.NoError(t, err)
newRoot, _, err := sTree.SetCode(ctx, tc.addr, tc.expectedResult, tc.root, txID)
require.NoError(t, err)
tc.root = newRoot
err = sTree.FinishBlock(ctx, common.Hash(tc.root), txID)
require.NoError(t, err)
err = sTree.Flush(ctx, common.Hash(newRoot), txID)
require.NoError(t, err)
},
},
{
name: "get non-existent code successfully",
addr: common.HexToAddress("0x2"),
root: common.HexToHash("0x0").Bytes(),
expectedResult: []byte{},
expectedError: nil,
setup: func(t *testing.T, tc *testCase, sTree *StateTree) {
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc := tc
tc.setup(t, &tc, sTree)
result, err := sTree.GetCode(ctx, tc.addr, tc.root)
require.NoError(t, err)
if tc.expectedResult != nil || result != nil {
require.Equal(t, len(tc.expectedResult), len(result))
require.ElementsMatch(t, tc.expectedResult, result)
}
if tc.expectedError != nil || err != nil {
require.Equal(t, tc.expectedError, err)
}
})
}
}