Skip to content

Commit

Permalink
chore(config): add num_of_validators to ConsensusConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware committed Jul 8, 2024
1 parent d05fefc commit 6f86d67
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 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.num_validators": {
"description": "The number of validators in the consensus.",
"privacy": "Public",
"value": 4
},
"consensus.start_height": {
"description": "The height to start the consensus from.",
"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.num_validators": {
"description": "The number of validators in the consensus.",
"value": {
"$serde_json::private::Number": "4"
},
"privacy": "Public"
},
"consensus.start_height": {
"description": "The height to start the consensus from.",
"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 @@ -120,6 +120,7 @@ fn run_consensus(
let context = PapyrusConsensusContext::new(
storage_reader.clone(),
consensus_channels.messages_to_broadcast_sender,
config.num_validators,
);
let start_height = config.start_height;

Expand Down
5 changes: 3 additions & 2 deletions crates/sequencing/papyrus_consensus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ versions. Breaking changes are expected to happen in the near future.
## How to run
1. You must turn consensus on and provide a validator ID by passing: `--consensus.#is_none false --consensus.validator_id 0x<UNIQUE>`
2. Start by running any nodes which are validators for `consensus.start_height` which is by default 0 to avoid them missing the proposal.
1. You can change the default topic by adding: `--consensus.topic "TOPIC"`
1. You can change the default number of validators by passing: `--consensus.num_validators <NUM_VALIDATORS>`
2. You can change the default topic by passing: `--consensus.topic "TOPIC"`

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

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

```
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>
```
- The validator set, (0, 1, 2, 3), is hardcoded in milestone 2.
- Node 0 is the first proposer and should be run last.

UNIQUE - a value unique among all nodes running locally.
10 changes: 10 additions & 0 deletions crates/sequencing/papyrus_consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct ConsensusConfig {
pub topic: String,
/// The height to start the consensus from.
pub start_height: BlockNumber,
/// The number of validators in the consensus.
// Used for testing in an early milestone.
pub num_validators: u64,
}

impl SerializeConfig for ConsensusConfig {
Expand All @@ -43,6 +46,12 @@ impl SerializeConfig for ConsensusConfig {
"The height to start the consensus from.",
ParamPrivacyInput::Public,
),
ser_param(
"num_validators",
&self.num_validators,
"The number of validators in the consensus.",
ParamPrivacyInput::Public,
),
])
}
}
Expand All @@ -53,6 +62,7 @@ impl Default for ConsensusConfig {
validator_id: ValidatorId::default(),
topic: "consensus".to_string(),
start_height: BlockNumber::default(),
num_validators: 4,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use papyrus_storage::body::BodyStorageReader;
use papyrus_storage::header::HeaderStorageReader;
use papyrus_storage::{StorageError, StorageReader};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ContractAddress;
use starknet_api::transaction::Transaction;
use tokio::sync::Mutex;
use tracing::debug;
Expand Down Expand Up @@ -47,6 +48,7 @@ impl ConsensusBlock for PapyrusConsensusBlock {
pub struct PapyrusConsensusContext {
storage_reader: StorageReader,
broadcast_sender: Arc<Mutex<SubscriberSender<ConsensusMessage>>>,
validators: Vec<ValidatorId>,
}

impl PapyrusConsensusContext {
Expand All @@ -55,8 +57,13 @@ impl PapyrusConsensusContext {
pub fn new(
storage_reader: StorageReader,
broadcast_sender: SubscriberSender<ConsensusMessage>,
num_validators: u64,
) -> Self {
Self { storage_reader, broadcast_sender: Arc::new(Mutex::new(broadcast_sender)) }
Self {
storage_reader,
broadcast_sender: Arc::new(Mutex::new(broadcast_sender)),
validators: (0..num_validators).map(ContractAddress::from).collect(),
}
}
}

Expand Down Expand Up @@ -158,11 +165,11 @@ impl ConsensusContext for PapyrusConsensusContext {
}

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

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

async fn broadcast(&self, message: ConsensusMessage) -> Result<(), ConsensusError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn test_setup() -> (Block, PapyrusConsensusContext, BroadcastNetworkMock<Consens
let papyrus_context = PapyrusConsensusContext::new(
storage_reader.clone(),
test_channels.subscriber_channels.messages_to_broadcast_sender,
4,
);
(block, papyrus_context, test_channels.mock_network)
}

0 comments on commit 6f86d67

Please sign in to comment.