-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathlib.rs
150 lines (123 loc) · 4.23 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use core::any::Any;
pub mod api;
pub mod macros;
pub use frame_metadata::RuntimeMetadataPrefixed;
pub use frame_support::weights::Weight;
use frame_support::{
sp_runtime::traits::Dispatchable,
traits::fungible::Inspect,
};
use frame_system::{
pallet_prelude::{
BlockNumberFor,
OriginFor,
},
EventRecord,
};
pub use macros::{
BlockBuilder,
DefaultSandbox,
};
use pallet_revive::{
ContractResult,
ExecReturnValue,
InstantiateReturnValue,
};
/// Export pallets that are used in [`crate::create_sandbox`]
pub use {
frame_support::sp_runtime::testing::H256,
frame_support::{
self,
sp_runtime::{
AccountId32,
DispatchError,
},
},
frame_system,
pallet_balances,
pallet_revive,
pallet_timestamp,
paste,
sp_core::crypto::Ss58Codec,
sp_externalities::{
self,
Extension,
},
sp_io::TestExternalities,
};
/// A snapshot of the storage.
#[derive(Clone, Debug)]
pub struct Snapshot {
/// The storage raw key-value pairs.
pub storage: RawStorage,
/// The storage root hash.
pub storage_root: StorageRoot,
}
pub type RawStorage = Vec<(Vec<u8>, (Vec<u8>, i32))>;
pub type StorageRoot = H256;
/// Alias for the balance type.
type BalanceOf<R> =
<<R as pallet_revive::Config>::Currency as Inspect<AccountIdFor<R>>>::Balance;
/// Alias for the account ID type.
pub type AccountIdFor<R> = <R as frame_system::Config>::AccountId;
/// Alias for the runtime call type.
pub type RuntimeCall<R> = <R as frame_system::Config>::RuntimeCall;
/// Alias for the event record type.
pub type EventRecordOf<Runtime> = EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<Runtime as frame_system::Config>::Hash,
>;
/// Alias for the contract instantiate result.
pub type ContractInstantiateResultFor<Runtime> =
ContractResult<OriginFor<Runtime>, BalanceOf<Runtime>, EventRecordOf<Runtime>>;
pub type ContractResultFor<Runtime> =
ContractResult<Runtime, BalanceOf<Runtime>, EventRecordOf<Runtime>>;
pub type ContractResultInstantiate<Runtime> =
ContractResult<InstantiateReturnValue, BalanceOf<Runtime>, EventRecordOf<Runtime>>;
/// Alias for the contract exec result.
pub type ContractExecResultFor<Runtime> =
ContractResult<ExecReturnValue, BalanceOf<Runtime>, EventRecordOf<Runtime>>;
/// Alias for the `map_account` result.
pub type MapAccountResultFor = Result<(), DispatchError>;
/// Alias for the runtime of a sandbox.
pub type RuntimeOf<S> = <S as Sandbox>::Runtime;
/// Alias for the runtime event of a sandbox.
pub type RuntimeEventOf<S> = <RuntimeOf<S> as frame_system::Config>::RuntimeEvent;
/// Sandbox defines the API of a sandboxed runtime.
pub trait Sandbox {
/// The runtime associated with the sandbox.
type Runtime: frame_system::Config;
/// Execute the given externalities.
fn execute_with<T>(&mut self, execute: impl FnOnce() -> T) -> T;
/// Dry run an action without modifying the storage.
fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T;
/// Register an extension.
fn register_extension<E: Any + Extension>(&mut self, ext: E);
/// Initialize a new block at particular height.
fn initialize_block(
_height: BlockNumberFor<Self::Runtime>,
_parent_hash: <Self::Runtime as frame_system::Config>::Hash,
) {
}
/// Finalize a block at particular height.
fn finalize_block(
_height: BlockNumberFor<Self::Runtime>,
) -> <Self::Runtime as frame_system::Config>::Hash {
Default::default()
}
/// Default actor for the sandbox.
fn default_actor() -> AccountIdFor<Self::Runtime>;
fn default_gas_limit() -> Weight {
Weight::from_parts(100_000_000_000_000, 6 * 1024 * 1024)
}
/// Metadata of the runtime.
fn get_metadata() -> RuntimeMetadataPrefixed;
/// Convert an account to a call origin.
fn convert_account_to_origin(
account: AccountIdFor<Self::Runtime>,
) -> <<Self::Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin;
/// Take a snapshot of the storage.
fn take_snapshot(&mut self) -> Snapshot;
/// Restore the storage from the given snapshot.
fn restore_snapshot(&mut self, snapshot: Snapshot);
}