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