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.

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