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
use std::{
collections::{HashMap, VecDeque},
fmt::Display,
};
use derive_more::From;
use casper_execution_engine::{
core::{
engine_state,
engine_state::{step::StepResult, ExecutionResults, RootNotFound},
},
storage::global_state::CommitResult,
};
use casper_types::ExecutionResult;
use crate::{
crypto::hash::Digest,
effect::requests::BlockExecutorRequest,
types::{Block, BlockHash, Deploy, DeployHash, DeployHeader, FinalizedBlock},
};
/// Block executor component event.
#[derive(Debug, From)]
pub enum Event {
/// Indicates that block has already been finalized and executed in the past.
BlockAlreadyExists(Box<Block>),
/// Indicates that a block is not known yet, and needs to be executed.
BlockIsNew(FinalizedBlock),
/// A request made of the Block executor component.
#[from]
Request(BlockExecutorRequest),
/// Received all requested deploys.
GetDeploysResult {
/// The block that needs the deploys for execution.
finalized_block: FinalizedBlock,
/// Contents of deploys. All deploys are expected to be present in the storage component.
deploys: VecDeque<Deploy>,
},
GetParentResult {
/// The block that needs the deploys for execution.
finalized_block: FinalizedBlock,
/// Contents of deploys. All deploys are expected to be present in the storage component.
deploys: VecDeque<Deploy>,
/// Parent of the newly finalized block.
/// If it's the first block after Genesis then `parent` is `None`.
parent: Option<(BlockHash, Digest, Digest)>,
},
/// The result of executing a single deploy.
DeployExecutionResult {
/// State of this request.
state: Box<State>,
/// The ID of the deploy currently being executed.
deploy_hash: DeployHash,
/// The header of the deploy currently being executed.
deploy_header: DeployHeader,
/// Result of deploy execution.
result: Result<ExecutionResults, RootNotFound>,
},
/// The result of committing a single set of transforms after executing a single deploy.
CommitExecutionEffects {
/// State of this request.
state: Box<State>,
/// Commit result for execution request.
commit_result: Result<CommitResult, engine_state::Error>,
},
/// The result of running the step on a switch block.
RunStepResult {
/// State of this request.
state: Box<State>,
/// The result.
result: Result<StepResult, engine_state::Error>,
},
}
impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Event::Request(req) => write!(f, "{}", req),
Event::GetDeploysResult {
finalized_block,
deploys,
} => write!(
f,
"fetch deploys for finalized block with height {} has {} deploys",
finalized_block.height(),
deploys.len()
),
Event::GetParentResult {
finalized_block,
parent,
..
} => write!(
f,
"found_parent={} for finalized block with height {}",
parent.is_some(),
finalized_block.height()
),
Event::DeployExecutionResult {
state,
deploy_hash,
result: Ok(_),
..
} => write!(
f,
"execution result for {} of finalized block with height {} with \
pre-state hash {}: success",
deploy_hash,
state.finalized_block.height(),
state.state_root_hash
),
Event::DeployExecutionResult {
state,
deploy_hash,
result: Err(_),
..
} => write!(
f,
"execution result for {} of finalized block with height {} with \
pre-state hash {}: root not found",
deploy_hash,
state.finalized_block.height(),
state.state_root_hash
),
Event::CommitExecutionEffects {
state,
commit_result: Ok(CommitResult::Success { state_root, .. }),
} => write!(
f,
"commit execution effects of finalized block with height {} with \
pre-state hash {}: success with post-state hash {}",
state.finalized_block.height(),
state.state_root_hash,
state_root,
),
Event::CommitExecutionEffects {
state,
commit_result,
} => write!(
f,
"commit execution effects of finalized block with height {} with \
pre-state hash {}: failed {:?}",
state.finalized_block.height(),
state.state_root_hash,
commit_result,
),
Event::RunStepResult { state, result } => write!(
f,
"result of running the step after finalized block with height {} \
with pre-state hash {}: {:?}",
state.finalized_block.height(),
state.state_root_hash,
result
),
Event::BlockAlreadyExists(block) => {
write!(f, "Block at height {} was executed before", block.height())
}
Event::BlockIsNew(fb) => write!(f, "Block at height {} is new", fb.height(),),
}
}
}
/// Holds the state of an ongoing execute-commit cycle spawned from a given `Event::Request`.
#[derive(Debug)]
pub struct State {
pub finalized_block: FinalizedBlock,
/// Deploys which have still to be executed.
pub remaining_deploys: VecDeque<Deploy>,
/// A collection of results of executing the deploys.
pub execution_results: HashMap<DeployHash, (DeployHeader, ExecutionResult)>,
/// Current state root hash of global storage. Is initialized with the parent block's
/// state hash, and is updated after each commit.
pub state_root_hash: Digest,
}