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 16, 2025
1 parent f1cfc9e commit dc0dd26
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
20 changes: 20 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,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 @@ -39,6 +39,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;
Expand Down Expand Up @@ -66,6 +67,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: ContextConfig,
// TODO(shahak): Make network non-optional once it's developed enough.
pub network: Option<NetworkConfig>,
Expand All @@ -85,6 +87,7 @@ impl Default for NodeConfig {
sync: Some(SyncConfig::default()),
p2p_sync: None,
consensus: None,
papyrus_consensus: None,
context: ContextConfig::default(),
network: None,
collect_profiling_metrics: false,
Expand All @@ -103,6 +106,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"),
append_sub_config_name(self.context.dump(), "context"),
ser_optional_sub_config(&self.network, "network"),
BTreeMap::from_iter([ser_param(
Expand Down Expand Up @@ -134,3 +138,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 @@ -342,6 +342,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
20 changes: 14 additions & 6 deletions crates/papyrus_node/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -181,25 +181,32 @@ fn spawn_monitoring_server(

fn spawn_consensus(
consensus_config: Option<&ConsensusConfig>,
papyrus_consensus_config: Option<&PapyrusConsensusConfig>,
context_config: ContextConfig,
storage_reader: StorageReader,
network_manager: Option<&mut NetworkManager>,
) -> anyhow::Result<JoinHandle<anyhow::Result<()>>> {
let (Some(consensus_config), Some(network_manager)) = (consensus_config, network_manager)
let (Some(consensus_config), Some(papyrus_consensus_config), Some(network_manager)) =
(consensus_config, papyrus_consensus_config, network_manager)
else {
info!("Consensus is disabled.");
return Ok(tokio::spawn(future::pending()));
};
let consensus_config = consensus_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<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 @@ -220,9 +227,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(),
Expand Down Expand Up @@ -362,6 +369,7 @@ async fn run_threads(
} else {
spawn_consensus(
config.consensus.as_ref(),
config.papyrus_consensus.as_ref(),
config.context.clone(),
resources.storage_reader.clone(),
resources.maybe_network_manager.as_mut(),
Expand Down

0 comments on commit dc0dd26

Please sign in to comment.