#![allow(clippy::type_complexity)]
pub use crate::journaled_state::StateLoad;
use crate::{
result::FromStringError, Block, Cfg, Database, Host, JournalTr, LocalContextTr, Transaction,
};
use auto_impl::auto_impl;
use primitives::StorageValue;
use std::string::String;
#[auto_impl(&mut, Box)]
pub trait ContextTr: Host {
type Block: Block;
type Tx: Transaction;
type Cfg: Cfg;
type Db: Database;
type Journal: JournalTr<Database = Self::Db>;
type Chain;
type Local: LocalContextTr;
fn all(
&self,
) -> (
&Self::Block,
&Self::Tx,
&Self::Cfg,
&Self::Db,
&Self::Journal,
&Self::Chain,
&Self::Local,
);
fn all_mut(
&mut self,
) -> (
&Self::Block,
&Self::Tx,
&Self::Cfg,
&mut Self::Journal,
&mut Self::Chain,
&mut Self::Local,
);
fn tx(&self) -> &Self::Tx {
let (_, tx, _, _, _, _, _) = self.all();
tx
}
fn block(&self) -> &Self::Block {
let (block, _, _, _, _, _, _) = self.all();
block
}
fn cfg(&self) -> &Self::Cfg {
let (_, _, cfg, _, _, _, _) = self.all();
cfg
}
fn journal(&self) -> &Self::Journal {
let (_, _, _, _, journal, _, _) = self.all();
journal
}
fn journal_mut(&mut self) -> &mut Self::Journal {
let (_, _, _, journal, _, _) = self.all_mut();
journal
}
fn journal_ref(&self) -> &Self::Journal {
self.journal()
}
fn db(&self) -> &Self::Db {
let (_, _, _, db, _, _, _) = self.all();
db
}
fn db_mut(&mut self) -> &mut Self::Db {
let db = self.journal_mut().db_mut();
db
}
fn db_ref(&self) -> &Self::Db {
self.db()
}
fn chain(&self) -> &Self::Chain {
let (_, _, _, _, _, chain, _) = self.all();
chain
}
fn chain_mut(&mut self) -> &mut Self::Chain {
let (_, _, _, _, chain, _) = self.all_mut();
chain
}
fn chain_ref(&self) -> &Self::Chain {
self.chain()
}
fn local(&self) -> &Self::Local {
let (_, _, _, _, _, _, local) = self.all();
local
}
fn local_mut(&mut self) -> &mut Self::Local {
let (_, _, _, _, _, local) = self.all_mut();
local
}
fn local_ref(&self) -> &Self::Local {
self.local()
}
fn error(&mut self) -> &mut Result<(), ContextError<<Self::Db as Database>::Error>>;
fn tx_journal_mut(&mut self) -> (&Self::Tx, &mut Self::Journal) {
let (_, tx, _, journal, _, _) = self.all_mut();
(tx, journal)
}
fn tx_block_cfg_journal_mut(
&mut self,
) -> (&Self::Tx, &Self::Block, &Self::Cfg, &mut Self::Journal) {
let (block, tx, cfg, journal, _, _) = self.all_mut();
(tx, block, cfg, journal)
}
fn tx_local_mut(&mut self) -> (&Self::Tx, &mut Self::Local) {
let (_, tx, _, _, _, local) = self.all_mut();
(tx, local)
}
#[inline]
fn cfg_journal_mut(&mut self) -> (&Self::Cfg, &mut Self::Journal) {
let (_, _, cfg, journal, _, _) = self.all_mut();
(cfg, journal)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ContextError<DbError> {
Db(DbError),
Custom(String),
}
#[inline]
pub fn take_error<E, DbError>(err: &mut Result<(), ContextError<DbError>>) -> Result<(), E>
where
E: From<DbError> + FromStringError,
{
match core::mem::replace(err, Ok(())) {
Err(ContextError::Db(e)) => Err(e.into()),
Err(ContextError::Custom(e)) => Err(E::from_string(e)),
Ok(()) => Ok(()),
}
}
impl<DbError> FromStringError for ContextError<DbError> {
fn from_string(value: String) -> Self {
Self::Custom(value)
}
}
impl<DbError> From<DbError> for ContextError<DbError> {
fn from(value: DbError) -> Self {
Self::Db(value)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SStoreResult {
pub original_value: StorageValue,
pub present_value: StorageValue,
pub new_value: StorageValue,
}
impl SStoreResult {
#[inline]
pub const fn is_new_eq_present(&self) -> bool {
self.new_value.const_eq(&self.present_value)
}
#[inline]
pub const fn new_values_changes_present(&self) -> bool {
!self.is_new_eq_present()
}
#[inline]
pub const fn have_changed_from_zero(&self) -> bool {
self.is_original_zero() && !self.is_new_zero()
}
#[inline]
pub const fn is_original_eq_present(&self) -> bool {
self.original_value.const_eq(&self.present_value)
}
#[inline]
pub const fn is_original_eq_new(&self) -> bool {
self.original_value.const_eq(&self.new_value)
}
#[inline]
pub const fn is_original_zero(&self) -> bool {
self.original_value.const_is_zero()
}
#[inline]
pub const fn is_present_zero(&self) -> bool {
self.present_value.const_is_zero()
}
#[inline]
pub const fn is_new_zero(&self) -> bool {
self.new_value.const_is_zero()
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SelfDestructResult {
pub had_value: bool,
pub target_exists: bool,
pub previously_destroyed: bool,
}
pub trait ContextSetters: ContextTr {
fn set_tx(&mut self, tx: Self::Tx);
fn set_block(&mut self, block: Self::Block);
}