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.

619 lines
19 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 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`.
3 years ago
  1. package til
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/big"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/crypto"
  8. "github.com/hermeznetwork/hermez-node/common"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestGenerateKeys(t *testing.T) {
  13. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  14. usernames := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
  15. tc.generateKeys(usernames)
  16. debug := false
  17. if debug {
  18. for i, username := range usernames {
  19. fmt.Println(i, username)
  20. sk := crypto.FromECDSA(tc.Users[username].EthSk)
  21. fmt.Println(" eth_sk", hex.EncodeToString(sk))
  22. fmt.Println(" eth_addr", tc.Users[username].Addr)
  23. fmt.Println(" bjj_sk", hex.EncodeToString(tc.Users[username].BJJ[:]))
  24. fmt.Println(" bjj_pub", tc.Users[username].BJJ.Public().Compress())
  25. }
  26. }
  27. }
  28. func TestGenerateBlocksNoBatches(t *testing.T) {
  29. set := `
  30. Type: Blockchain
  31. AddToken(1)
  32. AddToken(2)
  33. CreateAccountDeposit(1) A: 11
  34. CreateAccountDeposit(2) B: 22
  35. > block
  36. `
  37. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  38. blocks, err := tc.GenerateBlocks(set)
  39. require.NoError(t, err)
  40. assert.Equal(t, 1, len(blocks))
  41. assert.Equal(t, 0, len(blocks[0].Rollup.Batches))
  42. assert.Equal(t, 2, len(blocks[0].Rollup.AddedTokens))
  43. assert.Equal(t, 2, len(blocks[0].Rollup.L1UserTxs))
  44. }
  45. func TestGenerateBlocks(t *testing.T) {
  46. set := `
  47. Type: Blockchain
  48. AddToken(1)
  49. AddToken(2)
  50. AddToken(3)
  51. CreateAccountDeposit(1) A: 10
  52. CreateAccountDeposit(2) A: 20
  53. CreateAccountDeposit(1) B: 5
  54. CreateAccountDeposit(1) C: 5
  55. CreateAccountDeposit(1) D: 5
  56. > batchL1 // batchNum = 1
  57. > batchL1 // batchNum = 2
  58. CreateAccountDepositTransfer(1) F-A: 15, 10
  59. Transfer(1) A-B: 6 (1)
  60. Transfer(1) B-D: 3 (1)
  61. Transfer(1) A-D: 1 (1)
  62. // set new batch
  63. > batch // batchNum = 3
  64. CreateAccountCoordinator(1) E
  65. CreateAccountCoordinator(2) B
  66. DepositTransfer(1) A-B: 15, 10
  67. Transfer(1) C-A : 3 (1)
  68. Transfer(2) A-B: 15 (1)
  69. Transfer(1) A-E: 1 (1)
  70. CreateAccountDeposit(1) User0: 20
  71. CreateAccountDeposit(3) User1: 20
  72. CreateAccountCoordinator(1) User1
  73. CreateAccountCoordinator(3) User0
  74. > batchL1 // batchNum = 4
  75. Transfer(1) User0-User1: 15 (1)
  76. Transfer(3) User1-User0: 15 (1)
  77. Transfer(1) A-C: 1 (1)
  78. > batchL1 // batchNum = 5
  79. Transfer(1) User1-User0: 1 (1)
  80. > block
  81. // Exits
  82. Transfer(1) A-B: 1 (1)
  83. Exit(1) A: 5 (1)
  84. > batch // batchNum = 6
  85. > block
  86. // this transaction should not be generated, as it's after last
  87. // batch and last block
  88. Transfer(1) User1-User0: 1 (1)
  89. `
  90. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  91. blocks, err := tc.GenerateBlocks(set)
  92. require.NoError(t, err)
  93. assert.Equal(t, 2, len(blocks))
  94. assert.Equal(t, 5, len(blocks[0].Rollup.Batches))
  95. assert.Equal(t, 1, len(blocks[1].Rollup.Batches))
  96. assert.Equal(t, 9, len(blocks[0].Rollup.L1UserTxs))
  97. assert.Equal(t, 4, len(blocks[0].Rollup.Batches[3].L1CoordinatorTxs))
  98. assert.Equal(t, 0, len(blocks[1].Rollup.L1UserTxs))
  99. // Check expected values generated by each line
  100. // #0: Deposit(1) A: 10
  101. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1,
  102. "A", "", big.NewInt(10), nil)
  103. // #1: Deposit(2) A: 20
  104. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2,
  105. "A", "", big.NewInt(20), nil)
  106. // // #2: Deposit(1) A: 20
  107. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1,
  108. "B", "", big.NewInt(5), nil)
  109. // // #3: CreateAccountDeposit(1) C: 5
  110. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1,
  111. "C", "", big.NewInt(5), nil)
  112. // // #4: CreateAccountDeposit(1) D: 5
  113. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[4], common.TxTypeCreateAccountDeposit, 1,
  114. "D", "", big.NewInt(5), nil)
  115. // #5: Transfer(1) A-B: 6 (1)
  116. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "A",
  117. "B", big.NewInt(6), common.BatchNum(3))
  118. // #6: Transfer(1) B-D: 3 (1)
  119. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[1], common.TxTypeTransfer, 1, "B",
  120. "D", big.NewInt(3), common.BatchNum(3))
  121. // #7: Transfer(1) A-D: 1 (1)
  122. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[2], common.TxTypeTransfer, 1, "A",
  123. "D", big.NewInt(1), common.BatchNum(3))
  124. // change of Batch #8: CreateAccountDepositTransfer(1) F-A: 15, 10 (3)
  125. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[5],
  126. common.TxTypeCreateAccountDepositTransfer, 1, "F", "A", big.NewInt(15), big.NewInt(10))
  127. // #9: DepositTransfer(1) A-B: 15, 10 (1)
  128. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[6], common.TxTypeDepositTransfer, 1, "A",
  129. "B", big.NewInt(15), big.NewInt(10))
  130. // #11: Transfer(1) C-A : 3 (1)
  131. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[0], common.TxTypeTransfer, 1, "C",
  132. "A", big.NewInt(3), common.BatchNum(4))
  133. // #12: Transfer(2) A-B: 15 (1)
  134. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[1], common.TxTypeTransfer, 2, "A",
  135. "B", big.NewInt(15), common.BatchNum(4))
  136. // #13: Deposit(1) User0: 20
  137. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[7], common.TxTypeCreateAccountDeposit, 1,
  138. "User0", "", big.NewInt(20), nil)
  139. // // #14: Deposit(3) User1: 20
  140. tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[8], common.TxTypeCreateAccountDeposit, 3,
  141. "User1", "", big.NewInt(20), nil)
  142. // #15: Transfer(1) User0-User1: 15 (1)
  143. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[0], common.TxTypeTransfer, 1,
  144. "User0", "User1", big.NewInt(15), common.BatchNum(5))
  145. // #16: Transfer(3) User1-User0: 15 (1)
  146. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[1], common.TxTypeTransfer, 3,
  147. "User1", "User0", big.NewInt(15), common.BatchNum(5))
  148. // #17: Transfer(1) A-C: 1 (1)
  149. tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[2], common.TxTypeTransfer, 1, "A",
  150. "C", big.NewInt(1), common.BatchNum(5))
  151. // change of Batch #18: Transfer(1) User1-User0: 1 (1)
  152. tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[0], common.TxTypeTransfer, 1,
  153. "User1", "User0", big.NewInt(1), common.BatchNum(6))
  154. // change of Block (implies also a change of batch) #19: Transfer(1) A-B: 1 (1)
  155. tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "A",
  156. "B", big.NewInt(1), common.BatchNum(6))
  157. }
  158. func (tc *Context) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType,
  159. tokenID common.TokenID, from, to string, depositAmount, amount *big.Int) {
  160. assert.Equal(t, typ, tx.Type)
  161. if tx.FromIdx != common.Idx(0) {
  162. assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
  163. }
  164. assert.Equal(t, tc.Users[from].Addr.Hex(), tx.FromEthAddr.Hex())
  165. assert.Equal(t, tc.Users[from].BJJ.Public().Compress(), tx.FromBJJ)
  166. if tx.ToIdx != common.Idx(0) {
  167. assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
  168. }
  169. if depositAmount != nil {
  170. assert.Equal(t, depositAmount, tx.DepositAmount)
  171. }
  172. if amount != nil {
  173. assert.Equal(t, amount, tx.Amount)
  174. }
  175. }
  176. func (tc *Context) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType,
  177. tokenID common.TokenID, from, to string, amount *big.Int, batchNum common.BatchNum) {
  178. assert.Equal(t, typ, tx.Type)
  179. assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)
  180. if tx.Type != common.TxTypeExit {
  181. assert.Equal(t, tc.Users[to].Accounts[tokenID].Idx, tx.ToIdx)
  182. }
  183. if amount != nil {
  184. assert.Equal(t, amount, tx.Amount)
  185. }
  186. assert.Equal(t, batchNum, tx.BatchNum)
  187. }
  188. func TestGeneratePoolL2Txs(t *testing.T) {
  189. set := `
  190. Type: Blockchain
  191. AddToken(1)
  192. AddToken(2)
  193. AddToken(3)
  194. CreateAccountDeposit(1) A: 10
  195. CreateAccountDeposit(2) A: 20
  196. CreateAccountDeposit(1) B: 5
  197. CreateAccountDeposit(1) C: 5
  198. CreateAccountDeposit(1) User0: 5
  199. CreateAccountDeposit(1) User1: 0
  200. CreateAccountDeposit(3) User0: 0
  201. CreateAccountDeposit(3) User1: 5
  202. CreateAccountDeposit(2) B: 5
  203. CreateAccountDeposit(2) D: 0
  204. > batchL1
  205. > batchL1
  206. `
  207. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  208. _, err := tc.GenerateBlocks(set)
  209. require.NoError(t, err)
  210. set = `
  211. Type: PoolL2
  212. PoolTransfer(1) A-B: 6 (1)
  213. PoolTransfer(1) B-C: 3 (1)
  214. PoolTransfer(1) C-A: 3 (1)
  215. PoolTransfer(1) A-B: 1 (1)
  216. PoolTransfer(2) A-B: 15 (1)
  217. PoolTransfer(1) User0-User1: 15 (1)
  218. PoolTransfer(3) User1-User0: 15 (1)
  219. PoolTransfer(2) B-D: 3 (1)
  220. PoolExit(1) A: 3 (1)
  221. PoolTransferToEthAddr(1) A-B: 1 (1)
  222. PoolTransferToBJJ(1) A-B: 1 (1)
  223. `
  224. poolL2Txs, err := tc.GeneratePoolL2Txs(set)
  225. require.NoError(t, err)
  226. assert.Equal(t, 11, len(poolL2Txs))
  227. assert.Equal(t, common.TxTypeTransfer, poolL2Txs[0].Type)
  228. assert.Equal(t, common.TxTypeExit, poolL2Txs[8].Type)
  229. assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[0].ToEthAddr.Hex())
  230. assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[0].ToBJJ.String())
  231. assert.Equal(t, tc.Users["User1"].Addr.Hex(), poolL2Txs[5].ToEthAddr.Hex())
  232. assert.Equal(t, tc.Users["User1"].BJJ.Public().String(), poolL2Txs[5].ToBJJ.String())
  233. assert.Equal(t, common.Nonce(0), poolL2Txs[0].Nonce)
  234. assert.Equal(t, common.Nonce(0), poolL2Txs[1].Nonce)
  235. assert.Equal(t, common.Nonce(0), poolL2Txs[2].Nonce)
  236. assert.Equal(t, common.Nonce(1), poolL2Txs[3].Nonce)
  237. assert.Equal(t, common.Nonce(0), poolL2Txs[4].Nonce)
  238. assert.Equal(t, common.Nonce(0), poolL2Txs[5].Nonce)
  239. assert.Equal(t, common.Nonce(0), poolL2Txs[6].Nonce)
  240. assert.Equal(t, common.Nonce(0), poolL2Txs[7].Nonce)
  241. assert.Equal(t, common.Nonce(2), poolL2Txs[8].Nonce)
  242. assert.Equal(t, common.Nonce(3), poolL2Txs[9].Nonce)
  243. assert.Equal(t, tc.Users["B"].Addr.Hex(), poolL2Txs[9].ToEthAddr.Hex())
  244. assert.Equal(t, common.EmptyBJJComp, poolL2Txs[9].ToBJJ)
  245. assert.Equal(t, common.TxTypeTransferToEthAddr, poolL2Txs[9].Type)
  246. assert.Equal(t, common.FFAddr, poolL2Txs[10].ToEthAddr)
  247. assert.Equal(t, tc.Users["B"].BJJ.Public().String(), poolL2Txs[10].ToBJJ.String())
  248. assert.Equal(t, common.TxTypeTransferToBJJ, poolL2Txs[10].Type)
  249. // load another set in the same Context
  250. set = `
  251. Type: PoolL2
  252. PoolTransfer(1) A-B: 6 (1)
  253. PoolTransfer(1) B-C: 3 (1)
  254. PoolTransfer(1) A-C: 3 (1)
  255. `
  256. poolL2Txs, err = tc.GeneratePoolL2Txs(set)
  257. require.NoError(t, err)
  258. assert.Equal(t, common.Nonce(5), poolL2Txs[0].Nonce)
  259. assert.Equal(t, common.Nonce(1), poolL2Txs[1].Nonce)
  260. assert.Equal(t, common.Nonce(6), poolL2Txs[2].Nonce)
  261. // check that a PoolL2Tx can be done to a non existing ToIdx
  262. set = `
  263. Type: Blockchain
  264. AddToken(1)
  265. CreateAccountDeposit(1) A: 10
  266. > batchL1
  267. > batchL1
  268. > block
  269. `
  270. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  271. _, err = tc.GenerateBlocks(set)
  272. require.NoError(t, err)
  273. set = `
  274. Type: PoolL2
  275. PoolTransferToEthAddr(1) A-B: 3 (1)
  276. PoolTransferToBJJ(1) A-C: 3 (1)
  277. `
  278. _, err = tc.GeneratePoolL2Txs(set)
  279. require.NoError(t, err)
  280. // expect error, as FromIdx=B is still not created for TokenID=1
  281. set = `
  282. Type: PoolL2
  283. PoolTransferToEthAddr(1) B-A: 3 (1)
  284. PoolTransferToBJJ(1) B-A: 3 (1)
  285. `
  286. _, err = tc.GeneratePoolL2Txs(set)
  287. require.NotNil(t, err)
  288. }
  289. func TestGeneratePoolL2TxsFromInstructions(t *testing.T) {
  290. // Generate necessary L1 data
  291. set := `
  292. Type: Blockchain
  293. AddToken(1)
  294. CreateAccountCoordinator(1) A
  295. CreateAccountDeposit(1) B: 7
  296. > batchL1
  297. > batchL1
  298. `
  299. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  300. _, err := tc.GenerateBlocks(set)
  301. require.NoError(t, err)
  302. // Generate Pool txs using instructions
  303. instructionSet := []Instruction{}
  304. i := 0
  305. a := big.NewInt(3)
  306. instructionSet = append(instructionSet, Instruction{
  307. LineNum: i,
  308. // Literal: "PoolTransferToEthAddr(1) B-A: 3 (1)",
  309. Typ: common.TxTypeTransferToEthAddr,
  310. From: "B",
  311. To: "A",
  312. TokenID: 1,
  313. Amount: a,
  314. Fee: 1,
  315. })
  316. i++
  317. instructionSet = append(instructionSet, Instruction{
  318. LineNum: i,
  319. // Literal: "PoolTransferToBJJ(1) B-A: 3 (1)",
  320. Typ: common.TxTypeTransferToBJJ,
  321. From: "B",
  322. To: "A",
  323. TokenID: 1,
  324. Amount: a,
  325. Fee: 1,
  326. })
  327. txsFromInstructions, err := tc.GeneratePoolL2TxsFromInstructions(instructionSet)
  328. require.NoError(t, err)
  329. // Generate Pool txs using string
  330. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  331. _, err = tc.GenerateBlocks(set)
  332. require.NoError(t, err)
  333. stringSet := `
  334. Type: PoolL2
  335. PoolTransferToEthAddr(1) B-A: 3 (1)
  336. PoolTransferToBJJ(1) B-A: 3 (1)
  337. `
  338. txsFromString, err := tc.GeneratePoolL2Txs(stringSet)
  339. require.NoError(t, err)
  340. // Compare generated txs from instructions and string
  341. // timestamps will be different
  342. for i := 0; i < len(txsFromString); i++ {
  343. txsFromInstructions[i].Timestamp = txsFromString[i].Timestamp
  344. }
  345. assert.Equal(t, txsFromString, txsFromInstructions)
  346. }
  347. func TestGenerateErrors(t *testing.T) {
  348. // unregistered token
  349. set := `Type: Blockchain
  350. CreateAccountDeposit(1) A: 5
  351. > batchL1
  352. `
  353. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  354. _, err := tc.GenerateBlocks(set)
  355. assert.Equal(t,
  356. "Line 2: Can not process CreateAccountDeposit: TokenID 1 not registered, "+
  357. "last registered TokenID: 0", err.Error())
  358. // ensure AddToken sequentiality and not using 0
  359. set = `
  360. Type: Blockchain
  361. AddToken(0)
  362. `
  363. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  364. _, err = tc.GenerateBlocks(set)
  365. require.Equal(t, "Line 2: AddToken can not register TokenID 0", err.Error())
  366. set = `
  367. Type: Blockchain
  368. AddToken(2)
  369. `
  370. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  371. _, err = tc.GenerateBlocks(set)
  372. require.Equal(t, "Line 2: AddToken TokenID should be sequential, expected TokenID: "+
  373. "1, defined TokenID: 2", err.Error())
  374. set = `
  375. Type: Blockchain
  376. AddToken(1)
  377. AddToken(2)
  378. AddToken(3)
  379. AddToken(5)
  380. `
  381. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  382. _, err = tc.GenerateBlocks(set)
  383. require.Equal(t, "Line 5: AddToken TokenID should be sequential, expected TokenID: "+
  384. "4, defined TokenID: 5", err.Error())
  385. // check transactions when account is not created yet
  386. set = `
  387. Type: Blockchain
  388. AddToken(1)
  389. CreateAccountDeposit(1) A: 10
  390. > batchL1
  391. CreateAccountDeposit(1) B
  392. Transfer(1) A-B: 6 (1)
  393. > batch
  394. `
  395. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  396. _, err = tc.GenerateBlocks(set)
  397. require.Equal(t, "Line 5: CreateAccountDeposit(1)BTransfer(1) A-B: 6 (1)\n, err: "+
  398. "Expected ':', found 'Transfer'", err.Error())
  399. set = `
  400. Type: Blockchain
  401. AddToken(1)
  402. CreateAccountDeposit(1) A: 10
  403. > batchL1
  404. CreateAccountCoordinator(1) B
  405. > batchL1
  406. > batch
  407. Transfer(1) A-B: 6 (1)
  408. > batch
  409. `
  410. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  411. _, err = tc.GenerateBlocks(set)
  412. require.NoError(t, err)
  413. // check nonces
  414. set = `
  415. Type: Blockchain
  416. AddToken(1)
  417. CreateAccountDeposit(1) A: 10
  418. > batchL1
  419. CreateAccountCoordinator(1) B
  420. > batchL1
  421. Transfer(1) A-B: 6 (1)
  422. Transfer(1) A-B: 6 (1) // on purpose this is moving more money that
  423. // what it has in the account, Til should not fail
  424. Transfer(1) B-A: 6 (1)
  425. Exit(1) A: 3 (1)
  426. > batch
  427. `
  428. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  429. _, err = tc.GenerateBlocks(set)
  430. require.NoError(t, err)
  431. assert.Equal(t, common.Nonce(3), tc.Users["A"].Accounts[common.TokenID(1)].Nonce)
  432. assert.Equal(t, common.Idx(256), tc.Users["A"].Accounts[common.TokenID(1)].Idx)
  433. assert.Equal(t, common.Nonce(1), tc.Users["B"].Accounts[common.TokenID(1)].Nonce)
  434. assert.Equal(t, common.Idx(257), tc.Users["B"].Accounts[common.TokenID(1)].Idx)
  435. }
  436. func TestGenerateFromInstructions(t *testing.T) {
  437. // Generate block from instructions
  438. setInst := []Instruction{}
  439. i := 0
  440. setInst = append(setInst, Instruction{
  441. LineNum: i,
  442. // Literal: "AddToken(1)",
  443. Typ: TypeAddToken,
  444. TokenID: 1,
  445. })
  446. i++
  447. da := big.NewInt(10)
  448. setInst = append(setInst, Instruction{
  449. LineNum: i,
  450. // Literal: "CreateAccountDeposit(1) A: 10",
  451. Typ: common.TxTypeCreateAccountDeposit,
  452. From: "A",
  453. TokenID: 1,
  454. DepositAmount: da,
  455. })
  456. i++
  457. setInst = append(setInst, Instruction{
  458. LineNum: i,
  459. // Literal: "> batchL1",
  460. Typ: TypeNewBatchL1,
  461. })
  462. i++
  463. setInst = append(setInst, Instruction{
  464. LineNum: i,
  465. // Literal: "CreateAccountCoordinator(1) B",
  466. Typ: TxTypeCreateAccountDepositCoordinator,
  467. From: "B",
  468. TokenID: 1,
  469. })
  470. i++
  471. setInst = append(setInst, Instruction{
  472. LineNum: i,
  473. // Literal: "> batchL1",
  474. Typ: TypeNewBatchL1,
  475. })
  476. i++
  477. a := big.NewInt(6)
  478. setInst = append(setInst, Instruction{
  479. LineNum: i, // 5
  480. // Literal: "Transfer(1) A-B: 6 (1)",
  481. Typ: common.TxTypeTransfer,
  482. From: "A",
  483. To: "B",
  484. TokenID: 1,
  485. Amount: a,
  486. Fee: 1,
  487. })
  488. i++
  489. setInst = append(setInst, Instruction{
  490. LineNum: i,
  491. // Literal: "Transfer(1) A-B: 6 (1)",
  492. Typ: common.TxTypeTransfer,
  493. From: "A",
  494. To: "B",
  495. TokenID: 1,
  496. Amount: a,
  497. Fee: 1,
  498. })
  499. i++
  500. setInst = append(setInst, Instruction{
  501. LineNum: i,
  502. // Literal: "Transfer(1) B-A: 6 (1)",
  503. Typ: common.TxTypeTransfer,
  504. From: "B",
  505. To: "A",
  506. TokenID: 1,
  507. Amount: a,
  508. Fee: 1,
  509. })
  510. i++
  511. a = big.NewInt(3)
  512. setInst = append(setInst, Instruction{
  513. LineNum: i,
  514. // Literal: "Exit(1) A: 3 (1)",
  515. Typ: common.TxTypeExit,
  516. From: "A",
  517. TokenID: 1,
  518. Amount: a,
  519. Fee: 1,
  520. })
  521. i++
  522. setInst = append(setInst, Instruction{
  523. LineNum: i,
  524. // Literal: "> batch",
  525. Typ: TypeNewBatch,
  526. })
  527. setInst = append(setInst, Instruction{
  528. LineNum: i,
  529. // Literal: "> block",
  530. Typ: TypeNewBlock,
  531. })
  532. tc := NewContext(0, common.RollupConstMaxL1UserTx)
  533. blockFromInstructions, err := tc.GenerateBlocksFromInstructions(setInst)
  534. require.NoError(t, err)
  535. // Generate block from string
  536. setString := `
  537. Type: Blockchain
  538. AddToken(1)
  539. CreateAccountDeposit(1) A: 10
  540. > batchL1
  541. CreateAccountCoordinator(1) B
  542. > batchL1
  543. Transfer(1) A-B: 6 (1)
  544. Transfer(1) A-B: 6 (1) // on purpose this is moving more money that
  545. // what it has in the account, Til should not fail
  546. Transfer(1) B-A: 6 (1)
  547. Exit(1) A: 3 (1)
  548. > batch
  549. > block
  550. `
  551. tc = NewContext(0, common.RollupConstMaxL1UserTx)
  552. blockFromString, err := tc.GenerateBlocks(setString)
  553. require.NoError(t, err)
  554. // Generated data should be equivalent, except for Eth Addrs and BJJs
  555. for i, strBatch := range blockFromString[0].Rollup.Batches {
  556. // instBatch := blockFromInstructions[0].Rollup.Batches[i]
  557. for j := 0; j < len(strBatch.L1CoordinatorTxs); j++ {
  558. blockFromInstructions[0].Rollup.Batches[i].L1CoordinatorTxs[j].FromEthAddr =
  559. blockFromString[0].Rollup.Batches[i].L1CoordinatorTxs[j].FromEthAddr
  560. blockFromInstructions[0].Rollup.Batches[i].L1CoordinatorTxs[j].FromBJJ =
  561. blockFromString[0].Rollup.Batches[i].L1CoordinatorTxs[j].FromBJJ
  562. }
  563. for j := 0; j < len(strBatch.L1UserTxs); j++ {
  564. blockFromInstructions[0].Rollup.Batches[i].L1UserTxs[j].FromEthAddr =
  565. blockFromString[0].Rollup.Batches[i].L1UserTxs[j].FromEthAddr
  566. blockFromInstructions[0].Rollup.Batches[i].L1UserTxs[j].FromBJJ =
  567. blockFromString[0].Rollup.Batches[i].L1UserTxs[j].FromBJJ
  568. }
  569. }
  570. for i := 0; i < len(blockFromString[0].Rollup.L1UserTxs); i++ {
  571. blockFromInstructions[0].Rollup.L1UserTxs[i].FromEthAddr =
  572. blockFromString[0].Rollup.L1UserTxs[i].FromEthAddr
  573. blockFromInstructions[0].Rollup.L1UserTxs[i].FromBJJ =
  574. blockFromString[0].Rollup.L1UserTxs[i].FromBJJ
  575. }
  576. assert.Equal(t, blockFromString, blockFromInstructions)
  577. }