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