forked from icon-project/xcall-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.rs
374 lines (331 loc) · 11.1 KB
/
state.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use cosmwasm_std::{from_slice, to_vec, Order};
use cw_storage_plus::{KeyDeserialize, PrimaryKey};
use cw_xcall_lib::network_address::NetId;
use serde::de::DeserializeOwned;
use crate::types::config::Config;
use super::*;
/// These are constants defined in the `CwCallService` struct that are used throughout the codebase.
pub const MAX_DATA_SIZE: u64 = 2048;
pub const MAX_ROLLBACK_SIZE: u64 = 1024;
pub const EXECUTE_CALL_ID: u64 = 0;
pub const EXECUTE_ROLLBACK_ID: u64 = 1;
pub const SEND_CALL_MESSAGE_REPLY_ID: u64 = 2;
pub struct CwCallService<'a> {
sn: Item<'a, u128>,
config: Item<'a, Config>,
last_request_id: Item<'a, u128>,
admin: Item<'a, Addr>,
proxy_request: Map<'a, u128, CSMessageRequest>,
call_requests: Map<'a, u128, CallRequest>,
fee_handler: Item<'a, String>,
protocol_fee: Item<'a, u128>,
default_connections: Map<'a, NetId, Addr>,
pending_requests: Map<'a, (Vec<u8>, String), bool>,
pending_responses: Map<'a, (Vec<u8>, String), bool>,
successful_responses: Map<'a, u128, bool>,
callback_data: Map<'a, u64, Vec<u8>>,
}
impl<'a> Default for CwCallService<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a> CwCallService<'a> {
pub fn new() -> Self {
Self {
sn: Item::new(StorageKey::Sn.as_str()),
last_request_id: Item::new(StorageKey::RequestNo.as_str()),
admin: Item::new(StorageKey::Admin.as_str()),
proxy_request: Map::new(StorageKey::MessageRequest.as_str()),
call_requests: Map::new(StorageKey::Requests.as_str()),
fee_handler: Item::new(StorageKey::FeeHandler.as_str()),
protocol_fee: Item::new(StorageKey::ProtocolFee.as_str()),
default_connections: Map::new(StorageKey::DefaultConnections.as_str()),
pending_requests: Map::new(StorageKey::PendingRequests.as_str()),
pending_responses: Map::new(StorageKey::PendingResponses.as_str()),
successful_responses: Map::new(StorageKey::SuccessfulResponses.as_str()),
config: Item::new(StorageKey::Config.as_str()),
callback_data: Map::new(StorageKey::Callbackdata.as_str()),
}
}
pub fn get_next_sn(&self, store: &mut dyn Storage) -> Result<u128, ContractError> {
let mut sn = self.sn.load(store).unwrap_or(0);
sn += 1;
self.sn.save(store, &sn)?;
Ok(sn)
}
pub fn get_current_sn(&self, store: &dyn Storage) -> Result<u128, ContractError> {
self.sn.load(store).map_err(ContractError::Std)
}
pub fn sn(&self) -> &Item<'a, u128> {
&self.sn
}
pub fn get_config(&self, store: &dyn Storage) -> Result<Config, ContractError> {
self.config.load(store).map_err(ContractError::Std)
}
pub fn store_config(
&self,
store: &mut dyn Storage,
config: &Config,
) -> Result<(), ContractError> {
self.config.save(store, config).map_err(ContractError::Std)
}
pub fn last_request_id(&self) -> &Item<'a, u128> {
&self.last_request_id
}
pub fn store_execute_request_id(
&self,
store: &mut dyn Storage,
req_id: u128,
) -> Result<(), ContractError> {
self.store_callback_data(store, EXECUTE_CALL_ID, &req_id)
}
pub fn remove_execute_request_id(&self, store: &mut dyn Storage) {
self.clear_callback_data(store, EXECUTE_CALL_ID)
}
pub fn get_execute_request_id(&self, store: &dyn Storage) -> Result<u128, ContractError> {
self.get_callback_data(store, EXECUTE_CALL_ID)
}
pub fn admin(&self) -> &Item<'a, Addr> {
&self.admin
}
pub fn get_proxy_request(
&self,
store: &dyn Storage,
id: u128,
) -> Result<CSMessageRequest, ContractError> {
self.proxy_request
.load(store, id)
.map_err(ContractError::Std)
}
pub fn store_proxy_request(
&self,
store: &mut dyn Storage,
id: u128,
request: &CSMessageRequest,
) -> Result<(), ContractError> {
self.proxy_request
.save(store, id, request)
.map_err(ContractError::Std)
}
pub fn remove_proxy_request(&self, store: &mut dyn Storage, id: u128) {
self.proxy_request.remove(store, id)
}
pub fn contains_proxy_request(
&self,
store: &dyn Storage,
request_id: u128,
) -> Result<(), ContractError> {
match self.proxy_request.has(store, request_id) {
true => Ok(()),
false => Err(ContractError::InvalidRequestId { id: request_id }),
}
}
pub fn get_call_request(
&self,
store: &dyn Storage,
id: u128,
) -> Result<CallRequest, ContractError> {
self.call_requests
.load(store, id)
.map_err(ContractError::Std)
}
pub fn remove_call_request(&self, store: &mut dyn Storage, id: u128) {
self.call_requests.remove(store, id)
}
pub fn store_call_request(
&self,
store: &mut dyn Storage,
id: u128,
request: &CallRequest,
) -> Result<(), ContractError> {
self.call_requests
.save(store, id, request)
.map_err(ContractError::Std)
}
pub fn fee_handler(&self) -> &Item<'a, String> {
&self.fee_handler
}
pub fn store_default_connection(
&self,
store: &mut dyn Storage,
nid: NetId,
address: Addr,
) -> Result<(), ContractError> {
self.default_connections
.save(store, nid, &address)
.map_err(ContractError::Std)
}
pub fn get_default_connection(
&self,
store: &dyn Storage,
nid: NetId,
) -> Result<Addr, ContractError> {
self.default_connections
.load(store, nid)
.map_err(ContractError::Std)
}
pub fn get_pending_requests_by_hash(
&self,
store: &dyn Storage,
hash: Vec<u8>,
) -> Result<Vec<(String, bool)>, ContractError> {
self.get_by_prefix(store, &self.pending_requests, hash)
}
pub fn remove_pending_request_by_hash(
&self,
store: &mut dyn Storage,
hash: Vec<u8>,
) -> Result<(), ContractError> {
self.remove_by_prefix(store, &self.pending_requests, hash)
}
pub fn save_pending_requests(
&self,
store: &mut dyn Storage,
hash: Vec<u8>,
caller: String,
) -> Result<(), ContractError> {
self.pending_requests
.save(store, (hash, caller), &true)
.map_err(ContractError::Std)
}
pub fn get_pending_responses_by_hash(
&self,
store: &dyn Storage,
hash: Vec<u8>,
) -> Result<Vec<(String, bool)>, ContractError> {
self.get_by_prefix(store, &self.pending_responses, hash)
}
pub fn remove_pending_responses_by_hash(
&self,
store: &mut dyn Storage,
hash: Vec<u8>,
) -> Result<(), ContractError> {
self.remove_by_prefix(store, &self.pending_responses, hash)
}
pub fn save_pending_responses(
&self,
store: &mut dyn Storage,
hash: Vec<u8>,
caller: String,
) -> Result<(), ContractError> {
self.pending_responses
.save(store, (hash, caller), &true)
.map_err(ContractError::Std)
}
pub fn get_all_connections(&self, store: &dyn Storage) -> Result<Vec<String>, ContractError> {
let res = self.get_all_values::<NetId, Addr>(store, &self.default_connections)?;
let addresses: Vec<String> = res.into_iter().map(|a| a.to_string()).collect();
Ok(addresses)
}
fn get_by_prefix(
&self,
store: &dyn Storage,
map: &Map<(Vec<u8>, String), bool>,
hash: Vec<u8>,
) -> Result<Vec<(String, bool)>, ContractError> {
let requests: StdResult<Vec<(String, bool)>> = map
.prefix(hash)
.range(store, None, None, cosmwasm_std::Order::Ascending)
.collect();
requests.map_err(ContractError::Std)
}
fn remove_by_prefix(
&self,
store: &mut dyn Storage,
map: &Map<(Vec<u8>, String), bool>,
hash: Vec<u8>,
) -> Result<(), ContractError> {
let keys: StdResult<Vec<String>> = map
.prefix(hash.clone())
.keys(store, None, None, cosmwasm_std::Order::Ascending)
.collect();
let keys = keys.map_err(ContractError::Std)?;
for key in keys {
self.pending_requests.remove(store, (hash.clone(), key))
}
Ok(())
}
fn get_all_values<K: PrimaryKey<'a> + Clone + KeyDeserialize, V: DeserializeOwned + Serialize>(
&self,
store: &dyn Storage,
map: &Map<'a, K, V>,
) -> Result<Vec<V>, ContractError>
where
K::Output: 'static,
{
let values = map
.range(store, None, None, Order::Ascending)
.map(|r| r.map(|v| v.1))
.collect::<Result<Vec<V>, StdError>>();
values.map_err(ContractError::Std)
}
pub fn get_protocol_fee(&self, store: &dyn Storage) -> u128 {
self.protocol_fee.load(store).unwrap_or(0)
}
pub fn store_protocol_fee(
&self,
store: &mut dyn Storage,
fee: u128,
) -> Result<(), ContractError> {
self.protocol_fee
.save(store, &fee)
.map_err(ContractError::Std)
}
pub fn store_protocol_fee_handler(
&self,
store: &mut dyn Storage,
handler: String,
) -> Result<(), ContractError> {
self.fee_handler
.save(store, &handler)
.map_err(ContractError::Std)
}
pub fn get_successful_response(&self, store: &dyn Storage, sn: u128) -> bool {
self.successful_responses.load(store, sn).unwrap_or(false)
}
pub fn set_successful_response(
&self,
store: &mut dyn Storage,
sn: u128,
) -> Result<(), ContractError> {
self.successful_responses
.save(store, sn, &true)
.map_err(ContractError::Std)
}
pub fn store_callback_data<T>(
&self,
store: &mut dyn Storage,
id: u64,
data: &T,
) -> Result<(), ContractError>
where
T: DeserializeOwned + Serialize + ?Sized,
{
if self.has_callback_data(store, id) {
return Err(ContractError::CallAlreadyInProgress);
}
let bytes = to_vec(data).map_err(ContractError::Std)?;
self.callback_data
.save(store, id, &bytes)
.map_err(ContractError::Std)
}
pub fn has_callback_data(&self, store: &dyn Storage, id: u64) -> bool {
self.callback_data.load(store, id).is_ok()
}
pub fn clear_callback_data(&self, store: &mut dyn Storage, id: u64) {
self.callback_data.remove(store, id)
}
pub fn get_callback_data<T: DeserializeOwned>(
&self,
store: &dyn Storage,
id: u64,
) -> Result<T, ContractError> {
let bytes = self
.callback_data
.load(store, id)
.map_err(ContractError::Std)?;
let data = from_slice::<T>(&bytes).map_err(ContractError::Std)?;
Ok(data)
}
}