diff --git a/substrate/tendermint/machine/src/block.rs b/substrate/tendermint/machine/src/block.rs index 1a08ff4a..a6b7b190 100644 --- a/substrate/tendermint/machine/src/block.rs +++ b/substrate/tendermint/machine/src/block.rs @@ -1,4 +1,7 @@ -use std::collections::{HashSet, HashMap}; +use std::{ + sync::Arc, + collections::{HashSet, HashMap}, +}; use crate::{ time::CanonicalInstant, @@ -23,6 +26,29 @@ pub(crate) struct BlockData<N: Network> { } 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> { self.round.as_ref().unwrap() } diff --git a/substrate/tendermint/machine/src/lib.rs b/substrate/tendermint/machine/src/lib.rs index 02433318..cdd3e90a 100644 --- a/substrate/tendermint/machine/src/lib.rs +++ b/substrate/tendermint/machine/src/lib.rs @@ -3,7 +3,7 @@ use core::fmt::Debug; use std::{ sync::Arc, time::{SystemTime, Instant, Duration}, - collections::{VecDeque, HashSet, HashMap}, + collections::VecDeque, }; use parity_scale_codec::{Encode, Decode}; @@ -24,8 +24,7 @@ use round::RoundData; mod block; use block::BlockData; -mod message_log; -use message_log::MessageLog; +pub(crate) mod message_log; /// Traits and types of the external network being integrated with to provide consensus over. pub mod ext; @@ -218,21 +217,12 @@ impl<N: Network + 'static> TendermintMachine<N> { self.queue = VecDeque::new(); // Create the new block - self.block = BlockData { - number: BlockNumber(self.block.number.0 + 1), - validator_id: self.signer.validator_id().await, + self.block = BlockData::new( + self.weights.clone(), + BlockNumber(self.block.number.0 + 1), + self.signer.validator_id().await, 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 self.round(RoundNumber(0), Some(round_end)); @@ -298,21 +288,7 @@ impl<N: Network + 'static> TendermintMachine<N> { msg_recv, step_recv, - block: BlockData { - 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, - }, + block: BlockData::new(weights, BlockNumber(last.0 .0 + 1), validator_id, proposal), }; // The end time of the last block is the start time for this one