Skip to content

Commit

Permalink
chore(consensus): add PapyrusConsensusConfig struct
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-starkware committed Jan 23, 2025
1 parent b7e9378 commit 9902c7b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
20 changes: 20 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 46 additions & 0 deletions crates/papyrus_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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 starknet_consensus::config::ConsensusConfig;
Expand Down Expand Up @@ -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<P2pSyncClientConfig>,
pub consensus: Option<ConsensusConfig>,
pub papyrus_consensus: Option<PapyrusConsensusConfig>,
pub context: Option<ContextConfig>,
// TODO(shahak): Make network non-optional once it's developed enough.
pub network: Option<NetworkConfig>,
Expand All @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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<ParamPath, SerializedParam> {
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(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 18 additions & 7 deletions crates/papyrus_node/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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.
Expand Down Expand Up @@ -186,12 +186,17 @@ 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<JoinHandle<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()));
Expand All @@ -200,14 +205,19 @@ fn spawn_consensus(
let context_config = context_config.clone();
debug!("Consensus configuration: {consensus_config:?}");
debug!("Context configuration: {context_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<ProposalPart, HeightAndRound>,
> = 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,
Expand All @@ -228,9 +238,9 @@ fn spawn_consensus(
Ok(tokio::spawn(async move {
Ok(starknet_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(),
Expand Down Expand Up @@ -375,6 +385,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(),
Expand Down

0 comments on commit 9902c7b

Please sign in to comment.