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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use sp_core::{
storage::{ChildInfo, TrackedStorageKey},
traits::{Externalities, RuntimeSpawn, RuntimeSpawnExt, SpawnNamed, TaskExecutorExt},
};
use sp_externalities::{Extensions, ExternalitiesExt as _};
use std::any::{Any, TypeId};
#[derive(Debug)]
pub struct AsyncExternalities {
extensions: Extensions,
}
pub fn new_async_externalities(
scheduler: Box<dyn SpawnNamed>,
) -> Result<AsyncExternalities, &'static str> {
let mut res = AsyncExternalities { extensions: Default::default() };
let mut ext = &mut res as &mut dyn Externalities;
ext.register_extension::<TaskExecutorExt>(TaskExecutorExt(scheduler.clone()))
.map_err(|_| "Failed to register task executor extension.")?;
Ok(res)
}
impl AsyncExternalities {
pub fn with_runtime_spawn(
mut self,
runtime_ext: Box<dyn RuntimeSpawn>,
) -> Result<Self, &'static str> {
let mut ext = &mut self as &mut dyn Externalities;
ext.register_extension::<RuntimeSpawnExt>(RuntimeSpawnExt(runtime_ext))
.map_err(|_| "Failed to register task executor extension.")?;
Ok(self)
}
}
type StorageKey = Vec<u8>;
type StorageValue = Vec<u8>;
impl Externalities for AsyncExternalities {
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {
panic!("`set_offchain_storage`: should not be used in async externalities!")
}
fn storage(&self, _key: &[u8]) -> Option<StorageValue> {
panic!("`storage`: should not be used in async externalities!")
}
fn storage_hash(&self, _key: &[u8]) -> Option<Vec<u8>> {
panic!("`storage_hash`: should not be used in async externalities!")
}
fn child_storage(&self, _child_info: &ChildInfo, _key: &[u8]) -> Option<StorageValue> {
panic!("`child_storage`: should not be used in async externalities!")
}
fn child_storage_hash(&self, _child_info: &ChildInfo, _key: &[u8]) -> Option<Vec<u8>> {
panic!("`child_storage_hash`: should not be used in async externalities!")
}
fn next_storage_key(&self, _key: &[u8]) -> Option<StorageKey> {
panic!("`next_storage_key`: should not be used in async externalities!")
}
fn next_child_storage_key(&self, _child_info: &ChildInfo, _key: &[u8]) -> Option<StorageKey> {
panic!("`next_child_storage_key`: should not be used in async externalities!")
}
fn place_storage(&mut self, _key: StorageKey, _maybe_value: Option<StorageValue>) {
panic!("`place_storage`: should not be used in async externalities!")
}
fn place_child_storage(
&mut self,
_child_info: &ChildInfo,
_key: StorageKey,
_value: Option<StorageValue>,
) {
panic!("`place_child_storage`: should not be used in async externalities!")
}
fn kill_child_storage(&mut self, _child_info: &ChildInfo, _limit: Option<u32>) -> (bool, u32) {
panic!("`kill_child_storage`: should not be used in async externalities!")
}
fn clear_prefix(&mut self, _prefix: &[u8], _limit: Option<u32>) -> (bool, u32) {
panic!("`clear_prefix`: should not be used in async externalities!")
}
fn clear_child_prefix(
&mut self,
_child_info: &ChildInfo,
_prefix: &[u8],
_limit: Option<u32>,
) -> (bool, u32) {
panic!("`clear_child_prefix`: should not be used in async externalities!")
}
fn storage_append(&mut self, _key: Vec<u8>, _value: Vec<u8>) {
panic!("`storage_append`: should not be used in async externalities!")
}
fn storage_root(&mut self) -> Vec<u8> {
panic!("`storage_root`: should not be used in async externalities!")
}
fn child_storage_root(&mut self, _child_info: &ChildInfo) -> Vec<u8> {
panic!("`child_storage_root`: should not be used in async externalities!")
}
fn storage_changes_root(&mut self, _parent: &[u8]) -> Result<Option<Vec<u8>>, ()> {
panic!("`storage_changes_root`: should not be used in async externalities!")
}
fn storage_start_transaction(&mut self) {
unimplemented!("Transactions are not supported by AsyncExternalities");
}
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
unimplemented!("Transactions are not supported by AsyncExternalities");
}
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
unimplemented!("Transactions are not supported by AsyncExternalities");
}
fn wipe(&mut self) {}
fn commit(&mut self) {}
fn read_write_count(&self) -> (u32, u32, u32, u32) {
unimplemented!("read_write_count is not supported in AsyncExternalities")
}
fn reset_read_write_count(&mut self) {
unimplemented!("reset_read_write_count is not supported in AsyncExternalities")
}
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
unimplemented!("get_whitelist is not supported in AsyncExternalities")
}
fn set_whitelist(&mut self, _: Vec<TrackedStorageKey>) {
unimplemented!("set_whitelist is not supported in AsyncExternalities")
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
unimplemented!("get_read_and_written_keys is not supported in AsyncExternalities")
}
}
impl sp_externalities::ExtensionStore for AsyncExternalities {
fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
self.extensions.get_mut(type_id)
}
fn register_extension_with_type_id(
&mut self,
type_id: TypeId,
extension: Box<dyn sp_externalities::Extension>,
) -> Result<(), sp_externalities::Error> {
self.extensions.register_with_type_id(type_id, extension)
}
fn deregister_extension_by_type_id(
&mut self,
type_id: TypeId,
) -> Result<(), sp_externalities::Error> {
if self.extensions.deregister(type_id) {
Ok(())
} else {
Err(sp_externalities::Error::ExtensionIsNotRegistered(type_id))
}
}
}