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.

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