1#![allow(clippy::type_complexity)]
2pub use crate::journaled_state::StateLoad;
4use crate::{
5 result::FromStringError, Block, Cfg, Database, Host, JournalTr, LocalContextTr, Transaction,
6};
7use auto_impl::auto_impl;
8use primitives::StorageValue;
9use std::string::String;
10
11#[auto_impl(&mut, Box)]
19pub trait ContextTr: Host {
20 type Block: Block;
22 type Tx: Transaction;
24 type Cfg: Cfg;
26 type Db: Database;
28 type Journal: JournalTr<Database = Self::Db>;
30 type Chain;
32 type Local: LocalContextTr;
34
35 fn all(
37 &self,
38 ) -> (
39 &Self::Block,
40 &Self::Tx,
41 &Self::Cfg,
42 &Self::Db,
43 &Self::Journal,
44 &Self::Chain,
45 &Self::Local,
46 );
47
48 fn all_mut(
50 &mut self,
51 ) -> (
52 &Self::Block,
53 &Self::Tx,
54 &Self::Cfg,
55 &mut Self::Journal,
56 &mut Self::Chain,
57 &mut Self::Local,
58 );
59
60 fn tx(&self) -> &Self::Tx {
62 let (_, tx, _, _, _, _, _) = self.all();
63 tx
64 }
65 fn block(&self) -> &Self::Block {
67 let (block, _, _, _, _, _, _) = self.all();
68 block
69 }
70 fn cfg(&self) -> &Self::Cfg {
72 let (_, _, cfg, _, _, _, _) = self.all();
73 cfg
74 }
75 fn journal(&self) -> &Self::Journal {
77 let (_, _, _, _, journal, _, _) = self.all();
78 journal
79 }
80 fn journal_mut(&mut self) -> &mut Self::Journal {
82 let (_, _, _, journal, _, _) = self.all_mut();
83 journal
84 }
85 fn journal_ref(&self) -> &Self::Journal {
87 self.journal()
88 }
89 fn db(&self) -> &Self::Db {
91 let (_, _, _, db, _, _, _) = self.all();
92 db
93 }
94 fn db_mut(&mut self) -> &mut Self::Db {
96 let db = self.journal_mut().db_mut();
97 db
98 }
99 fn db_ref(&self) -> &Self::Db {
101 self.db()
102 }
103 fn chain(&self) -> &Self::Chain {
105 let (_, _, _, _, _, chain, _) = self.all();
106 chain
107 }
108 fn chain_mut(&mut self) -> &mut Self::Chain {
110 let (_, _, _, _, chain, _) = self.all_mut();
111 chain
112 }
113 fn chain_ref(&self) -> &Self::Chain {
115 self.chain()
116 }
117 fn local(&self) -> &Self::Local {
119 let (_, _, _, _, _, _, local) = self.all();
120 local
121 }
122 fn local_mut(&mut self) -> &mut Self::Local {
124 let (_, _, _, _, _, local) = self.all_mut();
125 local
126 }
127 fn local_ref(&self) -> &Self::Local {
129 self.local()
130 }
131 fn error(&mut self) -> &mut Result<(), ContextError<<Self::Db as Database>::Error>>;
133
134 fn tx_journal_mut(&mut self) -> (&Self::Tx, &mut Self::Journal) {
137 let (_, tx, _, journal, _, _) = self.all_mut();
138 (tx, journal)
139 }
140
141 fn tx_block_cfg_journal_mut(
143 &mut self,
144 ) -> (&Self::Tx, &Self::Block, &Self::Cfg, &mut Self::Journal) {
145 let (block, tx, cfg, journal, _, _) = self.all_mut();
146 (tx, block, cfg, journal)
147 }
148
149 fn tx_local_mut(&mut self) -> (&Self::Tx, &mut Self::Local) {
152 let (_, tx, _, _, _, local) = self.all_mut();
153 (tx, local)
154 }
155}
156
157#[derive(Clone, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
159#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
160pub enum ContextError<DbError> {
161 Db(DbError),
163 Custom(String),
165}
166
167impl<DbError> FromStringError for ContextError<DbError> {
168 fn from_string(value: String) -> Self {
169 Self::Custom(value)
170 }
171}
172
173impl<DbError> From<DbError> for ContextError<DbError> {
174 fn from(value: DbError) -> Self {
175 Self::Db(value)
176 }
177}
178
179#[derive(Clone, Debug, Default, PartialEq, Eq)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
182pub struct SStoreResult {
183 pub original_value: StorageValue,
185 pub present_value: StorageValue,
187 pub new_value: StorageValue,
189}
190
191impl SStoreResult {
192 #[inline]
194 pub const fn is_new_eq_present(&self) -> bool {
195 self.new_value.const_eq(&self.present_value)
196 }
197
198 #[inline]
200 pub const fn is_original_eq_present(&self) -> bool {
201 self.original_value.const_eq(&self.present_value)
202 }
203
204 #[inline]
206 pub const fn is_original_eq_new(&self) -> bool {
207 self.original_value.const_eq(&self.new_value)
208 }
209
210 #[inline]
212 pub const fn is_original_zero(&self) -> bool {
213 self.original_value.const_is_zero()
214 }
215
216 #[inline]
218 pub const fn is_present_zero(&self) -> bool {
219 self.present_value.const_is_zero()
220 }
221
222 #[inline]
224 pub const fn is_new_zero(&self) -> bool {
225 self.new_value.const_is_zero()
226 }
227}
228
229#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
233#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
234pub struct SelfDestructResult {
235 pub had_value: bool,
237 pub target_exists: bool,
239 pub previously_destroyed: bool,
241}
242
243pub trait ContextSetters: ContextTr {
245 fn set_tx(&mut self, tx: Self::Tx);
247 fn set_block(&mut self, block: Self::Block);
249}