This commit is contained in:
Luke Parker
2023-01-28 02:35:32 -05:00
parent b253529413
commit 9241bdc3b5
5 changed files with 41 additions and 33 deletions

View File

@@ -14,7 +14,7 @@ use crate::{
pub(crate) struct BlockData<N: Network> {
pub(crate) number: BlockNumber,
pub(crate) validator_id: Option<N::ValidatorId>,
pub(crate) proposal: N::Block,
pub(crate) proposal: Option<N::Block>,
pub(crate) log: MessageLog<N>,
pub(crate) slashes: HashSet<N::ValidatorId>,
@@ -35,7 +35,7 @@ impl<N: Network> BlockData<N> {
weights: Arc<N::Weights>,
number: BlockNumber,
validator_id: Option<N::ValidatorId>,
proposal: N::Block,
proposal: Option<N::Block>,
) -> BlockData<N> {
BlockData {
number,
@@ -106,12 +106,8 @@ impl<N: Network> BlockData<N> {
// 14-21
if Some(proposer) == self.validator_id {
let (round, block) = if let Some((round, block)) = &self.valid {
(Some(*round), block.clone())
} else {
(None, self.proposal.clone())
};
Some(Data::Proposal(round, block))
let (round, block) = self.valid.clone().unzip();
block.or_else(|| self.proposal.clone()).map(|block| Data::Proposal(round, block))
} else {
self.round_mut().set_timeout(Step::Propose);
None

View File

@@ -270,5 +270,5 @@ pub trait Network: Send + Sync {
&mut self,
block: Self::Block,
commit: Commit<Self::SignatureScheme>,
) -> Self::Block;
) -> Option<Self::Block>;
}

View File

@@ -135,7 +135,8 @@ pub struct TendermintMachine<N: Network> {
queue: VecDeque<MessageFor<N>>,
msg_recv: mpsc::UnboundedReceiver<SignedMessageFor<N>>,
step_recv: mpsc::UnboundedReceiver<(BlockNumber, Commit<N::SignatureScheme>, N::Block)>,
#[allow(clippy::type_complexity)]
step_recv: mpsc::UnboundedReceiver<(BlockNumber, Commit<N::SignatureScheme>, Option<N::Block>)>,
block: BlockData<N>,
}
@@ -143,7 +144,7 @@ pub struct TendermintMachine<N: Network> {
pub type StepSender<N> = mpsc::UnboundedSender<(
BlockNumber,
Commit<<N as Network>::SignatureScheme>,
<N as Network>::Block,
Option<<N as Network>::Block>,
)>;
pub type MessageSender<N> = mpsc::UnboundedSender<SignedMessageFor<N>>;
@@ -186,7 +187,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
}
// 53-54
async fn reset(&mut self, end_round: RoundNumber, proposal: N::Block) {
async fn reset(&mut self, end_round: RoundNumber, proposal: Option<N::Block>) {
// Ensure we have the end time data for the last round
self.block.populate_end_time(end_round);
@@ -209,7 +210,11 @@ impl<N: Network + 'static> TendermintMachine<N> {
self.round(RoundNumber(0), Some(round_end));
}
async fn reset_by_commit(&mut self, commit: Commit<N::SignatureScheme>, proposal: N::Block) {
async fn reset_by_commit(
&mut self,
commit: Commit<N::SignatureScheme>,
proposal: Option<N::Block>,
) {
let mut round = self.block.round().number;
// If this commit is for a round we don't have, jump up to it
while self.block.end_time[&round].canonical() < commit.end_time {
@@ -271,7 +276,12 @@ impl<N: Network + 'static> TendermintMachine<N> {
msg_recv,
step_recv,
block: BlockData::new(weights, BlockNumber(last_block.0 + 1), validator_id, proposal),
block: BlockData::new(
weights,
BlockNumber(last_block.0 + 1),
validator_id,
Some(proposal),
),
};
// The end time of the last block is the start time for this one

View File

@@ -140,11 +140,11 @@ impl Network for TestNetwork {
&mut self,
block: TestBlock,
commit: Commit<TestSignatureScheme>,
) -> TestBlock {
) -> Option<TestBlock> {
dbg!("Adding ", &block);
assert!(block.valid.is_ok());
assert!(self.verify_commit(block.id(), &commit));
TestBlock { id: (u32::from_le_bytes(block.id) + 1).to_le_bytes(), valid: Ok(()) }
Some(TestBlock { id: (u32::from_le_bytes(block.id) + 1).to_le_bytes(), valid: Ok(()) })
}
}