You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.6 KiB

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