forked from okx/xlayer-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l1infotree_test.go
149 lines (137 loc) · 4.8 KB
/
l1infotree_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package state_test
import (
"context"
"math"
"testing"
"github.com/0xPolygonHermez/zkevm-node/db"
"github.com/0xPolygonHermez/zkevm-node/l1infotree"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/mocks"
"github.com/0xPolygonHermez/zkevm-node/state/pgstatestorage"
"github.com/0xPolygonHermez/zkevm-node/test/dbutils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestFirstLeafOfL1InfoTreeIsIndex0(t *testing.T) {
stateDBCfg := dbutils.NewStateConfigFromEnv()
if err := dbutils.InitOrResetState(stateDBCfg); err != nil {
panic(err)
}
stateDb, err := db.NewSQLDB(stateDBCfg)
if err != nil {
panic(err)
}
forkID := uint64(state.FORKID_ETROG)
stateCfg := state.Config{
MaxCumulativeGasUsed: 800000,
ChainID: 1000,
MaxLogsCount: 10000,
MaxLogsBlockRange: 10000,
ForkIDIntervals: []state.ForkIDInterval{{
FromBatchNumber: 0,
ToBatchNumber: math.MaxUint64,
ForkId: forkID,
Version: "",
}},
}
ctx := context.Background()
storage := pgstatestorage.NewPostgresStorage(stateCfg, stateDb)
mt, err := l1infotree.NewL1InfoTree(32, [][32]byte{})
if err != nil {
panic(err)
}
testState := state.NewState(stateCfg, storage, nil, nil, nil, mt)
dbTx, err := testState.BeginStateTransaction(ctx)
defer func() {
_ = dbTx.Rollback(ctx)
}()
require.NoError(t, err)
block := state.Block{BlockNumber: 123}
err = testState.AddBlock(ctx, &block, dbTx)
require.NoError(t, err)
leaf := state.L1InfoTreeLeaf{
GlobalExitRoot: state.GlobalExitRoot{
GlobalExitRoot: common.Hash{},
BlockNumber: 123,
},
PreviousBlockHash: common.Hash{},
}
insertedLeaf, err := testState.AddL1InfoTreeLeaf(ctx, &leaf, dbTx)
require.NoError(t, err)
require.Equal(t, insertedLeaf.L1InfoTreeIndex, uint32(0))
}
func TestGetCurrentL1InfoRootBuildCacheIfNil(t *testing.T) {
mockStorage := mocks.NewStorageMock(t)
stateCfg := state.Config{
MaxCumulativeGasUsed: 800000,
ChainID: 1000,
MaxLogsCount: 10000,
MaxLogsBlockRange: 10000,
ForkIDIntervals: []state.ForkIDInterval{{
FromBatchNumber: 0,
ToBatchNumber: math.MaxUint64,
ForkId: uint64(state.FORKID_ETROG),
Version: "",
}},
}
ctx := context.Background()
testState := state.NewState(stateCfg, mockStorage, nil, nil, nil, nil)
mockStorage.EXPECT().GetAllL1InfoRootEntries(ctx, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil)
l1InfoRoot, err := testState.GetCurrentL1InfoRoot(ctx, nil)
require.NoError(t, err)
require.Equal(t, l1InfoRoot, common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757"))
}
func TestGetCurrentL1InfoRootNoBuildCacheIfNotNil(t *testing.T) {
mockStorage := mocks.NewStorageMock(t)
stateCfg := state.Config{
MaxCumulativeGasUsed: 800000,
ChainID: 1000,
MaxLogsCount: 10000,
MaxLogsBlockRange: 10000,
ForkIDIntervals: []state.ForkIDInterval{{
FromBatchNumber: 0,
ToBatchNumber: math.MaxUint64,
ForkId: uint64(state.FORKID_ETROG),
Version: "",
}},
}
ctx := context.Background()
l1InfoTree, err := l1infotree.NewL1InfoTree(uint8(32), nil)
require.NoError(t, err)
testState := state.NewState(stateCfg, mockStorage, nil, nil, nil, l1InfoTree)
// GetCurrentL1InfoRoot use the cache value in state.l1InfoTree
l1InfoRoot, err := testState.GetCurrentL1InfoRoot(ctx, nil)
require.NoError(t, err)
require.Equal(t, l1InfoRoot, common.HexToHash("0x27ae5ba08d7291c96c8cbddcc148bf48a6d68c7974b94356f53754ef6171d757"))
}
func TestAddL1InfoTreeLeafIfNil(t *testing.T) {
mockStorage := mocks.NewStorageMock(t)
stateCfg := state.Config{
MaxCumulativeGasUsed: 800000,
ChainID: 1000,
MaxLogsCount: 10000,
MaxLogsBlockRange: 10000,
ForkIDIntervals: []state.ForkIDInterval{{
FromBatchNumber: 0,
ToBatchNumber: math.MaxUint64,
ForkId: uint64(state.FORKID_ETROG),
Version: "",
}},
}
ctx := context.Background()
testState := state.NewState(stateCfg, mockStorage, nil, nil, nil, nil)
mockStorage.EXPECT().GetLatestIndex(ctx, mock.Anything).Return(uint32(0), state.ErrNotFound)
mockStorage.EXPECT().AddL1InfoRootToExitRoot(ctx, mock.Anything, mock.Anything).Return(nil)
// This call is for rebuild cache
mockStorage.EXPECT().GetAllL1InfoRootEntries(ctx, nil).Return([]state.L1InfoTreeExitRootStorageEntry{}, nil)
leaf := state.L1InfoTreeLeaf{
GlobalExitRoot: state.GlobalExitRoot{
GlobalExitRoot: common.Hash{},
},
}
addLeaf, err := testState.AddL1InfoTreeLeaf(ctx, &leaf, nil)
require.NoError(t, err)
require.Equal(t, addLeaf.L1InfoTreeRoot, common.HexToHash("0xea536769cad1a63ffb1ea52ae772983905c3f0e2f8914e6c0e2af956637e480c"))
require.Equal(t, addLeaf.L1InfoTreeIndex, uint32(0))
}