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.

234 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
  22. CreateAccountCoordinator(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
  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 (1)
  43. `
  44. parser := newParser(strings.NewReader(s))
  45. instructions, err := parser.parse()
  46. require.NoError(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,
  59. "Type: DepositTransfer, From: A, To: B, DepositAmount: 15, Amount: 10, Fee: 0, TokenID: 1\n",
  60. instructions.instructions[13].String())
  61. assert.Equal(t,
  62. "Type: Transfer, From: User1, To: User0, Amount: 15, Fee: 1, TokenID: 3\n",
  63. instructions.instructions[19].String())
  64. assert.Equal(t, "Transfer(2)A-B:15(1)", instructions.instructions[15].raw())
  65. assert.Equal(t,
  66. "Type: Transfer, From: A, To: B, Amount: 15, Fee: 1, TokenID: 2\n",
  67. instructions.instructions[15].String())
  68. assert.Equal(t, "Exit(1)A:5", instructions.instructions[24].raw())
  69. assert.Equal(t, "Type: Exit, From: A, Amount: 5, TokenID: 1\n",
  70. instructions.instructions[24].String())
  71. }
  72. func TestParsePoolTxs(t *testing.T) {
  73. s := `
  74. Type: PoolL2
  75. PoolTransfer(1) A-B: 6 (1)
  76. PoolTransfer(2) A-B: 3 (3)
  77. PoolTransfer(1) B-D: 3 (1)
  78. PoolTransfer(1) C-D: 3 (1)
  79. PoolExit(1) A: 5 (1)
  80. `
  81. parser := newParser(strings.NewReader(s))
  82. instructions, err := parser.parse()
  83. require.NoError(t, err)
  84. assert.Equal(t, 5, len(instructions.instructions))
  85. assert.Equal(t, 4, len(instructions.users))
  86. if debug {
  87. fmt.Println(instructions)
  88. for _, instruction := range instructions.instructions {
  89. fmt.Println(instruction.raw())
  90. }
  91. }
  92. assert.Equal(t, "Transfer(1)A-B:6(1)", instructions.instructions[0].raw())
  93. assert.Equal(t, "Transfer(2)A-B:3(3)", instructions.instructions[1].raw())
  94. assert.Equal(t, "Transfer(1)B-D:3(1)", instructions.instructions[2].raw())
  95. assert.Equal(t, "Transfer(1)C-D:3(1)", instructions.instructions[3].raw())
  96. assert.Equal(t, "Exit(1)A:5", instructions.instructions[4].raw())
  97. }
  98. func TestParseErrors(t *testing.T) {
  99. s := `
  100. Type: Blockchain
  101. Deposit(1) A:: 10
  102. `
  103. parser := newParser(strings.NewReader(s))
  104. _, err := parser.parse()
  105. assert.Equal(t, "Line 2: Deposit(1)A:: 10\n, err: Can not parse number for Amount: :", err.Error())
  106. s = `
  107. Type: Blockchain
  108. AddToken(1)
  109. Deposit(1) A: 10 20
  110. `
  111. parser = newParser(strings.NewReader(s))
  112. _, err = parser.parse()
  113. assert.Equal(t, "Line 4: 20, err: Unexpected Blockchain tx type: 20", err.Error())
  114. s = `
  115. Type: Blockchain
  116. Transfer(1) A: 10
  117. `
  118. parser = newParser(strings.NewReader(s))
  119. _, err = parser.parse()
  120. assert.Equal(t, "Line 2: Transfer(1)A:, err: Expected '-', found ':'", err.Error())
  121. s = `
  122. Type: Blockchain
  123. Transfer(1) A B: 10
  124. `
  125. parser = newParser(strings.NewReader(s))
  126. _, err = parser.parse()
  127. assert.Equal(t, "Line 2: Transfer(1)AB, err: Expected '-', found 'B'", err.Error())
  128. s = `
  129. Type: Blockchain
  130. AddToken(1)
  131. Transfer(1) A-B: 10 (255)
  132. `
  133. parser = newParser(strings.NewReader(s))
  134. _, err = parser.parse()
  135. assert.NoError(t, err)
  136. s = `
  137. Type: Blockchain
  138. Transfer(1) A-B: 10 (256)
  139. `
  140. parser = newParser(strings.NewReader(s))
  141. _, err = parser.parse()
  142. assert.Equal(t,
  143. "Line 2: Transfer(1)A-B:10(256)\n, err: Fee 256 can not be bigger than 255",
  144. err.Error())
  145. // check that the PoolTransfer & Transfer are only accepted in the
  146. // correct case case (PoolTxs/BlockchainTxs)
  147. s = `
  148. Type: PoolL2
  149. Transfer(1) A-B: 10 (1)
  150. `
  151. parser = newParser(strings.NewReader(s))
  152. _, err = parser.parse()
  153. assert.Equal(t, "Line 2: Transfer, err: Unexpected PoolL2 tx type: Transfer", err.Error())
  154. s = `
  155. Type: Blockchain
  156. PoolTransfer(1) A-B: 10 (1)
  157. `
  158. parser = newParser(strings.NewReader(s))
  159. _, err = parser.parse()
  160. assert.Equal(t,
  161. "Line 2: PoolTransfer, err: Unexpected Blockchain tx type: PoolTransfer",
  162. err.Error())
  163. s = `
  164. Type: Blockchain
  165. > btch
  166. `
  167. parser = newParser(strings.NewReader(s))
  168. _, err = parser.parse()
  169. assert.Equal(t,
  170. "Line 2: >, err: Unexpected '> btch', expected '> batch' or '> block'",
  171. err.Error())
  172. // check definition of set Type
  173. s = `PoolTransfer(1) A-B: 10 (1)`
  174. parser = newParser(strings.NewReader(s))
  175. _, err = parser.parse()
  176. assert.Equal(t, "Line 1: PoolTransfer, err: Set type not defined", err.Error())
  177. s = `Type: PoolL1`
  178. parser = newParser(strings.NewReader(s))
  179. _, err = parser.parse()
  180. assert.Equal(t,
  181. "Line 1: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'",
  182. err.Error())
  183. s = `Type: PoolL1
  184. Type: Blockchain`
  185. parser = newParser(strings.NewReader(s))
  186. _, err = parser.parse()
  187. assert.Equal(t,
  188. "Line 1: Type:, err: Invalid set type: 'PoolL1'. Valid set types: 'Blockchain', 'PoolL2'",
  189. err.Error())
  190. s = `Type: PoolL2
  191. Type: Blockchain`
  192. parser = newParser(strings.NewReader(s))
  193. _, err = parser.parse()
  194. assert.Equal(t,
  195. "Line 2: Instruction of 'Type: Blockchain' when there is already a previous "+
  196. "instruction 'Type: PoolL2' defined", err.Error())
  197. s = `Type: Blockchain
  198. AddToken(1)
  199. AddToken(0)
  200. `
  201. parser = newParser(strings.NewReader(s))
  202. _, err = parser.parse()
  203. assert.Equal(t, "Line 3: AddToken can not register TokenID 0", err.Error())
  204. }