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.

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