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.

196 lines
5.8 KiB

  1. package transakcio
  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. Type: Blockchain
  13. // deposits
  14. Deposit(1) A: 10
  15. Deposit(2) A: 20
  16. Deposit(1) B: 5
  17. CreateAccountDeposit(1) C: 5
  18. CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
  19. CreateAccountDepositCoordinator(1) E
  20. // L2 transactions
  21. Transfer(1) A-B: 6 (1)
  22. Transfer(1) B-D: 3 (1)
  23. Transfer(1) A-E: 1 (1)
  24. // set new batch
  25. > batch
  26. DepositTransfer(1) A-B: 15, 10 (1)
  27. Transfer(1) C-A : 3 (1)
  28. Transfer(2) A-B: 15 (1)
  29. Deposit(1) User0: 20
  30. Deposit(3) User1: 20
  31. Transfer(1) User0-User1: 15 (1)
  32. Transfer(3) User1-User0: 15 (1)
  33. > batch
  34. Transfer(1) User1-User0: 1 (1)
  35. > batch
  36. > block
  37. // Exits
  38. Exit(1) A: 5
  39. `
  40. parser := newParser(strings.NewReader(s))
  41. instructions, err := parser.parse()
  42. require.Nil(t, err)
  43. assert.Equal(t, 22, len(instructions.instructions))
  44. assert.Equal(t, 7, len(instructions.accounts))
  45. assert.Equal(t, 3, len(instructions.tokenIDs))
  46. if debug {
  47. fmt.Println(instructions)
  48. for _, instruction := range instructions.instructions {
  49. fmt.Println(instruction.raw())
  50. }
  51. }
  52. assert.Equal(t, txTypeCreateAccountDepositCoordinator, instructions.instructions[5].typ)
  53. assert.Equal(t, typeNewBatch, instructions.instructions[9].typ)
  54. assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[13].raw())
  55. assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.instructions[10].String())
  56. assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[16].String())
  57. assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[12].raw())
  58. assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[12].String())
  59. assert.Equal(t, "Exit(1)A:5", instructions.instructions[21].raw())
  60. assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.instructions[21].String())
  61. }
  62. func TestParsePoolTxs(t *testing.T) {
  63. s := `
  64. Type: PoolL2
  65. PoolTransfer(1) A-B: 6 (1)
  66. PoolTransfer(2) A-B: 3 (3)
  67. PoolTransfer(1) B-D: 3 (1)
  68. PoolTransfer(1) C-D: 3 (1)
  69. PoolExit(1) A: 5
  70. `
  71. parser := newParser(strings.NewReader(s))
  72. instructions, err := parser.parse()
  73. require.Nil(t, err)
  74. assert.Equal(t, 5, len(instructions.instructions))
  75. assert.Equal(t, 4, len(instructions.accounts))
  76. assert.Equal(t, 2, len(instructions.tokenIDs))
  77. if debug {
  78. fmt.Println(instructions)
  79. for _, instruction := range instructions.instructions {
  80. fmt.Println(instruction.raw())
  81. }
  82. }
  83. assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.instructions[0].raw())
  84. assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.instructions[1].raw())
  85. assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.instructions[2].raw())
  86. assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.instructions[3].raw())
  87. assert.Equal(t, "Exit(1)A:5", instructions.instructions[4].raw())
  88. }
  89. func TestParseErrors(t *testing.T) {
  90. s := `
  91. Type: Blockchain
  92. Deposit(1) A:: 10
  93. `
  94. parser := newParser(strings.NewReader(s))
  95. _, err := parser.parse()
  96. assert.Equal(t, "error parsing line 1: Deposit(1)A:: 10\n, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
  97. s = `
  98. Type: Blockchain
  99. Deposit(1) A: 10 20
  100. `
  101. parser = newParser(strings.NewReader(s))
  102. _, err = parser.parse()
  103. assert.Equal(t, "error parsing line 2: 20, err: Unexpected Blockchain tx type: 20", err.Error())
  104. s = `
  105. Type: Blockchain
  106. Transfer(1) A: 10
  107. `
  108. parser = newParser(strings.NewReader(s))
  109. _, err = parser.parse()
  110. assert.Equal(t, "error parsing line 1: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
  111. s = `
  112. Type: Blockchain
  113. Transfer(1) A B: 10
  114. `
  115. parser = newParser(strings.NewReader(s))
  116. _, err = parser.parse()
  117. assert.Equal(t, "error parsing line 1: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
  118. s = `
  119. Type: Blockchain
  120. Transfer(1) A-B: 10 (255)
  121. `
  122. parser = newParser(strings.NewReader(s))
  123. _, err = parser.parse()
  124. assert.Nil(t, err)
  125. s = `
  126. Type: Blockchain
  127. Transfer(1) A-B: 10 (256)
  128. `
  129. parser = newParser(strings.NewReader(s))
  130. _, err = parser.parse()
  131. assert.Equal(t, "error parsing line 1: Transfer(1)A-B:10(256)\n, err: Fee 256 can not be bigger than 255", err.Error())
  132. // check that the PoolTransfer & Transfer are only accepted in the
  133. // correct case case (PoolTxs/BlockchainTxs)
  134. s = `
  135. Type: PoolL2
  136. Transfer(1) A-B: 10 (1)
  137. `
  138. parser = newParser(strings.NewReader(s))
  139. _, err = parser.parse()
  140. assert.Equal(t, "error parsing line 1: Transfer, err: Unexpected PoolL2 tx type: Transfer", err.Error())
  141. s = `
  142. Type: Blockchain
  143. PoolTransfer(1) A-B: 10 (1)
  144. `
  145. parser = newParser(strings.NewReader(s))
  146. _, err = parser.parse()
  147. assert.Equal(t, "error parsing line 1: PoolTransfer, err: Unexpected Blockchain tx type: PoolTransfer", err.Error())
  148. s = `
  149. Type: Blockchain
  150. > btch
  151. `
  152. parser = newParser(strings.NewReader(s))
  153. _, err = parser.parse()
  154. assert.Equal(t, "error parsing line 1: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
  155. // check definition of set Type
  156. s = `PoolTransfer(1) A-B: 10 (1)`
  157. parser = newParser(strings.NewReader(s))
  158. _, err = parser.parse()
  159. assert.Equal(t, "error parsing line 0: PoolTransfer, err: Set type not defined", err.Error())
  160. s = `Type: PoolL1`
  161. parser = newParser(strings.NewReader(s))
  162. _, err = parser.parse()
  163. assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
  164. s = `Type: PoolL1
  165. Type: Blockchain`
  166. parser = newParser(strings.NewReader(s))
  167. _, err = parser.parse()
  168. assert.Equal(t, "error parsing line 0: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
  169. }