Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Commit a1bd31d

Browse files
chore(config): add num_of_validators to ConsensusConfig (#2194)
1 parent 37f82b4 commit a1bd31d

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

config/default_config.json

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
"privacy": "TemporaryValue",
8080
"value": true
8181
},
82+
"consensus.num_validators": {
83+
"description": "The number of validators in the consensus.",
84+
"privacy": "Public",
85+
"value": 4
86+
},
8287
"consensus.start_height": {
8388
"description": "The height to start the consensus from.",
8489
"privacy": "Public",

crates/papyrus_node/src/config/snapshots/papyrus_node__config__config_test__dump_default_config.snap

+7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ expression: dumped_default_config
8989
"value": true,
9090
"privacy": "TemporaryValue"
9191
},
92+
"consensus.num_validators": {
93+
"description": "The number of validators in the consensus.",
94+
"value": {
95+
"$serde_json::private::Number": "4"
96+
},
97+
"privacy": "Public"
98+
},
9299
"consensus.start_height": {
93100
"description": "The height to start the consensus from.",
94101
"value": {

crates/papyrus_node/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ fn run_consensus(
120120
let context = PapyrusConsensusContext::new(
121121
storage_reader.clone(),
122122
consensus_channels.messages_to_broadcast_sender,
123+
config.num_validators,
123124
);
124125
let start_height = config.start_height;
125126

crates/sequencing/papyrus_consensus/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ versions. Breaking changes are expected to happen in the near future.
99
## How to run
1010
1. You must turn consensus on and provide a validator ID by passing: `--consensus.#is_none false --consensus.validator_id 0x<UNIQUE>`
1111
2. Start by running any nodes which are validators for `consensus.start_height` which is by default 0 to avoid them missing the proposal.
12-
1. You can change the default topic by adding: `--consensus.topic "TOPIC"`
12+
1. You can change the default number of validators by passing: `--consensus.num_validators <NUM_VALIDATORS>`
13+
2. You can change the default topic by passing: `--consensus.topic "TOPIC"`
1314

1415
#### Bootstrap Node
1516
This must be run first:
@@ -19,11 +20,11 @@ cargo run --package papyrus_node --bin papyrus_node -- --base_layer.node_url <ET
1920
- This will log `local_peer_id` which is used by other nodes. (Alternatively pass `network.secret_key` to have a fixed peer id).
2021

2122
#### Other Nodes
23+
Run each of the other nodes separately, using different `consensus.validator_id` {`0x2`, `0x3`, `0x0`}:
2224

2325
```
2426
cargo run --package papyrus_node --bin papyrus_node -- --base_layer.node_url <ETH_NODE_URL> --network.#is_none false --consensus.#is_none false --consensus.validator_id 0x<UNIQUE> --network.tcp_port <UNIQUE> --network.bootstrap_peer_multiaddr.#is_none false --rpc.server_address 127.0.0.1:<UNIQUE> --monitoring_gateway.server_address 127.0.0.1:<UNIQUE> --storage.db_config.path_prefix <UNIQUE> --network.bootstrap_peer_multiaddr /ip4/127.0.0.1/tcp/10000/p2p/<BOOT_NODE_PEER_ID>
2527
```
26-
- The validator set, (0, 1, 2, 3), is hardcoded in milestone 2.
2728
- Node 0 is the first proposer and should be run last.
2829

2930
UNIQUE - a value unique among all nodes running locally.

crates/sequencing/papyrus_consensus/src/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub struct ConsensusConfig {
2020
pub topic: String,
2121
/// The height to start the consensus from.
2222
pub start_height: BlockNumber,
23+
/// The number of validators in the consensus.
24+
// Used for testing in an early milestones.
25+
pub num_validators: u64,
2326
}
2427

2528
impl SerializeConfig for ConsensusConfig {
@@ -43,6 +46,12 @@ impl SerializeConfig for ConsensusConfig {
4346
"The height to start the consensus from.",
4447
ParamPrivacyInput::Public,
4548
),
49+
ser_param(
50+
"num_validators",
51+
&self.num_validators,
52+
"The number of validators in the consensus.",
53+
ParamPrivacyInput::Public,
54+
),
4655
])
4756
}
4857
}
@@ -53,6 +62,7 @@ impl Default for ConsensusConfig {
5362
validator_id: ValidatorId::default(),
5463
topic: "consensus".to_string(),
5564
start_height: BlockNumber::default(),
65+
num_validators: 4,
5666
}
5767
}
5868
}

crates/sequencing/papyrus_consensus/src/papyrus_consensus_context.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use papyrus_storage::body::BodyStorageReader;
1616
use papyrus_storage::header::HeaderStorageReader;
1717
use papyrus_storage::{StorageError, StorageReader};
1818
use starknet_api::block::{BlockHash, BlockNumber};
19+
use starknet_api::core::ContractAddress;
1920
use starknet_api::transaction::Transaction;
2021
use tokio::sync::Mutex;
2122
use tracing::debug;
@@ -47,6 +48,7 @@ impl ConsensusBlock for PapyrusConsensusBlock {
4748
pub struct PapyrusConsensusContext {
4849
storage_reader: StorageReader,
4950
broadcast_sender: Arc<Mutex<SubscriberSender<ConsensusMessage>>>,
51+
validators: Vec<ValidatorId>,
5052
}
5153

5254
impl PapyrusConsensusContext {
@@ -55,8 +57,13 @@ impl PapyrusConsensusContext {
5557
pub fn new(
5658
storage_reader: StorageReader,
5759
broadcast_sender: SubscriberSender<ConsensusMessage>,
60+
num_validators: u64,
5861
) -> Self {
59-
Self { storage_reader, broadcast_sender: Arc::new(Mutex::new(broadcast_sender)) }
62+
Self {
63+
storage_reader,
64+
broadcast_sender: Arc::new(Mutex::new(broadcast_sender)),
65+
validators: (0..num_validators).map(ContractAddress::from).collect(),
66+
}
6067
}
6168
}
6269

@@ -158,11 +165,11 @@ impl ConsensusContext for PapyrusConsensusContext {
158165
}
159166

160167
async fn validators(&self, _height: BlockNumber) -> Vec<ValidatorId> {
161-
vec![0u8.into(), 1u8.into(), 2u8.into(), 3u8.into()]
168+
self.validators.clone()
162169
}
163170

164-
fn proposer(&self, _validators: &[ValidatorId], height: BlockNumber) -> ValidatorId {
165-
(height.0 % 2).into()
171+
fn proposer(&self, _validators: &[ValidatorId], _height: BlockNumber) -> ValidatorId {
172+
*self.validators.first().expect("validators should have at least 2 validators")
166173
}
167174

168175
async fn broadcast(&self, message: ConsensusMessage) -> Result<(), ConsensusError> {

crates/sequencing/papyrus_consensus/src/papyrus_consensus_context_test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn test_setup() -> (Block, PapyrusConsensusContext, BroadcastNetworkMock<Consens
115115
let papyrus_context = PapyrusConsensusContext::new(
116116
storage_reader.clone(),
117117
test_channels.subscriber_channels.messages_to_broadcast_sender,
118+
4,
118119
);
119120
(block, papyrus_context, test_channels.mock_network)
120121
}

0 commit comments

Comments
 (0)