From 28f6ed152cc33073d1c191faea0e744565a4ef73 Mon Sep 17 00:00:00 2001 From: Guy Nir Date: Thu, 16 Jan 2025 13:43:20 +0200 Subject: [PATCH] chore(consensus): add PapyrusConsensusConfig struct --- config/papyrus/default_config.json | 20 ++++++++ crates/papyrus_node/src/config/mod.rs | 46 +++++++++++++++++++ ...fig__config_test__dump_default_config.snap | 22 +++++++++ crates/papyrus_node/src/run.rs | 21 ++++++--- 4 files changed, 102 insertions(+), 7 deletions(-) diff --git a/config/papyrus/default_config.json b/config/papyrus/default_config.json index 4990a3e6029..228e39782c6 100644 --- a/config/papyrus/default_config.json +++ b/config/papyrus/default_config.json @@ -289,6 +289,26 @@ "privacy": "Public", "value": 50 }, + "papyrus_consensus.#is_none": { + "description": "Flag for an optional field.", + "privacy": "TemporaryValue", + "value": true + }, + "papyrus_consensus.proposals_topic": { + "description": "The topic for consensus proposals.", + "privacy": "Public", + "value": "consensus_proposals" + }, + "papyrus_consensus.start_height": { + "description": "The height to start the consensus from.", + "privacy": "Public", + "value": 0 + }, + "papyrus_consensus.votes_topic": { + "description": "The topic for consensus votes.", + "privacy": "Public", + "value": "consensus_votes" + }, "rpc.chain_id": { "description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.", "pointer_target": "chain_id", diff --git a/crates/papyrus_node/src/config/mod.rs b/crates/papyrus_node/src/config/mod.rs index 735ca22088e..2cee8c0e55e 100644 --- a/crates/papyrus_node/src/config/mod.rs +++ b/crates/papyrus_node/src/config/mod.rs @@ -38,6 +38,7 @@ use papyrus_sync::sources::central::CentralSourceConfig; use papyrus_sync::SyncConfig; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; +use starknet_api::block::BlockNumber; use starknet_api::core::ChainId; use starknet_client::RetryConfig; use validator::Validate; @@ -65,6 +66,7 @@ pub struct NodeConfig { // TODO(yair): Change NodeConfig to have an option of enum of SyncConfig or P2pSyncConfig. pub p2p_sync: Option, pub consensus: Option, + pub papyrus_consensus: Option, pub context: Option, // TODO(shahak): Make network non-optional once it's developed enough. pub network: Option, @@ -84,6 +86,7 @@ impl Default for NodeConfig { sync: Some(SyncConfig::default()), p2p_sync: None, consensus: None, + papyrus_consensus: None, context: None, network: None, collect_profiling_metrics: false, @@ -102,6 +105,7 @@ impl SerializeConfig for NodeConfig { ser_optional_sub_config(&self.sync, "sync"), ser_optional_sub_config(&self.p2p_sync, "p2p_sync"), ser_optional_sub_config(&self.consensus, "consensus"), + ser_optional_sub_config(&self.papyrus_consensus, "papyrus_consensus"), ser_optional_sub_config(&self.context, "context"), ser_optional_sub_config(&self.network, "network"), BTreeMap::from_iter([ser_param( @@ -133,3 +137,45 @@ pub fn node_command() -> Command { .version(VERSION_FULL) .about("Papyrus is a StarkNet full node written in Rust.") } + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Validate)] +pub struct PapyrusConsensusConfig { + pub start_height: BlockNumber, + pub proposals_topic: String, + pub votes_topic: String, +} + +impl SerializeConfig for PapyrusConsensusConfig { + fn dump(&self) -> BTreeMap { + BTreeMap::from_iter([ + ser_param( + "start_height", + &self.start_height, + "The height to start the consensus from.", + ParamPrivacyInput::Public, + ), + ser_param( + "proposals_topic", + &self.proposals_topic, + "The topic for consensus proposals.", + ParamPrivacyInput::Public, + ), + ser_param( + "votes_topic", + &self.votes_topic, + "The topic for consensus votes.", + ParamPrivacyInput::Public, + ), + ]) + } +} + +impl Default for PapyrusConsensusConfig { + fn default() -> Self { + Self { + start_height: BlockNumber::default(), + proposals_topic: "consensus_proposals".to_string(), + votes_topic: "consensus_votes".to_string(), + } + } +} diff --git a/crates/papyrus_node/src/config/snapshots/papyrus_node__config__config_test__dump_default_config.snap b/crates/papyrus_node/src/config/snapshots/papyrus_node__config__config_test__dump_default_config.snap index 08708fe21ac..0aa9b1d4fb1 100644 --- a/crates/papyrus_node/src/config/snapshots/papyrus_node__config__config_test__dump_default_config.snap +++ b/crates/papyrus_node/src/config/snapshots/papyrus_node__config__config_test__dump_default_config.snap @@ -347,6 +347,28 @@ expression: dumped_default_config }, "privacy": "Public" }, + "papyrus_consensus.#is_none": { + "description": "Flag for an optional field.", + "value": true, + "privacy": "TemporaryValue" + }, + "papyrus_consensus.proposals_topic": { + "description": "The topic for consensus proposals.", + "value": "consensus_proposals", + "privacy": "Public" + }, + "papyrus_consensus.start_height": { + "description": "The height to start the consensus from.", + "value": { + "$serde_json::private::Number": "0" + }, + "privacy": "Public" + }, + "papyrus_consensus.votes_topic": { + "description": "The topic for consensus votes.", + "value": "consensus_votes", + "privacy": "Public" + }, "rpc.chain_id": { "description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.", "value": "SN_MAIN", diff --git a/crates/papyrus_node/src/run.rs b/crates/papyrus_node/src/run.rs index 4674939b2bc..a163859c42b 100644 --- a/crates/papyrus_node/src/run.rs +++ b/crates/papyrus_node/src/run.rs @@ -44,7 +44,7 @@ use tracing::{debug, debug_span, error, info, warn, Instrument}; use tracing_subscriber::prelude::*; use tracing_subscriber::{fmt, EnvFilter}; -use crate::config::NodeConfig; +use crate::config::{NodeConfig, PapyrusConsensusConfig}; use crate::version::VERSION_FULL; // TODO(yair): Add to config. @@ -181,12 +181,13 @@ fn spawn_monitoring_server( fn spawn_consensus( consensus_config: Option<&ConsensusConfig>, + papyrus_consensus_config: Option<&PapyrusConsensusConfig>, context_config: Option<&ContextConfig>, storage_reader: StorageReader, network_manager: Option<&mut NetworkManager>, ) -> anyhow::Result>> { - let (Some(consensus_config), Some(context_config), Some(network_manager)) = - (consensus_config, context_config, network_manager) + let (Some(consensus_config), Some(papyrus_consensus_config), Some(context_config), Some(network_manager)) = + (consensus_config, papyrus_consensus_config, context_config, network_manager) else { info!("Consensus is disabled."); return Ok(tokio::spawn(future::pending())); @@ -194,14 +195,19 @@ fn spawn_consensus( let consensus_config = consensus_config.clone(); let context_config = context_config.clone(); debug!("Consensus configuration: {consensus_config:?}"); + let papyrus_consensus_config = papyrus_consensus_config.clone(); + debug!("Papyrus consensus configuration: {papyrus_consensus_config:?}"); let network_channels = network_manager.register_broadcast_topic( - Topic::new(consensus_config.network_topic.clone()), + Topic::new(papyrus_consensus_config.votes_topic.clone()), BUFFER_SIZE, )?; let proposal_network_channels: BroadcastTopicChannels< StreamMessage, - > = network_manager.register_broadcast_topic(Topic::new(NETWORK_TOPIC), BUFFER_SIZE)?; + > = network_manager.register_broadcast_topic( + Topic::new(papyrus_consensus_config.proposals_topic.clone()), + BUFFER_SIZE, + )?; let BroadcastTopicChannels { broadcasted_messages_receiver: inbound_network_receiver, broadcast_topic_client: outbound_network_sender, @@ -222,9 +228,9 @@ fn spawn_consensus( Ok(tokio::spawn(async move { Ok(papyrus_consensus::run_consensus( context, - consensus_config.start_height, + papyrus_consensus_config.start_height, // TODO(Asmaa): replace with the correct value. - consensus_config.start_height, + papyrus_consensus_config.start_height, consensus_config.validator_id, consensus_config.consensus_delay, consensus_config.timeouts.clone(), @@ -364,6 +370,7 @@ async fn run_threads( } else { spawn_consensus( config.consensus.as_ref(), + config.papyrus_consensus.as_ref(), config.context.as_ref(), resources.storage_reader.clone(), resources.maybe_network_manager.as_mut(),