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.

1443 lines
45 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 coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Update missing parts, improve til, and more - Node - Updated configuration to initialize the interface to all the smart contracts - Common - Moved BlockData and BatchData types to common so that they can be shared among: historydb, til and synchronizer - Remove hash.go (it was never used) - Remove slot.go (it was never used) - Remove smartcontractparams.go (it was never used, and appropriate structs are defined in `eth/`) - Comment state / status method until requirements of this method are properly defined, and move it to Synchronizer - Synchronizer - Simplify `Sync` routine to only sync one block per call, and return useful information. - Use BlockData and BatchData from common - Check that events belong to the expected block hash - In L1Batch, query L1UserTxs from HistoryDB - Fill ERC20 token information - Test AddTokens with test.Client - HistryDB - Use BlockData and BatchData from common - Add `GetAllTokens` method - Uncomment and update GetL1UserTxs (with corresponding tests) - Til - Rename all instances of RegisterToken to AddToken (to follow the smart contract implementation naming) - Use BlockData and BatchData from common - Move testL1CoordinatorTxs and testL2Txs to a separate struct from BatchData in Context - Start Context with BatchNum = 1 (which the protocol defines to be the first batchNum) - In every Batch, set StateRoot and ExitRoot to a non-nil big.Int (zero). - In all L1Txs, if LoadAmount is not used, set it to 0; if Amount is not used, set it to 0; so that no *big.Int is nil. - In L1UserTx, don't set BatchNum, because when L1UserTxs are created and obtained by the synchronizer, the BatchNum is not known yet (it's a synchronizer job to set it) - In L1UserTxs, set `UserOrigin` and set `ToForgeL1TxsNum`.
4 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Update coordinator, call all api update functions - Common: - Rename Block.EthBlockNum to Block.Num to avoid unneeded repetition - API: - Add UpdateNetworkInfoBlock to update just block information, to be used when the node is not yet synchronized - Node: - Call API.UpdateMetrics and UpdateRecommendedFee in a loop, with configurable time intervals - Synchronizer: - When mapping events by TxHash, use an array to support the possibility of multiple calls of the same function happening in the same transaction (for example, a smart contract in a single transaction could call withdraw with delay twice, which would generate 2 withdraw events, and 2 deposit events). - In Stats, keep entire LastBlock instead of just the blockNum - In Stats, add lastL1BatchBlock - Test Stats and SCVars - Coordinator: - Enable writing the BatchInfo in every step of the pipeline to disk (with JSON text files) for debugging purposes. - Move the Pipeline functionality from the Coordinator to its own struct (Pipeline) - Implement shouldL1lL2Batch - In TxManager, implement logic to perform several attempts when doing ethereum node RPC calls before considering the error. (Both for calls to forgeBatch and transaction receipt) - In TxManager, reorganize the flow and note the specific points in which actions are made when err != nil - HistoryDB: - Implement GetLastL1BatchBlockNum: returns the blockNum of the latest forged l1Batch, to help the coordinator decide when to forge an L1Batch. - EthereumClient and test.Client: - Update EthBlockByNumber to return the last block when the passed number is -1.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
3 years ago
  1. package historydb
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. "os"
  8. "strings"
  9. "testing"
  10. "time"
  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/log"
  15. "github.com/hermeznetwork/hermez-node/test"
  16. "github.com/hermeznetwork/hermez-node/test/til"
  17. "github.com/hermeznetwork/tracerr"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. )
  21. var historyDB *HistoryDB
  22. // In order to run the test you need to run a Posgres DB with
  23. // a database named "history" that is accessible by
  24. // user: "hermez"
  25. // pass: set it using the env var POSTGRES_PASS
  26. // This can be achieved by running: POSTGRES_PASS=your_strong_pass && sudo docker run --rm --name hermez-db-test -p 5432:5432 -e POSTGRES_DB=history -e POSTGRES_USER=hermez -e POSTGRES_PASSWORD=$POSTGRES_PASS -d postgres && sleep 2s && sudo docker exec -it hermez-db-test psql -a history -U hermez -c "CREATE DATABASE l2;"
  27. // After running the test you can stop the container by running: sudo docker kill hermez-db-test
  28. // If you already did that for the L2DB you don't have to do it again
  29. func TestMain(m *testing.M) {
  30. // init DB
  31. pass := os.Getenv("POSTGRES_PASS")
  32. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  33. if err != nil {
  34. panic(err)
  35. }
  36. historyDB = NewHistoryDB(db)
  37. if err != nil {
  38. panic(err)
  39. }
  40. // Run tests
  41. result := m.Run()
  42. // Close DB
  43. if err := db.Close(); err != nil {
  44. log.Error("Error closing the history DB:", err)
  45. }
  46. os.Exit(result)
  47. }
  48. func TestBlocks(t *testing.T) {
  49. var fromBlock, toBlock int64
  50. fromBlock = 0
  51. toBlock = 7
  52. // Reset DB
  53. test.WipeDB(historyDB.DB())
  54. // Generate blocks using til
  55. set1 := `
  56. Type: Blockchain
  57. // block 0 is stored as default in the DB
  58. // block 1 does not exist
  59. > block // blockNum=2
  60. > block // blockNum=3
  61. > block // blockNum=4
  62. > block // blockNum=5
  63. > block // blockNum=6
  64. `
  65. tc := til.NewContext(uint16(0), 1)
  66. blocks, err := tc.GenerateBlocks(set1)
  67. require.NoError(t, err)
  68. // Save timestamp of a block with UTC and change it without UTC
  69. timestamp := time.Now().Add(time.Second * 13)
  70. blocks[fromBlock].Block.Timestamp = timestamp
  71. // Insert blocks into DB
  72. for i := 0; i < len(blocks); i++ {
  73. err := historyDB.AddBlock(&blocks[i].Block)
  74. assert.NoError(t, err)
  75. }
  76. // Add block 0 to the generated blocks
  77. blocks = append(
  78. []common.BlockData{{Block: test.Block0}}, //nolint:gofmt
  79. blocks...,
  80. )
  81. // Get all blocks from DB
  82. fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
  83. assert.Equal(t, len(blocks), len(fetchedBlocks))
  84. // Compare generated vs getted blocks
  85. assert.NoError(t, err)
  86. for i := range fetchedBlocks {
  87. assertEqualBlock(t, &blocks[i].Block, &fetchedBlocks[i])
  88. }
  89. // Compare saved timestamp vs getted
  90. nameZoneUTC, offsetUTC := timestamp.UTC().Zone()
  91. zoneFetchedBlock, offsetFetchedBlock := fetchedBlocks[fromBlock].Timestamp.Zone()
  92. assert.Equal(t, nameZoneUTC, zoneFetchedBlock)
  93. assert.Equal(t, offsetUTC, offsetFetchedBlock)
  94. // Get blocks from the DB one by one
  95. for i := int64(2); i < toBlock; i++ { // avoid block 0 for simplicity
  96. fetchedBlock, err := historyDB.GetBlock(i)
  97. assert.NoError(t, err)
  98. assertEqualBlock(t, &blocks[i-1].Block, fetchedBlock)
  99. }
  100. // Get last block
  101. lastBlock, err := historyDB.GetLastBlock()
  102. assert.NoError(t, err)
  103. assertEqualBlock(t, &blocks[len(blocks)-1].Block, lastBlock)
  104. }
  105. func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
  106. assert.Equal(t, expected.Num, actual.Num)
  107. assert.Equal(t, expected.Hash, actual.Hash)
  108. assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
  109. }
  110. func TestBatches(t *testing.T) {
  111. // Reset DB
  112. test.WipeDB(historyDB.DB())
  113. // Generate batches using til (and blocks for foreign key)
  114. set := `
  115. Type: Blockchain
  116. AddToken(1) // Will have value in USD
  117. AddToken(2) // Will NOT have value in USD
  118. CreateAccountDeposit(1) A: 2000
  119. CreateAccountDeposit(2) A: 2000
  120. CreateAccountDeposit(1) B: 1000
  121. CreateAccountDeposit(2) B: 1000
  122. > batchL1
  123. > batchL1
  124. Transfer(1) A-B: 100 (5)
  125. Transfer(2) B-A: 100 (199)
  126. > batch // batchNum=2, L2 only batch, forges transfers (mixed case of with(out) USD value)
  127. > block
  128. Transfer(1) A-B: 100 (5)
  129. > batch // batchNum=3, L2 only batch, forges transfer (with USD value)
  130. Transfer(2) B-A: 100 (199)
  131. > batch // batchNum=4, L2 only batch, forges transfer (without USD value)
  132. > block
  133. `
  134. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  135. tilCfgExtra := til.ConfigExtra{
  136. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  137. CoordUser: "A",
  138. }
  139. blocks, err := tc.GenerateBlocks(set)
  140. require.NoError(t, err)
  141. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  142. require.NoError(t, err)
  143. // Insert to DB
  144. batches := []common.Batch{}
  145. tokensValue := make(map[common.TokenID]float64)
  146. lastL1TxsNum := new(int64)
  147. lastL1BatchBlockNum := int64(0)
  148. for _, block := range blocks {
  149. // Insert block
  150. assert.NoError(t, historyDB.AddBlock(&block.Block))
  151. // Insert tokens
  152. for i, token := range block.Rollup.AddedTokens {
  153. assert.NoError(t, historyDB.AddToken(&token)) //nolint:gosec
  154. if i%2 != 0 {
  155. // Set value to the token
  156. value := (float64(i) + 5) * 5.389329
  157. assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
  158. tokensValue[token.TokenID] = value / math.Pow(10, float64(token.Decimals))
  159. }
  160. }
  161. // Combine all generated batches into single array
  162. for _, batch := range block.Rollup.Batches {
  163. batches = append(batches, batch.Batch)
  164. forgeTxsNum := batch.Batch.ForgeL1TxsNum
  165. if forgeTxsNum != nil && (lastL1TxsNum == nil || *lastL1TxsNum < *forgeTxsNum) {
  166. *lastL1TxsNum = *forgeTxsNum
  167. lastL1BatchBlockNum = batch.Batch.EthBlockNum
  168. }
  169. }
  170. }
  171. // Insert batches
  172. assert.NoError(t, historyDB.AddBatches(batches))
  173. // Set expected total fee
  174. for _, batch := range batches {
  175. total := .0
  176. for tokenID, amount := range batch.CollectedFees {
  177. af := new(big.Float).SetInt(amount)
  178. amountFloat, _ := af.Float64()
  179. total += tokensValue[tokenID] * amountFloat
  180. }
  181. batch.TotalFeesUSD = &total
  182. }
  183. // Get batches from the DB
  184. fetchedBatches, err := historyDB.GetBatches(0, common.BatchNum(len(batches)+1))
  185. assert.NoError(t, err)
  186. assert.Equal(t, len(batches), len(fetchedBatches))
  187. for i, fetchedBatch := range fetchedBatches {
  188. assert.Equal(t, batches[i], fetchedBatch)
  189. }
  190. // Test GetLastBatchNum
  191. fetchedLastBatchNum, err := historyDB.GetLastBatchNum()
  192. assert.NoError(t, err)
  193. assert.Equal(t, batches[len(batches)-1].BatchNum, fetchedLastBatchNum)
  194. // Test GetLastL1TxsNum
  195. fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
  196. assert.NoError(t, err)
  197. assert.Equal(t, lastL1TxsNum, fetchedLastL1TxsNum)
  198. // Test GetLastL1BatchBlockNum
  199. fetchedLastL1BatchBlockNum, err := historyDB.GetLastL1BatchBlockNum()
  200. assert.NoError(t, err)
  201. assert.Equal(t, lastL1BatchBlockNum, fetchedLastL1BatchBlockNum)
  202. }
  203. func TestBids(t *testing.T) {
  204. const fromBlock int64 = 1
  205. const toBlock int64 = 5
  206. // Prepare blocks in the DB
  207. blocks := setTestBlocks(fromBlock, toBlock)
  208. // Generate fake coordinators
  209. const nCoords = 5
  210. coords := test.GenCoordinators(nCoords, blocks)
  211. err := historyDB.AddCoordinators(coords)
  212. assert.NoError(t, err)
  213. // Generate fake bids
  214. const nBids = 20
  215. bids := test.GenBids(nBids, blocks, coords)
  216. err = historyDB.AddBids(bids)
  217. assert.NoError(t, err)
  218. // Fetch bids
  219. fetchedBids, err := historyDB.GetAllBids()
  220. assert.NoError(t, err)
  221. // Compare fetched bids vs generated bids
  222. for i, bid := range fetchedBids {
  223. assert.Equal(t, bids[i], bid)
  224. }
  225. }
  226. func TestTokens(t *testing.T) {
  227. const fromBlock int64 = 1
  228. const toBlock int64 = 5
  229. // Prepare blocks in the DB
  230. blocks := setTestBlocks(fromBlock, toBlock)
  231. // Generate fake tokens
  232. const nTokens = 5
  233. tokens, ethToken := test.GenTokens(nTokens, blocks)
  234. err := historyDB.AddTokens(tokens)
  235. assert.NoError(t, err)
  236. tokens = append([]common.Token{ethToken}, tokens...)
  237. limit := uint(10)
  238. // Fetch tokens
  239. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  240. assert.NoError(t, err)
  241. // Compare fetched tokens vs generated tokens
  242. // All the tokens should have USDUpdate setted by the DB trigger
  243. for i, token := range fetchedTokens {
  244. assert.Equal(t, tokens[i].TokenID, token.TokenID)
  245. assert.Equal(t, tokens[i].EthBlockNum, token.EthBlockNum)
  246. assert.Equal(t, tokens[i].EthAddr, token.EthAddr)
  247. assert.Equal(t, tokens[i].Name, token.Name)
  248. assert.Equal(t, tokens[i].Symbol, token.Symbol)
  249. assert.Nil(t, token.USD)
  250. assert.Nil(t, token.USDUpdate)
  251. }
  252. // Update token value
  253. for i, token := range tokens {
  254. value := 1.01 * float64(i)
  255. assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
  256. }
  257. // Fetch tokens
  258. fetchedTokens, _, err = historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  259. assert.NoError(t, err)
  260. // Compare fetched tokens vs generated tokens
  261. // All the tokens should have USDUpdate setted by the DB trigger
  262. for i, token := range fetchedTokens {
  263. value := 1.01 * float64(i)
  264. assert.Equal(t, value, *token.USD)
  265. nameZone, offset := token.USDUpdate.Zone()
  266. assert.Equal(t, "UTC", nameZone)
  267. assert.Equal(t, 0, offset)
  268. }
  269. }
  270. func TestTokensUTF8(t *testing.T) {
  271. // Reset DB
  272. test.WipeDB(historyDB.DB())
  273. const fromBlock int64 = 1
  274. const toBlock int64 = 5
  275. // Prepare blocks in the DB
  276. blocks := setTestBlocks(fromBlock, toBlock)
  277. // Generate fake tokens
  278. const nTokens = 5
  279. tokens, ethToken := test.GenTokens(nTokens, blocks)
  280. nonUTFTokens := make([]common.Token, len(tokens)+1)
  281. // Force token.name and token.symbol to be non UTF-8 Strings
  282. for i, token := range tokens {
  283. token.Name = fmt.Sprint("NON-UTF8-NAME-\xc5-", i)
  284. token.Symbol = fmt.Sprint("S-\xc5-", i)
  285. tokens[i] = token
  286. nonUTFTokens[i] = token
  287. }
  288. err := historyDB.AddTokens(tokens)
  289. assert.NoError(t, err)
  290. // Work with nonUTFTokens as tokens one gets updated and non UTF-8 characters are lost
  291. nonUTFTokens = append([]common.Token{ethToken}, nonUTFTokens...)
  292. limit := uint(10)
  293. // Fetch tokens
  294. fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  295. assert.NoError(t, err)
  296. // Compare fetched tokens vs generated tokens
  297. // All the tokens should have USDUpdate setted by the DB trigger
  298. for i, token := range fetchedTokens {
  299. assert.Equal(t, nonUTFTokens[i].TokenID, token.TokenID)
  300. assert.Equal(t, nonUTFTokens[i].EthBlockNum, token.EthBlockNum)
  301. assert.Equal(t, nonUTFTokens[i].EthAddr, token.EthAddr)
  302. assert.Equal(t, strings.ToValidUTF8(nonUTFTokens[i].Name, " "), token.Name)
  303. assert.Equal(t, strings.ToValidUTF8(nonUTFTokens[i].Symbol, " "), token.Symbol)
  304. assert.Nil(t, token.USD)
  305. assert.Nil(t, token.USDUpdate)
  306. }
  307. // Update token value
  308. for i, token := range nonUTFTokens {
  309. value := 1.01 * float64(i)
  310. assert.NoError(t, historyDB.UpdateTokenValue(token.Symbol, value))
  311. }
  312. // Fetch tokens
  313. fetchedTokens, _, err = historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
  314. assert.NoError(t, err)
  315. // Compare fetched tokens vs generated tokens
  316. // All the tokens should have USDUpdate setted by the DB trigger
  317. for i, token := range fetchedTokens {
  318. value := 1.01 * float64(i)
  319. assert.Equal(t, value, *token.USD)
  320. nameZone, offset := token.USDUpdate.Zone()
  321. assert.Equal(t, "UTC", nameZone)
  322. assert.Equal(t, 0, offset)
  323. }
  324. }
  325. func TestAccounts(t *testing.T) {
  326. const fromBlock int64 = 1
  327. const toBlock int64 = 5
  328. // Prepare blocks in the DB
  329. blocks := setTestBlocks(fromBlock, toBlock)
  330. // Generate fake tokens
  331. const nTokens = 5
  332. tokens, ethToken := test.GenTokens(nTokens, blocks)
  333. err := historyDB.AddTokens(tokens)
  334. assert.NoError(t, err)
  335. tokens = append([]common.Token{ethToken}, tokens...)
  336. // Generate fake batches
  337. const nBatches = 10
  338. batches := test.GenBatches(nBatches, blocks)
  339. err = historyDB.AddBatches(batches)
  340. assert.NoError(t, err)
  341. // Generate fake accounts
  342. const nAccounts = 3
  343. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  344. err = historyDB.AddAccounts(accs)
  345. assert.NoError(t, err)
  346. // Fetch accounts
  347. fetchedAccs, err := historyDB.GetAllAccounts()
  348. assert.NoError(t, err)
  349. // Compare fetched accounts vs generated accounts
  350. for i, acc := range fetchedAccs {
  351. accs[i].Balance = nil
  352. assert.Equal(t, accs[i], acc)
  353. }
  354. }
  355. func TestTxs(t *testing.T) {
  356. // Reset DB
  357. test.WipeDB(historyDB.DB())
  358. set := `
  359. Type: Blockchain
  360. AddToken(1)
  361. AddToken(2)
  362. CreateAccountDeposit(1) A: 10
  363. CreateAccountDeposit(1) B: 10
  364. > batchL1
  365. > batchL1
  366. > block
  367. CreateAccountDepositTransfer(1) C-A: 20, 10
  368. CreateAccountCoordinator(1) User0
  369. > batchL1
  370. > batchL1
  371. > block
  372. Deposit(1) B: 10
  373. Deposit(1) C: 10
  374. Transfer(1) C-A : 10 (1)
  375. Transfer(1) B-C : 10 (1)
  376. Transfer(1) A-B : 10 (1)
  377. Exit(1) A: 10 (1)
  378. > batch
  379. > block
  380. DepositTransfer(1) A-B: 10, 10
  381. > batchL1
  382. > block
  383. ForceTransfer(1) A-B: 10
  384. ForceExit(1) A: 5
  385. > batchL1
  386. > batchL1
  387. > block
  388. CreateAccountDeposit(2) D: 10
  389. > batchL1
  390. > block
  391. CreateAccountDeposit(2) E: 10
  392. > batchL1
  393. > batchL1
  394. > block
  395. `
  396. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  397. tilCfgExtra := til.ConfigExtra{
  398. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  399. CoordUser: "A",
  400. }
  401. blocks, err := tc.GenerateBlocks(set)
  402. require.NoError(t, err)
  403. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  404. require.NoError(t, err)
  405. // Sanity check
  406. require.Equal(t, 7, len(blocks))
  407. require.Equal(t, 2, len(blocks[0].Rollup.L1UserTxs))
  408. require.Equal(t, 1, len(blocks[1].Rollup.L1UserTxs))
  409. require.Equal(t, 2, len(blocks[2].Rollup.L1UserTxs))
  410. require.Equal(t, 1, len(blocks[3].Rollup.L1UserTxs))
  411. require.Equal(t, 2, len(blocks[4].Rollup.L1UserTxs))
  412. require.Equal(t, 1, len(blocks[5].Rollup.L1UserTxs))
  413. require.Equal(t, 1, len(blocks[6].Rollup.L1UserTxs))
  414. var null *common.BatchNum = nil
  415. var txID common.TxID
  416. // Insert blocks into DB
  417. for i := range blocks {
  418. if i == len(blocks)-1 {
  419. blocks[i].Block.Timestamp = time.Now()
  420. dbL1Txs, err := historyDB.GetAllL1UserTxs()
  421. assert.NoError(t, err)
  422. // Check batch_num is nil before forging
  423. assert.Equal(t, null, dbL1Txs[len(dbL1Txs)-1].BatchNum)
  424. // Save this TxId
  425. txID = dbL1Txs[len(dbL1Txs)-1].TxID
  426. }
  427. err = historyDB.AddBlockSCData(&blocks[i])
  428. assert.NoError(t, err)
  429. }
  430. // Check blocks
  431. dbBlocks, err := historyDB.GetAllBlocks()
  432. assert.NoError(t, err)
  433. assert.Equal(t, len(blocks)+1, len(dbBlocks))
  434. // Check batches
  435. batches, err := historyDB.GetAllBatches()
  436. assert.NoError(t, err)
  437. assert.Equal(t, 11, len(batches))
  438. // Check L1 Transactions
  439. dbL1Txs, err := historyDB.GetAllL1UserTxs()
  440. assert.NoError(t, err)
  441. assert.Equal(t, 10, len(dbL1Txs))
  442. // Tx Type
  443. assert.Equal(t, common.TxTypeCreateAccountDeposit, dbL1Txs[0].Type)
  444. assert.Equal(t, common.TxTypeCreateAccountDeposit, dbL1Txs[1].Type)
  445. assert.Equal(t, common.TxTypeCreateAccountDepositTransfer, dbL1Txs[2].Type)
  446. assert.Equal(t, common.TxTypeDeposit, dbL1Txs[3].Type)
  447. assert.Equal(t, common.TxTypeDeposit, dbL1Txs[4].Type)
  448. assert.Equal(t, common.TxTypeDepositTransfer, dbL1Txs[5].Type)
  449. assert.Equal(t, common.TxTypeForceTransfer, dbL1Txs[6].Type)
  450. assert.Equal(t, common.TxTypeForceExit, dbL1Txs[7].Type)
  451. assert.Equal(t, common.TxTypeCreateAccountDeposit, dbL1Txs[8].Type)
  452. assert.Equal(t, common.TxTypeCreateAccountDeposit, dbL1Txs[9].Type)
  453. // Tx ID
  454. assert.Equal(t, "0x000000000000000001000000", dbL1Txs[0].TxID.String())
  455. assert.Equal(t, "0x000000000000000001000100", dbL1Txs[1].TxID.String())
  456. assert.Equal(t, "0x000000000000000003000000", dbL1Txs[2].TxID.String())
  457. assert.Equal(t, "0x000000000000000005000000", dbL1Txs[3].TxID.String())
  458. assert.Equal(t, "0x000000000000000005000100", dbL1Txs[4].TxID.String())
  459. assert.Equal(t, "0x000000000000000005000200", dbL1Txs[5].TxID.String())
  460. assert.Equal(t, "0x000000000000000006000000", dbL1Txs[6].TxID.String())
  461. assert.Equal(t, "0x000000000000000006000100", dbL1Txs[7].TxID.String())
  462. assert.Equal(t, "0x000000000000000008000000", dbL1Txs[8].TxID.String())
  463. assert.Equal(t, "0x000000000000000009000000", dbL1Txs[9].TxID.String())
  464. // Tx From IDx
  465. assert.Equal(t, common.Idx(0), dbL1Txs[0].FromIdx)
  466. assert.Equal(t, common.Idx(0), dbL1Txs[1].FromIdx)
  467. assert.Equal(t, common.Idx(0), dbL1Txs[2].FromIdx)
  468. assert.NotEqual(t, common.Idx(0), dbL1Txs[3].FromIdx)
  469. assert.NotEqual(t, common.Idx(0), dbL1Txs[4].FromIdx)
  470. assert.NotEqual(t, common.Idx(0), dbL1Txs[5].FromIdx)
  471. assert.NotEqual(t, common.Idx(0), dbL1Txs[6].FromIdx)
  472. assert.NotEqual(t, common.Idx(0), dbL1Txs[7].FromIdx)
  473. assert.Equal(t, common.Idx(0), dbL1Txs[8].FromIdx)
  474. assert.Equal(t, common.Idx(0), dbL1Txs[9].FromIdx)
  475. assert.Equal(t, common.Idx(0), dbL1Txs[9].FromIdx)
  476. assert.Equal(t, dbL1Txs[5].FromIdx, dbL1Txs[6].FromIdx)
  477. assert.Equal(t, dbL1Txs[5].FromIdx, dbL1Txs[7].FromIdx)
  478. // Tx to IDx
  479. assert.Equal(t, dbL1Txs[2].ToIdx, dbL1Txs[5].FromIdx)
  480. assert.Equal(t, dbL1Txs[5].ToIdx, dbL1Txs[3].FromIdx)
  481. assert.Equal(t, dbL1Txs[6].ToIdx, dbL1Txs[3].FromIdx)
  482. // Token ID
  483. assert.Equal(t, common.TokenID(1), dbL1Txs[0].TokenID)
  484. assert.Equal(t, common.TokenID(1), dbL1Txs[1].TokenID)
  485. assert.Equal(t, common.TokenID(1), dbL1Txs[2].TokenID)
  486. assert.Equal(t, common.TokenID(1), dbL1Txs[3].TokenID)
  487. assert.Equal(t, common.TokenID(1), dbL1Txs[4].TokenID)
  488. assert.Equal(t, common.TokenID(1), dbL1Txs[5].TokenID)
  489. assert.Equal(t, common.TokenID(1), dbL1Txs[6].TokenID)
  490. assert.Equal(t, common.TokenID(1), dbL1Txs[7].TokenID)
  491. assert.Equal(t, common.TokenID(2), dbL1Txs[8].TokenID)
  492. assert.Equal(t, common.TokenID(2), dbL1Txs[9].TokenID)
  493. // Batch Number
  494. var bn common.BatchNum = common.BatchNum(2)
  495. assert.Equal(t, &bn, dbL1Txs[0].BatchNum)
  496. assert.Equal(t, &bn, dbL1Txs[1].BatchNum)
  497. bn = common.BatchNum(4)
  498. assert.Equal(t, &bn, dbL1Txs[2].BatchNum)
  499. bn = common.BatchNum(7)
  500. assert.Equal(t, &bn, dbL1Txs[3].BatchNum)
  501. assert.Equal(t, &bn, dbL1Txs[4].BatchNum)
  502. assert.Equal(t, &bn, dbL1Txs[5].BatchNum)
  503. bn = common.BatchNum(8)
  504. assert.Equal(t, &bn, dbL1Txs[6].BatchNum)
  505. assert.Equal(t, &bn, dbL1Txs[7].BatchNum)
  506. bn = common.BatchNum(10)
  507. assert.Equal(t, &bn, dbL1Txs[8].BatchNum)
  508. bn = common.BatchNum(11)
  509. assert.Equal(t, &bn, dbL1Txs[9].BatchNum)
  510. // eth_block_num
  511. assert.Equal(t, int64(2), dbL1Txs[0].EthBlockNum)
  512. assert.Equal(t, int64(2), dbL1Txs[1].EthBlockNum)
  513. assert.Equal(t, int64(3), dbL1Txs[2].EthBlockNum)
  514. assert.Equal(t, int64(4), dbL1Txs[3].EthBlockNum)
  515. assert.Equal(t, int64(4), dbL1Txs[4].EthBlockNum)
  516. assert.Equal(t, int64(5), dbL1Txs[5].EthBlockNum)
  517. assert.Equal(t, int64(6), dbL1Txs[6].EthBlockNum)
  518. assert.Equal(t, int64(6), dbL1Txs[7].EthBlockNum)
  519. assert.Equal(t, int64(7), dbL1Txs[8].EthBlockNum)
  520. assert.Equal(t, int64(8), dbL1Txs[9].EthBlockNum)
  521. // User Origin
  522. assert.Equal(t, true, dbL1Txs[0].UserOrigin)
  523. assert.Equal(t, true, dbL1Txs[1].UserOrigin)
  524. assert.Equal(t, true, dbL1Txs[2].UserOrigin)
  525. assert.Equal(t, true, dbL1Txs[3].UserOrigin)
  526. assert.Equal(t, true, dbL1Txs[4].UserOrigin)
  527. assert.Equal(t, true, dbL1Txs[5].UserOrigin)
  528. assert.Equal(t, true, dbL1Txs[6].UserOrigin)
  529. assert.Equal(t, true, dbL1Txs[7].UserOrigin)
  530. assert.Equal(t, true, dbL1Txs[8].UserOrigin)
  531. assert.Equal(t, true, dbL1Txs[9].UserOrigin)
  532. // Deposit Amount
  533. assert.Equal(t, big.NewInt(10), dbL1Txs[0].DepositAmount)
  534. assert.Equal(t, big.NewInt(10), dbL1Txs[1].DepositAmount)
  535. assert.Equal(t, big.NewInt(20), dbL1Txs[2].DepositAmount)
  536. assert.Equal(t, big.NewInt(10), dbL1Txs[3].DepositAmount)
  537. assert.Equal(t, big.NewInt(10), dbL1Txs[4].DepositAmount)
  538. assert.Equal(t, big.NewInt(10), dbL1Txs[5].DepositAmount)
  539. assert.Equal(t, big.NewInt(0), dbL1Txs[6].DepositAmount)
  540. assert.Equal(t, big.NewInt(0), dbL1Txs[7].DepositAmount)
  541. assert.Equal(t, big.NewInt(10), dbL1Txs[8].DepositAmount)
  542. assert.Equal(t, big.NewInt(10), dbL1Txs[9].DepositAmount)
  543. // Check saved txID's batch_num is not nil
  544. assert.Equal(t, txID, dbL1Txs[len(dbL1Txs)-2].TxID)
  545. assert.NotEqual(t, null, dbL1Txs[len(dbL1Txs)-2].BatchNum)
  546. // Check Coordinator TXs
  547. coordTxs, err := historyDB.GetAllL1CoordinatorTxs()
  548. assert.NoError(t, err)
  549. assert.Equal(t, 1, len(coordTxs))
  550. assert.Equal(t, common.TxTypeCreateAccountDeposit, coordTxs[0].Type)
  551. assert.Equal(t, false, coordTxs[0].UserOrigin)
  552. // Check L2 TXs
  553. dbL2Txs, err := historyDB.GetAllL2Txs()
  554. assert.NoError(t, err)
  555. assert.Equal(t, 4, len(dbL2Txs))
  556. // Tx Type
  557. assert.Equal(t, common.TxTypeTransfer, dbL2Txs[0].Type)
  558. assert.Equal(t, common.TxTypeTransfer, dbL2Txs[1].Type)
  559. assert.Equal(t, common.TxTypeTransfer, dbL2Txs[2].Type)
  560. assert.Equal(t, common.TxTypeExit, dbL2Txs[3].Type)
  561. // Tx ID
  562. assert.Equal(t, "0x020000000001030000000000", dbL2Txs[0].TxID.String())
  563. assert.Equal(t, "0x020000000001010000000000", dbL2Txs[1].TxID.String())
  564. assert.Equal(t, "0x020000000001000000000000", dbL2Txs[2].TxID.String())
  565. assert.Equal(t, "0x020000000001000000000001", dbL2Txs[3].TxID.String())
  566. // Tx From and To IDx
  567. assert.Equal(t, dbL2Txs[0].ToIdx, dbL2Txs[2].FromIdx)
  568. assert.Equal(t, dbL2Txs[1].ToIdx, dbL2Txs[0].FromIdx)
  569. assert.Equal(t, dbL2Txs[2].ToIdx, dbL2Txs[1].FromIdx)
  570. // Batch Number
  571. assert.Equal(t, common.BatchNum(5), dbL2Txs[0].BatchNum)
  572. assert.Equal(t, common.BatchNum(5), dbL2Txs[1].BatchNum)
  573. assert.Equal(t, common.BatchNum(5), dbL2Txs[2].BatchNum)
  574. assert.Equal(t, common.BatchNum(5), dbL2Txs[3].BatchNum)
  575. // eth_block_num
  576. assert.Equal(t, int64(4), dbL2Txs[0].EthBlockNum)
  577. assert.Equal(t, int64(4), dbL2Txs[1].EthBlockNum)
  578. assert.Equal(t, int64(4), dbL2Txs[2].EthBlockNum)
  579. // Amount
  580. assert.Equal(t, big.NewInt(10), dbL2Txs[0].Amount)
  581. assert.Equal(t, big.NewInt(10), dbL2Txs[1].Amount)
  582. assert.Equal(t, big.NewInt(10), dbL2Txs[2].Amount)
  583. assert.Equal(t, big.NewInt(10), dbL2Txs[3].Amount)
  584. }
  585. func TestExitTree(t *testing.T) {
  586. nBatches := 17
  587. blocks := setTestBlocks(1, 10)
  588. batches := test.GenBatches(nBatches, blocks)
  589. err := historyDB.AddBatches(batches)
  590. assert.NoError(t, err)
  591. const nTokens = 50
  592. tokens, ethToken := test.GenTokens(nTokens, blocks)
  593. err = historyDB.AddTokens(tokens)
  594. assert.NoError(t, err)
  595. tokens = append([]common.Token{ethToken}, tokens...)
  596. const nAccounts = 3
  597. accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
  598. assert.NoError(t, historyDB.AddAccounts(accs))
  599. exitTree := test.GenExitTree(nBatches, batches, accs, blocks)
  600. err = historyDB.AddExitTree(exitTree)
  601. assert.NoError(t, err)
  602. }
  603. func TestGetUnforgedL1UserTxs(t *testing.T) {
  604. test.WipeDB(historyDB.DB())
  605. set := `
  606. Type: Blockchain
  607. AddToken(1)
  608. AddToken(2)
  609. AddToken(3)
  610. CreateAccountDeposit(1) A: 20
  611. CreateAccountDeposit(2) A: 20
  612. CreateAccountDeposit(1) B: 5
  613. CreateAccountDeposit(1) C: 5
  614. CreateAccountDeposit(1) D: 5
  615. > block
  616. `
  617. tc := til.NewContext(uint16(0), 128)
  618. blocks, err := tc.GenerateBlocks(set)
  619. require.NoError(t, err)
  620. // Sanity check
  621. require.Equal(t, 1, len(blocks))
  622. require.Equal(t, 5, len(blocks[0].Rollup.L1UserTxs))
  623. toForgeL1TxsNum := int64(1)
  624. for i := range blocks {
  625. err = historyDB.AddBlockSCData(&blocks[i])
  626. require.NoError(t, err)
  627. }
  628. l1UserTxs, err := historyDB.GetUnforgedL1UserTxs(toForgeL1TxsNum)
  629. require.NoError(t, err)
  630. assert.Equal(t, 5, len(l1UserTxs))
  631. assert.Equal(t, blocks[0].Rollup.L1UserTxs, l1UserTxs)
  632. // No l1UserTxs for this toForgeL1TxsNum
  633. l1UserTxs, err = historyDB.GetUnforgedL1UserTxs(2)
  634. require.NoError(t, err)
  635. assert.Equal(t, 0, len(l1UserTxs))
  636. }
  637. func exampleInitSCVars() (*common.RollupVariables, *common.AuctionVariables, *common.WDelayerVariables) {
  638. //nolint:govet
  639. rollup := &common.RollupVariables{
  640. 0,
  641. big.NewInt(10),
  642. 12,
  643. 13,
  644. [5]common.BucketParams{},
  645. false,
  646. }
  647. //nolint:govet
  648. auction := &common.AuctionVariables{
  649. 0,
  650. ethCommon.BigToAddress(big.NewInt(2)),
  651. ethCommon.BigToAddress(big.NewInt(3)),
  652. "https://boot.coord.com",
  653. [6]*big.Int{
  654. big.NewInt(1), big.NewInt(2), big.NewInt(3),
  655. big.NewInt(4), big.NewInt(5), big.NewInt(6),
  656. },
  657. 0,
  658. 2,
  659. 4320,
  660. [3]uint16{10, 11, 12},
  661. 1000,
  662. 20,
  663. }
  664. //nolint:govet
  665. wDelayer := &common.WDelayerVariables{
  666. 0,
  667. ethCommon.BigToAddress(big.NewInt(2)),
  668. ethCommon.BigToAddress(big.NewInt(3)),
  669. 13,
  670. 14,
  671. false,
  672. }
  673. return rollup, auction, wDelayer
  674. }
  675. func TestSetInitialSCVars(t *testing.T) {
  676. test.WipeDB(historyDB.DB())
  677. _, _, _, err := historyDB.GetSCVars()
  678. assert.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
  679. rollup, auction, wDelayer := exampleInitSCVars()
  680. err = historyDB.SetInitialSCVars(rollup, auction, wDelayer)
  681. require.NoError(t, err)
  682. dbRollup, dbAuction, dbWDelayer, err := historyDB.GetSCVars()
  683. require.NoError(t, err)
  684. require.Equal(t, rollup, dbRollup)
  685. require.Equal(t, auction, dbAuction)
  686. require.Equal(t, wDelayer, dbWDelayer)
  687. }
  688. func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
  689. test.WipeDB(historyDB.DB())
  690. set := `
  691. Type: Blockchain
  692. AddToken(1)
  693. CreateAccountDeposit(1) A: 2000
  694. CreateAccountDeposit(1) B: 500
  695. CreateAccountDeposit(1) C: 500
  696. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{*}
  697. > block // blockNum=2
  698. > batchL1 // forge defined L1UserTxs{*}
  699. > block // blockNum=3
  700. `
  701. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  702. tilCfgExtra := til.ConfigExtra{
  703. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  704. CoordUser: "A",
  705. }
  706. blocks, err := tc.GenerateBlocks(set)
  707. require.NoError(t, err)
  708. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  709. require.NoError(t, err)
  710. err = tc.FillBlocksForgedL1UserTxs(blocks)
  711. require.NoError(t, err)
  712. // Add only first block so that the L1UserTxs are not marked as forged
  713. for i := range blocks[:1] {
  714. err = historyDB.AddBlockSCData(&blocks[i])
  715. require.NoError(t, err)
  716. }
  717. // Add second batch to trigger the update of the batch_num,
  718. // while avoiding the implicit call of setL1UserTxEffectiveAmounts
  719. err = historyDB.addBlock(historyDB.db, &blocks[1].Block)
  720. assert.NoError(t, err)
  721. err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch)
  722. assert.NoError(t, err)
  723. require.NoError(t, err)
  724. // Set the Effective{Amount,DepositAmount} of the L1UserTxs that are forged in the second block
  725. l1Txs := blocks[1].Rollup.Batches[0].L1UserTxs
  726. require.Equal(t, 3, len(l1Txs))
  727. // Change some values to test all cases
  728. l1Txs[1].EffectiveAmount = big.NewInt(0)
  729. l1Txs[2].EffectiveDepositAmount = big.NewInt(0)
  730. l1Txs[2].EffectiveAmount = big.NewInt(0)
  731. err = historyDB.setL1UserTxEffectiveAmounts(historyDB.db, l1Txs)
  732. require.NoError(t, err)
  733. dbL1Txs, err := historyDB.GetAllL1UserTxs()
  734. require.NoError(t, err)
  735. for i, tx := range dbL1Txs {
  736. log.Infof("%d %v %v", i, tx.EffectiveAmount, tx.EffectiveDepositAmount)
  737. assert.NotNil(t, tx.EffectiveAmount)
  738. assert.NotNil(t, tx.EffectiveDepositAmount)
  739. switch tx.TxID {
  740. case l1Txs[0].TxID:
  741. assert.Equal(t, l1Txs[0].DepositAmount, tx.EffectiveDepositAmount)
  742. assert.Equal(t, l1Txs[0].Amount, tx.EffectiveAmount)
  743. case l1Txs[1].TxID:
  744. assert.Equal(t, l1Txs[1].DepositAmount, tx.EffectiveDepositAmount)
  745. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  746. case l1Txs[2].TxID:
  747. assert.Equal(t, big.NewInt(0), tx.EffectiveDepositAmount)
  748. assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
  749. }
  750. }
  751. }
  752. func TestUpdateExitTree(t *testing.T) {
  753. test.WipeDB(historyDB.DB())
  754. set := `
  755. Type: Blockchain
  756. AddToken(1)
  757. CreateAccountDeposit(1) C: 2000 // Idx=256+2=258
  758. CreateAccountDeposit(1) D: 500 // Idx=256+3=259
  759. CreateAccountCoordinator(1) A // Idx=256+0=256
  760. CreateAccountCoordinator(1) B // Idx=256+1=257
  761. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{5}
  762. > batchL1 // forge defined L1UserTxs{5}, freeze L1UserTxs{nil}
  763. > block // blockNum=2
  764. ForceExit(1) A: 100
  765. ForceExit(1) B: 80
  766. Exit(1) C: 50 (172)
  767. Exit(1) D: 30 (172)
  768. > batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{3}
  769. > batchL1 // forge L1UserTxs{3}, freeze defined L1UserTxs{nil}
  770. > block // blockNum=3
  771. > block // blockNum=4 (empty block)
  772. > block // blockNum=5 (empty block)
  773. `
  774. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  775. tilCfgExtra := til.ConfigExtra{
  776. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  777. CoordUser: "A",
  778. }
  779. blocks, err := tc.GenerateBlocks(set)
  780. require.NoError(t, err)
  781. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  782. require.NoError(t, err)
  783. // Add all blocks except for the last two
  784. for i := range blocks[:len(blocks)-2] {
  785. err = historyDB.AddBlockSCData(&blocks[i])
  786. require.NoError(t, err)
  787. }
  788. // Add withdraws to the second-to-last block, and insert block into the DB
  789. block := &blocks[len(blocks)-2]
  790. require.Equal(t, int64(4), block.Block.Num)
  791. tokenAddr := blocks[0].Rollup.AddedTokens[0].EthAddr
  792. // block.WDelayer.Deposits = append(block.WDelayer.Deposits,
  793. // common.WDelayerTransfer{Owner: tc.UsersByIdx[257].Addr, Token: tokenAddr, Amount: big.NewInt(80)}, // 257
  794. // common.WDelayerTransfer{Owner: tc.UsersByIdx[259].Addr, Token: tokenAddr, Amount: big.NewInt(15)}, // 259
  795. // )
  796. block.Rollup.Withdrawals = append(block.Rollup.Withdrawals,
  797. common.WithdrawInfo{Idx: 256, NumExitRoot: 4, InstantWithdraw: true},
  798. common.WithdrawInfo{Idx: 257, NumExitRoot: 4, InstantWithdraw: false,
  799. Owner: tc.UsersByIdx[257].Addr, Token: tokenAddr},
  800. common.WithdrawInfo{Idx: 258, NumExitRoot: 3, InstantWithdraw: true},
  801. common.WithdrawInfo{Idx: 259, NumExitRoot: 3, InstantWithdraw: false,
  802. Owner: tc.UsersByIdx[259].Addr, Token: tokenAddr},
  803. )
  804. err = historyDB.addBlock(historyDB.db, &block.Block)
  805. require.NoError(t, err)
  806. err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
  807. block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
  808. require.NoError(t, err)
  809. // Check that exits in DB match with the expected values
  810. dbExits, err := historyDB.GetAllExits()
  811. require.NoError(t, err)
  812. assert.Equal(t, 4, len(dbExits))
  813. dbExitsByIdx := make(map[common.Idx]common.ExitInfo)
  814. for _, dbExit := range dbExits {
  815. dbExitsByIdx[dbExit.AccountIdx] = dbExit
  816. }
  817. for _, withdraw := range block.Rollup.Withdrawals {
  818. assert.Equal(t, withdraw.NumExitRoot, dbExitsByIdx[withdraw.Idx].BatchNum)
  819. if withdraw.InstantWithdraw {
  820. assert.Equal(t, &block.Block.Num, dbExitsByIdx[withdraw.Idx].InstantWithdrawn)
  821. } else {
  822. assert.Equal(t, &block.Block.Num, dbExitsByIdx[withdraw.Idx].DelayedWithdrawRequest)
  823. }
  824. }
  825. // Add delayed withdraw to the last block, and insert block into the DB
  826. block = &blocks[len(blocks)-1]
  827. require.Equal(t, int64(5), block.Block.Num)
  828. block.WDelayer.Withdrawals = append(block.WDelayer.Withdrawals,
  829. common.WDelayerTransfer{
  830. Owner: tc.UsersByIdx[257].Addr,
  831. Token: tokenAddr,
  832. Amount: big.NewInt(80),
  833. })
  834. err = historyDB.addBlock(historyDB.db, &block.Block)
  835. require.NoError(t, err)
  836. err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
  837. block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
  838. require.NoError(t, err)
  839. // Check that delayed withdrawn has been set
  840. dbExits, err = historyDB.GetAllExits()
  841. require.NoError(t, err)
  842. for _, dbExit := range dbExits {
  843. dbExitsByIdx[dbExit.AccountIdx] = dbExit
  844. }
  845. require.Equal(t, &block.Block.Num, dbExitsByIdx[257].DelayedWithdrawn)
  846. }
  847. func TestGetBestBidCoordinator(t *testing.T) {
  848. test.WipeDB(historyDB.DB())
  849. rollup, auction, wDelayer := exampleInitSCVars()
  850. err := historyDB.SetInitialSCVars(rollup, auction, wDelayer)
  851. require.NoError(t, err)
  852. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  853. blocks, err := tc.GenerateBlocks(`
  854. Type: Blockchain
  855. > block // blockNum=2
  856. `)
  857. require.NoError(t, err)
  858. err = historyDB.AddBlockSCData(&blocks[0])
  859. require.NoError(t, err)
  860. coords := []common.Coordinator{
  861. {
  862. Bidder: ethCommon.BigToAddress(big.NewInt(1)),
  863. Forger: ethCommon.BigToAddress(big.NewInt(2)),
  864. EthBlockNum: 2,
  865. URL: "foo",
  866. },
  867. {
  868. Bidder: ethCommon.BigToAddress(big.NewInt(3)),
  869. Forger: ethCommon.BigToAddress(big.NewInt(4)),
  870. EthBlockNum: 2,
  871. URL: "bar",
  872. },
  873. }
  874. err = historyDB.addCoordinators(historyDB.db, coords)
  875. require.NoError(t, err)
  876. bids := []common.Bid{
  877. {
  878. SlotNum: 10,
  879. BidValue: big.NewInt(10),
  880. EthBlockNum: 2,
  881. Bidder: coords[0].Bidder,
  882. },
  883. {
  884. SlotNum: 10,
  885. BidValue: big.NewInt(20),
  886. EthBlockNum: 2,
  887. Bidder: coords[1].Bidder,
  888. },
  889. }
  890. err = historyDB.addBids(historyDB.db, bids)
  891. require.NoError(t, err)
  892. forger10, err := historyDB.GetBestBidCoordinator(10)
  893. require.NoError(t, err)
  894. require.Equal(t, coords[1].Forger, forger10.Forger)
  895. require.Equal(t, coords[1].Bidder, forger10.Bidder)
  896. require.Equal(t, coords[1].URL, forger10.URL)
  897. require.Equal(t, bids[1].SlotNum, forger10.SlotNum)
  898. require.Equal(t, bids[1].BidValue, forger10.BidValue)
  899. for i := range forger10.DefaultSlotSetBid {
  900. require.Equal(t, auction.DefaultSlotSetBid[i], forger10.DefaultSlotSetBid[i])
  901. }
  902. _, err = historyDB.GetBestBidCoordinator(11)
  903. require.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
  904. }
  905. func TestAddBucketUpdates(t *testing.T) {
  906. test.WipeDB(historyDB.DB())
  907. const fromBlock int64 = 1
  908. const toBlock int64 = 5 + 1
  909. setTestBlocks(fromBlock, toBlock)
  910. bucketUpdates := []common.BucketUpdate{
  911. {
  912. EthBlockNum: 4,
  913. NumBucket: 0,
  914. BlockStamp: 4,
  915. Withdrawals: big.NewInt(123),
  916. },
  917. {
  918. EthBlockNum: 5,
  919. NumBucket: 2,
  920. BlockStamp: 5,
  921. Withdrawals: big.NewInt(42),
  922. },
  923. }
  924. err := historyDB.addBucketUpdates(historyDB.db, bucketUpdates)
  925. require.NoError(t, err)
  926. dbBucketUpdates, err := historyDB.GetAllBucketUpdates()
  927. require.NoError(t, err)
  928. assert.Equal(t, bucketUpdates, dbBucketUpdates)
  929. }
  930. func TestAddTokenExchanges(t *testing.T) {
  931. test.WipeDB(historyDB.DB())
  932. const fromBlock int64 = 1
  933. const toBlock int64 = 5 + 1
  934. setTestBlocks(fromBlock, toBlock)
  935. tokenExchanges := []common.TokenExchange{
  936. {
  937. EthBlockNum: 4,
  938. Address: ethCommon.BigToAddress(big.NewInt(111)),
  939. ValueUSD: 12345,
  940. },
  941. {
  942. EthBlockNum: 5,
  943. Address: ethCommon.BigToAddress(big.NewInt(222)),
  944. ValueUSD: 67890,
  945. },
  946. }
  947. err := historyDB.addTokenExchanges(historyDB.db, tokenExchanges)
  948. require.NoError(t, err)
  949. dbTokenExchanges, err := historyDB.GetAllTokenExchanges()
  950. require.NoError(t, err)
  951. assert.Equal(t, tokenExchanges, dbTokenExchanges)
  952. }
  953. func TestAddEscapeHatchWithdrawals(t *testing.T) {
  954. test.WipeDB(historyDB.DB())
  955. const fromBlock int64 = 1
  956. const toBlock int64 = 5 + 1
  957. setTestBlocks(fromBlock, toBlock)
  958. escapeHatchWithdrawals := []common.WDelayerEscapeHatchWithdrawal{
  959. {
  960. EthBlockNum: 4,
  961. Who: ethCommon.BigToAddress(big.NewInt(111)),
  962. To: ethCommon.BigToAddress(big.NewInt(222)),
  963. TokenAddr: ethCommon.BigToAddress(big.NewInt(333)),
  964. Amount: big.NewInt(10002),
  965. },
  966. {
  967. EthBlockNum: 5,
  968. Who: ethCommon.BigToAddress(big.NewInt(444)),
  969. To: ethCommon.BigToAddress(big.NewInt(555)),
  970. TokenAddr: ethCommon.BigToAddress(big.NewInt(666)),
  971. Amount: big.NewInt(20003),
  972. },
  973. }
  974. err := historyDB.addEscapeHatchWithdrawals(historyDB.db, escapeHatchWithdrawals)
  975. require.NoError(t, err)
  976. dbEscapeHatchWithdrawals, err := historyDB.GetAllEscapeHatchWithdrawals()
  977. require.NoError(t, err)
  978. assert.Equal(t, escapeHatchWithdrawals, dbEscapeHatchWithdrawals)
  979. }
  980. func TestGetMetrics(t *testing.T) {
  981. test.WipeDB(historyDB.DB())
  982. set := `
  983. Type: Blockchain
  984. AddToken(1)
  985. CreateAccountDeposit(1) A: 1000 // numTx=1
  986. CreateAccountDeposit(1) B: 2000 // numTx=2
  987. CreateAccountDeposit(1) C: 3000 //numTx=3
  988. // block 0 is stored as default in the DB
  989. // block 1 does not exist
  990. > batchL1 // numBatches=1
  991. > batchL1 // numBatches=2
  992. > block // blockNum=2
  993. Transfer(1) C-A : 10 (1) // numTx=4
  994. > batch // numBatches=3
  995. > block // blockNum=3
  996. Transfer(1) B-C : 10 (1) // numTx=5
  997. > batch // numBatches=5
  998. > block // blockNum=4
  999. Transfer(1) A-B : 10 (1) // numTx=6
  1000. > batch // numBatches=5
  1001. > block // blockNum=5
  1002. Transfer(1) A-B : 10 (1) // numTx=7
  1003. > batch // numBatches=6
  1004. > block // blockNum=6
  1005. `
  1006. const numBatches int = 6
  1007. const numTx int = 7
  1008. const blockNum = 6 - 1
  1009. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  1010. tilCfgExtra := til.ConfigExtra{
  1011. BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
  1012. CoordUser: "A",
  1013. }
  1014. blocks, err := tc.GenerateBlocks(set)
  1015. require.NoError(t, err)
  1016. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  1017. require.NoError(t, err)
  1018. // Sanity check
  1019. require.Equal(t, blockNum, len(blocks))
  1020. // Adding one batch per block
  1021. // batch frequency can be chosen
  1022. const frequency int = 15
  1023. for i := range blocks {
  1024. blocks[i].Block.Timestamp = time.Now().Add(-time.Second * time.Duration(frequency*(len(blocks)-i)))
  1025. err = historyDB.AddBlockSCData(&blocks[i])
  1026. assert.NoError(t, err)
  1027. }
  1028. res, err := historyDB.GetMetrics(common.BatchNum(numBatches))
  1029. assert.NoError(t, err)
  1030. assert.Equal(t, float64(numTx)/float64(numBatches-1), res.TransactionsPerBatch)
  1031. // Frequency is not exactly the desired one, some decimals may appear
  1032. assert.GreaterOrEqual(t, res.BatchFrequency, float64(frequency))
  1033. assert.Less(t, res.BatchFrequency, float64(frequency+1))
  1034. // Truncate frecuency into an int to do an exact check
  1035. assert.Equal(t, frequency, int(res.BatchFrequency))
  1036. // This may also be different in some decimals
  1037. // Truncate it to the third decimal to compare
  1038. assert.Equal(t, math.Trunc((float64(numTx)/float64(frequency*blockNum-frequency))/0.001)*0.001, math.Trunc(res.TransactionsPerSecond/0.001)*0.001)
  1039. assert.Equal(t, int64(3), res.TotalAccounts)
  1040. assert.Equal(t, int64(3), res.TotalBJJs)
  1041. // Til does not set fees
  1042. assert.Equal(t, float64(0), res.AvgTransactionFee)
  1043. }
  1044. func TestGetMetricsMoreThan24Hours(t *testing.T) {
  1045. test.WipeDB(historyDB.DB())
  1046. testUsersLen := 3
  1047. var set []til.Instruction
  1048. for user := 0; user < testUsersLen; user++ {
  1049. set = append(set, til.Instruction{
  1050. Typ: common.TxTypeCreateAccountDeposit,
  1051. TokenID: common.TokenID(0),
  1052. DepositAmount: big.NewInt(1000000),
  1053. Amount: big.NewInt(0),
  1054. From: fmt.Sprintf("User%02d", user),
  1055. })
  1056. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1057. }
  1058. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1059. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1060. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1061. // Transfers
  1062. for x := 0; x < 6000; x++ {
  1063. set = append(set, til.Instruction{
  1064. Typ: common.TxTypeTransfer,
  1065. TokenID: common.TokenID(0),
  1066. DepositAmount: big.NewInt(1),
  1067. Amount: big.NewInt(0),
  1068. From: "User00",
  1069. To: "User01",
  1070. })
  1071. set = append(set, til.Instruction{Typ: til.TypeNewBatch})
  1072. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1073. }
  1074. var chainID uint16 = 0
  1075. tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
  1076. blocks, err := tc.GenerateBlocksFromInstructions(set)
  1077. assert.NoError(t, err)
  1078. tilCfgExtra := til.ConfigExtra{
  1079. CoordUser: "A",
  1080. }
  1081. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  1082. require.NoError(t, err)
  1083. const numBatches int = 6002
  1084. const numTx int = 6003
  1085. const blockNum = 6005 - 1
  1086. // Sanity check
  1087. require.Equal(t, blockNum, len(blocks))
  1088. // Adding one batch per block
  1089. // batch frequency can be chosen
  1090. const frequency int = 15
  1091. for i := range blocks {
  1092. blocks[i].Block.Timestamp = time.Now().Add(-time.Second * time.Duration(frequency*(len(blocks)-i)))
  1093. err = historyDB.AddBlockSCData(&blocks[i])
  1094. assert.NoError(t, err)
  1095. }
  1096. res, err := historyDB.GetMetrics(common.BatchNum(numBatches))
  1097. assert.NoError(t, err)
  1098. assert.Equal(t, math.Trunc((float64(numTx)/float64(numBatches-1))/0.001)*0.001, math.Trunc(res.TransactionsPerBatch/0.001)*0.001)
  1099. // Frequency is not exactly the desired one, some decimals may appear
  1100. assert.GreaterOrEqual(t, res.BatchFrequency, float64(frequency))
  1101. assert.Less(t, res.BatchFrequency, float64(frequency+1))
  1102. // Truncate frecuency into an int to do an exact check
  1103. assert.Equal(t, frequency, int(res.BatchFrequency))
  1104. // This may also be different in some decimals
  1105. // Truncate it to the third decimal to compare
  1106. assert.Equal(t, math.Trunc((float64(numTx)/float64(frequency*blockNum-frequency))/0.001)*0.001, math.Trunc(res.TransactionsPerSecond/0.001)*0.001)
  1107. assert.Equal(t, int64(3), res.TotalAccounts)
  1108. assert.Equal(t, int64(3), res.TotalBJJs)
  1109. // Til does not set fees
  1110. assert.Equal(t, float64(0), res.AvgTransactionFee)
  1111. }
  1112. func TestGetMetricsEmpty(t *testing.T) {
  1113. test.WipeDB(historyDB.DB())
  1114. _, err := historyDB.GetMetrics(0)
  1115. assert.NoError(t, err)
  1116. }
  1117. func TestGetAvgTxFeeEmpty(t *testing.T) {
  1118. test.WipeDB(historyDB.DB())
  1119. _, err := historyDB.GetAvgTxFee()
  1120. assert.NoError(t, err)
  1121. }
  1122. func TestGetLastL1TxsNum(t *testing.T) {
  1123. test.WipeDB(historyDB.DB())
  1124. _, err := historyDB.GetLastL1TxsNum()
  1125. assert.NoError(t, err)
  1126. }
  1127. func TestGetLastTxsPosition(t *testing.T) {
  1128. test.WipeDB(historyDB.DB())
  1129. _, err := historyDB.GetLastTxsPosition(0)
  1130. assert.Equal(t, sql.ErrNoRows.Error(), err.Error())
  1131. }
  1132. func TestGetFirstBatchBlockNumBySlot(t *testing.T) {
  1133. test.WipeDB(historyDB.DB())
  1134. set := `
  1135. Type: Blockchain
  1136. // Slot = 0
  1137. > block // 2
  1138. > block // 3
  1139. > block // 4
  1140. > block // 5
  1141. // Slot = 1
  1142. > block // 6
  1143. > block // 7
  1144. > batch
  1145. > block // 8
  1146. > block // 9
  1147. // Slot = 2
  1148. > batch
  1149. > block // 10
  1150. > block // 11
  1151. > block // 12
  1152. > block // 13
  1153. `
  1154. tc := til.NewContext(uint16(0), common.RollupConstMaxL1UserTx)
  1155. blocks, err := tc.GenerateBlocks(set)
  1156. assert.NoError(t, err)
  1157. tilCfgExtra := til.ConfigExtra{
  1158. CoordUser: "A",
  1159. }
  1160. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  1161. require.NoError(t, err)
  1162. for i := range blocks {
  1163. for j := range blocks[i].Rollup.Batches {
  1164. blocks[i].Rollup.Batches[j].Batch.SlotNum = int64(i) / 4
  1165. }
  1166. }
  1167. // Add all blocks
  1168. for i := range blocks {
  1169. err = historyDB.AddBlockSCData(&blocks[i])
  1170. require.NoError(t, err)
  1171. }
  1172. _, err = historyDB.GetFirstBatchBlockNumBySlot(0)
  1173. require.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
  1174. bn1, err := historyDB.GetFirstBatchBlockNumBySlot(1)
  1175. require.NoError(t, err)
  1176. assert.Equal(t, int64(8), bn1)
  1177. bn2, err := historyDB.GetFirstBatchBlockNumBySlot(2)
  1178. require.NoError(t, err)
  1179. assert.Equal(t, int64(10), bn2)
  1180. }
  1181. func TestTxItemID(t *testing.T) {
  1182. test.WipeDB(historyDB.DB())
  1183. testUsersLen := 10
  1184. var set []til.Instruction
  1185. for user := 0; user < testUsersLen; user++ {
  1186. set = append(set, til.Instruction{
  1187. Typ: common.TxTypeCreateAccountDeposit,
  1188. TokenID: common.TokenID(0),
  1189. DepositAmount: big.NewInt(1000000),
  1190. Amount: big.NewInt(0),
  1191. From: fmt.Sprintf("User%02d", user),
  1192. })
  1193. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1194. }
  1195. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1196. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1197. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1198. for user := 0; user < testUsersLen; user++ {
  1199. set = append(set, til.Instruction{
  1200. Typ: common.TxTypeDeposit,
  1201. TokenID: common.TokenID(0),
  1202. DepositAmount: big.NewInt(100000),
  1203. Amount: big.NewInt(0),
  1204. From: fmt.Sprintf("User%02d", user),
  1205. })
  1206. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1207. }
  1208. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1209. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1210. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1211. for user := 0; user < testUsersLen; user++ {
  1212. set = append(set, til.Instruction{
  1213. Typ: common.TxTypeDepositTransfer,
  1214. TokenID: common.TokenID(0),
  1215. DepositAmount: big.NewInt(10000 * int64(user+1)),
  1216. Amount: big.NewInt(1000 * int64(user+1)),
  1217. From: fmt.Sprintf("User%02d", user),
  1218. To: fmt.Sprintf("User%02d", (user+1)%testUsersLen),
  1219. })
  1220. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1221. }
  1222. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1223. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1224. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1225. for user := 0; user < testUsersLen; user++ {
  1226. set = append(set, til.Instruction{
  1227. Typ: common.TxTypeForceTransfer,
  1228. TokenID: common.TokenID(0),
  1229. Amount: big.NewInt(100 * int64(user+1)),
  1230. DepositAmount: big.NewInt(0),
  1231. From: fmt.Sprintf("User%02d", user),
  1232. To: fmt.Sprintf("User%02d", (user+1)%testUsersLen),
  1233. })
  1234. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1235. }
  1236. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1237. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1238. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1239. for user := 0; user < testUsersLen; user++ {
  1240. set = append(set, til.Instruction{
  1241. Typ: common.TxTypeForceExit,
  1242. TokenID: common.TokenID(0),
  1243. Amount: big.NewInt(10 * int64(user+1)),
  1244. DepositAmount: big.NewInt(0),
  1245. From: fmt.Sprintf("User%02d", user),
  1246. })
  1247. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1248. }
  1249. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1250. set = append(set, til.Instruction{Typ: til.TypeNewBatchL1})
  1251. set = append(set, til.Instruction{Typ: til.TypeNewBlock})
  1252. var chainID uint16 = 0
  1253. tc := til.NewContext(chainID, common.RollupConstMaxL1UserTx)
  1254. blocks, err := tc.GenerateBlocksFromInstructions(set)
  1255. assert.NoError(t, err)
  1256. tilCfgExtra := til.ConfigExtra{
  1257. CoordUser: "A",
  1258. }
  1259. err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
  1260. require.NoError(t, err)
  1261. // Add all blocks
  1262. for i := range blocks {
  1263. err = historyDB.AddBlockSCData(&blocks[i])
  1264. require.NoError(t, err)
  1265. }
  1266. txs, err := historyDB.GetAllL1UserTxs()
  1267. require.NoError(t, err)
  1268. position := 0
  1269. for _, tx := range txs {
  1270. if tx.Position == 0 {
  1271. position = 0
  1272. }
  1273. assert.Equal(t, position, tx.Position)
  1274. position++
  1275. }
  1276. }
  1277. // setTestBlocks WARNING: this will delete the blocks and recreate them
  1278. func setTestBlocks(from, to int64) []common.Block {
  1279. test.WipeDB(historyDB.DB())
  1280. blocks := test.GenBlocks(from, to)
  1281. if err := historyDB.AddBlocks(blocks); err != nil {
  1282. panic(err)
  1283. }
  1284. return blocks
  1285. }