Consolidate state transition operations.
This commit is contained in:
parent
a6279c6952
commit
14adf6a43e
|
@ -96,7 +96,6 @@ impl<T: Contribution> Handler<T> {
|
|||
match state.state {
|
||||
State::Disconnected { .. } | State::DeterminingNetworkState { .. } => {
|
||||
state.update_peer_connection_added(&peers);
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
}
|
||||
State::AwaitingMorePeersForKeyGeneration { .. } => {
|
||||
if peers.count_validators() >= self.hdb.config().keygen_peer_count {
|
||||
|
@ -112,7 +111,6 @@ impl<T: Contribution> Handler<T> {
|
|||
peers,
|
||||
self.hdb.config(),
|
||||
)?;
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
|
||||
info!("KEY GENERATION: Sending initial parts and our own ack.");
|
||||
self.wire_to_validators(
|
||||
|
@ -393,8 +391,6 @@ impl<T: Contribution> Handler<T> {
|
|||
}
|
||||
}
|
||||
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
|
||||
// Handle previously queued input and messages:
|
||||
if let Some(iom_queue) = iom_queue_opt {
|
||||
while let Some(iom) = iom_queue.try_pop() {
|
||||
|
@ -429,12 +425,10 @@ impl<T: Contribution> Handler<T> {
|
|||
NetworkState::Unknown(p_infos) => {
|
||||
peer_infos = p_infos;
|
||||
state.update_peer_connection_added(peers);
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
}
|
||||
NetworkState::AwaitingMorePeersForKeyGeneration(p_infos) => {
|
||||
peer_infos = p_infos;
|
||||
state.set_awaiting_more_peers();
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
}
|
||||
NetworkState::GeneratingKeys(p_infos, public_keys) => {
|
||||
peer_infos = p_infos;
|
||||
|
@ -450,7 +444,6 @@ impl<T: Contribution> Handler<T> {
|
|||
// Key generation has completed and we were not a part
|
||||
// of it. Need to restart as a freshly connecting node.
|
||||
state.set_determining_network_state_active(net_info);
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
self.reset_peer_connections(state, peers)?;
|
||||
}
|
||||
State::Disconnected { .. }
|
||||
|
@ -493,7 +486,6 @@ impl<T: Contribution> Handler<T> {
|
|||
peers: &Peers<T>,
|
||||
) -> Result<(), Error> {
|
||||
state.update_peer_connection_dropped(peers);
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
|
||||
// TODO: Send a node removal (Change-Remove) vote?
|
||||
|
||||
|
@ -546,7 +538,6 @@ impl<T: Contribution> Handler<T> {
|
|||
match state.state {
|
||||
State::Disconnected {} => {
|
||||
state.set_awaiting_more_peers();
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
net_state = state.network_state(&peers);
|
||||
}
|
||||
State::DeterminingNetworkState {
|
||||
|
@ -590,7 +581,6 @@ impl<T: Contribution> Handler<T> {
|
|||
|
||||
let peers = self.hdb.peers();
|
||||
state.update_peer_connection_added(&peers);
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
}
|
||||
|
||||
InternalMessageKind::HbContribution(contrib) => {
|
||||
|
@ -775,7 +765,6 @@ impl<T: Contribution> Future for Handler<T> {
|
|||
assert_eq!(*pk, self.hdb.secret_key().public_key());
|
||||
assert!(state.dhb().unwrap().netinfo().is_validator());
|
||||
state.promote_to_validator()?;
|
||||
self.hdb.set_state_discriminant(state.discriminant());
|
||||
}
|
||||
}
|
||||
// FIXME
|
||||
|
|
|
@ -200,13 +200,6 @@ impl<T: Contribution> Hydrabadger<T> {
|
|||
self.inner.state.write()
|
||||
}
|
||||
|
||||
/// Sets the publicly visible state discriminant and returns the previous value.
|
||||
pub(super) fn set_state_discriminant(&self, dsct: StateDsct) -> StateDsct {
|
||||
let sd = StateDsct::from(self.inner.state_dsct.swap(dsct.into(), Ordering::Release));
|
||||
info!("State has been set from '{}' to '{}'.", sd, dsct);
|
||||
sd
|
||||
}
|
||||
|
||||
/// Returns a recent state discriminant.
|
||||
///
|
||||
/// The returned value may not be up to date and is to be considered
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::sync::{Arc, atomic::AtomicUsize};
|
||||
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
|
||||
use super::{Config, Error, InputOrMessage};
|
||||
use crossbeam::queue::SegQueue;
|
||||
use hbbft::{
|
||||
|
@ -131,6 +131,14 @@ impl<T: Contribution> StateMachine<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sets the publicly visible state discriminant and returns the previous value.
|
||||
fn set_state_discriminant(&self) -> StateDsct {
|
||||
let sd = StateDsct::from(self.dsct.swap(self.state.discriminant().into(),
|
||||
Ordering::Release));
|
||||
info!("State has been set from '{}' to '{}'.", sd, self.state.discriminant());
|
||||
sd
|
||||
}
|
||||
|
||||
/// Sets the state to `AwaitingMorePeersForKeyGeneration`.
|
||||
pub(super) fn set_awaiting_more_peers(&mut self) {
|
||||
self.state = match self.state {
|
||||
|
@ -165,6 +173,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
return;
|
||||
}
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
}
|
||||
|
||||
/// Sets state to `DeterminingNetworkState` if
|
||||
|
@ -181,6 +190,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
}
|
||||
_ => panic!("Cannot reset network state when state is not `AwaitingMorePeersForKeyGeneration`."),
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
}
|
||||
|
||||
/// Sets the state to `AwaitingMorePeersForKeyGeneration`.
|
||||
|
@ -244,7 +254,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
Must be State::AwaitingMorePeersForKeyGeneration"
|
||||
),
|
||||
};
|
||||
|
||||
self.set_state_discriminant();
|
||||
Ok((part, ack))
|
||||
}
|
||||
|
||||
|
@ -297,6 +307,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
s.discriminant()
|
||||
),
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
Ok(iom_queue_ret)
|
||||
}
|
||||
|
||||
|
@ -366,6 +377,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
s.discriminant()
|
||||
),
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
Ok(iom_queue_ret)
|
||||
}
|
||||
|
||||
|
@ -381,6 +393,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
s.discriminant()
|
||||
),
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -398,6 +411,7 @@ impl<T: Contribution> StateMachine<T> {
|
|||
}
|
||||
_ => return,
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
}
|
||||
|
||||
/// Sets state to `Disconnected` if peer count is zero, otherwise does nothing.
|
||||
|
@ -433,7 +447,8 @@ impl<T: Contribution> StateMachine<T> {
|
|||
debug!("Ignoring peer disconnection when `State::Validator`.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
self.set_state_discriminant();
|
||||
}
|
||||
|
||||
/// Returns the network state, if possible.
|
||||
|
|
Loading…
Reference in New Issue