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.

577 lines
19 KiB

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