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.

528 lines
18 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
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
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
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. "crypto/ecdsa"
  4. "fmt"
  5. "math/big"
  6. "strconv"
  7. "strings"
  8. "time"
  9. ethCommon "github.com/ethereum/go-ethereum/common"
  10. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/hermeznetwork/hermez-node/log"
  13. "github.com/iden3/go-iden3-crypto/babyjub"
  14. )
  15. // Context contains the data of the test
  16. type Context struct {
  17. Instructions []instruction
  18. accountsNames []string
  19. Users map[string]*User
  20. lastRegisteredTokenID common.TokenID
  21. l1CreatedAccounts map[string]*Account
  22. // rollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
  23. rollupConstMaxL1UserTx int
  24. idx int
  25. currBlock common.BlockData
  26. currBatch common.BatchData
  27. currBatchNum int
  28. queues [][]L1Tx
  29. toForgeNum int
  30. openToForge int
  31. currBatchTest struct {
  32. l1CoordinatorTxs []L1Tx
  33. l2Txs []L2Tx
  34. }
  35. }
  36. // NewContext returns a new Context
  37. func NewContext(rollupConstMaxL1UserTx int) *Context {
  38. currBatchNum := 1 // The protocol defines the first batchNum to be 1
  39. return &Context{
  40. Users: make(map[string]*User),
  41. l1CreatedAccounts: make(map[string]*Account),
  42. lastRegisteredTokenID: 0,
  43. rollupConstMaxL1UserTx: rollupConstMaxL1UserTx,
  44. idx: common.UserThreshold,
  45. // We use some placeholder values for StateRoot and ExitTree
  46. // because these values will never be nil
  47. currBatch: common.BatchData{Batch: common.Batch{
  48. BatchNum: common.BatchNum(currBatchNum),
  49. StateRoot: big.NewInt(0), ExitRoot: big.NewInt(0)}},
  50. currBatchNum: currBatchNum,
  51. // start with 2 queues, one for toForge, and the other for openToForge
  52. queues: make([][]L1Tx, 2),
  53. toForgeNum: 0,
  54. openToForge: 1,
  55. }
  56. }
  57. // Account contains the data related to the account for a specific TokenID of a User
  58. type Account struct {
  59. Idx common.Idx
  60. Nonce common.Nonce
  61. }
  62. // User contains the data related to a testing user
  63. type User struct {
  64. BJJ *babyjub.PrivateKey
  65. Addr ethCommon.Address
  66. Accounts map[common.TokenID]*Account
  67. }
  68. // L1Tx is the data structure used internally for transaction test generation,
  69. // which contains a common.L1Tx data plus some intermediate data for the
  70. // transaction generation.
  71. type L1Tx struct {
  72. lineNum int
  73. fromIdxName string
  74. toIdxName string
  75. L1Tx common.L1Tx
  76. }
  77. // L2Tx is the data structure used internally for transaction test generation,
  78. // which contains a common.L2Tx data plus some intermediate data for the
  79. // transaction generation.
  80. type L2Tx struct {
  81. lineNum int
  82. fromIdxName string
  83. toIdxName string
  84. tokenID common.TokenID
  85. L2Tx common.L2Tx
  86. }
  87. // GenerateBlocks returns an array of BlockData for a given set. It uses the
  88. // accounts (keys & nonces) of the Context.
  89. func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
  90. parser := newParser(strings.NewReader(set))
  91. parsedSet, err := parser.parse()
  92. if err != nil {
  93. return nil, err
  94. }
  95. tc.Instructions = parsedSet.instructions
  96. tc.accountsNames = parsedSet.accounts
  97. tc.generateKeys(tc.accountsNames)
  98. var blocks []common.BlockData
  99. for _, inst := range parsedSet.instructions {
  100. switch inst.typ {
  101. case txTypeCreateAccountDepositCoordinator:
  102. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  103. log.Error(err)
  104. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  105. }
  106. tx := common.L1Tx{
  107. FromEthAddr: tc.Users[inst.from].Addr,
  108. FromBJJ: tc.Users[inst.from].BJJ.Public(),
  109. TokenID: inst.tokenID,
  110. Amount: big.NewInt(0),
  111. LoadAmount: big.NewInt(int64(inst.loadAmount)),
  112. Type: common.TxTypeCreateAccountDeposit, // as txTypeCreateAccountDepositCoordinator is not valid oustide Til package
  113. }
  114. testTx := L1Tx{
  115. lineNum: inst.lineNum,
  116. fromIdxName: inst.from,
  117. L1Tx: tx,
  118. }
  119. tc.currBatchTest.l1CoordinatorTxs = append(tc.currBatchTest.l1CoordinatorTxs, testTx)
  120. case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer:
  121. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  122. log.Error(err)
  123. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  124. }
  125. tx := common.L1Tx{
  126. FromEthAddr: tc.Users[inst.from].Addr,
  127. FromBJJ: tc.Users[inst.from].BJJ.Public(),
  128. TokenID: inst.tokenID,
  129. Amount: big.NewInt(0),
  130. LoadAmount: big.NewInt(int64(inst.loadAmount)),
  131. Type: inst.typ,
  132. }
  133. if inst.typ == common.TxTypeCreateAccountDepositTransfer {
  134. tx.Amount = big.NewInt(int64(inst.amount))
  135. }
  136. testTx := L1Tx{
  137. lineNum: inst.lineNum,
  138. fromIdxName: inst.from,
  139. toIdxName: inst.to,
  140. L1Tx: tx,
  141. }
  142. tc.addToL1Queue(testTx)
  143. case common.TxTypeDeposit, common.TxTypeDepositTransfer:
  144. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  145. log.Error(err)
  146. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  147. }
  148. if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
  149. log.Error(err)
  150. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  151. }
  152. tx := common.L1Tx{
  153. TokenID: inst.tokenID,
  154. Amount: big.NewInt(0),
  155. LoadAmount: big.NewInt(int64(inst.loadAmount)),
  156. Type: inst.typ,
  157. }
  158. if inst.typ == common.TxTypeDepositTransfer {
  159. tx.Amount = big.NewInt(int64(inst.amount))
  160. }
  161. testTx := L1Tx{
  162. lineNum: inst.lineNum,
  163. fromIdxName: inst.from,
  164. toIdxName: inst.to,
  165. L1Tx: tx,
  166. }
  167. tc.addToL1Queue(testTx)
  168. case common.TxTypeTransfer:
  169. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  170. log.Error(err)
  171. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  172. }
  173. tx := common.L2Tx{
  174. Amount: big.NewInt(int64(inst.amount)),
  175. Fee: common.FeeSelector(inst.fee),
  176. Type: common.TxTypeTransfer,
  177. }
  178. tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
  179. testTx := L2Tx{
  180. lineNum: inst.lineNum,
  181. fromIdxName: inst.from,
  182. toIdxName: inst.to,
  183. tokenID: inst.tokenID,
  184. L2Tx: tx,
  185. }
  186. tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
  187. case common.TxTypeExit:
  188. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  189. log.Error(err)
  190. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  191. }
  192. tx := common.L2Tx{
  193. ToIdx: common.Idx(1), // as is an Exit
  194. Amount: big.NewInt(int64(inst.amount)),
  195. Type: common.TxTypeExit,
  196. }
  197. tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
  198. testTx := L2Tx{
  199. lineNum: inst.lineNum,
  200. fromIdxName: inst.from,
  201. toIdxName: inst.to,
  202. tokenID: inst.tokenID,
  203. L2Tx: tx,
  204. }
  205. tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
  206. case common.TxTypeForceExit:
  207. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  208. log.Error(err)
  209. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  210. }
  211. tx := common.L1Tx{
  212. ToIdx: common.Idx(1), // as is an Exit
  213. TokenID: inst.tokenID,
  214. Amount: big.NewInt(int64(inst.amount)),
  215. LoadAmount: big.NewInt(0),
  216. Type: common.TxTypeExit,
  217. }
  218. testTx := L1Tx{
  219. lineNum: inst.lineNum,
  220. fromIdxName: inst.from,
  221. toIdxName: inst.to,
  222. L1Tx: tx,
  223. }
  224. tc.addToL1Queue(testTx)
  225. case typeNewBatch:
  226. if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
  227. return nil, err
  228. }
  229. if err = tc.setIdxs(); err != nil {
  230. log.Error(err)
  231. return nil, err
  232. }
  233. case typeNewBatchL1:
  234. // for each L1UserTx of the queues[ToForgeNum], calculate the Idx
  235. if err = tc.calculateIdxForL1Txs(false, tc.queues[tc.toForgeNum]); err != nil {
  236. return nil, err
  237. }
  238. if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
  239. return nil, err
  240. }
  241. // once Idxs are calculated, update transactions to use the real Idxs
  242. for i := 0; i < len(tc.queues[tc.toForgeNum]); i++ {
  243. testTx := &tc.queues[tc.toForgeNum][i]
  244. if testTx.L1Tx.Type != common.TxTypeCreateAccountDeposit && testTx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
  245. testTx.L1Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.L1Tx.TokenID].Idx
  246. }
  247. testTx.L1Tx.FromEthAddr = tc.Users[testTx.fromIdxName].Addr
  248. testTx.L1Tx.FromBJJ = tc.Users[testTx.fromIdxName].BJJ.Public()
  249. if testTx.toIdxName == "" {
  250. testTx.L1Tx.ToIdx = common.Idx(0)
  251. } else {
  252. testTx.L1Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.L1Tx.TokenID].Idx
  253. }
  254. if testTx.L1Tx.Type == common.TxTypeExit {
  255. testTx.L1Tx.ToIdx = common.Idx(1)
  256. }
  257. // bn := common.BatchNum(tc.currBatchNum)
  258. // testTx.L1Tx.BatchNum = &bn
  259. nTx, err := common.NewL1Tx(&testTx.L1Tx)
  260. if err != nil {
  261. fmt.Println(testTx)
  262. return nil, fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
  263. }
  264. testTx.L1Tx = *nTx
  265. tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, testTx.L1Tx)
  266. }
  267. if err = tc.setIdxs(); err != nil {
  268. log.Error(err)
  269. return nil, err
  270. }
  271. // advance batch
  272. tc.toForgeNum++
  273. if tc.toForgeNum == tc.openToForge {
  274. tc.openToForge++
  275. newQueue := []L1Tx{}
  276. tc.queues = append(tc.queues, newQueue)
  277. }
  278. case typeNewBlock:
  279. blocks = append(blocks, tc.currBlock)
  280. tc.currBlock = common.BlockData{}
  281. case typeAddToken:
  282. newToken := common.Token{
  283. TokenID: inst.tokenID,
  284. EthBlockNum: int64(len(blocks)),
  285. }
  286. if inst.tokenID != tc.lastRegisteredTokenID+1 {
  287. return nil, fmt.Errorf("Line %d: AddToken TokenID should be sequential, expected TokenID: %d, defined TokenID: %d", inst.lineNum, tc.lastRegisteredTokenID+1, inst.tokenID)
  288. }
  289. tc.lastRegisteredTokenID++
  290. tc.currBlock.AddedTokens = append(tc.currBlock.AddedTokens, newToken)
  291. default:
  292. return nil, fmt.Errorf("Line %d: Unexpected type: %s", inst.lineNum, inst.typ)
  293. }
  294. }
  295. return blocks, nil
  296. }
  297. // calculateIdxsForL1Txs calculates new Idx for new created accounts. If
  298. // 'isCoordinatorTxs==true', adds the tx to tc.currBatch.L1CoordinatorTxs.
  299. func (tc *Context) calculateIdxForL1Txs(isCoordinatorTxs bool, txs []L1Tx) error {
  300. // for each batch.L1CoordinatorTxs of the queues[ToForgeNum], calculate the Idx
  301. for i := 0; i < len(txs); i++ {
  302. tx := txs[i]
  303. if tx.L1Tx.Type == common.TxTypeCreateAccountDeposit || tx.L1Tx.Type == common.TxTypeCreateAccountDepositTransfer {
  304. if tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] != nil { // if account already exists, return error
  305. return fmt.Errorf("Can not create same account twice (same User & same TokenID) (this is a design property of Til)")
  306. }
  307. tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] = &Account{
  308. Idx: common.Idx(tc.idx),
  309. Nonce: common.Nonce(0),
  310. }
  311. tc.l1CreatedAccounts[idxTokenIDToString(tx.fromIdxName, tx.L1Tx.TokenID)] = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID]
  312. tc.idx++
  313. }
  314. if isCoordinatorTxs {
  315. tc.currBatch.L1CoordinatorTxs = append(tc.currBatch.L1CoordinatorTxs, tx.L1Tx)
  316. }
  317. }
  318. return nil
  319. }
  320. // setIdxs sets the Idxs to the transactions of the tc.currBatch
  321. func (tc *Context) setIdxs() error {
  322. // once Idxs are calculated, update transactions to use the new Idxs
  323. for i := 0; i < len(tc.currBatchTest.l2Txs); i++ {
  324. testTx := &tc.currBatchTest.l2Txs[i]
  325. if tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID] == nil {
  326. return fmt.Errorf("Line %d: %s from User %s for TokenID %d while account not created yet", testTx.lineNum, testTx.L2Tx.Type, testTx.fromIdxName, testTx.tokenID)
  327. }
  328. if testTx.L2Tx.Type == common.TxTypeTransfer {
  329. if _, ok := tc.l1CreatedAccounts[idxTokenIDToString(testTx.toIdxName, testTx.tokenID)]; !ok {
  330. return fmt.Errorf("Line %d: Can not create Transfer for a non existing account. Batch %d, ToIdx name: %s, TokenID: %d", testTx.lineNum, tc.currBatchNum, testTx.toIdxName, testTx.tokenID)
  331. }
  332. }
  333. tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce++
  334. testTx.L2Tx.Nonce = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce
  335. // set real Idx
  336. testTx.L2Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Idx
  337. if testTx.L2Tx.Type == common.TxTypeTransfer {
  338. testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx
  339. }
  340. // in case Type==Exit, ToIdx=1, already set at the
  341. // GenerateBlocks main switch inside TxTypeExit case
  342. nTx, err := common.NewL2Tx(&testTx.L2Tx)
  343. if err != nil {
  344. return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
  345. }
  346. testTx.L2Tx = *nTx
  347. tc.currBatch.L2Txs = append(tc.currBatch.L2Txs, testTx.L2Tx)
  348. }
  349. tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
  350. tc.currBatchNum++
  351. tc.currBatch = common.BatchData{Batch: tc.currBatch.Batch}
  352. tc.currBatch.Batch.BatchNum = common.BatchNum(tc.currBatchNum)
  353. tc.currBatchTest.l1CoordinatorTxs = nil
  354. tc.currBatchTest.l2Txs = nil
  355. return nil
  356. }
  357. // addToL1Queue adds the L1Tx into the queue that is open and has space
  358. func (tc *Context) addToL1Queue(tx L1Tx) {
  359. if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
  360. // if current OpenToForge queue reached its Max, move into a
  361. // new queue
  362. tc.openToForge++
  363. newQueue := []L1Tx{}
  364. tc.queues = append(tc.queues, newQueue)
  365. }
  366. tx.L1Tx.UserOrigin = true
  367. toForgeL1TxsNum := int64(tc.openToForge)
  368. tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
  369. tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
  370. }
  371. func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {
  372. if tc.Users[tf].Accounts[inst.tokenID] == nil {
  373. return fmt.Errorf("%s at User: %s, for TokenID: %d, while account not created yet", inst.typ, tf, inst.tokenID)
  374. }
  375. return nil
  376. }
  377. func (tc *Context) checkIfTokenIsRegistered(inst instruction) error {
  378. if inst.tokenID > tc.lastRegisteredTokenID {
  379. return fmt.Errorf("Can not process %s: TokenID %d not registered, last registered TokenID: %d", inst.typ, inst.tokenID, tc.lastRegisteredTokenID)
  380. }
  381. return nil
  382. }
  383. // GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
  384. // uses the accounts (keys & nonces) of the Context.
  385. func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
  386. parser := newParser(strings.NewReader(set))
  387. parsedSet, err := parser.parse()
  388. if err != nil {
  389. return nil, err
  390. }
  391. tc.Instructions = parsedSet.instructions
  392. tc.accountsNames = parsedSet.accounts
  393. tc.generateKeys(tc.accountsNames)
  394. txs := []common.PoolL2Tx{}
  395. for _, inst := range tc.Instructions {
  396. switch inst.typ {
  397. case common.TxTypeTransfer:
  398. if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
  399. log.Error(err)
  400. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  401. }
  402. if err := tc.checkIfAccountExists(inst.to, inst); err != nil {
  403. log.Error(err)
  404. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  405. }
  406. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  407. // if account of receiver does not exist, don't use
  408. // ToIdx, and use only ToEthAddr & ToBJJ
  409. tx := common.PoolL2Tx{
  410. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  411. ToIdx: tc.Users[inst.to].Accounts[inst.tokenID].Idx,
  412. ToEthAddr: tc.Users[inst.to].Addr,
  413. ToBJJ: tc.Users[inst.to].BJJ.Public(),
  414. TokenID: inst.tokenID,
  415. Amount: big.NewInt(int64(inst.amount)),
  416. Fee: common.FeeSelector(inst.fee),
  417. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  418. State: common.PoolL2TxStatePending,
  419. Timestamp: time.Now(),
  420. RqToEthAddr: common.EmptyAddr,
  421. RqToBJJ: nil,
  422. Type: common.TxTypeTransfer,
  423. }
  424. nTx, err := common.NewPoolL2Tx(&tx)
  425. if err != nil {
  426. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  427. }
  428. tx = *nTx
  429. // perform signature and set it to tx.Signature
  430. toSign, err := tx.HashToSign()
  431. if err != nil {
  432. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  433. }
  434. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  435. tx.Signature = sig.Compress()
  436. txs = append(txs, tx)
  437. case common.TxTypeExit:
  438. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  439. tx := common.PoolL2Tx{
  440. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  441. ToIdx: common.Idx(1), // as is an Exit
  442. TokenID: inst.tokenID,
  443. Amount: big.NewInt(int64(inst.amount)),
  444. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  445. Type: common.TxTypeExit,
  446. }
  447. nTx, err := common.NewPoolL2Tx(&tx)
  448. if err != nil {
  449. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  450. }
  451. tx = *nTx
  452. // perform signature and set it to tx.Signature
  453. toSign, err := tx.HashToSign()
  454. if err != nil {
  455. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  456. }
  457. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  458. tx.Signature = sig.Compress()
  459. txs = append(txs, tx)
  460. default:
  461. return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ)
  462. }
  463. }
  464. return txs, nil
  465. }
  466. // generateKeys generates BabyJubJub & Address keys for the given list of
  467. // account names in a deterministic way. This means, that for the same given
  468. // 'accNames' in a certain order, the keys will be always the same.
  469. func (tc *Context) generateKeys(accNames []string) {
  470. for i := 1; i < len(accNames)+1; i++ {
  471. if _, ok := tc.Users[accNames[i-1]]; ok {
  472. // account already created
  473. continue
  474. }
  475. // babyjubjub key
  476. var sk babyjub.PrivateKey
  477. copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
  478. // eth address
  479. var key ecdsa.PrivateKey
  480. key.D = big.NewInt(int64(i)) // only for testing
  481. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  482. key.Curve = ethCrypto.S256()
  483. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  484. u := User{
  485. BJJ: &sk,
  486. Addr: addr,
  487. Accounts: make(map[common.TokenID]*Account),
  488. }
  489. tc.Users[accNames[i-1]] = &u
  490. }
  491. }