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.

610 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. Fee: common.FeeSelector(inst.fee),
  242. Amount: big.NewInt(int64(inst.amount)),
  243. Type: common.TxTypeExit,
  244. EthBlockNum: tc.blockNum,
  245. }
  246. tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
  247. testTx := L2Tx{
  248. lineNum: inst.lineNum,
  249. fromIdxName: inst.from,
  250. toIdxName: inst.to,
  251. tokenID: inst.tokenID,
  252. L2Tx: tx,
  253. }
  254. tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
  255. case common.TxTypeForceExit: // tx source: L1UserTx
  256. if err := tc.checkIfTokenIsRegistered(inst); err != nil {
  257. log.Error(err)
  258. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  259. }
  260. tx := common.L1Tx{
  261. ToIdx: common.Idx(1), // as is an Exit
  262. TokenID: inst.tokenID,
  263. Amount: big.NewInt(int64(inst.amount)),
  264. LoadAmount: big.NewInt(0),
  265. Type: common.TxTypeForceExit,
  266. }
  267. testTx := L1Tx{
  268. lineNum: inst.lineNum,
  269. fromIdxName: inst.from,
  270. toIdxName: inst.to,
  271. L1Tx: tx,
  272. }
  273. if err := tc.addToL1Queue(testTx); err != nil {
  274. return nil, err
  275. }
  276. case typeNewBatch:
  277. if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
  278. return nil, err
  279. }
  280. if err = tc.setIdxs(); err != nil {
  281. log.Error(err)
  282. return nil, err
  283. }
  284. case typeNewBatchL1:
  285. // for each L1UserTx of the queues[ToForgeNum], calculate the Idx
  286. if err = tc.calculateIdxForL1Txs(false, tc.queues[tc.toForgeNum]); err != nil {
  287. return nil, err
  288. }
  289. if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
  290. return nil, err
  291. }
  292. tc.currBatch.L1Batch = true
  293. if err = tc.setIdxs(); err != nil {
  294. log.Error(err)
  295. return nil, err
  296. }
  297. // advance batch
  298. tc.toForgeNum++
  299. if tc.toForgeNum == tc.openToForge {
  300. tc.openToForge++
  301. newQueue := []L1Tx{}
  302. tc.queues = append(tc.queues, newQueue)
  303. }
  304. case typeNewBlock:
  305. blocks = append(blocks, tc.currBlock)
  306. tc.blockNum++
  307. tc.currBlock = newBlock(tc.blockNum)
  308. case typeAddToken:
  309. newToken := common.Token{
  310. EthAddr: ethCommon.BigToAddress(big.NewInt(int64(inst.tokenID * 100))), //nolint:gomnd
  311. // Name: fmt.Sprintf("Token %d", inst.tokenID),
  312. // Symbol: fmt.Sprintf("TK%d", inst.tokenID),
  313. // Decimals: 18,
  314. TokenID: inst.tokenID,
  315. EthBlockNum: tc.blockNum,
  316. }
  317. if inst.tokenID != tc.LastRegisteredTokenID+1 {
  318. return nil, fmt.Errorf("Line %d: AddToken TokenID should be sequential, expected TokenID: %d, defined TokenID: %d", inst.lineNum, tc.LastRegisteredTokenID+1, inst.tokenID)
  319. }
  320. tc.LastRegisteredTokenID++
  321. tc.currBlock.AddedTokens = append(tc.currBlock.AddedTokens, newToken)
  322. default:
  323. return nil, fmt.Errorf("Line %d: Unexpected type: %s", inst.lineNum, inst.typ)
  324. }
  325. }
  326. return blocks, nil
  327. }
  328. // calculateIdxsForL1Txs calculates new Idx for new created accounts. If
  329. // 'isCoordinatorTxs==true', adds the tx to tc.currBatch.L1CoordinatorTxs.
  330. func (tc *Context) calculateIdxForL1Txs(isCoordinatorTxs bool, txs []L1Tx) error {
  331. // for each batch.L1CoordinatorTxs of the queues[ToForgeNum], calculate the Idx
  332. for i := 0; i < len(txs); i++ {
  333. tx := txs[i]
  334. if tx.L1Tx.Type == common.TxTypeCreateAccountDeposit || tx.L1Tx.Type == common.TxTypeCreateAccountDepositTransfer {
  335. if tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] != nil { // if account already exists, return error
  336. return fmt.Errorf("Can not create same account twice (same User & same TokenID) (this is a design property of Til)")
  337. }
  338. tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] = &Account{
  339. Idx: common.Idx(tc.idx),
  340. Nonce: common.Nonce(0),
  341. }
  342. tc.l1CreatedAccounts[idxTokenIDToString(tx.fromIdxName, tx.L1Tx.TokenID)] = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID]
  343. tc.idx++
  344. }
  345. if isCoordinatorTxs {
  346. tc.currBatch.L1CoordinatorTxs = append(tc.currBatch.L1CoordinatorTxs, tx.L1Tx)
  347. }
  348. }
  349. return nil
  350. }
  351. // setIdxs sets the Idxs to the transactions of the tc.currBatch
  352. func (tc *Context) setIdxs() error {
  353. // once Idxs are calculated, update transactions to use the new Idxs
  354. for i := 0; i < len(tc.currBatchTest.l2Txs); i++ {
  355. testTx := &tc.currBatchTest.l2Txs[i]
  356. if tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID] == nil {
  357. 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)
  358. }
  359. if testTx.L2Tx.Type == common.TxTypeTransfer {
  360. if _, ok := tc.l1CreatedAccounts[idxTokenIDToString(testTx.toIdxName, testTx.tokenID)]; !ok {
  361. 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)
  362. }
  363. }
  364. tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce++
  365. // next line is commented to avoid Blockchain L2Txs to have
  366. // Nonce different from 0, as from Blockchain those
  367. // transactions will come without Nonce
  368. // testTx.L2Tx.Nonce = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce
  369. // set real Idx
  370. testTx.L2Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Idx
  371. if testTx.L2Tx.Type == common.TxTypeTransfer {
  372. testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx
  373. }
  374. // in case Type==Exit, ToIdx=1, already set at the
  375. // GenerateBlocks main switch inside TxTypeExit case
  376. nTx, err := common.NewL2Tx(&testTx.L2Tx)
  377. if err != nil {
  378. return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
  379. }
  380. testTx.L2Tx = *nTx
  381. tc.currBatch.L2Txs = append(tc.currBatch.L2Txs, testTx.L2Tx)
  382. }
  383. tc.currBatch.Batch.LastIdx = int64(tc.idx - 1) // `-1` because tc.idx is the next available idx
  384. tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
  385. tc.currBatchNum++
  386. tc.currBatch = newBatchData(tc.currBatchNum)
  387. tc.currBatchTest.l1CoordinatorTxs = nil
  388. tc.currBatchTest.l2Txs = nil
  389. return nil
  390. }
  391. // addToL1Queue adds the L1Tx into the queue that is open and has space
  392. func (tc *Context) addToL1Queue(tx L1Tx) error {
  393. if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
  394. // if current OpenToForge queue reached its Max, move into a
  395. // new queue
  396. tc.openToForge++
  397. newQueue := []L1Tx{}
  398. tc.queues = append(tc.queues, newQueue)
  399. }
  400. // Fill L1UserTx specific parameters
  401. tx.L1Tx.UserOrigin = true
  402. toForgeL1TxsNum := int64(tc.openToForge)
  403. tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
  404. tx.L1Tx.EthBlockNum = tc.blockNum
  405. tx.L1Tx.Position = len(tc.queues[tc.openToForge])
  406. // When an L1UserTx is generated, all idxs must be available (except when idx == 0 or idx == 1)
  407. if tx.L1Tx.Type != common.TxTypeCreateAccountDeposit && tx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
  408. tx.L1Tx.FromIdx = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID].Idx
  409. }
  410. tx.L1Tx.FromEthAddr = tc.Users[tx.fromIdxName].Addr
  411. tx.L1Tx.FromBJJ = tc.Users[tx.fromIdxName].BJJ.Public()
  412. if tx.toIdxName == "" {
  413. tx.L1Tx.ToIdx = common.Idx(0)
  414. } else {
  415. account, ok := tc.Users[tx.toIdxName].Accounts[tx.L1Tx.TokenID]
  416. if !ok {
  417. return fmt.Errorf("Line %d: Transfer to User: %s, for TokenID: %d, "+
  418. "while account not created yet", tx.lineNum, tx.toIdxName, tx.L1Tx.TokenID)
  419. }
  420. tx.L1Tx.ToIdx = account.Idx
  421. }
  422. if tx.L1Tx.Type == common.TxTypeForceExit {
  423. tx.L1Tx.ToIdx = common.Idx(1)
  424. }
  425. nTx, err := common.NewL1Tx(&tx.L1Tx)
  426. if err != nil {
  427. return fmt.Errorf("Line %d: %s", tx.lineNum, err.Error())
  428. }
  429. tx.L1Tx = *nTx
  430. tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
  431. tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, tx.L1Tx)
  432. return nil
  433. }
  434. func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {
  435. if tc.Users[tf].Accounts[inst.tokenID] == nil {
  436. return fmt.Errorf("%s at User: %s, for TokenID: %d, while account not created yet", inst.typ, tf, inst.tokenID)
  437. }
  438. return nil
  439. }
  440. func (tc *Context) checkIfTokenIsRegistered(inst instruction) error {
  441. if inst.tokenID > tc.LastRegisteredTokenID {
  442. return fmt.Errorf("Can not process %s: TokenID %d not registered, last registered TokenID: %d", inst.typ, inst.tokenID, tc.LastRegisteredTokenID)
  443. }
  444. return nil
  445. }
  446. // GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
  447. // uses the users (keys) of the Context.
  448. func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
  449. parser := newParser(strings.NewReader(set))
  450. parsedSet, err := parser.parse()
  451. if err != nil {
  452. return nil, err
  453. }
  454. if parsedSet.typ != setTypePoolL2 {
  455. return nil, fmt.Errorf("Expected set type: %s, found: %s", setTypePoolL2, parsedSet.typ)
  456. }
  457. tc.Instructions = parsedSet.instructions
  458. tc.userNames = parsedSet.users
  459. tc.generateKeys(tc.userNames)
  460. txs := []common.PoolL2Tx{}
  461. for _, inst := range tc.Instructions {
  462. switch inst.typ {
  463. case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ:
  464. if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
  465. log.Error(err)
  466. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  467. }
  468. if inst.typ == common.TxTypeTransfer {
  469. // if TxTypeTransfer, need to exist the ToIdx account
  470. if err := tc.checkIfAccountExists(inst.to, inst); err != nil {
  471. log.Error(err)
  472. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  473. }
  474. }
  475. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  476. // if account of receiver does not exist, don't use
  477. // ToIdx, and use only ToEthAddr & ToBJJ
  478. tx := common.PoolL2Tx{
  479. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  480. TokenID: inst.tokenID,
  481. Amount: big.NewInt(int64(inst.amount)),
  482. Fee: common.FeeSelector(inst.fee),
  483. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  484. State: common.PoolL2TxStatePending,
  485. Timestamp: time.Now(),
  486. RqToEthAddr: common.EmptyAddr,
  487. RqToBJJ: nil,
  488. Type: inst.typ,
  489. }
  490. if tx.Type == common.TxTypeTransfer {
  491. tx.ToIdx = tc.Users[inst.to].Accounts[inst.tokenID].Idx
  492. tx.ToEthAddr = tc.Users[inst.to].Addr
  493. tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
  494. } else if tx.Type == common.TxTypeTransferToEthAddr {
  495. tx.ToIdx = common.Idx(0)
  496. tx.ToEthAddr = tc.Users[inst.to].Addr
  497. } else if tx.Type == common.TxTypeTransferToBJJ {
  498. tx.ToIdx = common.Idx(0)
  499. tx.ToEthAddr = common.FFAddr
  500. tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
  501. }
  502. nTx, err := common.NewPoolL2Tx(&tx)
  503. if err != nil {
  504. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  505. }
  506. tx = *nTx
  507. // perform signature and set it to tx.Signature
  508. toSign, err := tx.HashToSign()
  509. if err != nil {
  510. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  511. }
  512. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  513. tx.Signature = sig.Compress()
  514. txs = append(txs, tx)
  515. case common.TxTypeExit:
  516. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  517. tx := common.PoolL2Tx{
  518. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  519. ToIdx: common.Idx(1), // as is an Exit
  520. TokenID: inst.tokenID,
  521. Amount: big.NewInt(int64(inst.amount)),
  522. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  523. Type: common.TxTypeExit,
  524. }
  525. nTx, err := common.NewPoolL2Tx(&tx)
  526. if err != nil {
  527. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  528. }
  529. tx = *nTx
  530. // perform signature and set it to tx.Signature
  531. toSign, err := tx.HashToSign()
  532. if err != nil {
  533. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  534. }
  535. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  536. tx.Signature = sig.Compress()
  537. txs = append(txs, tx)
  538. default:
  539. return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ)
  540. }
  541. }
  542. return txs, nil
  543. }
  544. // generateKeys generates BabyJubJub & Address keys for the given list of user
  545. // names in a deterministic way. This means, that for the same given
  546. // 'userNames' in a certain order, the keys will be always the same.
  547. func (tc *Context) generateKeys(userNames []string) {
  548. for i := 1; i < len(userNames)+1; i++ {
  549. if _, ok := tc.Users[userNames[i-1]]; ok {
  550. // account already created
  551. continue
  552. }
  553. // babyjubjub key
  554. var sk babyjub.PrivateKey
  555. copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
  556. // eth address
  557. var key ecdsa.PrivateKey
  558. key.D = big.NewInt(int64(i)) // only for testing
  559. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  560. key.Curve = ethCrypto.S256()
  561. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  562. u := User{
  563. BJJ: &sk,
  564. Addr: addr,
  565. Accounts: make(map[common.TokenID]*Account),
  566. }
  567. tc.Users[userNames[i-1]] = &u
  568. }
  569. }