hermez-node/test/til/txs_test.go
test/til/txs_test.goRaw
1package til
2
3import (
4 "math/big"
5 "testing"
6
7 "github.com/hermeznetwork/hermez-node/common"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10)
11
12func TestGenerateBlocksNoBatches(t *testing.T) {
13 set := `
14 Type: Blockchain
15 AddToken(1)
16 AddToken(2)
17
18 CreateAccountDeposit(1) A: 11
19 CreateAccountDeposit(2) B: 22
20
21 > block
22 `
23 tc := NewContext(common.RollupConstMaxL1UserTx)
24 blocks, err := tc.GenerateBlocks(set)
25 require.Nil(t, err)
26 assert.Equal(t, 1, len(blocks))
27 assert.Equal(t, 0, len(blocks[0].Rollup.Batches))
28 assert.Equal(t, 2, len(blocks[0].Rollup.AddedTokens))
29 assert.Equal(t, 2, len(blocks[0].Rollup.L1UserTxs))
30}
31
32func TestGenerateBlocks(t *testing.T) {
33 set := `
34 Type: Blockchain
35 AddToken(1)
36 AddToken(2)
37 AddToken(3)
38
39 CreateAccountDeposit(1) A: 10
40 CreateAccountDeposit(2) A: 20
41 CreateAccountDeposit(1) B: 5
42 CreateAccountDeposit(1) C: 5
43 CreateAccountDeposit(1) D: 5
44
45 > batchL1 // batchNum = 1
46 > batchL1 // batchNum = 2
47
48 CreateAccountDepositTransfer(1) F-A: 15, 10
49
50 Transfer(1) A-B: 6 (1)
51 Transfer(1) B-D: 3 (1)
52 Transfer(1) A-D: 1 (1)
53
54 // set new batch
55 > batch // batchNum = 3
56 CreateAccountCoordinator(1) E
57 CreateAccountCoordinator(2) B
58
59 DepositTransfer(1) A-B: 15, 10
60 Transfer(1) C-A : 3 (1)
61 Transfer(2) A-B: 15 (1)
62 Transfer(1) A-E: 1 (1)
63
64 CreateAccountDeposit(1) User0: 20
65 CreateAccountDeposit(3) User1: 20
66 CreateAccountCoordinator(1) User1
67 CreateAccountCoordinator(3) User0
68 > batchL1 // batchNum = 4
69 Transfer(1) User0-User1: 15 (1)
70 Transfer(3) User1-User0: 15 (1)
71 Transfer(1) A-C: 1 (1)
72
73 > batchL1 // batchNum = 5
74
75 Transfer(1) User1-User0: 1 (1)
76
77 > block
78
79 // Exits
80 Transfer(1) A-B: 1 (1)
81 Exit(1) A: 5 (1)
82
83 > batch // batchNum = 6
84 > block
85
86 // this transaction should not be generated, as it's after last
87 // batch and last block
88 Transfer(1) User1-User0: 1 (1)
89 `
90 tc := NewContext(common.RollupConstMaxL1UserTx)
91 blocks, err := tc.GenerateBlocks(set)
92 require.Nil(t, err)
93 assert.Equal(t, 2, len(blocks))
94 assert.Equal(t, 5, len(blocks[0].Rollup.Batches))
95 assert.Equal(t, 1, len(blocks[1].Rollup.Batches))
96 assert.Equal(t, 9, len(blocks[0].Rollup.L1UserTxs))
97 assert.Equal(t, 4, len(blocks[0].Rollup.Batches[3].L1CoordinatorTxs))
98 assert.Equal(t, 0, len(blocks[1].Rollup.L1UserTxs))
99
100 // Check expected values generated by each line
101 // #0: Deposit(1) A: 10
102 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1, "A", "", big.NewInt(10), nil)
103 // #1: Deposit(2) A: 20
104 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2, "A", "", big.NewInt(20), nil)
105 // // #2: Deposit(1) A: 20
106 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1, "B", "", big.NewInt(5), nil)
107 // // #3: CreateAccountDeposit(1) C: 5
108 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1, "C", "", big.NewInt(5), nil)
109 // // #4: CreateAccountDeposit(1) D: 5
110 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[4], common.TxTypeCreateAccountDeposit, 1, "D", "", big.NewInt(5), nil)
111 // #5: Transfer(1) A-B: 6 (1)
112 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(6), common.BatchNum(3))
113 // #6: Transfer(1) B-D: 3 (1)
114 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[1], common.TxTypeTransfer, 1, "B", "D", big.NewInt(3), common.BatchNum(3))
115 // #7: Transfer(1) A-D: 1 (1)
116 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[2], common.TxTypeTransfer, 1, "A", "D", big.NewInt(1), common.BatchNum(3))
117 // change of Batch
118 // #8: CreateAccountDepositTransfer(1) F-A: 15, 10 (3)
119 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[5], common.TxTypeCreateAccountDepositTransfer, 1, "F", "A", big.NewInt(15), big.NewInt(10))
120 // #9: DepositTransfer(1) A-B: 15, 10 (1)
121 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[6], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10))
122 // #11: Transfer(1) C-A : 3 (1)
123 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[0], common.TxTypeTransfer, 1, "C", "A", big.NewInt(3), common.BatchNum(4))
124 // #12: Transfer(2) A-B: 15 (1)
125 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[1], common.TxTypeTransfer, 2, "A", "B", big.NewInt(15), common.BatchNum(4))
126 // #13: Deposit(1) User0: 20
127 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[7], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil)
128 // // #14: Deposit(3) User1: 20
129 tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[8], common.TxTypeCreateAccountDeposit, 3, "User1", "", big.NewInt(20), nil)
130 // #15: Transfer(1) User0-User1: 15 (1)
131 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[0], common.TxTypeTransfer, 1, "User0", "User1", big.NewInt(15), common.BatchNum(5))
132 // #16: Transfer(3) User1-User0: 15 (1)
133 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[1], common.TxTypeTransfer, 3, "User1", "User0", big.NewInt(15), common.BatchNum(5))
134 // #17: Transfer(1) A-C: 1 (1)
135 tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[2], common.TxTypeTransfer, 1, "A", "C", big.NewInt(1), common.BatchNum(5))
136 // change of Batch
137 // #18: Transfer(1) User1-User0: 1 (1)
138 tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "User1", "User0", big.NewInt(1), common.BatchNum(6))
139 // change of Block (implies also a change of batch)
140 // #19: Transfer(1) A-B: 1 (1)
141 tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "A", "B", big.NewInt(1), common.BatchNum(6))
142}
143
144func (tc *Context) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType, tokenID common.TokenID, from, to string, loadAmount, amount *big.Int) {
145 assert.Equal(t, typ, tx.Type)
146 if tx.FromIdx != common.Idx(0) {
147 assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
148 }
149 assert.Equal(t, tc.Users[from].Addr.Hex(), tx.FromEthAddr.Hex())
150 assert.Equal(t, tc.Users[from].BJJ.Public(), tx.FromBJJ)
151 if tx.ToIdx != common.Idx(0) {
152 assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
153 }
154 if loadAmount != nil {
155 assert.Equal(t, loadAmount, tx.LoadAmount)
156 }
157 if amount != nil {
158 assert.Equal(t, amount, tx.Amount)
159 }
160}
161
162func (tc *Context) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType, tokenID common.TokenID, from, to string, amount *big.Int, batchNum common.BatchNum) {
163 assert.Equal(t, typ, tx.Type)
164 assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
165 if tx.Type != common.TxTypeExit {
166 assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
167 }
168 if amount != nil {
169 assert.Equal(t, amount, tx.Amount)
170 }
171 assert.Equal(t, batchNum, tx.BatchNum)
172}
173
174func TestGeneratePoolL2Txs(t *testing.T) {
175 set := `
176 Type: Blockchain
177 AddToken(1)
178 AddToken(2)
179 AddToken(3)
180
181 CreateAccountDeposit(1) A: 10
182 CreateAccountDeposit(2) A: 20
183 CreateAccountDeposit(1) B: 5
184 CreateAccountDeposit(1) C: 5
185 CreateAccountDeposit(1) User0: 5
186 CreateAccountDeposit(1) User1: 0
187 CreateAccountDeposit(3) User0: 0
188 CreateAccountDeposit(3) User1: 5
189 CreateAccountDeposit(2) B: 5
190 CreateAccountDeposit(2) D: 0
191 > batchL1
192 > batchL1
193 `
194 tc := NewContext(common.RollupConstMaxL1UserTx)
195 _, err := tc.GenerateBlocks(set)
196 require.Nil(t, err)
197 set = `
198 Type: PoolL2
199 PoolTransfer(1) A-B: 6 (1)
200 PoolTransfer(1) B-C: 3 (1)
201 PoolTransfer(1) C-A: 3 (1)
202 PoolTransfer(1) A-B: 1 (1)
203 PoolTransfer(2) A-B: 15 (1)
204 PoolTransfer(1) User0-User1: 15 (1)
205 PoolTransfer(3) User1-User0: 15 (1)
206 PoolTransfer(2) B-D: 3 (1)
207 PoolExit(1) A: 3 (1)
208 PoolTransferToEthAddr(1) A-B: 1 (1)
209 PoolTransferToBJJ(1) A-B: 1 (1)
210 `
211 poolL2Txs, err := tc.GeneratePoolL2Txs(set)
212 require.Nil(t, err)
213 assert.Equal(t, 11, len(poolL2Txs))
214 assert.Equal(t, common.TxTypeTransfer, poolL2Txs[0].Type)
215 assert.Equal(t, common.TxTypeExit, poolL2Txs[8].Type)
216 assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[0].ToEthAddr.Hex())
217 assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[0].ToBJJ.String())
218 assert.Equal(t, tc.Users["User1"].Addr.Hex(), poolL2Txs[5].ToEthAddr.Hex())
219 assert.Equal(t, tc.Users["User1"].BJJ.Public().String(), poolL2Txs[5].ToBJJ.String())
220
221 assert.Equal(t, common.Nonce(1), poolL2Txs[0].Nonce)
222 assert.Equal(t, common.Nonce(2), poolL2Txs[3].Nonce)
223 assert.Equal(t, common.Nonce(3), poolL2Txs[8].Nonce)
224
225 assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[9].ToEthAddr.Hex())
226 assert.Nil(t, poolL2Txs[9].ToBJJ)
227 assert.Equal(t, common.TxTypeTransferToEthAddr, poolL2Txs[9].Type)
228 assert.Equal(t, common.FFAddr, poolL2Txs[10].ToEthAddr)
229 assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[10].ToBJJ.String())
230 assert.Equal(t, common.TxTypeTransferToBJJ, poolL2Txs[10].Type)
231
232 // load another set in the same Context
233 set = `
234 Type: PoolL2
235 PoolTransfer(1) A-B: 6 (1)
236 PoolTransfer(1) B-C: 3 (1)
237 PoolTransfer(1) A-C: 3 (1)
238 `
239 poolL2Txs, err = tc.GeneratePoolL2Txs(set)
240 require.Nil(t, err)
241 assert.Equal(t, common.Nonce(6), poolL2Txs[0].Nonce)
242 assert.Equal(t, common.Nonce(2), poolL2Txs[1].Nonce)
243 assert.Equal(t, common.Nonce(7), poolL2Txs[2].Nonce)
244
245 // check that a PoolL2Tx can be done to a non existing ToIdx
246 set = `
247 Type: Blockchain
248 AddToken(1)
249 CreateAccountDeposit(1) A: 10
250 > batchL1
251 > batchL1
252 > block
253 `
254 tc = NewContext(common.RollupConstMaxL1UserTx)
255 _, err = tc.GenerateBlocks(set)
256 require.Nil(t, err)
257 set = `
258 Type: PoolL2
259 PoolTransferToEthAddr(1) A-B: 3 (1)
260 PoolTransferToBJJ(1) A-C: 3 (1)
261 `
262 _, err = tc.GeneratePoolL2Txs(set)
263 require.Nil(t, err)
264 // expect error, as FromIdx=B is still not created for TokenID=1
265 set = `
266 Type: PoolL2
267 PoolTransferToEthAddr(1) B-A: 3 (1)
268 PoolTransferToBJJ(1) B-A: 3 (1)
269 `
270 _, err = tc.GeneratePoolL2Txs(set)
271 require.NotNil(t, err)
272}
273
274func TestGenerateErrors(t *testing.T) {
275 // unregistered token
276 set := `Type: Blockchain
277 CreateAccountDeposit(1) A: 5
278 > batchL1
279 `
280 tc := NewContext(common.RollupConstMaxL1UserTx)
281 _, err := tc.GenerateBlocks(set)
282 assert.Equal(t, "Line 2: Can not process CreateAccountDeposit: TokenID 1 not registered, last registered TokenID: 0", err.Error())
283
284 // ensure AddToken sequentiality and not using 0
285 set = `
286 Type: Blockchain
287 AddToken(0)
288 `
289 tc = NewContext(common.RollupConstMaxL1UserTx)
290 _, err = tc.GenerateBlocks(set)
291 require.Equal(t, "Line 2: AddToken can not register TokenID 0", err.Error())
292
293 set = `
294 Type: Blockchain
295 AddToken(2)
296 `
297 tc = NewContext(common.RollupConstMaxL1UserTx)
298 _, err = tc.GenerateBlocks(set)
299 require.Equal(t, "Line 2: AddToken TokenID should be sequential, expected TokenID: 1, defined TokenID: 2", err.Error())
300
301 set = `
302 Type: Blockchain
303 AddToken(1)
304 AddToken(2)
305 AddToken(3)
306 AddToken(5)
307 `
308 tc = NewContext(common.RollupConstMaxL1UserTx)
309 _, err = tc.GenerateBlocks(set)
310 require.Equal(t, "Line 5: AddToken TokenID should be sequential, expected TokenID: 4, defined TokenID: 5", err.Error())
311
312 // check transactions when account is not created yet
313 set = `
314 Type: Blockchain
315 AddToken(1)
316 CreateAccountDeposit(1) A: 10
317 > batchL1
318 CreateAccountDeposit(1) B
319 Transfer(1) A-B: 6 (1)
320 > batch
321 `
322 tc = NewContext(common.RollupConstMaxL1UserTx)
323 _, err = tc.GenerateBlocks(set)
324 require.Equal(t, "Line 5: CreateAccountDeposit(1)BTransfer(1) A-B: 6 (1)\n, err: Expected ':', found 'Transfer'", err.Error())
325 set = `
326 Type: Blockchain
327 AddToken(1)
328 CreateAccountDeposit(1) A: 10
329 > batchL1
330 CreateAccountCoordinator(1) B
331 > batchL1
332 > batch
333 Transfer(1) A-B: 6 (1)
334 > batch
335 `
336 tc = NewContext(common.RollupConstMaxL1UserTx)
337 _, err = tc.GenerateBlocks(set)
338 require.Nil(t, err)
339
340 // check nonces
341 set = `
342 Type: Blockchain
343 AddToken(1)
344 CreateAccountDeposit(1) A: 10
345 > batchL1
346 CreateAccountCoordinator(1) B
347 > batchL1
348 Transfer(1) A-B: 6 (1)
349 Transfer(1) A-B: 6 (1) // on purpose this is moving more money that what it has in the account, Til should not fail
350 Transfer(1) B-A: 6 (1)
351 Exit(1) A: 3 (1)
352 > batch
353 `
354 tc = NewContext(common.RollupConstMaxL1UserTx)
355 _, err = tc.GenerateBlocks(set)
356 require.Nil(t, err)
357 assert.Equal(t, common.Nonce(3), tc.Users["A"].Accounts[common.TokenID(1)].Nonce)
358 assert.Equal(t, common.Idx(256), tc.Users["A"].Accounts[common.TokenID(1)].Idx)
359 assert.Equal(t, common.Nonce(1), tc.Users["B"].Accounts[common.TokenID(1)].Nonce)
360 assert.Equal(t, common.Idx(257), tc.Users["B"].Accounts[common.TokenID(1)].Idx)
361}