Skip to content

Commit

Permalink
chore(config): add short consensus delay to prevent InsufficientPeers…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
asmaastarkware committed Jul 23, 2024
1 parent faa2d2d commit 647e080
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"privacy": "TemporaryValue",
"value": true
},
"consensus.consensus_delay": {
"description": "Delay (seconds) before starting consensus to give time for network peering.",
"privacy": "Public",
"value": 5
},
"consensus.num_validators": {
"description": "The number of validators in the consensus.",
"privacy": "Public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ expression: dumped_default_config
"value": true,
"privacy": "TemporaryValue"
},
"consensus.consensus_delay": {
"description": "Delay (seconds) before starting consensus to give time for network peering.",
"value": {
"$serde_json::private::Number": "5"
},
"privacy": "Public"
},
"consensus.num_validators": {
"description": "The number of validators in the consensus.",
"value": {
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ fn run_consensus(
context,
start_height,
validator_id,
config.consensus_delay,
consensus_channels.broadcasted_messages_receiver,
)))
}
Expand Down
12 changes: 12 additions & 0 deletions crates/sequencing/papyrus_consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
//! such as the validator ID, the network topic of the consensus, and the starting block height.
use std::collections::BTreeMap;
use std::time::Duration;

use papyrus_config::converters::deserialize_seconds_to_duration;
use papyrus_config::dumping::{ser_param, ser_required_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializationType, SerializedParam};
use serde::{Deserialize, Serialize};
Expand All @@ -23,6 +25,9 @@ pub struct ConsensusConfig {
/// The number of validators in the consensus.
// Used for testing in an early milestones.
pub num_validators: u64,
/// The delay (seconds) before starting consensus to give time for network peering.
#[serde(deserialize_with = "deserialize_seconds_to_duration")]
pub consensus_delay: Duration,
}

impl SerializeConfig for ConsensusConfig {
Expand Down Expand Up @@ -52,6 +57,12 @@ impl SerializeConfig for ConsensusConfig {
"The number of validators in the consensus.",
ParamPrivacyInput::Public,
),
ser_param(
"consensus_delay",
&self.consensus_delay.as_secs(),
"Delay (seconds) before starting consensus to give time for network peering.",
ParamPrivacyInput::Public,
),
])
}
}
Expand All @@ -63,6 +74,7 @@ impl Default for ConsensusConfig {
topic: "consensus".to_string(),
start_height: BlockNumber::default(),
num_validators: 4,
consensus_delay: Duration::from_secs(5),
}
}
}
5 changes: 5 additions & 0 deletions crates/sequencing/papyrus_consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// TODO(Matan): fix #[allow(missing_docs)].
//! A consensus implementation for a [`Starknet`](https://www.starknet.io/) node.
use std::time::Duration;

use futures::channel::{mpsc, oneshot};
use papyrus_common::metrics as papyrus_metrics;
use papyrus_network::network_manager::BroadcastSubscriberReceiver;
Expand Down Expand Up @@ -106,12 +108,15 @@ pub async fn run_consensus<BlockT: ConsensusBlock, ContextT: ConsensusContext<Bl
context: ContextT,
start_height: BlockNumber,
validator_id: ValidatorId,
consensus_delay: Duration,
mut network_receiver: BroadcastSubscriberReceiver<ConsensusMessage>,
) -> Result<(), ConsensusError>
where
ProposalWrapper:
Into<(ProposalInit, mpsc::Receiver<BlockT::ProposalChunk>, oneshot::Receiver<BlockHash>)>,
{
// Add a short delay to allow peers to connect and avoid "InsufficientPeers" error
tokio::time::sleep(consensus_delay).await;
let mut current_height = start_height;
let mut future_messages = Vec::new();
loop {
Expand Down

0 comments on commit 647e080

Please sign in to comment.