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.

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