use std::convert::TryInto;
use parity_wasm::elements::Module;
use casper_types::{
account::AccountHash, CLValue, Contract, ContractHash, ContractPackage, ContractPackageHash,
ContractWasm, ContractWasmHash, Key,
};
use crate::{
core::{execution, tracking_copy::TrackingCopy},
shared::{
account::Account,
motes::Motes,
newtypes::CorrelationId,
stored_value::StoredValue,
wasm,
wasm_prep::{self, Preprocessor},
TypeMismatch,
},
storage::{global_state::StateReader, trie::merkle_proof::TrieMerkleProof},
};
pub trait TrackingCopyExt<R> {
type Error;
fn get_account(
&mut self,
correlation_id: CorrelationId,
account_hash: AccountHash,
) -> Result<Account, Self::Error>;
fn read_account(
&mut self,
correlation_id: CorrelationId,
account_hash: AccountHash,
) -> Result<Account, Self::Error>;
fn get_purse_balance_key(
&mut self,
correlation_id: CorrelationId,
purse_key: Key,
) -> Result<Key, Self::Error>;
fn get_purse_balance(
&mut self,
correlation_id: CorrelationId,
balance_key: Key,
) -> Result<Motes, Self::Error>;
fn get_purse_balance_key_with_proof(
&self,
correlation_id: CorrelationId,
purse_key: Key,
) -> Result<(Key, TrieMerkleProof<Key, StoredValue>), Self::Error>;
fn get_purse_balance_with_proof(
&self,
correlation_id: CorrelationId,
balance_key: Key,
) -> Result<(Motes, TrieMerkleProof<Key, StoredValue>), Self::Error>;
fn get_contract_wasm(
&mut self,
correlation_id: CorrelationId,
contract_wasm_hash: ContractWasmHash,
) -> Result<ContractWasm, Self::Error>;
fn get_contract(
&mut self,
correlation_id: CorrelationId,
contract_hash: ContractHash,
) -> Result<Contract, Self::Error>;
fn get_contract_package(
&mut self,
correlation_id: CorrelationId,
contract_package_hash: ContractPackageHash,
) -> Result<ContractPackage, Self::Error>;
fn get_system_module(
&mut self,
correlation_id: CorrelationId,
contract_wasm_hash: ContractWasmHash,
use_system_contracts: bool,
preprocessor: &Preprocessor,
) -> Result<Module, Self::Error>;
}
impl<R> TrackingCopyExt<R> for TrackingCopy<R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
type Error = execution::Error;
fn get_account(
&mut self,
correlation_id: CorrelationId,
account_hash: AccountHash,
) -> Result<Account, Self::Error> {
let account_key = Key::Account(account_hash);
match self.get(correlation_id, &account_key).map_err(Into::into)? {
Some(StoredValue::Account(account)) => Ok(account),
Some(other) => Err(execution::Error::TypeMismatch(TypeMismatch::new(
"Account".to_string(),
other.type_name(),
))),
None => Err(execution::Error::KeyNotFound(account_key)),
}
}
fn read_account(
&mut self,
correlation_id: CorrelationId,
account_hash: AccountHash,
) -> Result<Account, Self::Error> {
let account_key = Key::Account(account_hash);
match self
.read(correlation_id, &account_key)
.map_err(Into::into)?
{
Some(StoredValue::Account(account)) => Ok(account),
Some(other) => Err(execution::Error::TypeMismatch(TypeMismatch::new(
"Account".to_string(),
other.type_name(),
))),
None => Err(execution::Error::KeyNotFound(account_key)),
}
}
fn get_purse_balance_key(
&mut self,
correlation_id: CorrelationId,
purse_key: Key,
) -> Result<Key, Self::Error> {
let balance_key: Key = purse_key
.uref_to_hash()
.ok_or(execution::Error::KeyIsNotAURef(purse_key))?;
let stored_value: StoredValue = self
.read(correlation_id, &balance_key)
.map_err(Into::into)?
.ok_or(execution::Error::KeyNotFound(purse_key))?;
let cl_value: CLValue = stored_value
.try_into()
.map_err(execution::Error::TypeMismatch)?;
let balance_key: Key = cl_value.into_t()?;
Ok(balance_key)
}
fn get_purse_balance(
&mut self,
correlation_id: CorrelationId,
key: Key,
) -> Result<Motes, Self::Error> {
let stored_value: StoredValue = self
.read(correlation_id, &key)
.map_err(Into::into)?
.ok_or(execution::Error::KeyNotFound(key))?;
let cl_value: CLValue = stored_value
.try_into()
.map_err(execution::Error::TypeMismatch)?;
let balance = Motes::new(cl_value.into_t()?);
Ok(balance)
}
fn get_purse_balance_key_with_proof(
&self,
correlation_id: CorrelationId,
purse_key: Key,
) -> Result<(Key, TrieMerkleProof<Key, StoredValue>), Self::Error> {
let balance_key: Key = purse_key
.uref_to_hash()
.ok_or(execution::Error::KeyIsNotAURef(purse_key))?;
let proof: TrieMerkleProof<Key, StoredValue> = self
.read_with_proof(correlation_id, &balance_key) .map_err(Into::into)?
.ok_or(execution::Error::KeyNotFound(purse_key))?;
let stored_value_ref: &StoredValue = proof.value();
let cl_value: CLValue = stored_value_ref
.to_owned()
.try_into()
.map_err(execution::Error::TypeMismatch)?;
let balance_key: Key = cl_value.into_t()?;
Ok((balance_key, proof))
}
fn get_purse_balance_with_proof(
&self,
correlation_id: CorrelationId,
key: Key,
) -> Result<(Motes, TrieMerkleProof<Key, StoredValue>), Self::Error> {
let proof: TrieMerkleProof<Key, StoredValue> = self
.read_with_proof(correlation_id, &key.normalize())
.map_err(Into::into)?
.ok_or(execution::Error::KeyNotFound(key))?;
let cl_value: CLValue = proof
.value()
.to_owned()
.try_into()
.map_err(execution::Error::TypeMismatch)?;
let balance = Motes::new(cl_value.into_t()?);
Ok((balance, proof))
}
fn get_contract_wasm(
&mut self,
correlation_id: CorrelationId,
contract_wasm_hash: ContractWasmHash,
) -> Result<ContractWasm, Self::Error> {
let key = contract_wasm_hash.into();
match self.get(correlation_id, &key).map_err(Into::into)? {
Some(StoredValue::ContractWasm(contract_wasm)) => Ok(contract_wasm),
Some(other) => Err(execution::Error::TypeMismatch(TypeMismatch::new(
"ContractHeader".to_string(),
other.type_name(),
))),
None => Err(execution::Error::KeyNotFound(key)),
}
}
fn get_contract(
&mut self,
correlation_id: CorrelationId,
contract_hash: ContractHash,
) -> Result<Contract, Self::Error> {
let key = contract_hash.into();
match self.get(correlation_id, &key).map_err(Into::into)? {
Some(StoredValue::Contract(contract)) => Ok(contract),
Some(other) => Err(execution::Error::TypeMismatch(TypeMismatch::new(
"ContractHeader".to_string(),
other.type_name(),
))),
None => Err(execution::Error::KeyNotFound(key)),
}
}
fn get_contract_package(
&mut self,
correlation_id: CorrelationId,
contract_package_hash: ContractPackageHash,
) -> Result<ContractPackage, Self::Error> {
let key = contract_package_hash.into();
match self.get(correlation_id, &key).map_err(Into::into)? {
Some(StoredValue::ContractPackage(contract_package)) => Ok(contract_package),
Some(other) => Err(execution::Error::TypeMismatch(TypeMismatch::new(
"ContractPackage".to_string(),
other.type_name(),
))),
None => Err(execution::Error::KeyNotFound(key)),
}
}
fn get_system_module(
&mut self,
correlation_id: CorrelationId,
contract_wasm_hash: ContractWasmHash,
use_system_contracts: bool,
preprocessor: &Preprocessor,
) -> Result<Module, Self::Error> {
match {
if use_system_contracts {
let contract_wasm = match self.get_contract_wasm(correlation_id, contract_wasm_hash)
{
Ok(contract_wasm) => contract_wasm,
Err(error) => {
return Err(error);
}
};
wasm_prep::deserialize(contract_wasm.bytes())
} else {
wasm::do_nothing_module(preprocessor)
}
} {
Ok(module) => Ok(module),
Err(error) => Err(error.into()),
}
}
}