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.

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