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 crate::{
crypto::hash::Digest,
effect::requests::BlockExecutorRequest,
types::{
json_compatibility::ExecutionResult, BlockHash, Deploy, DeployHash, DeployHeader,
FinalizedBlock,
},
};
#[derive(Debug, From)]
pub enum Event {
#[from]
Request(BlockExecutorRequest),
GetDeploysResult {
finalized_block: FinalizedBlock,
deploys: VecDeque<Deploy>,
},
GetParentResult {
finalized_block: FinalizedBlock,
deploys: VecDeque<Deploy>,
parent: Option<(BlockHash, Digest, Digest)>,
},
DeployExecutionResult {
state: Box<State>,
deploy_hash: DeployHash,
deploy_header: DeployHeader,
result: Result<ExecutionResults, RootNotFound>,
},
CommitExecutionEffects {
state: Box<State>,
commit_result: Result<CommitResult, engine_state::Error>,
},
RunStepResult {
state: Box<State>,
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
),
}
}
}
#[derive(Debug)]
pub struct State {
pub finalized_block: FinalizedBlock,
pub remaining_deploys: VecDeque<Deploy>,
pub execution_results: HashMap<DeployHash, (DeployHeader, ExecutionResult)>,
pub state_root_hash: Digest,
}