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.

164 lines
6.7 KiB

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