1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::types::*;
#[derive(Debug, Default)]
pub struct ConsensusStateValidator {
state: ConsensusState,
}
impl ConsensusStateValidator {
pub fn on_info_response(&mut self, info_response: &ResponseInfo) {
if self.state == ConsensusState::NoInfo {
let block_height = info_response.last_block_height;
if block_height == 0 {
self.state = ConsensusState::NotInitialized;
} else {
self.state = ConsensusState::WaitingForBlock {
block_height: block_height + 1,
app_hash: info_response.last_block_app_hash.clone(),
};
}
}
}
pub fn on_init_chain_request(&mut self) -> Result<(), String> {
if self.state != ConsensusState::NotInitialized {
return Err("Received `InitChain` call when chain is already initialized".to_string());
}
self.state = ConsensusState::InitChain;
Ok(())
}
pub fn on_begin_block_request(
&mut self,
begin_block_request: &RequestBeginBlock,
) -> Result<(), String> {
let new_state = match self.state {
ConsensusState::InitChain => {
let header = begin_block_request
.header
.as_ref()
.ok_or("`BeginBlock` request does not contain a header")?;
ConsensusState::ExecutingBlock {
block_height: header.height,
execution_state: BlockExecutionState::BeginBlock,
}
}
ConsensusState::WaitingForBlock {
ref block_height,
ref app_hash,
} => {
let block_height = *block_height;
let header = begin_block_request
.header
.as_ref()
.ok_or("`BeginBlock` request does not contain a header")?;
if header.height != block_height {
return Err(format!(
"Expected height {} in `BeginBlock` request. Got {}",
block_height, header.height
));
}
if &header.app_hash != app_hash {
return Err(format!(
"Expected app hash {:?} in `BeginBlock`. Got {:?}",
app_hash, header.app_hash
));
}
ConsensusState::ExecutingBlock {
block_height,
execution_state: BlockExecutionState::BeginBlock,
}
}
_ => {
return Err(format!(
"`BeginBlock` cannot be called after {:?}",
self.state
))
}
};
self.state = new_state;
Ok(())
}
pub fn on_deliver_tx_request(&mut self) -> Result<(), String> {
match self.state {
ConsensusState::ExecutingBlock {
ref mut execution_state,
..
} => execution_state.validate(BlockExecutionState::DeliverTx),
_ => Err(format!(
"`DeliverTx` cannot be called after {:?}",
self.state
)),
}
}
pub fn on_end_block_request(
&mut self,
end_block_request: &RequestEndBlock,
) -> Result<(), String> {
match self.state {
ConsensusState::ExecutingBlock {
ref mut execution_state,
ref block_height,
} => {
let block_height = *block_height;
if block_height != end_block_request.height {
return Err(format!(
"Expected `EndBlock` for height {}. But received for {}",
block_height, end_block_request.height
));
}
execution_state.validate(BlockExecutionState::EndBlock)
}
_ => Err(format!(
"`EndBlock` cannot be called after {:?}",
self.state
)),
}
}
#[inline]
pub fn on_commit_request(&mut self) -> Result<(), String> {
match self.state {
ConsensusState::ExecutingBlock {
ref mut execution_state,
..
} => execution_state.validate(BlockExecutionState::Commit),
_ => Err(format!("`Commit` cannot be called after {:?}", self.state)),
}
}
pub fn on_commit_response(&mut self, commit_response: &ResponseCommit) -> Result<(), String> {
let new_state = match self.state {
ConsensusState::ExecutingBlock {
execution_state: BlockExecutionState::Commit,
block_height,
} => ConsensusState::WaitingForBlock {
block_height: block_height + 1,
app_hash: commit_response.data.clone(),
},
_ => return Err(format!("Received `CommitResponse` after {:?}", self.state)),
};
self.state = new_state;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConsensusState {
NoInfo,
NotInitialized,
InitChain,
WaitingForBlock {
block_height: i64,
app_hash: Vec<u8>,
},
ExecutingBlock {
block_height: i64,
execution_state: BlockExecutionState,
},
}
impl Default for ConsensusState {
fn default() -> Self {
Self::NoInfo
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockExecutionState {
BeginBlock,
DeliverTx,
EndBlock,
Commit,
}
impl BlockExecutionState {
pub fn validate(&mut self, next: Self) -> Result<(), String> {
let is_valid = matches!(
(*self, next),
(Self::BeginBlock, Self::DeliverTx)
| (Self::BeginBlock, Self::EndBlock)
| (Self::DeliverTx, Self::DeliverTx)
| (Self::DeliverTx, Self::EndBlock)
| (Self::EndBlock, Self::Commit)
);
if is_valid {
*self = next;
Ok(())
} else {
Err(format!("{:?} cannot be called after {:?}", next, self))
}
}
}