From fa206370ec0bb24cbea9021c735c5a6c9350e594 Mon Sep 17 00:00:00 2001 From: Renan Santos Date: Thu, 4 Jul 2024 16:09:02 -0300 Subject: [PATCH] fix: todo --- CHANGELOG.md | 9 +++- build/compose-devnet.yaml | 1 - build/compose-sepolia.yaml | 1 - cmd/gen-devnet/deployer.go | 21 +++----- cmd/gen-devnet/types.go | 1 - docs/config.md | 8 +-- internal/node/config/config.go | 6 +-- internal/node/config/generate/Config.toml | 7 +-- internal/node/config/generated.go | 18 ++----- internal/node/services.go | 2 +- offchain/dispatcher/src/config.rs | 6 +-- offchain/dispatcher/src/drivers/context.rs | 49 ++++++++++++------- offchain/dispatcher/src/drivers/machine.rs | 18 +++---- offchain/dispatcher/src/drivers/mock.rs | 19 +++++-- .../dispatcher/src/machine/rollups_broker.rs | 21 +++++++- offchain/dispatcher/src/setup.rs | 15 +++--- offchain/types/src/blockchain_config.rs | 15 +++--- setup_env.sh | 1 - test/config.go | 4 +- 19 files changed, 115 insertions(+), 107 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a908394e0..6e76fa8b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,10 +19,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Changed the dispatcher to close epochs based on block numbers instead of block timestamps. -- **BREAKING**: replaced the environment variable `CARTESI_EPOCH_DURATION` with `CARTESI_EPOCH_LENGTH_IN_BLOCKS` to match the new epoch algorithm, and set its default value to 7200 (1 day worth of blocks, in average, considering one block is mined every 12 seconds). -- **BREAKING**: replaced the internal environment variable `RD_EPOCH_DURATION` with `RD_EPOCH_LENGTH_IN_BLOCKS` to match the new epoch algorithm, and also set its default value to 7200. +- **BREAKING**: changed the dispatcher to use the `CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER` environment variable instead of `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER`. +- **BREAKING**: replaced the environment variable `CARTESI_EPOCH_DURATION` with `CARTESI_EPOCH_LENGTH` to match the new epoch algorithm, and set its default value to 7200 (1 day worth of blocks, in average, considering one block is mined every 12 seconds). +- **BREAKING**: replaced the internal environment variable `RD_EPOCH_DURATION` with `RD_EPOCH_LENGTH` to match the new epoch algorithm, and also set its default value to 7200. - Bumped Rust Version to 1.78.0 +### Removed + +- Removed `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER` environment variable. + ## [1.4.0] 2024-04-09 ### Added diff --git a/build/compose-devnet.yaml b/build/compose-devnet.yaml index e645e54d4..5fb3b62b7 100644 --- a/build/compose-devnet.yaml +++ b/build/compose-devnet.yaml @@ -20,7 +20,6 @@ services: CARTESI_BLOCKCHAIN_IS_LEGACY: "false" CARTESI_BLOCKCHAIN_FINALITY_OFFSET: "1" CARTESI_CONTRACTS_APPLICATION_ADDRESS: "0x7C54E3f7A8070a54223469965A871fB8f6f88c22" - CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER: "20" CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x325272217ae6815b494bF38cED004c5Eb8a7CdA7" CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0x58c93F83fb3304730C95aad2E360cdb88b782010" CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768" diff --git a/build/compose-sepolia.yaml b/build/compose-sepolia.yaml index e340b8b94..8229efdcd 100644 --- a/build/compose-sepolia.yaml +++ b/build/compose-sepolia.yaml @@ -15,7 +15,6 @@ services: CARTESI_BLOCKCHAIN_IS_LEGACY: "false" CARTESI_BLOCKCHAIN_FINALITY_OFFSET: "1" CARTESI_CONTRACTS_APPLICATION_ADDRESS: "0x9f12D4365806FC000D6555ACB85c5371b464E506" - CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER: "4152308" CARTESI_CONTRACTS_HISTORY_ADDRESS: "0x76f4dCaC0920826541EE718214EEE4be07346cEE" CARTESI_CONTRACTS_AUTHORITY_ADDRESS: "0x5827Ec9365D3a9b27bF1dB982d258Ad234D37242" CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768" diff --git a/cmd/gen-devnet/deployer.go b/cmd/gen-devnet/deployer.go index 9fcc4a578..25ef0fc22 100644 --- a/cmd/gen-devnet/deployer.go +++ b/cmd/gen-devnet/deployer.go @@ -5,7 +5,6 @@ package main import ( "context" - "encoding/json" "fmt" "os/exec" "strings" @@ -44,7 +43,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error) var depInfo DeploymentInfo // Create the Authority/History pair - contractAddresses, _, err := createContracts(ctx, + contractAddresses, err := createContracts(ctx, common.Address.Hex(addresses.GetTestBook().AuthorityHistoryPairFactory), "newAuthorityHistoryPair(address,bytes32)(address,address)", CONTRACT_OWNER_ADDRESS, @@ -57,7 +56,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error) depInfo.HistoryAddress = contractAddresses[1] // Create the Application, passing the address of the newly created Authority - contractAddresses, blockNumber, err := createContracts(ctx, + contractAddresses, err = createContracts(ctx, common.Address.Hex(addresses.GetTestBook().CartesiDAppFactory), "newApplication(address,address,bytes32,bytes32)(address)", depInfo.AuthorityAddress, @@ -69,7 +68,6 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error) } depInfo.ApplicationAddress = contractAddresses[0] - depInfo.BlockNumber = blockNumber return depInfo, nil } @@ -80,7 +78,7 @@ func createApplication(ctx context.Context, hash string) (DeploymentInfo, error) // // Warning: a second call to a contract with the same arguments will fail. func createContracts(ctx context.Context, - args ...string) ([]string, string, error) { + args ...string) ([]string, error) { commonArgs := []string{"--rpc-url", RPC_URL} commonArgs = append(commonArgs, args...) @@ -94,7 +92,7 @@ func createContracts(ctx context.Context, castCall.Stdout = &outStrBuilder err := castCall.Run() if err != nil { - return contractAddresses, "", fmt.Errorf("command failed %v: %v", castCall.Args, err) + return contractAddresses, fmt.Errorf("command failed %v: %v", castCall.Args, err) } contractAddresses = strings.Fields(outStrBuilder.String()) @@ -110,20 +108,13 @@ func createContracts(ctx context.Context, castSend.Stdout = &outStrBuilder err = castSend.Run() if err != nil { - return contractAddresses, "", fmt.Errorf("command failed %v: %v", castSend.Args, err) + return contractAddresses, fmt.Errorf("command failed %v: %v", castSend.Args, err) } if VerboseLog { fmt.Printf("deployer: command: %s\n", castSend.Args) fmt.Printf("deployer: output: %s\n", outStrBuilder.String()) } - // Extract blockNumber from JSON output - jsonMap := make(map[string](any)) - err = json.Unmarshal([]byte([]byte(outStrBuilder.String())), &jsonMap) - if err != nil { - return contractAddresses, "", fmt.Errorf("failed to extract block number, %s", err.Error()) - } - blockNumber := jsonMap["blockNumber"].(string) - return contractAddresses, blockNumber, nil + return contractAddresses, nil } diff --git a/cmd/gen-devnet/types.go b/cmd/gen-devnet/types.go index c55181bb3..954a05882 100644 --- a/cmd/gen-devnet/types.go +++ b/cmd/gen-devnet/types.go @@ -7,5 +7,4 @@ type DeploymentInfo struct { AuthorityAddress string `json:"CARTESI_CONTRACTS_AUTHORITY_ADDRESS"` HistoryAddress string `json:"CARTESI_CONTRACTS_HISTORY_ADDRESS"` ApplicationAddress string `json:"CARTESI_CONTRACTS_APPLICATION_ADDRESS"` - BlockNumber string `json:"CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER"` } diff --git a/docs/config.md b/docs/config.md index fa6278d7f..a48b45f4e 100644 --- a/docs/config.md +++ b/docs/config.md @@ -116,12 +116,6 @@ Address of the DApp's contract. * **Type:** `string` -## `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER` - -Block in which the DApp's contract was deployed. - -* **Type:** `int64` - ## `CARTESI_CONTRACTS_AUTHORITY_ADDRESS` Address of the Authority contract. @@ -236,7 +230,7 @@ for more information. * **Type:** `string` * **Default:** `""` -## `CARTESI_EPOCH_LENGTH_IN_BLOCKS` +## `CARTESI_EPOCH_LENGTH` Length of a rollups epoch in blocks. diff --git a/internal/node/config/config.go b/internal/node/config/config.go index 5c6a3d3eb..fea24421d 100644 --- a/internal/node/config/config.go +++ b/internal/node/config/config.go @@ -15,7 +15,7 @@ import ( type NodeConfig struct { LogLevel LogLevel LogPretty bool - RollupsEpochLengthInBlocks uint64 + RollupsEpochLength uint64 BlockchainID uint64 BlockchainHttpEndpoint Redacted[string] BlockchainWsEndpoint Redacted[string] @@ -75,7 +75,7 @@ func FromEnv() NodeConfig { var config NodeConfig config.LogLevel = getLogLevel() config.LogPretty = getLogPretty() - config.RollupsEpochLengthInBlocks = getEpochLengthInBlocks() + config.RollupsEpochLength = getEpochLength() config.BlockchainID = getBlockchainId() config.BlockchainHttpEndpoint = Redacted[string]{getBlockchainHttpEndpoint()} config.BlockchainWsEndpoint = Redacted[string]{getBlockchainWsEndpoint()} @@ -83,8 +83,6 @@ func FromEnv() NodeConfig { config.BlockchainFinalityOffset = getBlockchainFinalityOffset() config.BlockchainBlockTimeout = getBlockchainBlockTimeout() config.ContractsApplicationAddress = getContractsApplicationAddress() - config.ContractsApplicationDeploymentBlockNumber = - getContractsApplicationDeploymentBlockNumber() config.ContractsHistoryAddress = getContractsHistoryAddress() config.ContractsAuthorityAddress = getContractsAuthorityAddress() config.ContractsInputBoxAddress = getContractsInputBoxAddress() diff --git a/internal/node/config/generate/Config.toml b/internal/node/config/generate/Config.toml index d0ddaeca9..bda385b6c 100644 --- a/internal/node/config/generate/Config.toml +++ b/internal/node/config/generate/Config.toml @@ -45,7 +45,7 @@ the snapshot matches the hash in the Application contract.""" # Rollups # -[rollups.CARTESI_EPOCH_LENGTH_IN_BLOCKS] +[rollups.CARTESI_EPOCH_LENGTH] default = "7200" # 1 day (average) in blocks (considering one block is mined every 12 seconds) go-type = "uint64" description = """ @@ -101,11 +101,6 @@ go-type = "string" description = """ Address of the DApp's contract.""" -[contracts.CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER] -go-type = "int64" -description = """ -Block in which the DApp's contract was deployed.""" - [contracts.CARTESI_CONTRACTS_HISTORY_ADDRESS] go-type = "string" description = """ diff --git a/internal/node/config/generated.go b/internal/node/config/generated.go index 656715e23..bb60cf9d5 100644 --- a/internal/node/config/generated.go +++ b/internal/node/config/generated.go @@ -281,18 +281,6 @@ func getContractsApplicationAddress() string { return val } -func getContractsApplicationDeploymentBlockNumber() int64 { - s, ok := os.LookupEnv("CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER") - if !ok { - panic("missing env var CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER") - } - val, err := toInt64(s) - if err != nil { - panic(fmt.Sprintf("failed to parse CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER: %v", err)) - } - return val -} - func getContractsAuthorityAddress() string { s, ok := os.LookupEnv("CARTESI_CONTRACTS_AUTHORITY_ADDRESS") if !ok { @@ -473,14 +461,14 @@ func getPostgresEndpoint() string { return val } -func getEpochLengthInBlocks() uint64 { - s, ok := os.LookupEnv("CARTESI_EPOCH_LENGTH_IN_BLOCKS") +func getEpochLength() uint64 { + s, ok := os.LookupEnv("CARTESI_EPOCH_LENGTH") if !ok { s = "7200" } val, err := toUint64(s) if err != nil { - panic(fmt.Sprintf("failed to parse CARTESI_EPOCH_LENGTH_IN_BLOCKS: %v", err)) + panic(fmt.Sprintf("failed to parse CARTESI_EPOCH_LENGTH: %v", err)) } return val } diff --git a/internal/node/services.go b/internal/node/services.go index 83e290777..f76193532 100644 --- a/internal/node/services.go +++ b/internal/node/services.go @@ -160,7 +160,7 @@ func newDispatcher(c config.NodeConfig, workDir string) services.CommandService s.Env = append(s.Env, fmt.Sprintf("HISTORY_ADDRESS=%v", c.ContractsHistoryAddress)) s.Env = append(s.Env, fmt.Sprintf("AUTHORITY_ADDRESS=%v", c.ContractsAuthorityAddress)) s.Env = append(s.Env, fmt.Sprintf("INPUT_BOX_ADDRESS=%v", c.ContractsInputBoxAddress)) - s.Env = append(s.Env, fmt.Sprintf("RD_EPOCH_LENGTH_IN_BLOCKS=%v", c.RollupsEpochLengthInBlocks)) + s.Env = append(s.Env, fmt.Sprintf("RD_EPOCH_LENGTH=%v", c.RollupsEpochLength)) s.Env = append(s.Env, fmt.Sprintf("CHAIN_ID=%v", c.BlockchainID)) s.Env = append(s.Env, fmt.Sprintf("DISPATCHER_HTTP_SERVER_PORT=%v", getPort(c, portOffsetDispatcher))) diff --git a/offchain/dispatcher/src/config.rs b/offchain/dispatcher/src/config.rs index e5caf540e..eb1f16124 100644 --- a/offchain/dispatcher/src/config.rs +++ b/offchain/dispatcher/src/config.rs @@ -32,7 +32,7 @@ pub struct DispatcherEnvCLIConfig { /// Duration of rollups epoch in blocks, for which dispatcher will make claims. #[arg(long, env, default_value = "7200")] - pub rd_epoch_length_in_blocks: u64, + pub rd_epoch_length: u64, /// Chain ID #[arg(long, env)] @@ -46,7 +46,7 @@ pub struct DispatcherConfig { pub log_config: LogConfig, pub blockchain_config: BlockchainConfig, - pub epoch_length_in_blocks: u64, + pub epoch_length: u64, pub chain_id: u64, } @@ -86,7 +86,7 @@ impl Config { broker_config, log_config, blockchain_config, - epoch_length_in_blocks: dispatcher_config.rd_epoch_length_in_blocks, + epoch_length: dispatcher_config.rd_epoch_length, chain_id: dispatcher_config.chain_id, }; diff --git a/offchain/dispatcher/src/drivers/context.rs b/offchain/dispatcher/src/drivers/context.rs index 33d607cf3..357e69902 100644 --- a/offchain/dispatcher/src/drivers/context.rs +++ b/offchain/dispatcher/src/drivers/context.rs @@ -76,10 +76,14 @@ impl Context { .inc(); self.inputs_sent += 1; - self.last_input_epoch = Some(self.calculate_epoch(input_block_number)); + + let input_epoch = self.calculate_epoch(input_block_number); self.last_finished_epoch.map(|last_finished_epoch| { - assert!(self.last_input_epoch.unwrap() >= last_finished_epoch) + // Asserting that the calculated epoch comes after the last finished epoch. + // (If last_finished_epoch == None then we don't need the assertion.) + assert!(input_epoch > last_finished_epoch) }); + self.last_input_epoch = Some(input_epoch); Ok(()) } @@ -129,6 +133,13 @@ impl Context { .get_or_create(&self.dapp_metadata) .inc(); + // Asserting that there are inputs in the current epoch. + assert!(self.inputs_sent > 0); + assert!(self.last_input_epoch.is_some()); + self.last_finished_epoch.map(|last_finished_epoch| { + assert!(self.last_input_epoch.unwrap() > last_finished_epoch) + }); + self.last_finished_epoch = self.last_input_epoch; Ok(()) } @@ -404,7 +415,7 @@ mod tests { let result = context.finish_epoch_if_needed(12, &broker).await; assert!(result.is_ok()); broker.assert_state(vec![ - Event::Finish, // + Event::FinishEpoch(0), // ]); } @@ -457,7 +468,7 @@ mod tests { assert_eq!(context.last_finished_epoch, Some(1)); broker.assert_state(vec![ - Event::Finish, + Event::FinishEpoch(0), Event::Input(number_of_inputs_sent), ]); } @@ -499,10 +510,10 @@ mod tests { expected: vec![ Event::Input(0), Event::Input(1), - Event::Finish, + Event::FinishEpoch(0), Event::Input(2), Event::Input(3), - Event::Finish, + Event::FinishEpoch(1), ], }, Case { @@ -513,10 +524,10 @@ mod tests { Event::Input(0), Event::Input(1), Event::Input(2), - Event::Finish, + Event::FinishEpoch(0), Event::Input(3), Event::Input(4), - Event::Finish, + Event::FinishEpoch(1), ], }, Case { @@ -525,9 +536,9 @@ mod tests { last_block: 4, expected: vec![ Event::Input(0), - Event::Finish, + Event::FinishEpoch(0), Event::Input(1), - Event::Finish, + Event::FinishEpoch(1), ], }, Case { @@ -536,11 +547,11 @@ mod tests { last_block: 6, expected: vec![ Event::Input(0), - Event::Finish, + Event::FinishEpoch(0), Event::Input(1), - Event::Finish, + Event::FinishEpoch(1), Event::Input(2), - Event::Finish, + Event::FinishEpoch(2), ], }, Case { @@ -551,15 +562,15 @@ mod tests { Event::Input(0), Event::Input(1), Event::Input(2), - Event::Finish, + Event::FinishEpoch(0), Event::Input(3), Event::Input(4), - Event::Finish, + Event::FinishEpoch(1), Event::Input(5), Event::Input(6), Event::Input(7), Event::Input(8), - Event::Finish, + Event::FinishEpoch(2), Event::Input(9), Event::Input(10), ], @@ -570,11 +581,11 @@ mod tests { last_block: 10, expected: vec![ Event::Input(0), - Event::Finish, + Event::FinishEpoch(0), Event::Input(1), - Event::Finish, + Event::FinishEpoch(1), Event::Input(2), - Event::Finish, + Event::FinishEpoch(2), ], }, ]; diff --git a/offchain/dispatcher/src/drivers/machine.rs b/offchain/dispatcher/src/drivers/machine.rs index d8cb8656a..f899aae51 100644 --- a/offchain/dispatcher/src/drivers/machine.rs +++ b/offchain/dispatcher/src/drivers/machine.rs @@ -232,7 +232,7 @@ mod tests { let expected = vec![ mock::Event::Input(0), mock::Event::Input(1), - mock::Event::Finish, + mock::Event::FinishEpoch(0), ]; test_react(block, context, None, None, input_blocks, expected).await; } @@ -244,7 +244,7 @@ mod tests { let input_blocks = vec![9, 10]; let expected = vec![ mock::Event::Input(0), - mock::Event::Finish, + mock::Event::FinishEpoch(0), mock::Event::Input(1), ]; test_react(block, context, None, None, input_blocks, expected).await; @@ -269,7 +269,7 @@ mod tests { mock::Event::Input(0), mock::Event::Input(1), mock::Event::Input(2), - mock::Event::Finish, + mock::Event::FinishEpoch(0), mock::Event::Input(3), ]; test_react(block, context, None, None, input_blocks, expected).await; @@ -279,7 +279,7 @@ mod tests { async fn react_is_deterministic() { let final_expected = vec![ mock::Event::Input(0), - mock::Event::Finish, + mock::Event::FinishEpoch(0), mock::Event::Input(1), mock::Event::Input(2), mock::Event::Input(3), @@ -289,14 +289,14 @@ mod tests { mock::Event::Input(7), mock::Event::Input(8), mock::Event::Input(9), - mock::Event::Finish, + mock::Event::FinishEpoch(1), mock::Event::Input(10), mock::Event::Input(11), mock::Event::Input(12), mock::Event::Input(13), mock::Event::Input(14), mock::Event::Input(15), - mock::Event::Finish, + mock::Event::FinishEpoch(2), mock::Event::Input(16), mock::Event::Input(17), mock::Event::Input(18), @@ -338,7 +338,7 @@ mod tests { let expected1 = vec![ mock::Event::Input(0), - mock::Event::Finish, + mock::Event::FinishEpoch(0), mock::Event::Input(1), mock::Event::Input(2), mock::Event::Input(3), @@ -348,14 +348,14 @@ mod tests { mock::Event::Input(7), mock::Event::Input(8), mock::Event::Input(9), - mock::Event::Finish, + mock::Event::FinishEpoch(1), mock::Event::Input(10), mock::Event::Input(11), mock::Event::Input(12), mock::Event::Input(13), mock::Event::Input(14), mock::Event::Input(15), - mock::Event::Finish, + mock::Event::FinishEpoch(2), ]; let (context, broker, input_box) = test_react( diff --git a/offchain/dispatcher/src/drivers/mock.rs b/offchain/dispatcher/src/drivers/mock.rs index 1c39a6a40..951eb743e 100644 --- a/offchain/dispatcher/src/drivers/mock.rs +++ b/offchain/dispatcher/src/drivers/mock.rs @@ -12,7 +12,10 @@ use snafu::whatever; use std::{ collections::VecDeque, ops::{Deref, DerefMut}, - sync::{Arc, Mutex}, + sync::{ + atomic::{AtomicU16, Ordering}, + Arc, Mutex, + }, }; use types::foldables::{DAppInputBox, Input, InputBox}; @@ -78,8 +81,8 @@ pub fn update_input_box( #[derive(Debug, Clone, Copy, PartialEq)] pub enum Event { - Input(u64), // input index - Finish, + Input(u64), // input index + FinishEpoch(u64), // index of the "external" epoch } #[derive(Debug)] @@ -87,6 +90,8 @@ pub struct Broker { pub rollup_statuses: Mutex>, pub next_claims: Mutex>, pub events: Mutex>, + + finish_epochs: AtomicU16, status_error: bool, enqueue_input_error: bool, finish_epoch_error: bool, @@ -98,6 +103,7 @@ impl Broker { rollup_statuses: Mutex::new(VecDeque::new()), next_claims: Mutex::new(VecDeque::new()), events: Mutex::new(Vec::new()), + finish_epochs: AtomicU16::new(0), status_error: false, enqueue_input_error: false, finish_epoch_error: false, @@ -186,7 +192,12 @@ impl BrokerSend for Broker { whatever!("finish_epoch error") } else { let mut mutex_guard = self.events.lock().unwrap(); - mutex_guard.deref_mut().push(Event::Finish); + let current_epoch = self.finish_epochs.load(Ordering::SeqCst); + mutex_guard + .deref_mut() + .push(Event::FinishEpoch(current_epoch.into())); + self.finish_epochs + .store(current_epoch + 1, Ordering::SeqCst); Ok(()) } } diff --git a/offchain/dispatcher/src/machine/rollups_broker.rs b/offchain/dispatcher/src/machine/rollups_broker.rs index 4ba712bfa..a2f729766 100644 --- a/offchain/dispatcher/src/machine/rollups_broker.rs +++ b/offchain/dispatcher/src/machine/rollups_broker.rs @@ -69,7 +69,24 @@ impl BrokerFacade { broker: &mut sync::MutexGuard<'_, Broker>, ) -> Result { let event = self.peek(broker).await?; - Ok(event.into()) + + let old_epoch_index = event + .clone() + .map(|event| event.payload.epoch_index) + .unwrap_or(0); + + // The epoch gets incremented inside this ".into()"! + // Check From> for BrokerStreamStatus + let status: BrokerStreamStatus = event.into(); + + // Asserting that the epoch_index is continuous. + let new_epoch_index = status.epoch_number; + assert!( + new_epoch_index == old_epoch_index + || new_epoch_index == old_epoch_index + 1 + ); + + Ok(status) } #[tracing::instrument(level = "trace", skip_all)] @@ -209,7 +226,7 @@ impl From> for BrokerStreamStatus { RollupsData::FinishEpoch { .. } => Self { id, - epoch_number: epoch_index + 1, + epoch_number: epoch_index + 1, // Epoch number being incremented! status: payload.into(), }, } diff --git a/offchain/dispatcher/src/setup.rs b/offchain/dispatcher/src/setup.rs index b923b5969..c95d02bc0 100644 --- a/offchain/dispatcher/src/setup.rs +++ b/offchain/dispatcher/src/setup.rs @@ -80,15 +80,14 @@ pub async fn create_context( dapp_metadata: DAppMetadata, metrics: DispatcherMetrics, ) -> Result { - let dapp_deployment_block_number = - U64::from(config.blockchain_config.dapp_deployment_block_number); + let input_box_deployment_block_number = + U64::from(config.blockchain_config.input_box_deployment_block_number); let genesis_block = block_server - .query_block(dapp_deployment_block_number) + .query_block(input_box_deployment_block_number) .await .context(StateServerSnafu)? .number .as_u64(); - let epoch_length = config.epoch_length_in_blocks; let status = broker.status().await.context(BrokerSnafu)?; @@ -96,8 +95,12 @@ pub async fn create_context( // Hence, we make sure that the broker is in a clean state before starting. ensure!(status.inputs_sent_count == 0, DirtyBrokerSnafu); - let context = - Context::new(genesis_block, epoch_length, dapp_metadata, metrics); + let context = Context::new( + genesis_block, + config.epoch_length, + dapp_metadata, + metrics, + ); Ok(context) } diff --git a/offchain/types/src/blockchain_config.rs b/offchain/types/src/blockchain_config.rs index c38c02cd1..e6250a0a3 100644 --- a/offchain/types/src/blockchain_config.rs +++ b/offchain/types/src/blockchain_config.rs @@ -35,9 +35,9 @@ pub struct BlockchainCLIConfig { #[arg(long, env)] pub dapp_address: Option, - /// DApp deployment block number + /// Input box deployment block number #[arg(long, env)] - pub dapp_deployment_block_number: Option, + pub input_box_deployment_block_number: Option, /// History contract address #[arg(long, env)] @@ -63,7 +63,7 @@ pub struct BlockchainCLIConfig { #[derive(Clone, Debug)] pub struct BlockchainConfig { pub dapp_address: Address, - pub dapp_deployment_block_number: u64, + pub input_box_deployment_block_number: u64, pub history_address: Address, pub authority_address: Address, pub input_box_address: Address, @@ -96,7 +96,8 @@ impl TryFrom for BlockchainConfig { // try to get the values from the environment values let mut dapp_address = cli.dapp_address.map(deserialize::
).transpose()?; - let mut dapp_deployment_block_number = cli.dapp_deployment_block_number; + let input_box_deployment_block_number = + cli.input_box_deployment_block_number; let mut history_address = cli .history_address .map(deserialize::
) @@ -115,8 +116,6 @@ impl TryFrom for BlockchainConfig { cli.dapp_deployment_file.map(read::).transpose()? { dapp_address = dapp_address.or(file.address); - dapp_deployment_block_number = - dapp_deployment_block_number.or(file.block_number); } if let Some(file) = cli .rollups_deployment_file @@ -142,8 +141,8 @@ impl TryFrom for BlockchainConfig { Ok(BlockchainConfig { dapp_address: check_missing!(dapp_address), - dapp_deployment_block_number: check_missing!( - dapp_deployment_block_number + input_box_deployment_block_number: check_missing!( + input_box_deployment_block_number ), history_address: check_missing!(history_address), authority_address: check_missing!(authority_address), diff --git a/setup_env.sh b/setup_env.sh index 7cb6c4816..d25f14fcc 100644 --- a/setup_env.sh +++ b/setup_env.sh @@ -11,7 +11,6 @@ export CARTESI_BLOCKCHAIN_IS_LEGACY="false" export CARTESI_BLOCKCHAIN_FINALITY_OFFSET="1" export CARTESI_BLOCKCHAIN_BLOCK_TIMEOUT="60" export CARTESI_CONTRACTS_APPLICATION_ADDRESS="0x7C54E3f7A8070a54223469965A871fB8f6f88c22" -export CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER="20" export CARTESI_CONTRACTS_HISTORY_ADDRESS="0x325272217ae6815b494bF38cED004c5Eb8a7CdA7" export CARTESI_CONTRACTS_AUTHORITY_ADDRESS="0x58c93F83fb3304730C95aad2E360cdb88b782010" export CARTESI_CONTRACTS_INPUT_BOX_ADDRESS="0x59b22D57D4f067708AB0c00552767405926dc768" diff --git a/test/config.go b/test/config.go index 0ea7facdc..5752def7a 100644 --- a/test/config.go +++ b/test/config.go @@ -20,7 +20,7 @@ const ( LocalHttpPort = 10000 LocalBlockTimeout = 120 LocalFinalityOffset = 1 - LocalEpochLengthInBlocks = 5 + LocalEpochLength = 5 ) func NewLocalNodeConfig(localPostgresEnpoint string, localBlockchainHttpEndpoint string, @@ -39,7 +39,7 @@ func NewLocalNodeConfig(localPostgresEnpoint string, localBlockchainHttpEndpoint config.Redacted[string]{Value: localPostgresEnpoint} //Epoch - nodeConfig.RollupsEpochLengthInBlocks = LocalEpochLengthInBlocks + nodeConfig.RollupsEpochLength = LocalEpochLength //Blochain nodeConfig.BlockchainID = LocalBlockchainID