#[cfg(not(feature = "std"))]
use crate::vec::Vec;
use crate::{
types::{LogError, StateError},
AccountSignatures, CallContractResult, CheckAccountSignatureResult, EntryRaw, ExchangeRates,
HashKeccak256, HashSha2256, HashSha3256, Key, OccupiedEntryRaw, PublicKeyEcdsaSecp256k1,
PublicKeyEd25519, QueryAccountBalanceResult, QueryAccountPublicKeysResult,
QueryContractBalanceResult, ReadOnlyCallContractResult, SignatureEcdsaSecp256k1,
SignatureEd25519, StateBuilder, TransferResult, UpgradeResult, VacantEntryRaw,
};
use concordium_contracts_common::*;
pub trait HasParameter: Read + Seek + HasSize {}
pub trait HasCallResponse: Read {
fn size(&self) -> u32;
}
pub trait HasChainMetadata {
fn slot_time(&self) -> SlotTime;
#[inline(always)]
fn block_time(&self) -> Timestamp { self.slot_time() }
}
pub trait HasPolicy: Sized {
type Iterator: Iterator<Item = (AttributeTag, AttributeValue)>;
fn identity_provider(&self) -> IdentityProvider;
fn created_at(&self) -> Timestamp;
fn valid_to(&self) -> Timestamp;
fn next_item(&mut self, buf: &mut [u8; 31]) -> Option<(AttributeTag, u8)>;
fn attributes(&self) -> Self::Iterator;
}
pub trait HasCommonData {
type PolicyType: HasPolicy;
type MetadataType: HasChainMetadata;
type ParamType: HasParameter + Read;
type PolicyIteratorType: ExactSizeIterator<Item = Self::PolicyType>;
fn policies(&self) -> Self::PolicyIteratorType;
fn metadata(&self) -> &Self::MetadataType;
fn parameter_cursor(&self) -> Self::ParamType;
}
pub trait HasInitContext<Error: Default = ()>: HasCommonData {
type InitData;
fn open(data: Self::InitData) -> Self;
fn init_origin(&self) -> AccountAddress;
}
pub trait HasReceiveContext<Error: Default = ()>: HasCommonData {
type ReceiveData;
fn open(data: Self::ReceiveData) -> Self;
fn invoker(&self) -> AccountAddress;
fn self_address(&self) -> ContractAddress;
fn sender(&self) -> Address;
fn owner(&self) -> AccountAddress;
fn named_entrypoint(&self) -> OwnedEntrypointName;
}
pub trait HasStateEntry
where
Self: Read,
Self: Write<Err = Self::Error>,
Self: Seek<Err = Self::Error>, {
type StateEntryData;
type StateEntryKey;
type Error: Default;
fn move_to_start(&mut self);
fn size(&self) -> Result<u32, Self::Error>;
fn truncate(&mut self, new_size: u32) -> Result<(), Self::Error>;
fn reserve(&mut self, len: u32) -> Result<(), Self::Error> {
if self.size()? < len {
self.resize(len)
} else {
Ok(())
}
}
fn get_key(&self) -> &[u8];
fn resize(&mut self, new_size: u32) -> Result<(), Self::Error>;
}
pub trait HasStateApi: Clone {
type EntryType: HasStateEntry;
type IterType: Iterator<Item = Self::EntryType>;
fn create_entry(&mut self, key: &[u8]) -> Result<Self::EntryType, StateError>;
fn lookup_entry(&self, key: &[u8]) -> Option<Self::EntryType>;
fn entry<K: AsRef<[u8]> + Into<Key>>(&mut self, key: K) -> EntryRaw<Self> {
match self.lookup_entry(key.as_ref()) {
Some(e) => EntryRaw::Occupied(OccupiedEntryRaw::new(e)),
None => EntryRaw::Vacant(VacantEntryRaw::new(key.into(), self.clone())),
}
}
fn delete_entry(&mut self, key: Self::EntryType) -> Result<(), StateError>;
fn delete_prefix(&mut self, prefix: &[u8]) -> Result<bool, StateError>;
fn iterator(&self, prefix: &[u8]) -> Result<Self::IterType, StateError>;
fn delete_iterator(&mut self, iter: Self::IterType);
fn read_root<A: DeserialWithState<Self>>(&self) -> ParseResult<A> {
A::deserial_with_state(self, &mut self.lookup_entry(&[]).ok_or(ParseError {})?)
}
fn write_root<A: Serial>(&mut self, new_state: &A) {
new_state.serial(&mut self.create_entry(&[]).unwrap_abort()).unwrap_abort()
}
}
pub trait HasHost<State>: Sized {
type StateApiType: HasStateApi;
type ReturnValueType: HasCallResponse + crate::fmt::Debug;
fn invoke_transfer(&self, receiver: &AccountAddress, amount: Amount) -> TransferResult;
fn invoke_contract_raw(
&mut self,
to: &ContractAddress,
parameter: Parameter,
method: EntrypointName,
amount: Amount,
) -> CallContractResult<Self::ReturnValueType>;
fn invoke_contract<P: Serial>(
&mut self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName,
amount: Amount,
) -> CallContractResult<Self::ReturnValueType> {
let param = to_bytes(parameter);
self.invoke_contract_raw(to, Parameter::new_unchecked(¶m), method, amount)
}
fn upgrade(&mut self, module: ModuleReference) -> UpgradeResult;
fn invoke_contract_raw_read_only(
&self,
to: &ContractAddress,
parameter: Parameter<'_>,
method: EntrypointName<'_>,
amount: Amount,
) -> ReadOnlyCallContractResult<Self::ReturnValueType>;
fn invoke_contract_read_only<P: Serial>(
&self,
to: &ContractAddress,
parameter: &P,
method: EntrypointName,
amount: Amount,
) -> ReadOnlyCallContractResult<Self::ReturnValueType> {
let param = to_bytes(parameter);
self.invoke_contract_raw_read_only(to, Parameter::new_unchecked(¶m), method, amount)
}
fn exchange_rates(&self) -> ExchangeRates;
fn account_balance(&self, address: AccountAddress) -> QueryAccountBalanceResult;
fn contract_balance(&self, address: ContractAddress) -> QueryContractBalanceResult;
fn account_public_keys(&self, address: AccountAddress) -> QueryAccountPublicKeysResult;
fn check_account_signature(
&self,
address: AccountAddress,
signatures: &AccountSignatures,
data: &[u8],
) -> CheckAccountSignatureResult;
fn state(&self) -> &State;
fn state_mut(&mut self) -> &mut State;
fn commit_state(&mut self);
fn state_builder(&mut self) -> &mut StateBuilder<Self::StateApiType>;
fn state_and_builder(&mut self) -> (&mut State, &mut StateBuilder<Self::StateApiType>);
fn self_balance(&self) -> Amount;
}
pub trait Deletable {
fn delete(self);
}
pub trait HasLogger {
fn init() -> Self;
fn log_raw(&mut self, event: &[u8]) -> Result<(), LogError>;
#[inline(always)]
fn log<S: Serial>(&mut self, event: &S) -> Result<(), LogError> {
let mut out = Vec::new();
if event.serial(&mut out).is_err() {
crate::trap(); }
self.log_raw(&out)
}
}
pub trait HasCryptoPrimitives {
fn verify_ed25519_signature(
&self,
public_key: PublicKeyEd25519,
signature: SignatureEd25519,
message: &[u8],
) -> bool;
fn verify_ecdsa_secp256k1_signature(
&self,
public_key: PublicKeyEcdsaSecp256k1,
signature: SignatureEcdsaSecp256k1,
message_hash: [u8; 32],
) -> bool;
fn hash_sha2_256(&self, data: &[u8]) -> HashSha2256;
fn hash_sha3_256(&self, data: &[u8]) -> HashSha3256;
fn hash_keccak_256(&self, data: &[u8]) -> HashKeccak256;
}
pub trait UnwrapAbort {
type Unwrap;
fn unwrap_abort(self) -> Self::Unwrap;
}
pub trait ExpectReport {
type Unwrap;
fn expect_report(self, msg: &str) -> Self::Unwrap;
}
pub trait ExpectErrReport {
type Unwrap;
fn expect_err_report(self, msg: &str) -> Self::Unwrap;
}
pub trait ExpectNoneReport {
fn expect_none_report(self, msg: &str);
}
pub trait DeserialWithState<S>: Sized
where
S: HasStateApi, {
fn deserial_with_state<R: Read>(state: &S, source: &mut R) -> ParseResult<Self>;
}
pub trait DeserialCtxWithState<S>: Sized
where
S: HasStateApi, {
fn deserial_ctx_with_state<R: Read>(
size_length: schema::SizeLength,
ensure_ordered: bool,
state: &S,
source: &mut R,
) -> ParseResult<Self>;
}