Skip to content

Commit b43d5a3

Browse files
committed
Change the possible tendermint states
To accept multiple proposals and wait for their imports, I introduced a new type for TendermintState::Propose.
1 parent 128c952 commit b43d5a3

File tree

2 files changed

+156
-95
lines changed

2 files changed

+156
-95
lines changed

core/src/consensus/tendermint/types.rs

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,62 @@ use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
2424
use super::super::BitSet;
2525
use super::message::{ProposalSummary, VoteStep};
2626
use crate::block::{IsBlock, SealedBlock};
27-
use crate::consensus::{sortition::seed::SeedInfo, Priority};
27+
use crate::consensus::{sortition::seed::SeedInfo, Priority, PriorityInfo};
2828

2929
pub type Height = u64;
3030
pub type View = u64;
3131

32+
#[derive(Clone)]
33+
pub struct ProposeInner {
34+
wait_block_generation: Option<(PriorityInfo, BlockHash)>,
35+
wait_imported: Vec<(PriorityInfo, SealedBlock)>,
36+
}
37+
38+
impl ProposeInner {
39+
pub fn generation_completed(&mut self) -> Option<(PriorityInfo, BlockHash)> {
40+
self.wait_block_generation.take()
41+
}
42+
43+
pub fn generation_halted(&mut self) {
44+
self.wait_block_generation = None;
45+
}
46+
47+
fn import_completed(&mut self, target_block_hash: BlockHash) -> Option<(PriorityInfo, SealedBlock)> {
48+
let position = self
49+
.wait_imported
50+
.iter()
51+
.position(|(_, sealed_block)| sealed_block.header().hash() == target_block_hash)?;
52+
Some(self.wait_imported.remove(position))
53+
}
54+
55+
fn wait_block_generation(&mut self, my_priority_info: PriorityInfo, parent_hash: BlockHash) {
56+
self.wait_block_generation = Some((my_priority_info, parent_hash));
57+
}
58+
59+
fn wait_imported(&mut self, target_priority_info: PriorityInfo, target_block: SealedBlock) {
60+
self.wait_imported.insert(0, (target_priority_info, target_block));
61+
}
62+
63+
pub fn get_wait_block_generation(&self) -> &Option<(PriorityInfo, BlockHash)> {
64+
&self.wait_block_generation
65+
}
66+
}
67+
68+
impl fmt::Debug for ProposeInner {
69+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70+
write!(
71+
f,
72+
"waiting block generation {:?} and waiting block imports {:?}",
73+
self.wait_block_generation,
74+
self.wait_imported.iter().map(|(_, sealed)| sealed.header().hash()).collect::<Vec<_>>()
75+
)
76+
}
77+
}
78+
3279
#[derive(Clone)]
3380
pub enum TendermintState {
34-
Propose,
35-
ProposeWaitBlockGeneration {
36-
parent_hash: BlockHash,
37-
},
38-
ProposeWaitImported {
39-
block: Box<SealedBlock>,
40-
},
81+
// wait block generation
82+
Propose(Box<ProposeInner>),
4183
Prevote,
4284
Precommit,
4385
Commit {
@@ -51,13 +93,50 @@ pub enum TendermintState {
5193
}
5294

5395
impl TendermintState {
96+
pub fn new_propose_step() -> Self {
97+
TendermintState::Propose(Box::new(ProposeInner {
98+
wait_block_generation: None,
99+
wait_imported: Vec::new(),
100+
}))
101+
}
102+
103+
pub fn generation_completed(&mut self) -> Option<(PriorityInfo, BlockHash)> {
104+
if let Self::Propose(inner) = self {
105+
inner.generation_completed()
106+
} else {
107+
None
108+
}
109+
}
110+
111+
pub fn generation_halted(&mut self) {
112+
if let Self::Propose(inner) = self {
113+
inner.generation_halted()
114+
}
115+
}
116+
117+
pub fn import_completed(&mut self, target_block_hash: BlockHash) -> Option<(PriorityInfo, SealedBlock)> {
118+
if let Self::Propose(inner) = self {
119+
inner.import_completed(target_block_hash)
120+
} else {
121+
None
122+
}
123+
}
124+
125+
pub fn wait_block_generation(&mut self, my_priority_info: PriorityInfo, parent_hash: BlockHash) {
126+
if let Self::Propose(inner) = self {
127+
inner.wait_block_generation(my_priority_info, parent_hash);
128+
}
129+
}
130+
131+
pub fn wait_imported(&mut self, target_priority_info: PriorityInfo, target_block: SealedBlock) {
132+
if let Self::Propose(inner) = self {
133+
inner.wait_imported(target_priority_info, target_block)
134+
}
135+
}
136+
54137
pub fn to_step(&self) -> Step {
55138
match self {
56-
TendermintState::Propose => Step::Propose,
57-
TendermintState::ProposeWaitBlockGeneration {
58-
..
59-
} => Step::Propose,
60-
TendermintState::ProposeWaitImported {
139+
TendermintState::Propose {
61140
..
62141
} => Step::Propose,
63142
TendermintState::Prevote => Step::Prevote,
@@ -102,11 +181,7 @@ impl TendermintState {
102181
block_hash,
103182
view,
104183
} => Some((*view, *block_hash)),
105-
TendermintState::Propose => None,
106-
TendermintState::ProposeWaitBlockGeneration {
107-
..
108-
} => None,
109-
TendermintState::ProposeWaitImported {
184+
TendermintState::Propose {
110185
..
111186
} => None,
112187
TendermintState::Prevote => None,
@@ -118,13 +193,7 @@ impl TendermintState {
118193
impl fmt::Debug for TendermintState {
119194
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120195
match self {
121-
TendermintState::Propose => write!(f, "TendermintState::Propose"),
122-
TendermintState::ProposeWaitBlockGeneration {
123-
parent_hash,
124-
} => write!(f, "TendermintState::ProposeWaitBlockGeneration({})", parent_hash),
125-
TendermintState::ProposeWaitImported {
126-
block,
127-
} => write!(f, "TendermintState::ProposeWaitImported({})", block.header().hash()),
196+
TendermintState::Propose(inner) => write!(f, "TenderminState::Propose, {:?}", inner),
128197
TendermintState::Prevote => write!(f, "TendermintState::Prevote"),
129198
TendermintState::Precommit => write!(f, "TendermintState::Precommit"),
130199
TendermintState::Commit {

0 commit comments

Comments
 (0)