use std::collections::BTreeSet;
use casper_execution_engine::engine_state::{BlockInfo, InvalidRequest, WasmV1Request};
use casper_types::{
account::AccountHash, Deploy, DeployHash, ExecutableDeployItem, Gas, InitiatorAddr,
TransactionHash,
};
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DeployItem {
pub address: AccountHash,
pub session: ExecutableDeployItem,
pub payment: ExecutableDeployItem,
pub gas_price: u8,
pub authorization_keys: BTreeSet<AccountHash>,
pub deploy_hash: DeployHash,
}
impl DeployItem {
pub fn new(
address: AccountHash,
session: ExecutableDeployItem,
payment: ExecutableDeployItem,
gas_price: u8,
authorization_keys: BTreeSet<AccountHash>,
deploy_hash: DeployHash,
) -> Self {
DeployItem {
address,
session,
payment,
gas_price,
authorization_keys,
deploy_hash,
}
}
pub fn is_native_transfer(&self) -> bool {
matches!(self.session, ExecutableDeployItem::Transfer { .. })
}
pub fn new_session_from_deploy_item(
&self,
block_info: BlockInfo,
gas_limit: Gas,
) -> Result<WasmV1Request, InvalidRequest> {
let address = &self.address;
let session = &self.session;
let authorization_keys = &self.authorization_keys;
let deploy_hash = &self.deploy_hash;
let transaction_hash = TransactionHash::Deploy(*deploy_hash);
let initiator_addr = InitiatorAddr::AccountHash(*address);
let authorization_keys = authorization_keys.clone();
WasmV1Request::new_from_executable_deploy_item(
block_info,
gas_limit,
transaction_hash,
initiator_addr,
authorization_keys,
session,
)
}
pub fn new_custom_payment_from_deploy_item(
&self,
block_info: BlockInfo,
gas_limit: Gas,
) -> Result<WasmV1Request, InvalidRequest> {
let address = &self.address;
let payment = &self.payment;
let authorization_keys = &self.authorization_keys;
let deploy_hash = &self.deploy_hash;
let transaction_hash = TransactionHash::Deploy(*deploy_hash);
let initiator_addr = InitiatorAddr::AccountHash(*address);
let authorization_keys = authorization_keys.clone();
WasmV1Request::new_payment_from_executable_deploy_item(
block_info,
gas_limit,
transaction_hash,
initiator_addr,
authorization_keys,
payment,
)
}
}
impl From<Deploy> for DeployItem {
fn from(deploy: Deploy) -> Self {
let address = deploy.header().account().to_account_hash();
let authorization_keys = deploy
.approvals()
.iter()
.map(|approval| approval.signer().to_account_hash())
.collect();
DeployItem::new(
address,
deploy.session().clone(),
deploy.payment().clone(),
deploy.header().gas_price() as u8,
authorization_keys,
DeployHash::new(*deploy.hash().inner()),
)
}
}