BlockData::new

This commit is contained in:
Luke Parker
2022-11-12 11:52:55 -05:00
parent 2de4ab8c9d
commit 4ba469e653
2 changed files with 35 additions and 33 deletions

View File

@@ -1,4 +1,7 @@
use std::collections::{HashSet, HashMap}; use std::{
sync::Arc,
collections::{HashSet, HashMap},
};
use crate::{ use crate::{
time::CanonicalInstant, time::CanonicalInstant,
@@ -23,6 +26,29 @@ pub(crate) struct BlockData<N: Network> {
} }
impl<N: Network> BlockData<N> { impl<N: Network> BlockData<N> {
pub(crate) fn new(
weights: Arc<N::Weights>,
number: BlockNumber,
validator_id: Option<N::ValidatorId>,
proposal: N::Block,
) -> BlockData<N> {
BlockData {
number,
validator_id,
proposal,
log: MessageLog::new(weights),
slashes: HashSet::new(),
end_time: HashMap::new(),
// The caller of BlockData::new is expected to be populated after by the caller
round: None,
locked: None,
valid: None,
}
}
pub(crate) fn round(&self) -> &RoundData<N> { pub(crate) fn round(&self) -> &RoundData<N> {
self.round.as_ref().unwrap() self.round.as_ref().unwrap()
} }

View File

@@ -3,7 +3,7 @@ use core::fmt::Debug;
use std::{ use std::{
sync::Arc, sync::Arc,
time::{SystemTime, Instant, Duration}, time::{SystemTime, Instant, Duration},
collections::{VecDeque, HashSet, HashMap}, collections::VecDeque,
}; };
use parity_scale_codec::{Encode, Decode}; use parity_scale_codec::{Encode, Decode};
@@ -24,8 +24,7 @@ use round::RoundData;
mod block; mod block;
use block::BlockData; use block::BlockData;
mod message_log; pub(crate) mod message_log;
use message_log::MessageLog;
/// Traits and types of the external network being integrated with to provide consensus over. /// Traits and types of the external network being integrated with to provide consensus over.
pub mod ext; pub mod ext;
@@ -218,21 +217,12 @@ impl<N: Network + 'static> TendermintMachine<N> {
self.queue = VecDeque::new(); self.queue = VecDeque::new();
// Create the new block // Create the new block
self.block = BlockData { self.block = BlockData::new(
number: BlockNumber(self.block.number.0 + 1), self.weights.clone(),
validator_id: self.signer.validator_id().await, BlockNumber(self.block.number.0 + 1),
self.signer.validator_id().await,
proposal, proposal,
);
log: MessageLog::new(self.weights.clone()),
slashes: HashSet::new(),
end_time: HashMap::new(),
// This will be populated in the following round() call
round: None,
locked: None,
valid: None,
};
// Start the first round // Start the first round
self.round(RoundNumber(0), Some(round_end)); self.round(RoundNumber(0), Some(round_end));
@@ -298,21 +288,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
msg_recv, msg_recv,
step_recv, step_recv,
block: BlockData { block: BlockData::new(weights, BlockNumber(last.0 .0 + 1), validator_id, proposal),
number: BlockNumber(last.0 .0 + 1),
validator_id,
proposal,
log: MessageLog::new(weights),
slashes: HashSet::new(),
end_time: HashMap::new(),
// This will be populated in the following round() call
round: None,
locked: None,
valid: None,
},
}; };
// The end time of the last block is the start time for this one // The end time of the last block is the start time for this one