hermez-node/test/transakcio/txs_test.go
test/transakcio/txs_test.goRaw
1package transakcio
2
3import (
4 "math/big"
5 "testing"
6
7 "github.com/hermeznetwork/hermez-node/common"
8 "github.com/stretchr/testify/assert"
9)
10
11func TestGenerateBlocks(t *testing.T) {
12 set := `
13 Type: Blockchain
14
15 CreateAccountDeposit(1) A: 10
16 CreateAccountDeposit(2) A: 20
17 CreateAccountDeposit(1) B: 5
18 CreateAccountDeposit(1) C: 5
19 CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
20
21 Transfer(1) A-B: 6 (1)
22 Transfer(1) B-D: 3 (1)
23 Transfer(1) A-D: 1 (1)
24
25 // set new batch
26 > batch
27
28 DepositTransfer(1) A-B: 15, 10 (1)
29 Transfer(1) C-A : 3 (1)
30 Transfer(2) A-B: 15 (1)
31
32 CreateAccountDeposit(1) User0: 20
33 CreateAccountDeposit(3) User1: 20
34 Transfer(1) User0-User1: 15 (1)
35 Transfer(3) User1-User0: 15 (1)
36 Transfer(1) A-C: 1 (1)
37
38 > batch
39
40 Transfer(1) User1-User0: 1 (1)
41
42 > block
43
44 // Exits
45 Transfer(1) A-B: 1 (1)
46 Exit(1) A: 5
47 `
48 tc := NewTestContext(t)
49 blocks := tc.GenerateBlocks(set)
50 assert.Equal(t, 2, len(blocks))
51 assert.Equal(t, 3, len(blocks[0].Batches))
52 assert.Equal(t, 1, len(blocks[1].Batches))
53 assert.Equal(t, 5, len(blocks[0].Batches[0].L1UserTxs))
54 assert.Equal(t, 0, len(blocks[1].Batches[0].L1UserTxs))
55
56 // Check expected values generated by each line
57 // #0: Deposit(1) A: 10
58 tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1, "A", "", big.NewInt(10), nil)
59 // #1: Deposit(2) A: 20
60 tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2, "A", "", big.NewInt(20), nil)
61 // #2: Deposit(1) A: 20
62 tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1, "B", "", big.NewInt(5), nil)
63 // #3: CreateAccountDeposit(1) C: 5
64 tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1, "C", "", big.NewInt(5), nil)
65 // #4: CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
66 tc.checkL1TxParams(t, blocks[0].Batches[0].L1UserTxs[4], common.TxTypeCreateAccountDepositTransfer, 1, "D", "A", big.NewInt(15), big.NewInt(10))
67 // #5: Transfer(1) A-B: 6 (1)
68 tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(6), common.BatchNum(0), common.Nonce(1))
69 // #6: Transfer(1) B-D: 3 (1)
70 tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "B", "D", big.NewInt(3), common.BatchNum(0), common.Nonce(1))
71 // #7: Transfer(1) A-D: 1 (1)
72 tc.checkL2TxParams(t, blocks[0].Batches[0].L2Txs[2], common.TxTypeTransfer, 1, "A", "D", big.NewInt(1), common.BatchNum(0), common.Nonce(2))
73 // change of Batch
74 // #8: DepositTransfer(1) A-B: 15, 10 (1)
75 tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10))
76 // #9: Deposit(1) User0: 20
77 tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[0], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10))
78 // #10: Transfer(1) C-A : 3 (1)
79 tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[0], common.TxTypeTransfer, 1, "C", "A", big.NewInt(3), common.BatchNum(1), common.Nonce(1))
80 // #11: Transfer(2) A-B: 15 (1)
81 tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[1], common.TxTypeTransfer, 2, "A", "B", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
82 // #12: Deposit(1) User0: 20
83 tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[1], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil)
84 // #13: Deposit(3) User1: 20
85 tc.checkL1TxParams(t, blocks[0].Batches[1].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 3, "User1", "", big.NewInt(20), nil)
86 // #14: Transfer(1) User0-User1: 15 (1)
87 tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[2], common.TxTypeTransfer, 1, "User0", "User1", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
88 // #15: Transfer(3) User1-User0: 15 (1)
89 tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[3], common.TxTypeTransfer, 3, "User1", "User0", big.NewInt(15), common.BatchNum(1), common.Nonce(1))
90 // #16: Transfer(1) A-C: 1 (1)
91 tc.checkL2TxParams(t, blocks[0].Batches[1].L2Txs[4], common.TxTypeTransfer, 1, "A", "C", big.NewInt(1), common.BatchNum(1), common.Nonce(3))
92 // change of Batch
93 // #17: Transfer(1) User1-User0: 1 (1)
94 tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "User1", "User0", big.NewInt(1), common.BatchNum(2), common.Nonce(1))
95 // change of Block (implies also a change of batch)
96 // #18: Transfer(1) A-B: 1 (1)
97 tc.checkL2TxParams(t, blocks[1].Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(1), common.BatchNum(3), common.Nonce(4))
98}
99
100func (tc *TestContext) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType, tokenID common.TokenID, from, to string, loadAmount, amount *big.Int) {
101 assert.Equal(t, typ, tx.Type)
102 if tx.FromIdx != common.Idx(0) {
103 assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
104 }
105 assert.Equal(t, tc.Users[from].Addr.Hex(), tx.FromEthAddr.Hex())
106 assert.Equal(t, tc.Users[from].BJJ.Public(), tx.FromBJJ)
107 if tx.ToIdx != common.Idx(0) {
108 assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
109 }
110 if loadAmount != nil {
111 assert.Equal(t, loadAmount, tx.LoadAmount)
112 }
113 if amount != nil {
114 assert.Equal(t, amount, tx.Amount)
115 }
116}
117func (tc *TestContext) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType, tokenID common.TokenID, from, to string, amount *big.Int, batchNum common.BatchNum, nonce common.Nonce) {
118 assert.Equal(t, typ, tx.Type)
119 assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
120 if tx.Type != common.TxTypeExit {
121 assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
122 }
123 if amount != nil {
124 assert.Equal(t, amount, tx.Amount)
125 }
126 assert.Equal(t, batchNum, tx.BatchNum)
127 assert.Equal(t, nonce, tx.Nonce)
128}
129
130func TestGeneratePoolL2Txs(t *testing.T) {
131 set := `
132 Type: Blockchain
133
134 CreateAccountDeposit(1) A: 10
135 CreateAccountDeposit(2) A: 20
136 CreateAccountDeposit(1) B: 5
137 CreateAccountDeposit(1) C: 5
138 CreateAccountDeposit(1) User0: 5
139 CreateAccountDeposit(1) User1: 0
140 CreateAccountDeposit(3) User0: 0
141 CreateAccountDeposit(3) User1: 5
142 CreateAccountDeposit(2) B: 5
143 CreateAccountDeposit(2) D: 0
144 `
145 tc := NewTestContext(t)
146 _ = tc.GenerateBlocks(set)
147 set = `
148 Type: PoolL2
149 PoolTransfer(1) A-B: 6 (1)
150 PoolTransfer(1) B-C: 3 (1)
151 PoolTransfer(1) C-A: 3 (1)
152 PoolTransfer(1) A-B: 1 (1)
153 PoolTransfer(2) A-B: 15 (1)
154 PoolTransfer(1) User0-User1: 15 (1)
155 PoolTransfer(3) User1-User0: 15 (1)
156 PoolTransfer(2) B-D: 3 (1)
157 PoolExit(1) A: 3
158 `
159 poolL2Txs := tc.GeneratePoolL2Txs(set)
160 assert.Equal(t, 9, len(poolL2Txs))
161 assert.Equal(t, common.TxTypeTransfer, poolL2Txs[0].Type)
162 assert.Equal(t, common.TxTypeExit, poolL2Txs[8].Type)
163 assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[0].ToEthAddr.Hex())
164 assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[0].ToBJJ.String())
165 assert.Equal(t, tc.Users["User1"].Addr.Hex(), poolL2Txs[5].ToEthAddr.Hex())
166 assert.Equal(t, tc.Users["User1"].BJJ.Public().String(), poolL2Txs[5].ToBJJ.String())
167
168 assert.Equal(t, common.Nonce(1), poolL2Txs[0].Nonce)
169 assert.Equal(t, common.Nonce(2), poolL2Txs[3].Nonce)
170 assert.Equal(t, common.Nonce(3), poolL2Txs[8].Nonce)
171
172 // load another set in the same TestContext
173 set = `
174 Type: PoolL2
175 PoolTransfer(1) A-B: 6 (1)
176 PoolTransfer(1) B-C: 3 (1)
177 PoolTransfer(1) A-C: 3 (1)
178 `
179 poolL2Txs = tc.GeneratePoolL2Txs(set)
180 assert.Equal(t, common.Nonce(4), poolL2Txs[0].Nonce)
181 assert.Equal(t, common.Nonce(2), poolL2Txs[1].Nonce)
182 assert.Equal(t, common.Nonce(5), poolL2Txs[2].Nonce)
183}