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.

215 lines
6.1 KiB

Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
  1. package til
  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. // token registrations
  14. AddToken(1)
  15. AddToken(2)
  16. // deposits
  17. Deposit(1) A: 10
  18. Deposit(2) A: 20
  19. Deposit(1) B: 5
  20. CreateAccountDeposit(1) C: 5
  21. CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
  22. CreateAccountDepositCoordinator(1) E
  23. // L2 transactions
  24. Transfer(1) A-B: 6 (1)
  25. Transfer(1) B-D: 3 (1)
  26. Transfer(1) A-E: 1 (1)
  27. // set new batch
  28. > batch
  29. AddToken(3)
  30. DepositTransfer(1) A-B: 15, 10 (1)
  31. Transfer(1) C-A : 3 (1)
  32. Transfer(2) A-B: 15 (1)
  33. Deposit(1) User0: 20
  34. Deposit(3) User1: 20
  35. Transfer(1) User0-User1: 15 (1)
  36. Transfer(3) User1-User0: 15 (1)
  37. > batch
  38. Transfer(1) User1-User0: 1 (1)
  39. > batch
  40. > block
  41. // Exits
  42. Exit(1) A: 5
  43. `
  44. parser := newParser(strings.NewReader(s))
  45. instructions, err := parser.parse()
  46. require.Nil(t, err)
  47. assert.Equal(t, 25, len(instructions.instructions))
  48. assert.Equal(t, 7, len(instructions.users))
  49. if debug {
  50. fmt.Println(instructions)
  51. for _, instruction := range instructions.instructions {
  52. fmt.Println(instruction.raw())
  53. }
  54. }
  55. assert.Equal(t, txTypeCreateAccountDepositCoordinator, instructions.instructions[7].typ)
  56. assert.Equal(t, typeNewBatch, instructions.instructions[11].typ)
  57. assert.Equal(t, "Deposit(1)User0:20", instructions.instructions[16].raw())
  58. assert.Equal(t, "Type: DepositTransfer, From: A, To: B, LoadAmount: 15, Amount: 10, Fee: 1, TokenID: 1\n", instructions.instructions[13].String())
  59. assert.Equal(t, "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n", instructions.instructions[19].String())
  60. assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[15].raw())
  61. assert.Equal(t, "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n", instructions.instructions[15].String())
  62. assert.Equal(t, "Exit(1)A:5", instructions.instructions[24].raw())
  63. assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n", instructions.instructions[24].String())
  64. }
  65. func TestParsePoolTxs(t *testing.T) {
  66. s := `
  67. Type: PoolL2
  68. PoolTransfer(1) A-B: 6 (1)
  69. PoolTransfer(2) A-B: 3 (3)
  70. PoolTransfer(1) B-D: 3 (1)
  71. PoolTransfer(1) C-D: 3 (1)
  72. PoolExit(1) A: 5
  73. `
  74. parser := newParser(strings.NewReader(s))
  75. instructions, err := parser.parse()
  76. require.Nil(t, err)
  77. assert.Equal(t, 5, len(instructions.instructions))
  78. assert.Equal(t, 4, len(instructions.users))
  79. if debug {
  80. fmt.Println(instructions)
  81. for _, instruction := range instructions.instructions {
  82. fmt.Println(instruction.raw())
  83. }
  84. }
  85. assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.instructions[0].raw())
  86. assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.instructions[1].raw())
  87. assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.instructions[2].raw())
  88. assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.instructions[3].raw())
  89. assert.Equal(t, "Exit(1)A:5", instructions.instructions[4].raw())
  90. }
  91. func TestParseErrors(t *testing.T) {
  92. s := `
  93. Type: Blockchain
  94. Deposit(1) A:: 10
  95. `
  96. parser := newParser(strings.NewReader(s))
  97. _, err := parser.parse()
  98. assert.Equal(t, "Line 2: Deposit(1)A:: 10\n, err: strconv.Atoi: parsing \":\": invalid syntax", err.Error())
  99. s = `
  100. Type: Blockchain
  101. AddToken(1)
  102. Deposit(1) A: 10 20
  103. `
  104. parser = newParser(strings.NewReader(s))
  105. _, err = parser.parse()
  106. assert.Equal(t, "Line 4: 20, err: Unexpected Blockchain tx type: 20", err.Error())
  107. s = `
  108. Type: Blockchain
  109. Transfer(1) A: 10
  110. `
  111. parser = newParser(strings.NewReader(s))
  112. _, err = parser.parse()
  113. assert.Equal(t, "Line 2: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
  114. s = `
  115. Type: Blockchain
  116. Transfer(1) A B: 10
  117. `
  118. parser = newParser(strings.NewReader(s))
  119. _, err = parser.parse()
  120. assert.Equal(t, "Line 2: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
  121. s = `
  122. Type: Blockchain
  123. AddToken(1)
  124. Transfer(1) A-B: 10 (255)
  125. `
  126. parser = newParser(strings.NewReader(s))
  127. _, err = parser.parse()
  128. assert.Nil(t, err)
  129. s = `
  130. Type: Blockchain
  131. Transfer(1) A-B: 10 (256)
  132. `
  133. parser = newParser(strings.NewReader(s))
  134. _, err = parser.parse()
  135. assert.Equal(t, "Line 2: Transfer(1)A-B:10(256)\n, err: Fee 256 can not be bigger than 255", err.Error())
  136. // check that the PoolTransfer & Transfer are only accepted in the
  137. // correct case case (PoolTxs/BlockchainTxs)
  138. s = `
  139. Type: PoolL2
  140. Transfer(1) A-B: 10 (1)
  141. `
  142. parser = newParser(strings.NewReader(s))
  143. _, err = parser.parse()
  144. assert.Equal(t, "Line 2: Transfer, err: Unexpected PoolL2 tx type: Transfer", err.Error())
  145. s = `
  146. Type: Blockchain
  147. PoolTransfer(1) A-B: 10 (1)
  148. `
  149. parser = newParser(strings.NewReader(s))
  150. _, err = parser.parse()
  151. assert.Equal(t, "Line 2: PoolTransfer, err: Unexpected Blockchain tx type: PoolTransfer", err.Error())
  152. s = `
  153. Type: Blockchain
  154. > btch
  155. `
  156. parser = newParser(strings.NewReader(s))
  157. _, err = parser.parse()
  158. assert.Equal(t, "Line 2: >, err: Unexpected '> btch', expected '> batch' or '> block'", err.Error())
  159. // check definition of set Type
  160. s = `PoolTransfer(1) A-B: 10 (1)`
  161. parser = newParser(strings.NewReader(s))
  162. _, err = parser.parse()
  163. assert.Equal(t, "Line 1: PoolTransfer, err: Set type not defined", err.Error())
  164. s = `Type: PoolL1`
  165. parser = newParser(strings.NewReader(s))
  166. _, err = parser.parse()
  167. assert.Equal(t, "Line 1: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
  168. s = `Type: PoolL1
  169. Type: Blockchain`
  170. parser = newParser(strings.NewReader(s))
  171. _, err = parser.parse()
  172. assert.Equal(t, "Line 1: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'", err.Error())
  173. s = `Type: PoolL2
  174. Type: Blockchain`
  175. parser = newParser(strings.NewReader(s))
  176. _, err = parser.parse()
  177. assert.Equal(t, "Line 2: Instruction of 'Type: Blockchain' when there is already a previous instruction 'Type: PoolL2' defined", err.Error())
  178. s = `Type: Blockchain
  179. AddToken(1)
  180. AddToken(0)
  181. `
  182. parser = newParser(strings.NewReader(s))
  183. _, err = parser.parse()
  184. assert.Equal(t, "Line 3: AddToken can not register TokenID 0", err.Error())
  185. }