forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
292 lines (261 loc) · 11 KB
/
worker_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package sequencer
import (
"context"
"math/big"
"testing"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
const (
validIP = "10.23.100.1"
)
var (
// Init ZKEVM resourceCostMax values
rcMax = state.BatchConstraintsCfg{
MaxCumulativeGasUsed: 10,
MaxArithmetics: 10,
MaxBinaries: 10,
MaxKeccakHashes: 10,
MaxMemAligns: 10,
MaxPoseidonHashes: 10,
MaxPoseidonPaddings: 10,
MaxSteps: 10,
MaxBatchBytesSize: 10,
}
)
type workerAddTxTestCase struct {
name string
from common.Address
txHash common.Hash
nonce uint64
cost *big.Int
counters state.ZKCounters
usedBytes uint64
gasPrice *big.Int
expectedTxSortedList []common.Hash
ip string
expectedErr error
}
type workerAddrQueueInfo struct {
from common.Address
nonce *big.Int
balance *big.Int
}
func processWorkerAddTxTestCases(ctx context.Context, t *testing.T, worker *Worker, testCases []workerAddTxTestCase) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
tx := TxTracker{}
tx.Hash = testCase.txHash
tx.HashStr = testCase.txHash.String()
tx.From = testCase.from
tx.FromStr = testCase.from.String()
tx.Nonce = testCase.nonce
tx.Cost = testCase.cost
tx.BatchResources.Bytes = testCase.usedBytes
tx.GasPrice = testCase.gasPrice
tx.updateZKCounters(testCase.counters)
if testCase.ip == "" {
// A random valid IP Address
tx.IP = validIP
} else {
tx.IP = testCase.ip
}
t.Logf("%s=%d", testCase.name, tx.GasPrice)
_, err := worker.AddTxTracker(ctx, &tx)
if err != nil && testCase.expectedErr != nil {
assert.ErrorIs(t, err, testCase.expectedErr)
return
}
el := worker.txSortedList
if el.len() != len(testCase.expectedTxSortedList) {
t.Fatalf("Error txSortedList.len(%d) != expectedTxSortedList.len(%d)", el.len(), len(testCase.expectedTxSortedList))
}
for i := 0; i < el.len(); i++ {
if el.getByIndex(i).HashStr != string(testCase.expectedTxSortedList[i].String()) {
t.Fatalf("Error txSortedList(%d). Expected=%s, Actual=%s", i, testCase.expectedTxSortedList[i].String(), el.getByIndex(i).HashStr)
}
}
})
}
}
func TestWorkerAddTx(t *testing.T) {
var nilErr error
stateMock := NewStateMock(t)
worker := initWorker(stateMock, rcMax)
ctx = context.Background()
stateMock.On("GetLastStateRoot", ctx, nil).Return(common.Hash{0}, nilErr)
addrQueueInfo := []workerAddrQueueInfo{
{from: common.Address{1}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{2}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{3}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{4}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
}
for _, aq := range addrQueueInfo {
stateMock.On("GetNonceByStateRoot", ctx, aq.from, common.Hash{0}).Return(aq.nonce, nilErr)
stateMock.On("GetBalanceByStateRoot", ctx, aq.from, common.Hash{0}).Return(aq.balance, nilErr)
}
addTxsTC := []workerAddTxTestCase{
{
name: "Adding from:0x01, tx:0x01/gp:10", from: common.Address{1}, txHash: common.Hash{1}, nonce: 1, gasPrice: new(big.Int).SetInt64(10),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 1, UsedKeccakHashes: 1, UsedPoseidonHashes: 1, UsedPoseidonPaddings: 1, UsedMemAligns: 1, UsedArithmetics: 1, UsedBinaries: 1, UsedSteps: 1},
usedBytes: 1,
expectedTxSortedList: []common.Hash{
{1},
},
},
{
name: "Adding from:0x02, tx:0x02/gp:4", from: common.Address{2}, txHash: common.Hash{2}, nonce: 1, gasPrice: new(big.Int).SetInt64(4),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 1, UsedKeccakHashes: 1, UsedPoseidonHashes: 1, UsedPoseidonPaddings: 1, UsedMemAligns: 1, UsedArithmetics: 1, UsedBinaries: 1, UsedSteps: 1},
usedBytes: 1,
expectedTxSortedList: []common.Hash{
{1}, {2},
},
},
{
name: "Readding from:0x02, tx:0x02/gp:20", from: common.Address{2}, txHash: common.Hash{2}, nonce: 1, gasPrice: new(big.Int).SetInt64(20),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 5, UsedKeccakHashes: 5, UsedPoseidonHashes: 5, UsedPoseidonPaddings: 5, UsedMemAligns: 5, UsedArithmetics: 5, UsedBinaries: 5, UsedSteps: 5},
usedBytes: 5,
expectedTxSortedList: []common.Hash{
{2}, {1},
},
},
{
name: "Readding from:0x03, tx:0x03/gp:25", from: common.Address{3}, txHash: common.Hash{3}, nonce: 1, gasPrice: new(big.Int).SetInt64(25),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 2, UsedKeccakHashes: 2, UsedPoseidonHashes: 2, UsedPoseidonPaddings: 2, UsedMemAligns: 2, UsedArithmetics: 2, UsedBinaries: 2, UsedSteps: 2},
usedBytes: 2,
expectedTxSortedList: []common.Hash{
{3}, {2}, {1},
},
},
{
name: "Invalid IP address", from: common.Address{5}, txHash: common.Hash{5}, nonce: 1,
counters: state.ZKCounters{CumulativeGasUsed: 1, UsedKeccakHashes: 1, UsedPoseidonHashes: 1, UsedPoseidonPaddings: 1, UsedMemAligns: 1, UsedArithmetics: 1, UsedBinaries: 1, UsedSteps: 1},
usedBytes: 1,
ip: "invalid IP",
expectedErr: pool.ErrInvalidIP,
},
{
name: "Out Of Counters Err",
from: common.Address{5}, txHash: common.Hash{5}, nonce: 1,
cost: new(big.Int).SetInt64(5),
// Here, we intentionally set the counters such that they violate the constraints
counters: state.ZKCounters{
CumulativeGasUsed: worker.batchConstraints.MaxCumulativeGasUsed + 1,
UsedKeccakHashes: worker.batchConstraints.MaxKeccakHashes + 1,
UsedPoseidonHashes: worker.batchConstraints.MaxPoseidonHashes + 1,
UsedPoseidonPaddings: worker.batchConstraints.MaxPoseidonPaddings + 1,
UsedMemAligns: worker.batchConstraints.MaxMemAligns + 1,
UsedArithmetics: worker.batchConstraints.MaxArithmetics + 1,
UsedBinaries: worker.batchConstraints.MaxBinaries + 1,
UsedSteps: worker.batchConstraints.MaxSteps + 1,
},
usedBytes: 1,
expectedErr: pool.ErrOutOfCounters,
},
{
name: "Adding from:0x04, tx:0x04/gp:100", from: common.Address{4}, txHash: common.Hash{4}, nonce: 1, gasPrice: new(big.Int).SetInt64(100),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 1, UsedKeccakHashes: 1, UsedPoseidonHashes: 1, UsedPoseidonPaddings: 1, UsedMemAligns: 1, UsedArithmetics: 1, UsedBinaries: 1, UsedSteps: 1},
usedBytes: 1,
expectedTxSortedList: []common.Hash{
{4}, {3}, {2}, {1},
},
},
}
processWorkerAddTxTestCases(ctx, t, worker, addTxsTC)
}
func TestWorkerGetBestTx(t *testing.T) {
var nilErr error
rc := state.BatchResources{
ZKCounters: state.ZKCounters{CumulativeGasUsed: 10, UsedKeccakHashes: 10, UsedPoseidonHashes: 10, UsedPoseidonPaddings: 10, UsedMemAligns: 10, UsedArithmetics: 10, UsedBinaries: 10, UsedSteps: 10},
Bytes: 10,
}
stateMock := NewStateMock(t)
worker := initWorker(stateMock, rcMax)
ctx := context.Background()
stateMock.On("GetLastStateRoot", ctx, nil).Return(common.Hash{0}, nilErr)
addrQueueInfo := []workerAddrQueueInfo{
{from: common.Address{1}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{2}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{3}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
{from: common.Address{4}, nonce: new(big.Int).SetInt64(1), balance: new(big.Int).SetInt64(10)},
}
for _, aq := range addrQueueInfo {
stateMock.On("GetNonceByStateRoot", ctx, aq.from, common.Hash{0}).Return(aq.nonce, nilErr)
stateMock.On("GetBalanceByStateRoot", ctx, aq.from, common.Hash{0}).Return(aq.balance, nilErr)
}
addTxsTC := []workerAddTxTestCase{
{
name: "Adding from:0x01, tx:0x01/gp:10", from: common.Address{1}, txHash: common.Hash{1}, nonce: 1, gasPrice: new(big.Int).SetInt64(10),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 1, UsedKeccakHashes: 1, UsedPoseidonHashes: 1, UsedPoseidonPaddings: 1, UsedMemAligns: 1, UsedArithmetics: 1, UsedBinaries: 1, UsedSteps: 1},
usedBytes: 1,
expectedTxSortedList: []common.Hash{
{1},
},
},
{
name: "Adding from:0x02, tx:0x02/gp:12", from: common.Address{2}, txHash: common.Hash{2}, nonce: 1, gasPrice: new(big.Int).SetInt64(12),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 5, UsedKeccakHashes: 5, UsedPoseidonHashes: 5, UsedPoseidonPaddings: 5, UsedMemAligns: 5, UsedArithmetics: 5, UsedBinaries: 5, UsedSteps: 5},
usedBytes: 5,
expectedTxSortedList: []common.Hash{
{2}, {1},
},
},
{
name: "Readding from:0x03, tx:0x03/gp:25", from: common.Address{3}, txHash: common.Hash{3}, nonce: 1, gasPrice: new(big.Int).SetInt64(25),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 2, UsedKeccakHashes: 2, UsedPoseidonHashes: 2, UsedPoseidonPaddings: 2, UsedMemAligns: 2, UsedArithmetics: 2, UsedBinaries: 2, UsedSteps: 2},
usedBytes: 2,
expectedTxSortedList: []common.Hash{
{3}, {2}, {1},
},
},
{
name: "Adding from:0x04, tx:0x04/gp:100", from: common.Address{4}, txHash: common.Hash{4}, nonce: 1, gasPrice: new(big.Int).SetInt64(100),
cost: new(big.Int).SetInt64(5),
counters: state.ZKCounters{CumulativeGasUsed: 4, UsedKeccakHashes: 4, UsedPoseidonHashes: 4, UsedPoseidonPaddings: 4, UsedMemAligns: 4, UsedArithmetics: 4, UsedBinaries: 4, UsedSteps: 4},
usedBytes: 4,
expectedTxSortedList: []common.Hash{
{4}, {3}, {2}, {1},
},
},
}
processWorkerAddTxTestCases(ctx, t, worker, addTxsTC)
expectedGetBestTx := []common.Hash{{4}, {3}, {1}}
ct := 0
for {
tx := worker.GetBestFittingTx(rc)
if tx != nil {
if ct >= len(expectedGetBestTx) {
t.Fatalf("Error getting more best tx than expected. Expected=%d, Actual=%d", len(expectedGetBestTx), ct+1)
}
if tx.HashStr != string(expectedGetBestTx[ct].String()) {
t.Fatalf("Error GetBestFittingTx(%d). Expected=%s, Actual=%s", ct, expectedGetBestTx[ct].String(), tx.HashStr)
}
err := rc.Sub(tx.BatchResources)
assert.NoError(t, err)
touch := make(map[common.Address]*state.InfoReadWrite)
var newNonce uint64 = tx.Nonce + 1
touch[tx.From] = &state.InfoReadWrite{Address: tx.From, Nonce: &newNonce, Balance: new(big.Int).SetInt64(10)}
worker.UpdateAfterSingleSuccessfulTxExecution(tx.From, touch)
ct++
} else {
if ct < len(expectedGetBestTx) {
t.Fatalf("Error expecting more best tx. Expected=%d, Actual=%d", len(expectedGetBestTx), ct)
}
break
}
}
}
func initWorker(stateMock *StateMock, rcMax state.BatchConstraintsCfg) *Worker {
worker := NewWorker(stateMock, rcMax)
return worker
}