Skip to content

Commit

Permalink
feat(consensus): add TimeoutsConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware committed Aug 21, 2024
1 parent 841af2f commit 8299959
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
15 changes: 15 additions & 0 deletions config/papyrus/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@
"privacy": "Public",
"value": "consensus_test_sync"
},
"consensus.timeouts.precommit_timeout": {
"description": "The timeout (seconds) for a precommit.",
"privacy": "Public",
"value": 1.0
},
"consensus.timeouts.prevote_timeout": {
"description": "The timeout (seconds) for a prevote.",
"privacy": "Public",
"value": 1.0
},
"consensus.timeouts.proposal_timeout": {
"description": "The timeout (seconds) for a proposal.",
"privacy": "Public",
"value": 3.0
},
"consensus.validator_id": {
"description": "A required param! The validator id of the node.",
"param_type": "String",
Expand Down
9 changes: 9 additions & 0 deletions crates/papyrus_config/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ where
Ok(Duration::from_secs(secs))
}

/// Deserializes float seconds to duration object.
pub fn deserialize_float_seconds_to_duration<'de, D>(de: D) -> Result<Duration, D::Error>
where
D: Deserializer<'de>,
{
let secs: f64 = Deserialize::deserialize(de)?;
Ok(Duration::from_secs_f64(secs))
}

/// Serializes a map to "k1:v1 k2:v2" string structure.
pub fn serialize_optional_map(optional_map: &Option<HashMap<String, String>>) -> String {
match optional_map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,27 @@ expression: dumped_default_config
"value": "consensus_test_sync",
"privacy": "Public"
},
"consensus.timeouts.precommit_timeout": {
"description": "The timeout (seconds) for a precommit.",
"value": {
"$serde_json::private::Number": "1.0"
},
"privacy": "Public"
},
"consensus.timeouts.prevote_timeout": {
"description": "The timeout (seconds) for a prevote.",
"value": {
"$serde_json::private::Number": "1.0"
},
"privacy": "Public"
},
"consensus.timeouts.proposal_timeout": {
"description": "The timeout (seconds) for a proposal.",
"value": {
"$serde_json::private::Number": "3.0"
},
"privacy": "Public"
},
"consensus.validator_id": {
"description": "A required param! The validator id of the node.",
"param_type": "String",
Expand Down
33 changes: 33 additions & 0 deletions crates/sequencing/papyrus_consensus/run_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def build_node(data_dir, logs_dir, i, papryus_args):
f"--rpc.server_address 127.0.0.1:{find_free_port()} "
f"--monitoring_gateway.server_address 127.0.0.1:{monitoring_gateway_server_port} "
f"--consensus.test.#is_none false "
f"--consensus.timeouts.proposal_timeout {papryus_args.proposal_timeout} "
f"--consensus.timeouts.prevote_timeout {papryus_args.prevote_timeout} "
f"--consensus.timeouts.precommit_timeout {papryus_args.precommit_timeout} "
f"--consensus.test.cache_size {papryus_args.cache_size} "
f"--consensus.test.random_seed {papryus_args.random_seed} "
f"--consensus.test.drop_probability {papryus_args.drop_probability} "
Expand Down Expand Up @@ -190,6 +193,9 @@ def __init__(
base_layer_node_url,
num_validators,
db_dir,
proposal_timeout,
prevote_timeout,
precommit_timeout,
cache_size,
random_seed,
drop_probability,
Expand All @@ -198,6 +204,9 @@ def __init__(
self.base_layer_node_url = base_layer_node_url
self.num_validators = num_validators
self.db_dir = db_dir
self.proposal_timeout = proposal_timeout
self.prevote_timeout = prevote_timeout
self.precommit_timeout = precommit_timeout
self.cache_size = cache_size
self.random_seed = random_seed
self.drop_probability = drop_probability
Expand Down Expand Up @@ -265,6 +274,27 @@ def main(papyrus_args, run_consensus_args):
help="Time in seconds to check for height stagnation.",
)
parser.add_argument("--duration", type=int, required=False, default=None)
parser.add_argument(
"--proposal_timeout",
type=float,
required=False,
default=3,
help="The timeout (seconds) for a proposal.",
)
parser.add_argument(
"--prevote_timeout",
type=float,
required=False,
default=1,
help="The timeout (seconds) for a prevote.",
)
parser.add_argument(
"--precommit_timeout",
type=float,
required=False,
default=1,
help="The timeout (seconds) for a precommit.",
)
parser.add_argument(
"--cache_size",
type=int,
Expand Down Expand Up @@ -299,6 +329,9 @@ def main(papyrus_args, run_consensus_args):
base_layer_node_url=args.base_layer_node_url,
num_validators=args.num_validators,
db_dir=args.db_dir,
proposal_timeout=args.proposal_timeout,
prevote_timeout=args.prevote_timeout,
precommit_timeout=args.precommit_timeout,
cache_size=args.cache_size,
random_seed=args.random_seed,
drop_probability=args.drop_probability,
Expand Down
59 changes: 58 additions & 1 deletion crates/sequencing/papyrus_consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
use std::collections::BTreeMap;
use std::time::Duration;

use papyrus_config::converters::deserialize_seconds_to_duration;
use papyrus_config::converters::{
deserialize_float_seconds_to_duration,
deserialize_seconds_to_duration,
};
use papyrus_config::dumping::{
append_sub_config_name,
ser_optional_sub_config,
ser_param,
ser_required_param,
Expand All @@ -33,6 +37,8 @@ pub struct ConsensusConfig {
/// The delay (seconds) before starting consensus to give time for network peering.
#[serde(deserialize_with = "deserialize_seconds_to_duration")]
pub consensus_delay: Duration,
/// Timeouts configuration for consensus.
pub timeouts: TimeoutsConfig,
/// Test configuration for consensus.
pub test: Option<ConsensusTestConfig>,
}
Expand Down Expand Up @@ -71,6 +77,7 @@ impl SerializeConfig for ConsensusConfig {
ParamPrivacyInput::Public,
),
]);
config.extend(append_sub_config_name(self.timeouts.dump(), "timeouts"));
config.extend(ser_optional_sub_config(&self.test, "test"));
config
}
Expand All @@ -84,6 +91,7 @@ impl Default for ConsensusConfig {
start_height: BlockNumber::default(),
num_validators: 4,
consensus_delay: Duration::from_secs(5),
timeouts: TimeoutsConfig::default(),
test: None,
}
}
Expand Down Expand Up @@ -152,3 +160,52 @@ impl Default for ConsensusTestConfig {
}
}
}

/// Configuration for consensus timeouts.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct TimeoutsConfig {
/// The timeout for a proposal.
#[serde(deserialize_with = "deserialize_float_seconds_to_duration")]
pub proposal_timeout: Duration,
/// The timeout for a prevote.
#[serde(deserialize_with = "deserialize_float_seconds_to_duration")]
pub prevote_timeout: Duration,
/// The timeout for a precommit.
#[serde(deserialize_with = "deserialize_float_seconds_to_duration")]
pub precommit_timeout: Duration,
}

impl SerializeConfig for TimeoutsConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::from_iter([
ser_param(
"proposal_timeout",
&self.proposal_timeout.as_secs_f64(),
"The timeout (seconds) for a proposal.",
ParamPrivacyInput::Public,
),
ser_param(
"prevote_timeout",
&self.prevote_timeout.as_secs_f64(),
"The timeout (seconds) for a prevote.",
ParamPrivacyInput::Public,
),
ser_param(
"precommit_timeout",
&self.precommit_timeout.as_secs_f64(),
"The timeout (seconds) for a precommit.",
ParamPrivacyInput::Public,
),
])
}
}

impl Default for TimeoutsConfig {
fn default() -> Self {
Self {
proposal_timeout: Duration::from_secs_f64(3.0),
prevote_timeout: Duration::from_secs_f64(1.0),
precommit_timeout: Duration::from_secs_f64(1.0),
}
}
}

0 comments on commit 8299959

Please sign in to comment.