use std::iter::Iterator;
use derive_more::{Display, From};
use crate::fixed_codec::FixedCodec;
use crate::types::{Address, Epoch, Hash, MerkleRoot, Receipt, ServiceContext, SignedTransaction};
use crate::{ProtocolError, ProtocolErrorKind, ProtocolResult};
#[derive(Debug, Display, From)]
pub enum BindingMacroError {
#[display(fmt = "service {:?} method {:?} was not found", service, method)]
NotFoundMethod { service: String, method: String },
#[display(fmt = "Parsing payload to json failed {:?}", _0)]
JsonParse(serde_json::Error),
}
impl std::error::Error for BindingMacroError {}
impl From<BindingMacroError> for ProtocolError {
fn from(err: BindingMacroError) -> ProtocolError {
ProtocolError::new(ProtocolErrorKind::BindingMacro, Box::new(err))
}
}
pub trait ServiceMapping: Send + Sync {
fn get_service<SDK: 'static + ServiceSDK>(
&self,
name: &str,
sdk: SDK,
) -> ProtocolResult<Box<dyn Service>>;
fn list_service_name(&self) -> Vec<String>;
}
pub trait ServiceState {
fn get<Key: FixedCodec, Ret: FixedCodec>(&self, key: &Key) -> ProtocolResult<Option<Ret>>;
fn contains<Key: FixedCodec>(&self, key: &Key) -> ProtocolResult<bool>;
fn insert<Key: FixedCodec, Value: FixedCodec>(
&mut self,
key: Key,
value: Value,
) -> ProtocolResult<()>;
fn get_account_value<Key: FixedCodec, Ret: FixedCodec>(
&self,
address: &Address,
key: &Key,
) -> ProtocolResult<Option<Ret>>;
fn set_account_value<Key: FixedCodec, Val: FixedCodec>(
&mut self,
address: &Address,
key: Key,
val: Val,
) -> ProtocolResult<()>;
fn revert_cache(&mut self) -> ProtocolResult<()>;
fn stash(&mut self) -> ProtocolResult<()>;
fn commit(&mut self) -> ProtocolResult<MerkleRoot>;
}
pub trait ChainQuerier {
fn get_transaction_by_hash(&self, tx_hash: &Hash) -> ProtocolResult<Option<SignedTransaction>>;
fn get_epoch_by_epoch_id(&self, epoch_id: Option<u64>) -> ProtocolResult<Option<Epoch>>;
fn get_receipt_by_hash(&self, tx_hash: &Hash) -> ProtocolResult<Option<Receipt>>;
}
pub trait AdmissionControl {
fn next<SDK: ServiceSDK>(&self, ctx: ServiceContext, sdk: SDK) -> ProtocolResult<()>;
}
pub trait Service {
fn hook_before_(&mut self) -> ProtocolResult<()> {
Ok(())
}
fn hook_after_(&mut self) -> ProtocolResult<()> {
Ok(())
}
fn write_(&mut self, ctx: ServiceContext) -> ProtocolResult<String>;
fn read_(&self, ctx: ServiceContext) -> ProtocolResult<String>;
}
pub trait ServiceSDK {
fn alloc_or_recover_map<Key: 'static + FixedCodec + PartialEq, Val: 'static + FixedCodec>(
&mut self,
var_name: &str,
) -> ProtocolResult<Box<dyn StoreMap<Key, Val>>>;
fn alloc_or_recover_array<Elm: 'static + FixedCodec>(
&mut self,
var_name: &str,
) -> ProtocolResult<Box<dyn StoreArray<Elm>>>;
fn alloc_or_recover_uint64(&mut self, var_name: &str) -> ProtocolResult<Box<dyn StoreUint64>>;
fn alloc_or_recover_string(&mut self, var_name: &str) -> ProtocolResult<Box<dyn StoreString>>;
fn alloc_or_recover_bool(&mut self, var_name: &str) -> ProtocolResult<Box<dyn StoreBool>>;
fn get_value<Key: FixedCodec, Ret: FixedCodec>(&self, key: &Key)
-> ProtocolResult<Option<Ret>>;
fn set_value<Key: FixedCodec, Val: FixedCodec>(
&mut self,
key: Key,
val: Val,
) -> ProtocolResult<()>;
fn get_account_value<Key: FixedCodec, Ret: FixedCodec>(
&self,
address: &Address,
key: &Key,
) -> ProtocolResult<Option<Ret>>;
fn set_account_value<Key: FixedCodec, Val: FixedCodec>(
&mut self,
address: &Address,
key: Key,
val: Val,
) -> ProtocolResult<()>;
fn get_transaction_by_hash(&self, tx_hash: &Hash) -> ProtocolResult<Option<SignedTransaction>>;
fn get_epoch_by_epoch_id(&self, epoch_id: Option<u64>) -> ProtocolResult<Option<Epoch>>;
fn get_receipt_by_hash(&self, tx_hash: &Hash) -> ProtocolResult<Option<Receipt>>;
fn read(&self, service: &str, method: &str, payload: &str) -> ProtocolResult<&str>;
fn write(&mut self, service: &str, method: &str, payload: &str) -> ProtocolResult<&str>;
}
pub trait StoreMap<Key: FixedCodec + PartialEq, Value: FixedCodec> {
fn get(&self, key: &Key) -> ProtocolResult<Value>;
fn contains(&self, key: &Key) -> ProtocolResult<bool>;
fn insert(&mut self, key: Key, value: Value) -> ProtocolResult<()>;
fn remove(&mut self, key: &Key) -> ProtocolResult<()>;
fn len(&self) -> ProtocolResult<u32>;
fn is_empty(&self) -> ProtocolResult<bool>;
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (&Key, Value)> + 'a>;
}
pub trait StoreArray<Elm: FixedCodec> {
fn get(&self, index: u32) -> ProtocolResult<Elm>;
fn push(&mut self, element: Elm) -> ProtocolResult<()>;
fn remove(&mut self, index: u32) -> ProtocolResult<()>;
fn len(&self) -> ProtocolResult<u32>;
fn is_empty(&self) -> ProtocolResult<bool>;
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (u32, Elm)> + 'a>;
}
pub trait StoreUint64 {
fn get(&self) -> ProtocolResult<u64>;
fn set(&mut self, val: u64) -> ProtocolResult<()>;
fn add(&mut self, val: u64) -> ProtocolResult<()>;
fn sub(&mut self, val: u64) -> ProtocolResult<()>;
fn mul(&mut self, val: u64) -> ProtocolResult<()>;
fn pow(&mut self, val: u32) -> ProtocolResult<()>;
fn div(&mut self, val: u64) -> ProtocolResult<()>;
fn rem(&mut self, val: u64) -> ProtocolResult<()>;
}
pub trait StoreString {
fn get(&self) -> ProtocolResult<String>;
fn set(&mut self, val: &str) -> ProtocolResult<()>;
fn len(&self) -> ProtocolResult<u32>;
fn is_empty(&self) -> ProtocolResult<bool>;
}
pub trait StoreBool {
fn get(&self) -> ProtocolResult<bool>;
fn set(&mut self, b: bool) -> ProtocolResult<()>;
}