use std::fmt::{self, Display, Formatter};
use serde::Serialize;
use super::Source;
use crate::{
components::deploy_acceptor::Error,
effect::{announcements::RpcServerAnnouncement, Responder},
types::{Block, Deploy, NodeId, Timestamp},
};
use casper_hashing::Digest;
use casper_types::{
account::{Account, AccountHash},
Contract, ContractHash, ContractPackage, ContractPackageHash, ContractVersion, U512,
};
#[derive(Debug, Serialize)]
pub(crate) struct EventMetadata {
pub(super) deploy: Box<Deploy>,
pub(super) source: Source<NodeId>,
pub(super) maybe_responder: Option<Responder<Result<(), Error>>>,
}
impl EventMetadata {
pub(super) fn new(
deploy: Box<Deploy>,
source: Source<NodeId>,
maybe_responder: Option<Responder<Result<(), Error>>>,
) -> Self {
EventMetadata {
deploy,
source,
maybe_responder,
}
}
}
#[derive(Debug, Serialize)]
pub(crate) enum Event {
Accept {
deploy: Box<Deploy>,
source: Source<NodeId>,
maybe_responder: Option<Responder<Result<(), Error>>>,
},
PutToStorageResult {
event_metadata: EventMetadata,
is_new: bool,
verification_start_timestamp: Timestamp,
},
GetBlockResult {
event_metadata: EventMetadata,
maybe_block: Box<Option<Block>>,
verification_start_timestamp: Timestamp,
},
GetAccountResult {
event_metadata: EventMetadata,
prestate_hash: Digest,
maybe_account: Option<Account>,
verification_start_timestamp: Timestamp,
},
GetBalanceResult {
event_metadata: EventMetadata,
prestate_hash: Digest,
maybe_balance_value: Option<U512>,
account_hash: AccountHash,
verification_start_timestamp: Timestamp,
},
GetContractResult {
event_metadata: EventMetadata,
prestate_hash: Digest,
is_payment: bool,
contract_hash: ContractHash,
maybe_contract: Option<Contract>,
verification_start_timestamp: Timestamp,
},
GetContractPackageResult {
event_metadata: EventMetadata,
prestate_hash: Digest,
is_payment: bool,
contract_package_hash: ContractPackageHash,
maybe_package_version: Option<ContractVersion>,
maybe_contract_package: Option<ContractPackage>,
verification_start_timestamp: Timestamp,
},
VerifyDeployCryptographicValidity {
event_metadata: EventMetadata,
verification_start_timestamp: Timestamp,
},
}
impl From<RpcServerAnnouncement> for Event {
fn from(announcement: RpcServerAnnouncement) -> Self {
match announcement {
RpcServerAnnouncement::DeployReceived { deploy, responder } => Event::Accept {
deploy,
source: Source::<NodeId>::Client,
maybe_responder: responder,
},
}
}
}
impl Display for Event {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Event::Accept { deploy, source, .. } => {
write!(formatter, "accept {} from {}", deploy.id(), source)
}
Event::PutToStorageResult {
event_metadata,
is_new,
..
} => {
if *is_new {
write!(
formatter,
"put new {} to storage",
event_metadata.deploy.id()
)
} else {
write!(
formatter,
"had already stored {}",
event_metadata.deploy.id()
)
}
}
Event::GetBlockResult { event_metadata, .. } => {
write!(
formatter,
"received highest block from storage to validate deploy with hash: {}.",
event_metadata.deploy.id()
)
}
Event::GetAccountResult { event_metadata, .. } => {
write!(
formatter,
"verifying account to validate deploy with hash {}.",
event_metadata.deploy.id()
)
}
Event::GetBalanceResult { event_metadata, .. } => {
write!(
formatter,
"verifying account balance to validate deploy with hash {}.",
event_metadata.deploy.id()
)
}
Event::GetContractResult {
event_metadata,
prestate_hash,
..
} => {
write!(
formatter,
"verifying contract to validate deploy with hash {} with state hash: {}.",
event_metadata.deploy.id(),
prestate_hash
)
}
Event::GetContractPackageResult {
event_metadata,
prestate_hash,
..
} => {
write!(
formatter,
"verifying contract package to validate deploy with hash {} with state hash: {}.",
event_metadata.deploy.id(),
prestate_hash
)
}
Event::VerifyDeployCryptographicValidity { event_metadata, .. } => {
write!(
formatter,
"verifying deploy cryptographic validity for deploy with hash {}.",
event_metadata.deploy.id(),
)
}
}
}
}