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.

607 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. testTx.L2Tx.Nonce = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce
  366. // set real Idx
  367. testTx.L2Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Idx
  368. if testTx.L2Tx.Type == common.TxTypeTransfer {
  369. testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx
  370. }
  371. // in case Type==Exit, ToIdx=1, already set at the
  372. // GenerateBlocks main switch inside TxTypeExit case
  373. nTx, err := common.NewL2Tx(&testTx.L2Tx)
  374. if err != nil {
  375. return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
  376. }
  377. testTx.L2Tx = *nTx
  378. tc.currBatch.L2Txs = append(tc.currBatch.L2Txs, testTx.L2Tx)
  379. }
  380. tc.currBatch.Batch.LastIdx = int64(tc.idx - 1) // `-1` because tc.idx is the next available idx
  381. tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
  382. tc.currBatchNum++
  383. tc.currBatch = newBatchData(tc.currBatchNum)
  384. tc.currBatchTest.l1CoordinatorTxs = nil
  385. tc.currBatchTest.l2Txs = nil
  386. return nil
  387. }
  388. // addToL1Queue adds the L1Tx into the queue that is open and has space
  389. func (tc *Context) addToL1Queue(tx L1Tx) error {
  390. if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
  391. // if current OpenToForge queue reached its Max, move into a
  392. // new queue
  393. tc.openToForge++
  394. newQueue := []L1Tx{}
  395. tc.queues = append(tc.queues, newQueue)
  396. }
  397. // Fill L1UserTx specific parameters
  398. tx.L1Tx.UserOrigin = true
  399. toForgeL1TxsNum := int64(tc.openToForge)
  400. tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
  401. tx.L1Tx.EthBlockNum = tc.blockNum
  402. tx.L1Tx.Position = len(tc.queues[tc.openToForge])
  403. // When an L1UserTx is generated, all idxs must be available (except when idx == 0 or idx == 1)
  404. if tx.L1Tx.Type != common.TxTypeCreateAccountDeposit && tx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
  405. tx.L1Tx.FromIdx = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID].Idx
  406. }
  407. tx.L1Tx.FromEthAddr = tc.Users[tx.fromIdxName].Addr
  408. tx.L1Tx.FromBJJ = tc.Users[tx.fromIdxName].BJJ.Public()
  409. if tx.toIdxName == "" {
  410. tx.L1Tx.ToIdx = common.Idx(0)
  411. } else {
  412. account, ok := tc.Users[tx.toIdxName].Accounts[tx.L1Tx.TokenID]
  413. if !ok {
  414. return fmt.Errorf("Line %d: Transfer to User: %s, for TokenID: %d, "+
  415. "while account not created yet", tx.lineNum, tx.toIdxName, tx.L1Tx.TokenID)
  416. }
  417. tx.L1Tx.ToIdx = account.Idx
  418. }
  419. if tx.L1Tx.Type == common.TxTypeForceExit {
  420. tx.L1Tx.ToIdx = common.Idx(1)
  421. }
  422. nTx, err := common.NewL1Tx(&tx.L1Tx)
  423. if err != nil {
  424. return fmt.Errorf("Line %d: %s", tx.lineNum, err.Error())
  425. }
  426. tx.L1Tx = *nTx
  427. tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
  428. tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, tx.L1Tx)
  429. return nil
  430. }
  431. func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {
  432. if tc.Users[tf].Accounts[inst.tokenID] == nil {
  433. return fmt.Errorf("%s at User: %s, for TokenID: %d, while account not created yet", inst.typ, tf, inst.tokenID)
  434. }
  435. return nil
  436. }
  437. func (tc *Context) checkIfTokenIsRegistered(inst instruction) error {
  438. if inst.tokenID > tc.LastRegisteredTokenID {
  439. return fmt.Errorf("Can not process %s: TokenID %d not registered, last registered TokenID: %d", inst.typ, inst.tokenID, tc.LastRegisteredTokenID)
  440. }
  441. return nil
  442. }
  443. // GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
  444. // uses the users (keys) of the Context.
  445. func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
  446. parser := newParser(strings.NewReader(set))
  447. parsedSet, err := parser.parse()
  448. if err != nil {
  449. return nil, err
  450. }
  451. if parsedSet.typ != setTypePoolL2 {
  452. return nil, fmt.Errorf("Expected set type: %s, found: %s", setTypePoolL2, parsedSet.typ)
  453. }
  454. tc.Instructions = parsedSet.instructions
  455. tc.userNames = parsedSet.users
  456. tc.generateKeys(tc.userNames)
  457. txs := []common.PoolL2Tx{}
  458. for _, inst := range tc.Instructions {
  459. switch inst.typ {
  460. case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ:
  461. if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
  462. log.Error(err)
  463. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  464. }
  465. if inst.typ == common.TxTypeTransfer {
  466. // if TxTypeTransfer, need to exist the ToIdx account
  467. if err := tc.checkIfAccountExists(inst.to, inst); err != nil {
  468. log.Error(err)
  469. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  470. }
  471. }
  472. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  473. // if account of receiver does not exist, don't use
  474. // ToIdx, and use only ToEthAddr & ToBJJ
  475. tx := common.PoolL2Tx{
  476. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  477. TokenID: inst.tokenID,
  478. Amount: big.NewInt(int64(inst.amount)),
  479. Fee: common.FeeSelector(inst.fee),
  480. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  481. State: common.PoolL2TxStatePending,
  482. Timestamp: time.Now(),
  483. RqToEthAddr: common.EmptyAddr,
  484. RqToBJJ: nil,
  485. Type: inst.typ,
  486. }
  487. if tx.Type == common.TxTypeTransfer {
  488. tx.ToIdx = tc.Users[inst.to].Accounts[inst.tokenID].Idx
  489. tx.ToEthAddr = tc.Users[inst.to].Addr
  490. tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
  491. } else if tx.Type == common.TxTypeTransferToEthAddr {
  492. tx.ToIdx = common.Idx(0)
  493. tx.ToEthAddr = tc.Users[inst.to].Addr
  494. } else if tx.Type == common.TxTypeTransferToBJJ {
  495. tx.ToIdx = common.Idx(0)
  496. tx.ToEthAddr = common.FFAddr
  497. tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
  498. }
  499. nTx, err := common.NewPoolL2Tx(&tx)
  500. if err != nil {
  501. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  502. }
  503. tx = *nTx
  504. // perform signature and set it to tx.Signature
  505. toSign, err := tx.HashToSign()
  506. if err != nil {
  507. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  508. }
  509. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  510. tx.Signature = sig.Compress()
  511. txs = append(txs, tx)
  512. case common.TxTypeExit:
  513. tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
  514. tx := common.PoolL2Tx{
  515. FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
  516. ToIdx: common.Idx(1), // as is an Exit
  517. TokenID: inst.tokenID,
  518. Amount: big.NewInt(int64(inst.amount)),
  519. Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
  520. Type: common.TxTypeExit,
  521. }
  522. nTx, err := common.NewPoolL2Tx(&tx)
  523. if err != nil {
  524. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  525. }
  526. tx = *nTx
  527. // perform signature and set it to tx.Signature
  528. toSign, err := tx.HashToSign()
  529. if err != nil {
  530. return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
  531. }
  532. sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
  533. tx.Signature = sig.Compress()
  534. txs = append(txs, tx)
  535. default:
  536. return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ)
  537. }
  538. }
  539. return txs, nil
  540. }
  541. // generateKeys generates BabyJubJub & Address keys for the given list of user
  542. // names in a deterministic way. This means, that for the same given
  543. // 'userNames' in a certain order, the keys will be always the same.
  544. func (tc *Context) generateKeys(userNames []string) {
  545. for i := 1; i < len(userNames)+1; i++ {
  546. if _, ok := tc.Users[userNames[i-1]]; ok {
  547. // account already created
  548. continue
  549. }
  550. // babyjubjub key
  551. var sk babyjub.PrivateKey
  552. copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
  553. // eth address
  554. var key ecdsa.PrivateKey
  555. key.D = big.NewInt(int64(i)) // only for testing
  556. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  557. key.Curve = ethCrypto.S256()
  558. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  559. u := User{
  560. BJJ: &sk,
  561. Addr: addr,
  562. Accounts: make(map[common.TokenID]*Account),
  563. }
  564. tc.Users[userNames[i-1]] = &u
  565. }
  566. }