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.

149 lines
4.7 KiB

  1. package test
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. var debug = false
  10. func TestParseBlockchainTxs(t *testing.T) {
  11. s := `
  12. // deposits
  13. Deposit(1) A: 10
  14. Deposit(2) A: 20
  15. Deposit(1) B: 5
  16. CreateAccountDeposit(1) C: 5
  17. CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
  18. // L2 transactions
  19. Transfer(1) A-B: 6 (1)
  20. Transfer(1) B-D: 3 (1)
  21. // set new batch
  22. > batch
  23. DepositTransfer(1) A-B: 15, 10 (1)
  24. Transfer(1) C-A : 3 (1)
  25. Transfer(2) A-B: 15 (1)
  26. Deposit(1) User0: 20
  27. Deposit(3) User1: 20
  28. Transfer(1) User0-User1: 15 (1)
  29. Transfer(3) User1-User0: 15 (1)
  30. > batch
  31. Transfer(1) User1-User0: 1 (1)
  32. > batch
  33. > block
  34. // Exits
  35. Exit(1) A: 5
  36. `
  37. parser := NewParser(strings.NewReader(s))
  38. instructions, err := parser.Parse(false)
  39. require.Nil(t, err)
  40. assert.Equal(t, 20, len(instructions.Instructions))
  41. assert.Equal(t, 10, len(instructions.Accounts))
  42. assert.Equal(t, 3, len(instructions.TokenIDs))
  43. if debug {
  44. fmt.Println(instructions)
  45. for _, instruction := range instructions.Instructions {
  46. fmt.Println(instruction.Raw())
  47. }
  48. }
  49. assert.Equal(t, TypeNewBatch, instructions.Instructions[7].Type)
  50. assert.Equal(t, "Deposit(1)User0:20", instructions.Instructions[11].Raw())
  51. assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.Instructions[8].String())
  52. assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.Instructions[14].String())
  53. assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.Instructions[10].Raw())
  54. assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.Instructions[10].String())
  55. assert.Equal(t, "Exit(1)A:5", instructions.Instructions[19].Raw())
  56. assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.Instructions[19].String())
  57. }
  58. func TestParsePoolTxs(t *testing.T) {
  59. s := `
  60. PoolTransfer(1) A-B: 6 (1)
  61. PoolTransfer(2) A-B: 3 (3)
  62. PoolTransfer(1) B-D: 3 (1)
  63. PoolTransfer(1) C-D: 3 (1)
  64. PoolExit(1) A: 5
  65. `
  66. parser := NewParser(strings.NewReader(s))
  67. instructions, err := parser.Parse(true)
  68. require.Nil(t, err)
  69. assert.Equal(t, 5, len(instructions.Instructions))
  70. assert.Equal(t, 6, len(instructions.Accounts))
  71. assert.Equal(t, 2, len(instructions.TokenIDs))
  72. if debug {
  73. fmt.Println(instructions)
  74. for _, instruction := range instructions.Instructions {
  75. fmt.Println(instruction.Raw())
  76. }
  77. }
  78. assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.Instructions[0].Raw())
  79. assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.Instructions[1].Raw())
  80. assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.Instructions[2].Raw())
  81. assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.Instructions[3].Raw())
  82. assert.Equal(t, "Exit(1)A:5", instructions.Instructions[4].Raw())
  83. }
  84. func TestParseErrors(t *testing.T) {
  85. s := "Deposit(1) A:: 10"
  86. parser := NewParser(strings.NewReader(s))
  87. _, err := parser.Parse(false)
  88. assert.Equal(t, "error parsing line 0: Deposit(1)A:: 10, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
  89. s = "Deposit(1) A: 10 20"
  90. parser = NewParser(strings.NewReader(s))
  91. _, err = parser.Parse(false)
  92. assert.Equal(t, "error parsing line 1: 20, err: Unexpected tx type: 20", err.Error())
  93. s = "Transfer(1) A: 10"
  94. parser = NewParser(strings.NewReader(s))
  95. _, err = parser.Parse(false)
  96. assert.Equal(t, "error parsing line 0: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
  97. s = "Transfer(1) A B: 10"
  98. parser = NewParser(strings.NewReader(s))
  99. _, err = parser.Parse(false)
  100. assert.Equal(t, "error parsing line 0: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
  101. s = "Transfer(1) A-B: 10 (255)"
  102. parser = NewParser(strings.NewReader(s))
  103. _, err = parser.Parse(false)
  104. assert.Nil(t, err)
  105. s = "Transfer(1) A-B: 10 (256)"
  106. parser = NewParser(strings.NewReader(s))
  107. _, err = parser.Parse(false)
  108. assert.Equal(t, "error parsing line 0: Transfer(1)A-B:10(256), err: Fee 256 can not be bigger than 255", err.Error())
  109. // check that the PoolTransfer & Transfer are only accepted in the
  110. // correct case case (PoolTxs/BlockchainTxs)
  111. s = "Transfer(1) A-B: 10 (1)"
  112. parser = NewParser(strings.NewReader(s))
  113. _, err = parser.Parse(true)
  114. assert.Equal(t, "error parsing line 0: Transfer, err: Unexpected 'Transfer' at PoolL2Txs set", err.Error())
  115. s = "PoolTransfer(1) A-B: 10 (1)"
  116. parser = NewParser(strings.NewReader(s))
  117. _, err = parser.Parse(false)
  118. assert.Equal(t, "error parsing line 0: PoolTransfer, err: Unexpected 'PoolTransfer' at BlockchainTxs set", err.Error())
  119. s = "> btch"
  120. parser = NewParser(strings.NewReader(s))
  121. _, err = parser.Parse(false)
  122. assert.Equal(t, "error parsing line 0: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
  123. }