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.

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