hermez-node/db/statedb/txprocessors_test.go
db/statedb/txprocessors_test.goRaw
1package statedb
2
3import (
4 "io/ioutil"
5 "math/big"
6 "os"
7 "testing"
8
9 ethCommon "github.com/ethereum/go-ethereum/common"
10 "github.com/hermeznetwork/hermez-node/common"
11 "github.com/hermeznetwork/hermez-node/log"
12 "github.com/hermeznetwork/hermez-node/test/til"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
16
17func checkBalance(t *testing.T, tc *til.Context, sdb *StateDB, username string, tokenid int, expected string) {
18 idx := tc.Users[username].Accounts[common.TokenID(tokenid)].Idx
19 acc, err := sdb.GetAccount(idx)
20 require.Nil(t, err)
21 assert.Equal(t, expected, acc.Balance.String())
22}
23
24func TestCheckL1TxInvalidData(t *testing.T) {
25 dir, err := ioutil.TempDir("", "tmpdb")
26 require.Nil(t, err)
27 defer assert.Nil(t, os.RemoveAll(dir))
28
29 sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
30 assert.Nil(t, err)
31
32 set := `
33 Type: Blockchain
34 AddToken(1)
35
36 CreateAccountDeposit(0) A: 10
37 CreateAccountDeposit(0) B: 10
38 CreateAccountDeposit(1) C: 10
39 > batchL1
40 > batchL1
41 > block
42 `
43 tc := til.NewContext(common.RollupConstMaxL1UserTx)
44 blocks, err := tc.GenerateBlocks(set)
45 require.Nil(t, err)
46
47 ptc := ProcessTxsConfig{
48 NLevels: 32,
49 MaxFeeTx: 64,
50 MaxTx: 512,
51 MaxL1Tx: 16,
52 }
53 _, err = sdb.ProcessTxs(ptc, nil, blocks[0].Rollup.L1UserTxs, nil, nil)
54 require.Nil(t, err)
55
56 tx := common.L1Tx{
57 FromIdx: 256,
58 ToIdx: 257,
59 Amount: big.NewInt(10),
60 DepositAmount: big.NewInt(0),
61 FromEthAddr: tc.Users["A"].Addr,
62 UserOrigin: true,
63 }
64 sdb.computeEffectiveAmounts(&tx)
65 assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
66 assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
67
68 // expect error due not enough funds
69 tx = common.L1Tx{
70 FromIdx: 256,
71 ToIdx: 257,
72 Amount: big.NewInt(11),
73 DepositAmount: big.NewInt(0),
74 FromEthAddr: tc.Users["A"].Addr,
75 UserOrigin: true,
76 }
77 sdb.computeEffectiveAmounts(&tx)
78 assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
79 assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
80
81 // expect no-error due not enough funds in a
82 // CreateAccountDepositTransfer transction
83 tx = common.L1Tx{
84 FromIdx: 0,
85 ToIdx: 257,
86 Amount: big.NewInt(10),
87 DepositAmount: big.NewInt(10),
88 UserOrigin: true,
89 }
90 sdb.computeEffectiveAmounts(&tx)
91 assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
92 assert.Equal(t, big.NewInt(10), tx.EffectiveAmount)
93
94 // expect error due not enough funds in a CreateAccountDepositTransfer
95 // transction
96 tx = common.L1Tx{
97 FromIdx: 0,
98 ToIdx: 257,
99 Amount: big.NewInt(11),
100 DepositAmount: big.NewInt(10),
101 UserOrigin: true,
102 }
103 sdb.computeEffectiveAmounts(&tx)
104 assert.Equal(t, big.NewInt(10), tx.EffectiveDepositAmount)
105 assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
106
107 // expect error due not same TokenID
108 tx = common.L1Tx{
109 FromIdx: 256,
110 ToIdx: 258,
111 Amount: big.NewInt(5),
112 DepositAmount: big.NewInt(0),
113 FromEthAddr: tc.Users["A"].Addr,
114 UserOrigin: true,
115 }
116 sdb.computeEffectiveAmounts(&tx)
117 assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
118 assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
119
120 // expect error due not same EthAddr
121 tx = common.L1Tx{
122 FromIdx: 256,
123 ToIdx: 257,
124 Amount: big.NewInt(8),
125 DepositAmount: big.NewInt(0),
126 FromEthAddr: tc.Users["B"].Addr,
127 UserOrigin: true,
128 }
129 sdb.computeEffectiveAmounts(&tx)
130 assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
131 assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
132}
133
134func TestProcessTxsBalances(t *testing.T) {
135 dir, err := ioutil.TempDir("", "tmpdb")
136 require.Nil(t, err)
137 defer assert.Nil(t, os.RemoveAll(dir))
138
139 sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
140 assert.Nil(t, err)
141
142 // generate test transactions from test.SetBlockchain0 code
143 tc := til.NewContext(common.RollupConstMaxL1UserTx)
144 blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0)
145 require.Nil(t, err)
146
147 // Coordinator Idx where to send the fees
148 coordIdxs := []common.Idx{256, 257}
149 ptc := ProcessTxsConfig{
150 NLevels: 32,
151 MaxFeeTx: 64,
152 MaxTx: 512,
153 MaxL1Tx: 16,
154 }
155
156 log.Debug("block:0 batch:0, only L1CoordinatorTxs")
157 _, err = sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
158 require.Nil(t, err)
159
160 log.Debug("block:0 batch:1")
161 l1UserTxs := []common.L1Tx{}
162 l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
163 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
164 require.Nil(t, err)
165
166 log.Debug("block:0 batch:2")
167 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum])
168 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
169 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
170 require.Nil(t, err)
171 checkBalance(t, tc, sdb, "A", 0, "500")
172
173 log.Debug("block:0 batch:3")
174 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum])
175 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[3].L2Txs)
176 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs)
177 require.Nil(t, err)
178 checkBalance(t, tc, sdb, "A", 0, "500")
179 checkBalance(t, tc, sdb, "A", 1, "500")
180
181 log.Debug("block:0 batch:4")
182 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum])
183 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[4].L2Txs)
184 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs)
185 require.Nil(t, err)
186 checkBalance(t, tc, sdb, "A", 0, "500")
187 checkBalance(t, tc, sdb, "A", 1, "500")
188
189 log.Debug("block:0 batch:5")
190 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum])
191 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[5].L2Txs)
192 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs)
193 require.Nil(t, err)
194 checkBalance(t, tc, sdb, "A", 0, "600")
195 checkBalance(t, tc, sdb, "A", 1, "500")
196 checkBalance(t, tc, sdb, "B", 0, "400")
197
198 log.Debug("block:0 batch:6")
199 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum])
200 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[6].L2Txs)
201 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[6].L1CoordinatorTxs, l2Txs)
202 require.Nil(t, err)
203 checkBalance(t, tc, sdb, "Coord", 0, "10")
204 checkBalance(t, tc, sdb, "Coord", 1, "20")
205 checkBalance(t, tc, sdb, "A", 0, "600")
206 checkBalance(t, tc, sdb, "A", 1, "280")
207 checkBalance(t, tc, sdb, "B", 0, "290")
208 checkBalance(t, tc, sdb, "B", 1, "200")
209 checkBalance(t, tc, sdb, "C", 0, "100")
210 checkBalance(t, tc, sdb, "D", 0, "800")
211
212 log.Debug("block:0 batch:7")
213 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[7].Batch.ForgeL1TxsNum])
214 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[7].L2Txs)
215 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[7].L1CoordinatorTxs, l2Txs)
216 require.Nil(t, err)
217 checkBalance(t, tc, sdb, "Coord", 0, "35")
218 checkBalance(t, tc, sdb, "Coord", 1, "30")
219 checkBalance(t, tc, sdb, "A", 0, "430")
220 checkBalance(t, tc, sdb, "A", 1, "280")
221 checkBalance(t, tc, sdb, "B", 0, "390")
222 checkBalance(t, tc, sdb, "B", 1, "90")
223 checkBalance(t, tc, sdb, "C", 0, "45")
224 checkBalance(t, tc, sdb, "C", 1, "100")
225 checkBalance(t, tc, sdb, "D", 0, "800")
226
227 log.Debug("block:1 batch:0")
228 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[0].Batch.ForgeL1TxsNum])
229 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
230 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
231 require.Nil(t, err)
232 checkBalance(t, tc, sdb, "Coord", 0, "75")
233 checkBalance(t, tc, sdb, "Coord", 1, "30")
234 checkBalance(t, tc, sdb, "A", 0, "730")
235 checkBalance(t, tc, sdb, "A", 1, "280")
236 checkBalance(t, tc, sdb, "B", 0, "380")
237 checkBalance(t, tc, sdb, "B", 1, "90")
238 checkBalance(t, tc, sdb, "C", 0, "845")
239 checkBalance(t, tc, sdb, "C", 1, "100")
240 checkBalance(t, tc, sdb, "D", 0, "470")
241
242 log.Debug("block:1 batch:1")
243 l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[1].Rollup.Batches[1].Batch.ForgeL1TxsNum])
244 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
245 _, err = sdb.ProcessTxs(ptc, coordIdxs, l1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
246 require.Nil(t, err)
247
248 // use Set of PoolL2 txs
249 poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPoolL2MinimumFlow1)
250 assert.Nil(t, err)
251
252 _, err = sdb.ProcessTxs(ptc, coordIdxs, []common.L1Tx{}, []common.L1Tx{}, poolL2Txs)
253 require.Nil(t, err)
254 checkBalance(t, tc, sdb, "Coord", 0, "105")
255 checkBalance(t, tc, sdb, "Coord", 1, "40")
256 checkBalance(t, tc, sdb, "A", 0, "510")
257 checkBalance(t, tc, sdb, "A", 1, "170")
258 checkBalance(t, tc, sdb, "B", 0, "480")
259 checkBalance(t, tc, sdb, "B", 1, "190")
260 checkBalance(t, tc, sdb, "C", 0, "845")
261 checkBalance(t, tc, sdb, "C", 1, "100")
262 checkBalance(t, tc, sdb, "D", 0, "360")
263 checkBalance(t, tc, sdb, "F", 0, "100")
264}
265
266func TestProcessTxsSynchronizer(t *testing.T) {
267 dir, err := ioutil.TempDir("", "tmpdb")
268 require.Nil(t, err)
269 defer assert.Nil(t, os.RemoveAll(dir))
270
271 sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
272 assert.Nil(t, err)
273
274 // generate test transactions from test.SetBlockchain0 code
275 tc := til.NewContext(common.RollupConstMaxL1UserTx)
276 blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
277 require.Nil(t, err)
278
279 assert.Equal(t, 31, len(blocks[0].Rollup.L1UserTxs))
280 assert.Equal(t, 4, len(blocks[0].Rollup.Batches[0].L1CoordinatorTxs))
281 assert.Equal(t, 0, len(blocks[0].Rollup.Batches[1].L1CoordinatorTxs))
282 assert.Equal(t, 22, len(blocks[0].Rollup.Batches[2].L2Txs))
283 assert.Equal(t, 1, len(blocks[1].Rollup.Batches[0].L1CoordinatorTxs))
284 assert.Equal(t, 62, len(blocks[1].Rollup.Batches[0].L2Txs))
285 assert.Equal(t, 1, len(blocks[1].Rollup.Batches[1].L1CoordinatorTxs))
286 assert.Equal(t, 8, len(blocks[1].Rollup.Batches[1].L2Txs))
287
288 // Coordinator Idx where to send the fees
289 coordIdxs := []common.Idx{256, 257, 258, 259}
290
291 // Idx of user 'A'
292 idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
293
294 ptc := ProcessTxsConfig{
295 NLevels: 32,
296 MaxFeeTx: 64,
297 MaxTx: 512,
298 MaxL1Tx: 32,
299 }
300
301 // Process the 1st batch, which contains the L1CoordinatorTxs necessary
302 // to create the Coordinator accounts to receive the fees
303 log.Debug("block:0 batch:0, only L1CoordinatorTxs")
304 ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
305 require.Nil(t, err)
306 assert.Equal(t, 4, len(ptOut.CreatedAccounts))
307 assert.Equal(t, 0, len(ptOut.CollectedFees))
308
309 log.Debug("block:0 batch:1")
310 l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
311 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs,
312 blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
313 require.Nil(t, err)
314 assert.Equal(t, 0, len(ptOut.ExitInfos))
315 assert.Equal(t, 31, len(ptOut.CreatedAccounts))
316 assert.Equal(t, 4, len(ptOut.CollectedFees))
317 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
318 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
319 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
320 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
321 acc, err := sdb.GetAccount(idxA1)
322 require.Nil(t, err)
323 assert.Equal(t, "50", acc.Balance.String())
324
325 log.Debug("block:0 batch:2")
326 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
327 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
328 require.Nil(t, err)
329 assert.Equal(t, 0, len(ptOut.ExitInfos))
330 assert.Equal(t, 0, len(ptOut.CreatedAccounts))
331 assert.Equal(t, 4, len(ptOut.CollectedFees))
332 assert.Equal(t, "2", ptOut.CollectedFees[common.TokenID(0)].String())
333 assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
334 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
335 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
336 acc, err = sdb.GetAccount(idxA1)
337 require.Nil(t, err)
338 assert.Equal(t, "35", acc.Balance.String())
339
340 log.Debug("block:1 batch:0")
341 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
342 // before processing expect l2Txs[0:2].Nonce==0
343 assert.Equal(t, common.Nonce(0), l2Txs[0].Nonce)
344 assert.Equal(t, common.Nonce(0), l2Txs[1].Nonce)
345 assert.Equal(t, common.Nonce(0), l2Txs[2].Nonce)
346
347 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
348 require.Nil(t, err)
349
350 // after processing expect l2Txs[0:2].Nonce!=0 and has expected value
351 assert.Equal(t, common.Nonce(6), l2Txs[0].Nonce)
352 assert.Equal(t, common.Nonce(7), l2Txs[1].Nonce)
353 assert.Equal(t, common.Nonce(8), l2Txs[2].Nonce)
354
355 assert.Equal(t, 4, len(ptOut.ExitInfos)) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
356 assert.Equal(t, 1, len(ptOut.CreatedAccounts))
357 assert.Equal(t, 4, len(ptOut.CollectedFees))
358 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
359 assert.Equal(t, "1", ptOut.CollectedFees[common.TokenID(1)].String())
360 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
361 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
362 acc, err = sdb.GetAccount(idxA1)
363 require.Nil(t, err)
364 assert.Equal(t, "57", acc.Balance.String())
365
366 log.Debug("block:1 batch:1")
367 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
368 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs,
369 blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
370 require.Nil(t, err)
371
372 assert.Equal(t, 2, len(ptOut.ExitInfos)) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
373 assert.Equal(t, 1, len(ptOut.CreatedAccounts))
374 assert.Equal(t, 4, len(ptOut.CollectedFees))
375 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(0)].String())
376 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(1)].String())
377 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(2)].String())
378 assert.Equal(t, "0", ptOut.CollectedFees[common.TokenID(3)].String())
379 acc, err = sdb.GetAccount(idxA1)
380 assert.Nil(t, err)
381 assert.Equal(t, "77", acc.Balance.String())
382
383 idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
384 acc, err = sdb.GetAccount(idxB0)
385 require.Nil(t, err)
386 assert.Equal(t, "51", acc.Balance.String())
387
388 // get balance of Coordinator account for TokenID==0
389 acc, err = sdb.GetAccount(common.Idx(256))
390 require.Nil(t, err)
391 assert.Equal(t, "2", acc.Balance.String())
392}
393
394func TestProcessTxsBatchBuilder(t *testing.T) {
395 dir, err := ioutil.TempDir("", "tmpdb")
396 require.Nil(t, err)
397 defer assert.Nil(t, os.RemoveAll(dir))
398
399 sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
400 assert.Nil(t, err)
401
402 // generate test transactions from test.SetBlockchain0 code
403 tc := til.NewContext(common.RollupConstMaxL1UserTx)
404 blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
405 require.Nil(t, err)
406
407 // Coordinator Idx where to send the fees
408 coordIdxs := []common.Idx{256, 257, 258, 259}
409
410 // Idx of user 'A'
411 idxA1 := tc.Users["A"].Accounts[common.TokenID(1)].Idx
412
413 ptc := ProcessTxsConfig{
414 NLevels: 32,
415 MaxFeeTx: 64,
416 MaxTx: 512,
417 MaxL1Tx: 32,
418 }
419
420 // Process the 1st batch, which contains the L1CoordinatorTxs necessary
421 // to create the Coordinator accounts to receive the fees
422 log.Debug("block:0 batch:0, only L1CoordinatorTxs")
423 ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
424 require.Nil(t, err)
425 // expect 0 at CreatedAccount, as is only computed when StateDB.Type==TypeSynchronizer
426 assert.Equal(t, 0, len(ptOut.CreatedAccounts))
427
428 log.Debug("block:0 batch:1")
429 l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs)
430 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[0].Rollup.L1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
431 require.Nil(t, err)
432 assert.Equal(t, 0, len(ptOut.ExitInfos))
433 assert.Equal(t, 0, len(ptOut.CreatedAccounts))
434 acc, err := sdb.GetAccount(idxA1)
435 require.Nil(t, err)
436 assert.Equal(t, "50", acc.Balance.String())
437
438 log.Debug("block:0 batch:2")
439 l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs)
440 ptOut, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs)
441 require.Nil(t, err)
442 assert.Equal(t, 0, len(ptOut.ExitInfos))
443 assert.Equal(t, 0, len(ptOut.CreatedAccounts))
444 acc, err = sdb.GetAccount(idxA1)
445 require.Nil(t, err)
446 assert.Equal(t, "35", acc.Balance.String())
447
448 log.Debug("block:1 batch:0")
449 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[0].L2Txs)
450 _, err = sdb.ProcessTxs(ptc, coordIdxs, nil, blocks[1].Rollup.Batches[0].L1CoordinatorTxs, l2Txs)
451 require.Nil(t, err)
452 acc, err = sdb.GetAccount(idxA1)
453 require.Nil(t, err)
454 assert.Equal(t, "57", acc.Balance.String())
455
456 log.Debug("block:1 batch:1")
457 l2Txs = common.L2TxsToPoolL2Txs(blocks[1].Rollup.Batches[1].L2Txs)
458 _, err = sdb.ProcessTxs(ptc, coordIdxs, blocks[1].Rollup.L1UserTxs, blocks[1].Rollup.Batches[1].L1CoordinatorTxs, l2Txs)
459 require.Nil(t, err)
460 acc, err = sdb.GetAccount(idxA1)
461 assert.Nil(t, err)
462 assert.Equal(t, "77", acc.Balance.String())
463
464 idxB0 := tc.Users["C"].Accounts[common.TokenID(0)].Idx
465 acc, err = sdb.GetAccount(idxB0)
466 require.Nil(t, err)
467 assert.Equal(t, "51", acc.Balance.String())
468
469 // get balance of Coordinator account for TokenID==0
470 acc, err = sdb.GetAccount(common.Idx(256))
471 require.Nil(t, err)
472 assert.Equal(t, common.TokenID(0), acc.TokenID)
473 assert.Equal(t, "2", acc.Balance.String())
474 acc, err = sdb.GetAccount(common.Idx(257))
475 require.Nil(t, err)
476 assert.Equal(t, common.TokenID(1), acc.TokenID)
477 assert.Equal(t, "2", acc.Balance.String())
478}
479
480func TestProcessTxsRootTestVectors(t *testing.T) {
481 dir, err := ioutil.TempDir("", "tmpdb")
482 require.Nil(t, err)
483 defer assert.Nil(t, os.RemoveAll(dir))
484
485 sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
486 assert.Nil(t, err)
487
488 // same values than in the js test
489 bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
490 assert.Nil(t, err)
491 l1Txs := []common.L1Tx{
492 {
493 FromIdx: 0,
494 DepositAmount: big.NewInt(16000000),
495 Amount: big.NewInt(0),
496 TokenID: 1,
497 FromBJJ: bjj0,
498 FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
499 ToIdx: 0,
500 Type: common.TxTypeCreateAccountDeposit,
501 UserOrigin: true,
502 },
503 }
504 l2Txs := []common.PoolL2Tx{
505 {
506 FromIdx: 256,
507 ToIdx: 256,
508 TokenID: 1,
509 Amount: big.NewInt(1000),
510 Nonce: 0,
511 Fee: 126,
512 Type: common.TxTypeTransfer,
513 },
514 }
515
516 ptc := ProcessTxsConfig{
517 NLevels: 32,
518 MaxFeeTx: 8,
519 MaxTx: 32,
520 MaxL1Tx: 16,
521 }
522 _, err = sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
523 require.Nil(t, err)
524 assert.Equal(t, "9827704113668630072730115158977131501210702363656902211840117643154933433410", sdb.mt.Root().BigInt().String())
525}