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.

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