Skip to content

Commit

Permalink
Merge #675
Browse files Browse the repository at this point in the history
675: {NDRS-694, HWY-220} Re-joining bugfixes r=fizyk20 a=fizyk20

https://casperlabs.atlassian.net/browse/NDRS-694
https://casperlabs.atlassian.net/browse/HWY-220

Co-authored-by: Bartłomiej Kamiński <[email protected]>
  • Loading branch information
bors[bot] and fizyk20 authored Dec 17, 2020
2 parents ec8cdd6 + d2f641b commit 3234e91
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
17 changes: 11 additions & 6 deletions node/src/components/block_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ use crate::{
effect::{
announcements::BlockExecutorAnnouncement,
requests::{
BlockExecutorRequest, ContractRuntimeRequest, LinearChainRequest, StorageRequest,
BlockExecutorRequest, ConsensusRequest, ContractRuntimeRequest, LinearChainRequest,
StorageRequest,
},
EffectBuilder, EffectExt, Effects,
},
Expand All @@ -53,6 +54,7 @@ pub trait ReactorEventT:
+ From<LinearChainRequest<NodeId>>
+ From<ContractRuntimeRequest>
+ From<BlockExecutorAnnouncement>
+ From<ConsensusRequest>
+ Send
{
}
Expand All @@ -63,6 +65,7 @@ impl<REv> ReactorEventT for REv where
+ From<LinearChainRequest<NodeId>>
+ From<ContractRuntimeRequest>
+ From<BlockExecutorAnnouncement>
+ From<ConsensusRequest>
+ Send
{
}
Expand Down Expand Up @@ -439,16 +442,18 @@ impl<REv: ReactorEventT> Component<REv> for BlockExecutor {
effect_builder
.get_block_at_height_local(finalized_block.height())
.event(move |maybe_block| {
Event::BlockAlreadyExists(maybe_block.is_some(), finalized_block)
Event::BlockAlreadyExists(maybe_block.map(Box::new), finalized_block)
})
}
Event::BlockAlreadyExists(exists, finalized_block) => {
if !exists {
Event::BlockAlreadyExists(maybe_block, finalized_block) => {
if let Some(block) = maybe_block {
effect_builder
.handle_linear_chain_block(block.take_header())
.ignore()
} else {
// If we haven't executed the block before in the past (for example during
// joining), do it now.
self.get_deploys(effect_builder, finalized_block)
} else {
Effects::new()
}
}
Event::GetDeploysResult {
Expand Down
8 changes: 4 additions & 4 deletions node/src/components/block_executor/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ use casper_types::ExecutionResult;
use crate::{
crypto::hash::Digest,
effect::requests::BlockExecutorRequest,
types::{BlockHash, Deploy, DeployHash, DeployHeader, FinalizedBlock},
types::{Block, BlockHash, Deploy, DeployHash, DeployHeader, FinalizedBlock},
};

/// Block executor component event.
#[derive(Debug, From)]
pub enum Event {
/// Indicates whether block has already been finalized and executed in the past.
BlockAlreadyExists(bool, FinalizedBlock),
BlockAlreadyExists(Option<Box<Block>>, FinalizedBlock),
/// A request made of the Block executor component.
#[from]
Request(BlockExecutorRequest),
Expand Down Expand Up @@ -150,12 +150,12 @@ impl Display for Event {
state.state_root_hash,
result
),
Event::BlockAlreadyExists(flag, fb) => {
Event::BlockAlreadyExists(maybe_block, fb) => {
write!(
f,
"Block at height: {} was executed before: {}",
fb.height(),
flag
maybe_block.is_some()
)
}
}
Expand Down
6 changes: 6 additions & 0 deletions node/src/components/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub enum Event<I> {
},
/// An event instructing us to shutdown if the latest era received no votes
Shutdown,
/// An event fired when the joiner reactor transitions into validator.
FinishedJoining(Timestamp),
}

impl Debug for ConsensusMessage {
Expand Down Expand Up @@ -186,6 +188,9 @@ impl<I: Debug> Display for Event<I> {
booking_block_hash, key_block_seed, get_validators_result
),
Event::Shutdown => write!(f, "Shutdown if current era is inactive"),
Event::FinishedJoining(timestamp) => {
write!(f, "The node finished joining the network at {}", timestamp)
}
}
}
}
Expand Down Expand Up @@ -307,6 +312,7 @@ where
)
}
Event::Shutdown => handling_es.shutdown_if_necessary(),
Event::FinishedJoining(timestamp) => handling_es.finished_joining(timestamp),
}
}
}
40 changes: 39 additions & 1 deletion node/src/components/consensus/era_supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub struct EraSupervisor<I> {
next_block_height: u64,
#[data_size(skip)]
metrics: ConsensusMetrics,
// TODO: discuss this quick fix
finished_joining: bool,
}

impl<I> Debug for EraSupervisor<I> {
Expand Down Expand Up @@ -143,6 +145,7 @@ where
bonded_eras,
next_block_height: 0,
metrics,
finished_joining: false,
};

let results = era_supervisor.new_era(
Expand Down Expand Up @@ -257,6 +260,9 @@ where
} else if !validators.contains_key(&our_id) {
info!(era = era_id.0, %our_id, "not voting; not a validator");
false
} else if !self.finished_joining {
info!(era = era_id.0, "not voting; still joining");
false
} else {
info!(era = era_id.0, "start voting");
true
Expand All @@ -283,7 +289,14 @@ where
Vec::new()
};

let era = Era::new(consensus, start_height, newly_slashed, slashed, validators);
let era = Era::new(
consensus,
start_time,
start_height,
newly_slashed,
slashed,
validators,
);
let _ = self.active_eras.insert(era_id, era);

// Remove the era that has become obsolete now. We keep 2 * bonded_eras past eras because
Expand Down Expand Up @@ -312,6 +325,26 @@ where
pub(crate) fn active_eras(&self) -> &HashMap<EraId, Era<I>> {
&self.active_eras
}

/// To be called when we transition from the joiner to the validator reactor.
pub(crate) fn finished_joining(
&mut self,
now: Timestamp,
) -> Vec<ProtocolOutcome<I, ClContext>> {
self.finished_joining = true;
let secret = Keypair::new(Rc::clone(&self.secret_signing_key), self.public_signing_key);
let public_key = self.public_signing_key;
self.active_eras
.get_mut(&self.current_era)
.map(|era| {
if era.start_time > now && era.validators().contains_key(&public_key) {
era.consensus.activate_validator(public_key, secret, now)
} else {
Vec::new()
}
})
.unwrap_or_default()
}
}

/// A mutable `EraSupervisor` reference, together with an `EffectBuilder`.
Expand Down Expand Up @@ -783,6 +816,11 @@ where
Default::default()
}
}

pub(crate) fn finished_joining(&mut self, now: Timestamp) -> Effects<Event<I>> {
let results = self.era_supervisor.finished_joining(now);
self.handle_consensus_results(self.era_supervisor.current_era, results)
}
}

/// Computes the instance ID for an era, given the state root hash, block height and chainspec.
Expand Down
8 changes: 7 additions & 1 deletion node/src/components/consensus/era_supervisor/era.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
ConsensusMessage,
},
crypto::asymmetric_key::PublicKey,
types::ProtoBlock,
types::{ProtoBlock, Timestamp},
};

#[derive(
Expand Down Expand Up @@ -126,6 +126,8 @@ impl PendingCandidate {
pub struct Era<I> {
/// The consensus protocol instance.
pub(crate) consensus: Box<dyn ConsensusProtocol<I, ClContext>>,
/// The scheduled starting time of this era.
pub(crate) start_time: Timestamp,
/// The height of this era's first block.
pub(crate) start_height: u64,
/// Pending candidate blocks, waiting for validation. The boolean is `true` if the proto block
Expand All @@ -146,13 +148,15 @@ pub struct Era<I> {
impl<I> Era<I> {
pub(crate) fn new(
consensus: Box<dyn ConsensusProtocol<I, ClContext>>,
start_time: Timestamp,
start_height: u64,
newly_slashed: Vec<PublicKey>,
slashed: HashSet<PublicKey>,
validators: BTreeMap<PublicKey, U512>,
) -> Self {
Era {
consensus,
start_time,
start_height,
candidates: Vec::new(),
newly_slashed,
Expand Down Expand Up @@ -262,6 +266,7 @@ where
// Destructure self, so we can't miss any fields.
let Era {
consensus,
start_time,
start_height,
candidates,
newly_slashed,
Expand All @@ -288,6 +293,7 @@ where
};

consensus_heap_size
+ start_time.estimate_heap_size()
+ start_height.estimate_heap_size()
+ candidates.estimate_heap_size()
+ newly_slashed.estimate_heap_size()
Expand Down
7 changes: 7 additions & 0 deletions node/src/reactor/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ impl reactor::Reactor for Reactor {
.event(|_| consensus::Event::Shutdown),
));

effects.extend(reactor::wrap_effects(
Event::Consensus,
effect_builder
.immediately()
.event(move |_| consensus::Event::FinishedJoining(now)),
));

Ok((
Reactor {
metrics,
Expand Down

0 comments on commit 3234e91

Please sign in to comment.