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.

680 lines
22 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
  1. package synchronizer
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math/big"
  8. "os"
  9. "sort"
  10. "testing"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. "github.com/hermeznetwork/hermez-node/common"
  13. dbUtils "github.com/hermeznetwork/hermez-node/db"
  14. "github.com/hermeznetwork/hermez-node/db/historydb"
  15. "github.com/hermeznetwork/hermez-node/db/statedb"
  16. "github.com/hermeznetwork/hermez-node/eth"
  17. "github.com/hermeznetwork/hermez-node/test"
  18. "github.com/hermeznetwork/hermez-node/test/til"
  19. "github.com/jinzhu/copier"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. )
  23. var tokenConsts = map[common.TokenID]eth.ERC20Consts{}
  24. type timer struct {
  25. time int64
  26. }
  27. func (t *timer) Time() int64 {
  28. currentTime := t.time
  29. t.time++
  30. return currentTime
  31. }
  32. func accountsCmp(accounts []common.Account) func(i, j int) bool {
  33. return func(i, j int) bool { return accounts[i].Idx < accounts[j].Idx }
  34. }
  35. // Check Sync output and HistoryDB state against expected values generated by
  36. // til
  37. func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBlock *common.BlockData) {
  38. // Check Blocks
  39. dbBlocks, err := s.historyDB.GetAllBlocks()
  40. require.Nil(t, err)
  41. dbBlocks = dbBlocks[1:] // ignore block 0, added by default in the DB
  42. assert.Equal(t, blockNum, len(dbBlocks))
  43. assert.Equal(t, int64(blockNum), dbBlocks[blockNum-1].Num)
  44. assert.NotEqual(t, dbBlocks[blockNum-1].Hash, dbBlocks[blockNum-2].Hash)
  45. assert.Greater(t, dbBlocks[blockNum-1].Timestamp.Unix(), dbBlocks[blockNum-2].Timestamp.Unix())
  46. // Check Tokens
  47. assert.Equal(t, len(block.Rollup.AddedTokens), len(syncBlock.Rollup.AddedTokens))
  48. dbTokens, err := s.historyDB.GetAllTokens()
  49. require.Nil(t, err)
  50. dbTokens = dbTokens[1:] // ignore token 0, added by default in the DB
  51. for i, token := range block.Rollup.AddedTokens {
  52. dbToken := dbTokens[i]
  53. syncToken := syncBlock.Rollup.AddedTokens[i]
  54. assert.Equal(t, block.Block.Num, syncToken.EthBlockNum)
  55. assert.Equal(t, token.TokenID, syncToken.TokenID)
  56. assert.Equal(t, token.EthAddr, syncToken.EthAddr)
  57. tokenConst := tokenConsts[token.TokenID]
  58. assert.Equal(t, tokenConst.Name, syncToken.Name)
  59. assert.Equal(t, tokenConst.Symbol, syncToken.Symbol)
  60. assert.Equal(t, tokenConst.Decimals, syncToken.Decimals)
  61. var tokenCpy historydb.TokenWithUSD
  62. //nolint:gosec
  63. require.Nil(t, copier.Copy(&tokenCpy, &token)) // copy common.Token to historydb.TokenWithUSD
  64. require.Nil(t, copier.Copy(&tokenCpy, &tokenConst)) // copy common.Token to historydb.TokenWithUSD
  65. tokenCpy.ItemID = dbToken.ItemID // we don't care about ItemID
  66. assert.Equal(t, tokenCpy, dbToken)
  67. }
  68. // Check L1UserTxs
  69. assert.Equal(t, len(block.Rollup.L1UserTxs), len(syncBlock.Rollup.L1UserTxs))
  70. dbL1UserTxs, err := s.historyDB.GetAllL1UserTxs()
  71. require.Nil(t, err)
  72. // Ignore BatchNum in syncBlock.L1UserTxs because this value is set by
  73. // the HistoryDB. Also ignore EffectiveAmount & EffectiveDepositAmount
  74. // because this value is set by StateDB.ProcessTxs.
  75. for i := range syncBlock.Rollup.L1UserTxs {
  76. syncBlock.Rollup.L1UserTxs[i].BatchNum = block.Rollup.L1UserTxs[i].BatchNum
  77. syncBlock.Rollup.L1UserTxs[i].EffectiveAmount = block.Rollup.L1UserTxs[i].EffectiveAmount
  78. syncBlock.Rollup.L1UserTxs[i].EffectiveDepositAmount =
  79. block.Rollup.L1UserTxs[i].EffectiveDepositAmount
  80. }
  81. assert.Equal(t, block.Rollup.L1UserTxs, syncBlock.Rollup.L1UserTxs)
  82. for _, tx := range block.Rollup.L1UserTxs {
  83. var dbTx *common.L1Tx
  84. // Find tx in DB output
  85. for _, _dbTx := range dbL1UserTxs {
  86. if *tx.ToForgeL1TxsNum == *_dbTx.ToForgeL1TxsNum &&
  87. tx.Position == _dbTx.Position {
  88. dbTx = new(common.L1Tx)
  89. *dbTx = _dbTx
  90. break
  91. }
  92. }
  93. tx.EffectiveAmount = tx.Amount
  94. tx.EffectiveDepositAmount = tx.DepositAmount
  95. assert.Equal(t, &tx, dbTx) //nolint:gosec
  96. }
  97. // Check Batches
  98. assert.Equal(t, len(block.Rollup.Batches), len(syncBlock.Rollup.Batches))
  99. dbBatches, err := s.historyDB.GetAllBatches()
  100. require.Nil(t, err)
  101. dbL1CoordinatorTxs, err := s.historyDB.GetAllL1CoordinatorTxs()
  102. require.Nil(t, err)
  103. dbL2Txs, err := s.historyDB.GetAllL2Txs()
  104. require.Nil(t, err)
  105. dbExits, err := s.historyDB.GetAllExits()
  106. require.Nil(t, err)
  107. // dbL1CoordinatorTxs := []common.L1Tx{}
  108. for i, batch := range block.Rollup.Batches {
  109. var dbBatch *common.Batch
  110. // Find batch in DB output
  111. for _, _dbBatch := range dbBatches {
  112. if batch.Batch.BatchNum == _dbBatch.BatchNum {
  113. dbBatch = new(common.Batch)
  114. *dbBatch = _dbBatch
  115. break
  116. }
  117. }
  118. syncBatch := syncBlock.Rollup.Batches[i]
  119. // We don't care about TotalFeesUSD. Use the syncBatch that
  120. // has a TotalFeesUSD inserted by the HistoryDB
  121. batch.Batch.TotalFeesUSD = syncBatch.Batch.TotalFeesUSD
  122. assert.Equal(t, batch.CreatedAccounts, syncBatch.CreatedAccounts)
  123. batch.Batch.NumAccounts = len(batch.CreatedAccounts)
  124. // Test field by field to facilitate debugging of errors
  125. assert.Equal(t, batch.L1CoordinatorTxs, syncBatch.L1CoordinatorTxs)
  126. assert.Equal(t, batch.L2Txs, syncBatch.L2Txs)
  127. // In exit tree, we only check AccountIdx and Balance, because
  128. // it's what we have precomputed before.
  129. require.Equal(t, len(batch.ExitTree), len(syncBatch.ExitTree))
  130. for j := range batch.ExitTree {
  131. exit := &batch.ExitTree[j]
  132. assert.Equal(t, exit.AccountIdx, syncBatch.ExitTree[j].AccountIdx)
  133. assert.Equal(t, exit.Balance, syncBatch.ExitTree[j].Balance)
  134. *exit = syncBatch.ExitTree[j]
  135. }
  136. assert.Equal(t, batch.Batch, syncBatch.Batch)
  137. assert.Equal(t, batch, syncBatch)
  138. assert.Equal(t, &batch.Batch, dbBatch) //nolint:gosec
  139. // Check L1CoordinatorTxs from DB
  140. for _, tx := range batch.L1CoordinatorTxs {
  141. var dbTx *common.L1Tx
  142. // Find tx in DB output
  143. for _, _dbTx := range dbL1CoordinatorTxs {
  144. if *tx.BatchNum == *_dbTx.BatchNum &&
  145. tx.Position == _dbTx.Position {
  146. dbTx = new(common.L1Tx)
  147. *dbTx = _dbTx
  148. break
  149. }
  150. }
  151. assert.Equal(t, &tx, dbTx) //nolint:gosec
  152. }
  153. // Check L2Txs from DB
  154. for _, tx := range batch.L2Txs {
  155. var dbTx *common.L2Tx
  156. // Find tx in DB output
  157. for _, _dbTx := range dbL2Txs {
  158. if tx.BatchNum == _dbTx.BatchNum &&
  159. tx.Position == _dbTx.Position {
  160. dbTx = new(common.L2Tx)
  161. *dbTx = _dbTx
  162. break
  163. }
  164. }
  165. assert.Equal(t, &tx, dbTx) //nolint:gosec
  166. }
  167. // Check Exits from DB
  168. for _, exit := range batch.ExitTree {
  169. var dbExit *common.ExitInfo
  170. // Find exit in DB output
  171. for _, _dbExit := range dbExits {
  172. if exit.BatchNum == _dbExit.BatchNum &&
  173. exit.AccountIdx == _dbExit.AccountIdx {
  174. dbExit = new(common.ExitInfo)
  175. *dbExit = _dbExit
  176. break
  177. }
  178. }
  179. // Compare MerkleProof in JSON because unmarshaled 0
  180. // big.Int leaves the internal big.Int array at nil,
  181. // and gives trouble when comparing big.Int with
  182. // internal big.Int array != nil but empty.
  183. mtp, err := json.Marshal(exit.MerkleProof)
  184. require.Nil(t, err)
  185. dbMtp, err := json.Marshal(dbExit.MerkleProof)
  186. require.Nil(t, err)
  187. assert.Equal(t, mtp, dbMtp)
  188. dbExit.MerkleProof = exit.MerkleProof
  189. assert.Equal(t, &exit, dbExit) //nolint:gosec
  190. }
  191. }
  192. // Compare accounts from HistoryDB with StateDB (they should match)
  193. dbAccounts, err := s.historyDB.GetAllAccounts()
  194. require.Nil(t, err)
  195. sdbAccounts, err := s.stateDB.GetAccounts()
  196. require.Nil(t, err)
  197. assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
  198. }
  199. func assertEqualAccountsHistoryDBStateDB(t *testing.T, hdbAccs, sdbAccs []common.Account) {
  200. assert.Equal(t, len(hdbAccs), len(sdbAccs))
  201. sort.SliceStable(hdbAccs, accountsCmp(hdbAccs))
  202. sort.SliceStable(sdbAccs, accountsCmp(sdbAccs))
  203. for i := range hdbAccs {
  204. hdbAcc := hdbAccs[i]
  205. sdbAcc := sdbAccs[i]
  206. assert.Equal(t, hdbAcc.Idx, sdbAcc.Idx)
  207. assert.Equal(t, hdbAcc.TokenID, sdbAcc.TokenID)
  208. assert.Equal(t, hdbAcc.EthAddr, sdbAcc.EthAddr)
  209. assert.Equal(t, hdbAcc.PublicKey, sdbAcc.PublicKey)
  210. }
  211. }
  212. // ethAddTokens adds the tokens from the blocks to the blockchain
  213. func ethAddTokens(blocks []common.BlockData, client *test.Client) {
  214. for _, block := range blocks {
  215. for _, token := range block.Rollup.AddedTokens {
  216. consts := eth.ERC20Consts{
  217. Name: fmt.Sprintf("Token %d", token.TokenID),
  218. Symbol: fmt.Sprintf("TK%d", token.TokenID),
  219. Decimals: 18,
  220. }
  221. tokenConsts[token.TokenID] = consts
  222. client.CtlAddERC20(token.EthAddr, consts)
  223. }
  224. }
  225. }
  226. // ethAddBlocks adds block data to the smart contracts
  227. func ethAddBlocks(t *testing.T, blocks []common.BlockData,
  228. client *test.Client, clientSetup *test.ClientSetup) {
  229. for _, block := range blocks {
  230. for _, token := range block.Rollup.AddedTokens {
  231. _, err := client.RollupAddTokenSimple(token.EthAddr, clientSetup.RollupVariables.FeeAddToken)
  232. require.Nil(t, err)
  233. }
  234. for _, tx := range block.Rollup.L1UserTxs {
  235. client.CtlSetAddr(tx.FromEthAddr)
  236. _, err := client.RollupL1UserTxERC20ETH(tx.FromBJJ, int64(tx.FromIdx), tx.DepositAmount, tx.Amount,
  237. uint32(tx.TokenID), int64(tx.ToIdx))
  238. require.Nil(t, err)
  239. }
  240. client.CtlSetAddr(clientSetup.AuctionVariables.BootCoordinator)
  241. for _, batch := range block.Rollup.Batches {
  242. _, err := client.RollupForgeBatch(&eth.RollupForgeBatchArgs{
  243. NewLastIdx: batch.Batch.LastIdx,
  244. NewStRoot: batch.Batch.StateRoot,
  245. NewExitRoot: batch.Batch.ExitRoot,
  246. L1CoordinatorTxs: batch.L1CoordinatorTxs,
  247. L1CoordinatorTxsAuths: [][]byte{}, // Intentionally empty
  248. L2TxsData: batch.L2Txs,
  249. FeeIdxCoordinator: batch.Batch.FeeIdxsCoordinator,
  250. // Circuit selector
  251. VerifierIdx: 0, // Intentionally empty
  252. L1Batch: batch.L1Batch,
  253. ProofA: [2]*big.Int{}, // Intentionally empty
  254. ProofB: [2][2]*big.Int{}, // Intentionally empty
  255. ProofC: [2]*big.Int{}, // Intentionally empty
  256. })
  257. require.Nil(t, err)
  258. }
  259. // Mine block and sync
  260. client.CtlMineBlock()
  261. }
  262. }
  263. func TestSync(t *testing.T) {
  264. //
  265. // Setup
  266. //
  267. ctx := context.Background()
  268. // Int State DB
  269. dir, err := ioutil.TempDir("", "tmpdb")
  270. require.Nil(t, err)
  271. defer assert.Nil(t, os.RemoveAll(dir))
  272. stateDB, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32)
  273. assert.Nil(t, err)
  274. // Init History DB
  275. pass := os.Getenv("POSTGRES_PASS")
  276. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  277. require.Nil(t, err)
  278. historyDB := historydb.NewHistoryDB(db)
  279. // Clear DB
  280. test.WipeDB(historyDB.DB())
  281. // Init eth client
  282. var timer timer
  283. clientSetup := test.NewClientSetupExample()
  284. bootCoordAddr := clientSetup.AuctionVariables.BootCoordinator
  285. client := test.NewClient(true, &timer, &ethCommon.Address{}, clientSetup)
  286. // Create Synchronizer
  287. s, err := NewSynchronizer(client, historyDB, stateDB, Config{
  288. StartBlockNum: ConfigStartBlockNum{
  289. Rollup: 1,
  290. Auction: 1,
  291. WDelayer: 1,
  292. },
  293. InitialVariables: SCVariables{
  294. Rollup: *clientSetup.RollupVariables,
  295. Auction: *clientSetup.AuctionVariables,
  296. WDelayer: *clientSetup.WDelayerVariables,
  297. },
  298. })
  299. require.Nil(t, err)
  300. //
  301. // First Sync from an initial state
  302. //
  303. var vars struct {
  304. Rollup *common.RollupVariables
  305. Auction *common.AuctionVariables
  306. WDelayer *common.WDelayerVariables
  307. }
  308. stats := s.Stats()
  309. assert.Equal(t, false, stats.Synced())
  310. // Test Sync for rollup genesis block
  311. syncBlock, discards, err := s.Sync2(ctx, nil)
  312. require.Nil(t, err)
  313. require.Nil(t, discards)
  314. require.NotNil(t, syncBlock)
  315. require.Nil(t, syncBlock.Rollup.Vars)
  316. require.Nil(t, syncBlock.Auction.Vars)
  317. require.Nil(t, syncBlock.WDelayer.Vars)
  318. assert.Equal(t, int64(1), syncBlock.Block.Num)
  319. stats = s.Stats()
  320. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  321. assert.Equal(t, int64(1), stats.Eth.LastBlock.Num)
  322. assert.Equal(t, int64(1), stats.Sync.LastBlock.Num)
  323. vars.Rollup, vars.Auction, vars.WDelayer = s.SCVars()
  324. assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
  325. assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
  326. assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
  327. dbBlocks, err := s.historyDB.GetAllBlocks()
  328. require.Nil(t, err)
  329. assert.Equal(t, 2, len(dbBlocks))
  330. assert.Equal(t, int64(1), dbBlocks[1].Num)
  331. // Sync again and expect no new blocks
  332. syncBlock, discards, err = s.Sync2(ctx, nil)
  333. require.Nil(t, err)
  334. require.Nil(t, discards)
  335. require.Nil(t, syncBlock)
  336. //
  337. // Generate blockchain and smart contract data, and fill the test smart contracts
  338. //
  339. // Generate blockchain data with til
  340. set1 := `
  341. Type: Blockchain
  342. AddToken(1)
  343. AddToken(2)
  344. AddToken(3)
  345. CreateAccountDeposit(1) C: 2000 // Idx=256+2=258
  346. CreateAccountDeposit(2) A: 2000 // Idx=256+3=259
  347. CreateAccountDeposit(1) D: 500 // Idx=256+4=260
  348. CreateAccountDeposit(2) B: 500 // Idx=256+5=261
  349. CreateAccountDeposit(2) C: 500 // Idx=256+6=262
  350. CreateAccountCoordinator(1) A // Idx=256+0=256
  351. CreateAccountCoordinator(1) B // Idx=256+1=257
  352. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{5}
  353. > batchL1 // forge defined L1UserTxs{5}, freeze L1UserTxs{nil}
  354. > block // blockNum=2
  355. CreateAccountDepositTransfer(1) E-A: 1000, 200 // Idx=256+7=263
  356. ForceTransfer(1) C-B: 80
  357. ForceExit(1) A: 100
  358. ForceExit(1) B: 80
  359. ForceTransfer(1) A-D: 100
  360. Transfer(1) C-A: 100 (200)
  361. Exit(1) C: 50 (200)
  362. Exit(1) D: 30 (200)
  363. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{3}
  364. > batchL1 // forge L1UserTxs{3}, freeze defined L1UserTxs{nil}
  365. > block // blockNum=3
  366. `
  367. tc := til.NewContext(common.RollupConstMaxL1UserTx)
  368. tilCfgExtra := til.ConfigExtra{
  369. BootCoordAddr: bootCoordAddr,
  370. CoordUser: "A",
  371. }
  372. blocks, err := tc.GenerateBlocks(set1)
  373. require.Nil(t, err)
  374. // Sanity check
  375. require.Equal(t, 2, len(blocks))
  376. // blocks 0 (blockNum=2)
  377. i := 0
  378. require.Equal(t, 2, int(blocks[i].Block.Num))
  379. require.Equal(t, 3, len(blocks[i].Rollup.AddedTokens))
  380. require.Equal(t, 5, len(blocks[i].Rollup.L1UserTxs))
  381. require.Equal(t, 2, len(blocks[i].Rollup.Batches))
  382. require.Equal(t, 2, len(blocks[i].Rollup.Batches[0].L1CoordinatorTxs))
  383. // blocks 1 (blockNum=3)
  384. i = 1
  385. require.Equal(t, 3, int(blocks[i].Block.Num))
  386. require.Equal(t, 5, len(blocks[i].Rollup.L1UserTxs))
  387. require.Equal(t, 2, len(blocks[i].Rollup.Batches))
  388. require.Equal(t, 3, len(blocks[i].Rollup.Batches[0].L2Txs))
  389. // Generate extra required data
  390. ethAddTokens(blocks, client)
  391. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  392. assert.Nil(t, err)
  393. tc.FillBlocksL1UserTxsBatchNum(blocks)
  394. // Add block data to the smart contracts
  395. ethAddBlocks(t, blocks, client, clientSetup)
  396. //
  397. // Sync to synchronize the current state from the test smart contracts,
  398. // and check the outcome
  399. //
  400. // Block 2
  401. syncBlock, discards, err = s.Sync2(ctx, nil)
  402. require.Nil(t, err)
  403. require.Nil(t, discards)
  404. require.NotNil(t, syncBlock)
  405. assert.Nil(t, syncBlock.Rollup.Vars)
  406. assert.Nil(t, syncBlock.Auction.Vars)
  407. assert.Nil(t, syncBlock.WDelayer.Vars)
  408. assert.Equal(t, int64(2), syncBlock.Block.Num)
  409. stats = s.Stats()
  410. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  411. assert.Equal(t, int64(3), stats.Eth.LastBlock.Num)
  412. assert.Equal(t, int64(2), stats.Sync.LastBlock.Num)
  413. checkSyncBlock(t, s, 2, &blocks[0], syncBlock)
  414. // Block 3
  415. syncBlock, discards, err = s.Sync2(ctx, nil)
  416. require.Nil(t, err)
  417. require.Nil(t, discards)
  418. require.NotNil(t, syncBlock)
  419. assert.Nil(t, syncBlock.Rollup.Vars)
  420. assert.Nil(t, syncBlock.Auction.Vars)
  421. assert.Nil(t, syncBlock.WDelayer.Vars)
  422. assert.Equal(t, int64(3), syncBlock.Block.Num)
  423. stats = s.Stats()
  424. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  425. assert.Equal(t, int64(3), stats.Eth.LastBlock.Num)
  426. assert.Equal(t, int64(3), stats.Sync.LastBlock.Num)
  427. checkSyncBlock(t, s, 3, &blocks[1], syncBlock)
  428. // Block 4
  429. // Generate 2 withdraws manually
  430. _, err = client.RollupWithdrawMerkleProof(tc.Users["A"].BJJ.Public(), 1, 4, 256, big.NewInt(100), []*big.Int{}, true)
  431. require.Nil(t, err)
  432. _, err = client.RollupWithdrawMerkleProof(tc.Users["C"].BJJ.Public(), 1, 3, 258, big.NewInt(50), []*big.Int{}, false)
  433. require.Nil(t, err)
  434. client.CtlMineBlock()
  435. syncBlock, discards, err = s.Sync2(ctx, nil)
  436. require.Nil(t, err)
  437. require.Nil(t, discards)
  438. require.NotNil(t, syncBlock)
  439. assert.Nil(t, syncBlock.Rollup.Vars)
  440. assert.Nil(t, syncBlock.Auction.Vars)
  441. assert.Nil(t, syncBlock.WDelayer.Vars)
  442. assert.Equal(t, int64(4), syncBlock.Block.Num)
  443. stats = s.Stats()
  444. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  445. assert.Equal(t, int64(4), stats.Eth.LastBlock.Num)
  446. assert.Equal(t, int64(4), stats.Sync.LastBlock.Num)
  447. vars.Rollup, vars.Auction, vars.WDelayer = s.SCVars()
  448. assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
  449. assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
  450. assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
  451. dbExits, err := s.historyDB.GetAllExits()
  452. require.Nil(t, err)
  453. foundA1, foundC1 := false, false
  454. for _, exit := range dbExits {
  455. if exit.AccountIdx == 256 && exit.BatchNum == 4 {
  456. foundA1 = true
  457. assert.Equal(t, int64(4), *exit.InstantWithdrawn)
  458. }
  459. if exit.AccountIdx == 258 && exit.BatchNum == 3 {
  460. foundC1 = true
  461. assert.Equal(t, int64(4), *exit.DelayedWithdrawRequest)
  462. }
  463. }
  464. assert.True(t, foundA1)
  465. assert.True(t, foundC1)
  466. // Block 5
  467. // Update variables manually
  468. rollupVars, auctionVars, wDelayerVars, err := s.historyDB.GetSCVars()
  469. require.Nil(t, err)
  470. rollupVars.ForgeL1L2BatchTimeout = 42
  471. _, err = client.RollupUpdateForgeL1L2BatchTimeout(rollupVars.ForgeL1L2BatchTimeout)
  472. require.Nil(t, err)
  473. auctionVars.OpenAuctionSlots = 17
  474. _, err = client.AuctionSetOpenAuctionSlots(auctionVars.OpenAuctionSlots)
  475. require.Nil(t, err)
  476. wDelayerVars.WithdrawalDelay = 99
  477. _, err = client.WDelayerChangeWithdrawalDelay(wDelayerVars.WithdrawalDelay)
  478. require.Nil(t, err)
  479. client.CtlMineBlock()
  480. syncBlock, discards, err = s.Sync2(ctx, nil)
  481. require.Nil(t, err)
  482. require.Nil(t, discards)
  483. require.NotNil(t, syncBlock)
  484. assert.NotNil(t, syncBlock.Rollup.Vars)
  485. assert.NotNil(t, syncBlock.Auction.Vars)
  486. assert.NotNil(t, syncBlock.WDelayer.Vars)
  487. assert.Equal(t, int64(5), syncBlock.Block.Num)
  488. stats = s.Stats()
  489. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  490. assert.Equal(t, int64(5), stats.Eth.LastBlock.Num)
  491. assert.Equal(t, int64(5), stats.Sync.LastBlock.Num)
  492. vars.Rollup, vars.Auction, vars.WDelayer = s.SCVars()
  493. assert.NotEqual(t, clientSetup.RollupVariables, vars.Rollup)
  494. assert.NotEqual(t, clientSetup.AuctionVariables, vars.Auction)
  495. assert.NotEqual(t, clientSetup.WDelayerVariables, vars.WDelayer)
  496. dbRollupVars, dbAuctionVars, dbWDelayerVars, err := s.historyDB.GetSCVars()
  497. require.Nil(t, err)
  498. // Set EthBlockNum for Vars to the blockNum in which they were updated (should be 5)
  499. rollupVars.EthBlockNum = syncBlock.Block.Num
  500. auctionVars.EthBlockNum = syncBlock.Block.Num
  501. wDelayerVars.EthBlockNum = syncBlock.Block.Num
  502. assert.Equal(t, rollupVars, dbRollupVars)
  503. assert.Equal(t, auctionVars, dbAuctionVars)
  504. assert.Equal(t, wDelayerVars, dbWDelayerVars)
  505. //
  506. // Reorg test
  507. //
  508. // Redo blocks 2-5 (as a reorg) only leaving:
  509. // - 2 create account transactions
  510. // - 2 add tokens
  511. // We add a 6th block so that the synchronizer can detect the reorg
  512. set2 := `
  513. Type: Blockchain
  514. AddToken(1)
  515. AddToken(2)
  516. CreateAccountDeposit(1) C: 2000 // Idx=256+1=257
  517. CreateAccountCoordinator(1) A // Idx=256+0=256
  518. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{1}
  519. > batchL1 // forge defined L1UserTxs{1}, freeze L1UserTxs{nil}
  520. > block // blockNum=2
  521. > block // blockNum=3
  522. > block // blockNum=4
  523. > block // blockNum=5
  524. > block // blockNum=6
  525. `
  526. tc = til.NewContext(common.RollupConstMaxL1UserTx)
  527. tilCfgExtra = til.ConfigExtra{
  528. BootCoordAddr: bootCoordAddr,
  529. CoordUser: "A",
  530. }
  531. blocks, err = tc.GenerateBlocks(set2)
  532. require.Nil(t, err)
  533. for i := 0; i < 4; i++ {
  534. client.CtlRollback()
  535. }
  536. block := client.CtlLastBlock()
  537. require.Equal(t, int64(1), block.Num)
  538. // Generate extra required data
  539. ethAddTokens(blocks, client)
  540. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  541. assert.Nil(t, err)
  542. tc.FillBlocksL1UserTxsBatchNum(blocks)
  543. // Add block data to the smart contracts
  544. ethAddBlocks(t, blocks, client, clientSetup)
  545. // First sync detects the reorg and discards 4 blocks
  546. syncBlock, discards, err = s.Sync2(ctx, nil)
  547. require.Nil(t, err)
  548. expetedDiscards := int64(4)
  549. require.Equal(t, &expetedDiscards, discards)
  550. require.Nil(t, syncBlock)
  551. stats = s.Stats()
  552. assert.Equal(t, false, stats.Synced())
  553. assert.Equal(t, int64(6), stats.Eth.LastBlock.Num)
  554. vars.Rollup, vars.Auction, vars.WDelayer = s.SCVars()
  555. assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
  556. assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
  557. assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
  558. // At this point, the DB only has data up to block 1
  559. dbBlock, err := s.historyDB.GetLastBlock()
  560. require.Nil(t, err)
  561. assert.Equal(t, int64(1), dbBlock.Num)
  562. // Accounts in HistoryDB and StateDB must be empty
  563. dbAccounts, err := s.historyDB.GetAllAccounts()
  564. require.Nil(t, err)
  565. sdbAccounts, err := s.stateDB.GetAccounts()
  566. require.Nil(t, err)
  567. assert.Equal(t, 0, len(dbAccounts))
  568. assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
  569. // Sync blocks 2-6
  570. for i := 0; i < 5; i++ {
  571. syncBlock, discards, err = s.Sync2(ctx, nil)
  572. require.Nil(t, err)
  573. require.Nil(t, discards)
  574. require.NotNil(t, syncBlock)
  575. assert.Nil(t, syncBlock.Rollup.Vars)
  576. assert.Nil(t, syncBlock.Auction.Vars)
  577. assert.Nil(t, syncBlock.WDelayer.Vars)
  578. assert.Equal(t, int64(2+i), syncBlock.Block.Num)
  579. stats = s.Stats()
  580. assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
  581. assert.Equal(t, int64(6), stats.Eth.LastBlock.Num)
  582. assert.Equal(t, int64(2+i), stats.Sync.LastBlock.Num)
  583. if i == 4 {
  584. assert.Equal(t, true, stats.Synced())
  585. } else {
  586. assert.Equal(t, false, stats.Synced())
  587. }
  588. vars.Rollup, vars.Auction, vars.WDelayer = s.SCVars()
  589. assert.Equal(t, clientSetup.RollupVariables, vars.Rollup)
  590. assert.Equal(t, clientSetup.AuctionVariables, vars.Auction)
  591. assert.Equal(t, clientSetup.WDelayerVariables, vars.WDelayer)
  592. }
  593. dbBlock, err = s.historyDB.GetLastBlock()
  594. require.Nil(t, err)
  595. assert.Equal(t, int64(6), dbBlock.Num)
  596. // Accounts in HistoryDB and StateDB is only 2 entries
  597. dbAccounts, err = s.historyDB.GetAllAccounts()
  598. require.Nil(t, err)
  599. sdbAccounts, err = s.stateDB.GetAccounts()
  600. require.Nil(t, err)
  601. assert.Equal(t, 2, len(dbAccounts))
  602. assertEqualAccountsHistoryDBStateDB(t, dbAccounts, sdbAccounts)
  603. }