Skip to content

Commit

Permalink
Check validator key in network crate
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 30, 2023
1 parent 783bc30 commit d8e8ff3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion node/actors/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ impl<S: WriteBlockStore + 'static> Executor<S> {
scope::run!(ctx, |ctx, s| async {
s.spawn_blocking(|| dispatcher.run(ctx).context("IO Dispatcher stopped"));
s.spawn(async {
let state = network::State::new(network_config, None, Some(sync_blocks_subscriber));
let state = network::State::new(network_config, None, Some(sync_blocks_subscriber))
.context("Invalid network config")?;
state.register_metrics();
network::run_network(ctx, state, network_actor_pipe)
.await
Expand Down
14 changes: 11 additions & 3 deletions node/actors/network/src/consensus/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ pub(crate) struct State {

impl State {
/// Constructs a new State.
pub(crate) fn new(cfg: Config, validators: &ValidatorSet) -> Self {
pub(crate) fn new(cfg: Config, validators: &ValidatorSet) -> anyhow::Result<Self> {
let validators: HashSet<_> = validators.iter().cloned().collect();
Self {
let current_validator_key = cfg.key.public();
anyhow::ensure!(
validators.contains(&current_validator_key),
"Validators' public keys {validators:?} do not contain the current validator \
{current_validator_key:?}; this is not yet supported"
);
// ^ This check will be relaxed once we support dynamic validator membership

Ok(Self {
cfg,
inbound: PoolWatch::new(validators.clone(), 0),
outbound: PoolWatch::new(validators, 0),
}
})
}
}
15 changes: 9 additions & 6 deletions node/actors/network/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ impl State {
cfg: Config,
events: Option<channel::UnboundedSender<Event>>,
sync_state: Option<watch::Receiver<SyncState>>,
) -> Arc<Self> {
Arc::new(Self {
) -> anyhow::Result<Arc<Self>> {
let consensus = cfg
.consensus
.map(|consensus_cfg| consensus::State::new(consensus_cfg, &cfg.validators))
.transpose()?;
let this = Self {
gossip: gossip::State::new(cfg.gossip, sync_state),
consensus: cfg
.consensus
.map(|consensus_cfg| consensus::State::new(consensus_cfg, &cfg.validators)),
consensus,
events,
cfg: SharedConfig {
server_addr: cfg.server_addr,
validators: cfg.validators,
},
})
};
Ok(Arc::new(this))
}

/// Registers metrics for this state.
Expand Down
2 changes: 1 addition & 1 deletion node/actors/network/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Instance {
pub(crate) fn from_cfg(cfg: Config) -> Self {
let (events_send, events_recv) = channel::unbounded();
Self {
state: State::new(cfg, Some(events_send), None),
state: State::new(cfg, Some(events_send), None).expect("Invalid network config"),
events: events_recv,
}
}
Expand Down

0 comments on commit d8e8ff3

Please sign in to comment.