Skip to content

Commit d2fc6f4

Browse files
authored
chore: Remove initial height and validator set from consensus params (#1190)
Closes: #XXX These parameters are a remnant from a very early time in Malachite's lifespan where consensus would always start at the initial height with a static validator set and move from height to height on its own. These parameters have therefore not had any effect for a while, given that it is now up to the app to tell consensus which height to start at after receiving the `ConsensusReady` message. As such, they can be removed without any loss of functionality. --- ### PR author checklist #### For all contributors - [ ] Reference a GitHub issue - [ ] Ensure the PR title follows the [conventional commits][conv-commits] spec - [ ] Add a release note in [`RELEASE_NOTES.md`](/RELEASE_NOTES.md) if the change warrants it - [ ] Add an entry in [`BREAKING_CHANGES.md`](/BREAKING_CHANGES.md) if the change warrants it #### For external contributors - [ ] Maintainers of Malachite are [allowed to push changes to the branch][gh-edit-branch] [conv-commits]: https://www.conventionalcommits.org/en/v1.0.0/#summary [gh-edit-branch]: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork#enabling-repository-maintainer-permissions-on-existing-pull-requests
1 parent d3f9f14 commit d2fc6f4

File tree

12 files changed

+68
-70
lines changed

12 files changed

+68
-70
lines changed

BREAKING_CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### `malachitebft-core-types`
66

77
- Move `SigningProvider` and `SigningProviderExt` traits into new `malachitebft-signing` crate ([#1191](https://github.com/informalsystems/malachite/pull/1191))
8+
- Remove `initial_validator_set` and `initial_height` fields from `Params` struct ([#1190](https://github.com/circlefin/malachite/pull/1190))
89

910
### `malachitebft-signing`
1011

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Add facility for app to request a consensus state dump at any time ([#1176](https://github.com/informalsystems/malachite/pull/1176))
1212
- Make libp2p protocol names configurable ([#1161](https://github.com/informalsystems/malachite/issues/1161))
1313
- Fix mismatched height of WAL entries emitted when processing `StartHeight` input ([#1232](https://github.com/circlefin/malachite/issues/1232))
14+
- Remove `initial_validator_set` and `initial_height` fields from `Params` struct ([#1190](https://github.com/circlefin/malachite/pull/1190))
1415

1516
## 0.5.0
1617

code/crates/app-channel/src/msgs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl From<oneshot::error::RecvError> for ConsensusRequestError {
9696
/// ```
9797
pub enum ConsensusRequest<Ctx: Context> {
9898
/// Request a state dump from consensus
99-
DumpState(Reply<StateDump<Ctx>>),
99+
DumpState(Reply<Option<StateDump<Ctx>>>),
100100
}
101101

102102
impl<Ctx: Context> ConsensusRequest<Ctx> {
@@ -105,7 +105,7 @@ impl<Ctx: Context> ConsensusRequest<Ctx> {
105105
/// If the request fails, `None` is returned.
106106
pub async fn dump_state(
107107
tx_request: &mpsc::Sender<ConsensusRequest<Ctx>>,
108-
) -> Result<StateDump<Ctx>, ConsensusRequestError> {
108+
) -> Result<Option<StateDump<Ctx>>, ConsensusRequestError> {
109109
let (tx, rx) = oneshot::channel();
110110

111111
tx_request

code/crates/app-channel/src/run.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pub async fn start_engine<Node, Ctx, WalCodec, NetCodec>(
2424
cfg: Node::Config,
2525
wal_codec: WalCodec,
2626
net_codec: NetCodec,
27-
start_height: Option<Ctx::Height>,
28-
initial_validator_set: Ctx::ValidatorSet,
2927
) -> Result<(Channels<Ctx>, EngineHandle)>
3028
where
3129
Ctx: Context,
@@ -35,8 +33,6 @@ where
3533
NetCodec: codec::ConsensusCodec<Ctx>,
3634
NetCodec: codec::SyncCodec<Ctx>,
3735
{
38-
let start_height = start_height.unwrap_or_default();
39-
4036
let registry = SharedRegistry::global().with_moniker(cfg.moniker());
4137
let metrics = Metrics::register(&registry);
4238

@@ -70,8 +66,6 @@ where
7066

7167
// Spawn consensus
7268
let consensus = spawn_consensus_actor(
73-
start_height,
74-
initial_validator_set,
7569
address,
7670
ctx.clone(),
7771
cfg.consensus().clone(),

code/crates/app/src/spawn.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ where
7171

7272
#[allow(clippy::too_many_arguments)]
7373
pub async fn spawn_consensus_actor<Ctx>(
74-
initial_height: Ctx::Height,
75-
initial_validator_set: Ctx::ValidatorSet,
7674
address: Ctx::Address,
7775
ctx: Ctx,
7876
mut cfg: ConsensusConfig,
@@ -97,8 +95,6 @@ where
9795
};
9896

9997
let consensus_params = ConsensusParams {
100-
initial_height,
101-
initial_validator_set,
10298
address,
10399
threshold_params: Default::default(),
104100
value_payload,

code/crates/core-consensus/src/params.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ pub use malachitebft_core_driver::ThresholdParams;
1111
/// Consensus parameters.
1212
#[derive_where(Clone, Debug)]
1313
pub struct Params<Ctx: Context> {
14-
/// The initial height
15-
pub initial_height: Ctx::Height,
16-
17-
/// The initial validator set
18-
pub initial_validator_set: Ctx::ValidatorSet,
19-
2014
/// The address of this validator
2115
pub address: Ctx::Address,
2216

code/crates/core-consensus/src/state.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,17 @@ impl<Ctx> State<Ctx>
4141
where
4242
Ctx: Context,
4343
{
44-
pub fn new(ctx: Ctx, params: Params<Ctx>, queue_capacity: usize) -> Self {
44+
pub fn new(
45+
ctx: Ctx,
46+
height: Ctx::Height,
47+
validator_set: Ctx::ValidatorSet,
48+
params: Params<Ctx>,
49+
queue_capacity: usize,
50+
) -> Self {
4551
let driver = Driver::new(
4652
ctx.clone(),
47-
params.initial_height,
48-
params.initial_validator_set.clone(),
53+
height,
54+
validator_set,
4955
params.address.clone(),
5056
params.threshold_params,
5157
);

code/crates/engine/src/consensus.rs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub enum Msg<Ctx: Context> {
118118
RestartHeight(Ctx::Height, Ctx::ValidatorSet),
119119

120120
/// Request to dump the current consensus state
121-
DumpState(RpcReplyPort<StateDump<Ctx>>),
121+
DumpState(RpcReplyPort<Option<StateDump<Ctx>>>),
122122
}
123123

124124
impl<Ctx: Context> fmt::Display for Msg<Ctx> {
@@ -235,8 +235,9 @@ pub struct State<Ctx: Context> {
235235
/// Timeouts configuration
236236
timeouts: Timeouts,
237237

238-
/// The state of the consensus state machine
239-
consensus: ConsensusState<Ctx>,
238+
/// The state of the consensus state machine,
239+
/// or `None` if consensus has not been started yet.
240+
consensus: Option<ConsensusState<Ctx>>,
240241

241242
/// The set of peers we are connected to.
242243
connected_peers: BTreeSet<PeerId>,
@@ -254,7 +255,17 @@ where
254255
Ctx: Context,
255256
{
256257
pub fn height(&self) -> Ctx::Height {
257-
self.consensus.height()
258+
self.consensus
259+
.as_ref()
260+
.map(|c| c.height())
261+
.unwrap_or_default()
262+
}
263+
264+
pub fn round(&self) -> Round {
265+
self.consensus
266+
.as_ref()
267+
.map(|c| c.round())
268+
.unwrap_or(Round::Nil)
258269
}
259270

260271
fn set_phase(&mut self, phase: Phase) {
@@ -315,7 +326,7 @@ where
315326
) -> Result<(), ConsensusError<Ctx>> {
316327
malachitebft_core_consensus::process!(
317328
input: input,
318-
state: &mut state.consensus,
329+
state: state.consensus.as_mut().expect("Consensus not started"),
319330
metrics: &self.metrics,
320331
with: effect => {
321332
let handler_state = HandlerState {
@@ -370,6 +381,17 @@ where
370381
return Err(eyre!("Validator set for height {height} is empty").into());
371382
}
372383

384+
// Initialize consensus state if this is the first height we start
385+
if state.consensus.is_none() {
386+
state.consensus = Some(ConsensusState::new(
387+
self.ctx.clone(),
388+
height,
389+
validator_set.clone(),
390+
self.params.clone(),
391+
self.consensus_config.queue_capacity,
392+
));
393+
}
394+
373395
self.tx_event
374396
.send(|| Event::StartedHeight(height, is_restart));
375397

@@ -463,13 +485,7 @@ where
463485
return Ok(());
464486
}
465487

466-
info!(%peer_id, "Connected to peer");
467-
468-
let validator_set = state.consensus.validator_set();
469-
let connected_peers = state.connected_peers.len();
470-
let total_peers = validator_set.count().saturating_sub(1);
471-
472-
debug!(connected = %connected_peers, total = %total_peers, "Connected to another peer");
488+
info!(%peer_id, total = %state.connected_peers.len(), "Connected to peer");
473489

474490
self.metrics.connected_peers.inc();
475491
}
@@ -536,7 +552,7 @@ where
536552
Event::Received(SignedConsensusMsg::Proposal(proposal.clone()))
537553
});
538554

539-
if state.consensus.params.value_payload.parts_only() {
555+
if self.params.value_payload.parts_only() {
540556
error!(%from, "Properly configured peer should never send proposal messages in BlockPart mode");
541557
return Ok(());
542558
}
@@ -576,7 +592,7 @@ where
576592
}
577593

578594
NetworkEvent::ProposalPart(from, part) => {
579-
if state.consensus.params.value_payload.proposal_only() {
595+
if self.params.value_payload.proposal_only() {
580596
error!(%from, "Properly configured peer should never send proposal part messages in Proposal mode");
581597
return Ok(());
582598
}
@@ -634,9 +650,20 @@ where
634650
}
635651

636652
Msg::DumpState(reply_to) => {
637-
let dump = StateDump::new(&state.consensus);
653+
let state_dump = if let Some(consensus) = &state.consensus {
654+
info!(
655+
height = %consensus.height(),
656+
round = %consensus.round(),
657+
"Dumping consensus state"
658+
);
638659

639-
if let Err(e) = reply_to.send(dump) {
660+
Some(StateDump::new(consensus))
661+
} else {
662+
info!("Dumping consensus state: not started");
663+
None
664+
};
665+
666+
if let Err(e) = reply_to.send(state_dump) {
640667
error!("Failed to reply with state dump: {e}");
641668
}
642669

@@ -690,7 +717,10 @@ where
690717
TimeoutKind::Prevote | TimeoutKind::Precommit | TimeoutKind::Rebroadcast
691718
) {
692719
info!(step = ?timeout.kind, "Timeout elapsed");
693-
state.consensus.print_state();
720+
721+
state.consensus.as_ref().inspect(|consensus| {
722+
consensus.print_state();
723+
});
694724
}
695725

696726
// Process the timeout event
@@ -1319,11 +1349,7 @@ where
13191349
Ok(State {
13201350
timers: Timers::new(Box::new(myself)),
13211351
timeouts: Timeouts::new(self.consensus_config.timeouts),
1322-
consensus: ConsensusState::new(
1323-
self.ctx.clone(),
1324-
self.params.clone(),
1325-
self.consensus_config.queue_capacity,
1326-
),
1352+
consensus: None,
13271353
connected_peers: BTreeSet::new(),
13281354
phase: Phase::Unstarted,
13291355
msg_buffer: MessageBuffer::new(MAX_BUFFER_SIZE),
@@ -1334,10 +1360,7 @@ where
13341360
name = "consensus",
13351361
parent = &self.span,
13361362
skip_all,
1337-
fields(
1338-
height = %state.consensus.height(),
1339-
round = %state.consensus.round()
1340-
)
1363+
fields(height = %state.height(), round = %state.round())
13411364
)]
13421365
async fn post_start(
13431366
&self,
@@ -1355,8 +1378,8 @@ where
13551378
parent = &self.span,
13561379
skip_all,
13571380
fields(
1358-
height = %span_height(state.consensus.height(), &msg),
1359-
round = %span_round(state.consensus.round(), &msg)
1381+
height = %span_height(state.height(), &msg),
1382+
round = %span_round(state.round(), &msg)
13601383
)
13611384
)]
13621385
async fn handle(
@@ -1383,8 +1406,8 @@ where
13831406
parent = &self.span,
13841407
skip_all,
13851408
fields(
1386-
height = %state.consensus.height(),
1387-
round = %state.consensus.round()
1409+
height = %state.height(),
1410+
round = %state.round()
13881411
)
13891412
)]
13901413
async fn post_stop(

code/crates/starknet/host/src/node.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use malachitebft_starknet_p2p_types::Ed25519Provider;
2020

2121
use crate::config::{load_config, Config};
2222
use crate::spawn::spawn_node_actor;
23-
use crate::types::{Address, Height, MockContext, PrivateKey, PublicKey, Validator, ValidatorSet};
23+
use crate::types::{Address, MockContext, PrivateKey, PublicKey, Validator, ValidatorSet};
2424

2525
#[derive(Clone, Debug, Serialize, Deserialize)]
2626
pub struct Genesis {
@@ -160,14 +160,11 @@ impl Node for StarknetNode {
160160
let genesis = self.load_genesis()?;
161161
let tx_event = TxEvent::new();
162162

163-
let start_height = self.start_height.map(|height| Height::new(height, 1));
164-
165163
let (actor, handle) = spawn_node_actor(
166164
config.clone(),
167165
self.home_dir.clone(),
168166
genesis.validator_set,
169167
private_key,
170-
start_height,
171168
tx_event.clone(),
172169
span.clone(),
173170
)

code/crates/starknet/host/src/spawn.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@ use crate::mempool::{Mempool, MempoolRef};
2828
use crate::mempool_load::{MempoolLoad, MempoolLoadRef, Params};
2929
use crate::metrics::Metrics as AppMetrics;
3030
use crate::types::MockContext;
31-
use crate::types::{Address, Height, PrivateKey, ValidatorSet};
31+
use crate::types::{Address, PrivateKey, ValidatorSet};
3232

3333
pub async fn spawn_node_actor(
3434
cfg: Config,
3535
home_dir: PathBuf,
3636
initial_validator_set: ValidatorSet,
3737
private_key: PrivateKey,
38-
start_height: Option<Height>,
3938
tx_event: TxEvent<MockContext>,
4039
span: tracing::Span,
4140
) -> (NodeRef, JoinHandle<()>) {
4241
let ctx = MockContext::new();
4342

44-
let start_height = start_height.unwrap_or(Height::new(1, 1));
45-
4643
let registry = SharedRegistry::global().with_moniker(cfg.moniker.as_str());
4744
let consensus_metrics = ConsensusMetrics::register(&registry);
4845
let app_metrics = AppMetrics::register(&registry);
@@ -88,8 +85,6 @@ pub async fn spawn_node_actor(
8885

8986
// Spawn consensus
9087
let consensus = spawn_consensus_actor(
91-
start_height,
92-
initial_validator_set,
9388
address,
9489
ctx,
9590
cfg,
@@ -179,8 +174,6 @@ async fn spawn_sync_actor(
179174

180175
#[allow(clippy::too_many_arguments)]
181176
async fn spawn_consensus_actor(
182-
initial_height: Height,
183-
initial_validator_set: ValidatorSet,
184177
address: Address,
185178
ctx: MockContext,
186179
mut cfg: Config,
@@ -194,8 +187,6 @@ async fn spawn_consensus_actor(
194187
span: &tracing::Span,
195188
) -> ConsensusRef<MockContext> {
196189
let consensus_params = ConsensusParams {
197-
initial_height,
198-
initial_validator_set,
199190
address,
200191
threshold_params: Default::default(),
201192
value_payload: ValuePayload::PartsOnly,

0 commit comments

Comments
 (0)