#![feature(non_exhaustive)]
use oasis_types::{AccountMeta, Address, Event};
pub trait Blockchain {
fn name(&self) -> &str;
fn block(&self, height: usize) -> Option<&dyn Block>;
fn last_block(&self) -> &dyn Block;
fn last_block_mut(&mut self) -> &mut dyn Block;
}
pub trait Block {
fn height(&self) -> u64;
#[allow(clippy::too_many_arguments)]
fn transact(
&mut self,
caller: Address,
callee: Address,
payer: Address,
value: u128,
input: &[u8],
gas: u64,
gas_price: u64,
) -> Box<dyn Receipt>;
fn code_at(&self, addr: &Address) -> Option<&[u8]>;
fn account_meta_at(&self, addr: &Address) -> Option<AccountMeta>;
fn state_at(&self, addr: &Address) -> Option<&dyn KVStore>;
fn events(&self) -> Vec<&Event>;
fn receipts(&self) -> Vec<&dyn Receipt>;
}
pub trait PendingTransaction {
fn address(&self) -> &Address;
fn sender(&self) -> &Address;
fn value(&self) -> u128;
fn input(&self) -> &[u8];
fn create(&mut self, value: u128, code: &[u8]) -> Box<dyn Receipt>;
fn transact(&mut self, callee: Address, value: u128, input: &[u8]) -> Box<dyn Receipt>;
fn ret(&mut self, data: &[u8]);
fn err(&mut self, data: &[u8]);
fn emit(&mut self, topics: &[&[u8]], data: &[u8]);
fn state(&self) -> &dyn KVStore;
fn state_mut(&mut self) -> &mut dyn KVStoreMut;
fn code_at(&self, addr: &Address) -> Option<&[u8]>;
fn account_meta_at(&self, addr: &Address) -> Option<AccountMeta>;
}
pub trait KVStore {
fn contains(&self, key: &[u8]) -> bool;
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
}
pub trait KVStoreMut: KVStore {
fn set(&mut self, key: &[u8], value: &[u8]);
fn remove(&mut self, key: &[u8]);
}
pub trait Receipt {
fn caller(&self) -> &Address;
fn callee(&self) -> &Address;
fn gas_used(&self) -> u64;
fn events(&self) -> Vec<&Event>;
fn outcome(&self) -> TransactionOutcome;
fn output(&self) -> &[u8];
fn reverted(&self) -> bool {
match self.outcome() {
TransactionOutcome::Success => false,
_ => true,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
#[repr(u16)]
pub enum TransactionOutcome {
Success,
InsufficientFunds,
InsufficientGas,
InvalidInput,
InvalidCallee,
Aborted, Fatal,
}
impl TransactionOutcome {
pub fn reverted(self) -> bool {
match self {
TransactionOutcome::Success => false,
_ => true,
}
}
}