-
Notifications
You must be signed in to change notification settings - Fork 104
/
lib.rs
802 lines (669 loc) · 24.1 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
// This file is part of Gear.
// Copyright (C) 2021-2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]
pub mod lazy_pages;
pub mod native;
pub mod storage_queue;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchError, DispatchResult},
traits::Imbalance,
weights::{IdentityFee, WeightToFeePolynomial},
};
use gear_runtime_interface as gear_ri;
use primitive_types::H256;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
use sp_core::crypto::UncheckedFrom;
use sp_std::{
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
prelude::*,
};
use gear_core::{
message::{DispatchKind, PayloadStore},
program::{CodeHash, Program as NativeProgram, ProgramId},
};
pub use storage_queue::Iterator;
pub use storage_queue::StorageQueue;
pub const STORAGE_PROGRAM_PREFIX: &[u8] = b"g::prog::";
pub const STORAGE_PROGRAM_PAGES_PREFIX: &[u8] = b"g::pages::";
pub const STORAGE_PROGRAM_STATE_WAIT_PREFIX: &[u8] = b"g::prog_wait::";
pub const STORAGE_MESSAGE_PREFIX: &[u8] = b"g::msg::";
pub const STORAGE_MESSAGE_NONCE_KEY: &[u8] = b"g::msg::nonce";
pub const STORAGE_MESSAGE_USER_NONCE_KEY: &[u8] = b"g::msg::user_nonce";
pub const STORAGE_CODE_PREFIX: &[u8] = b"g::code::";
pub const STORAGE_CODE_METADATA_PREFIX: &[u8] = b"g::code::metadata::";
pub const STORAGE_WAITLIST_PREFIX: &[u8] = b"g::wait::";
pub const GAS_VALUE_PREFIX: &[u8] = b"g::gas_tree";
pub type ExitCode = i32;
pub trait Origin: Sized {
fn into_origin(self) -> H256;
fn from_origin(val: H256) -> Self;
}
impl Origin for u64 {
fn into_origin(self) -> H256 {
let mut result = H256::zero();
result[0..8].copy_from_slice(&self.to_le_bytes());
result
}
fn from_origin(v: H256) -> Self {
// h256 -> u64 should not be used anywhere other than in tests!
let mut val = [0u8; 8];
val.copy_from_slice(&v[0..8]);
Self::from_le_bytes(val)
}
}
impl Origin for sp_runtime::AccountId32 {
fn into_origin(self) -> H256 {
H256::from(self.as_ref())
}
fn from_origin(v: H256) -> Self {
sp_runtime::AccountId32::unchecked_from(v)
}
}
impl Origin for H256 {
fn into_origin(self) -> H256 {
self
}
fn from_origin(val: H256) -> Self {
val
}
}
impl Origin for CodeHash {
fn into_origin(self) -> H256 {
self.inner().into()
}
fn from_origin(val: H256) -> Self {
val.to_fixed_bytes().into()
}
}
pub trait GasPrice {
type Balance: BaseArithmetic + From<u32> + Copy + Unsigned;
/// A price for the `gas` amount of gas.
/// In general case, this doesn't necessarily has to be constant.
fn gas_price(gas: u64) -> Self::Balance {
IdentityFee::<Self::Balance>::calc(&gas)
}
}
pub trait PaymentProvider<AccountId> {
type Balance;
fn withhold_reserved(
source: H256,
dest: &AccountId,
amount: Self::Balance,
) -> Result<(), DispatchError>;
}
/// Abstraction for a chain of value items each piece of which has an attributed owner and
/// can be traced up to some root origin.
/// The definition is largely inspired by the `frame_support::traits::Currency` -
/// https://github.com/paritytech/substrate/blob/master/frame/support/src/traits/tokens/currency.rs,
/// however, the intended use is very close to the UTxO based ledger model.
pub trait DAGBasedLedger {
/// Type representing the external owner of a value (gas) item.
type ExternalOrigin;
/// Type that identifies a particular value item.
type Key;
/// Type representing a quantity of value.
type Balance;
/// Types to denote a result of some unbalancing operation - that is operations that create
/// inequality between the underlying value supply and some hypothetical "collateral" asset.
/// `PositiveImbalance` indicates that some value has been created, which will eventually
/// lead to an increase in total supply.
type PositiveImbalance: Imbalance<Self::Balance, Opposite = Self::NegativeImbalance>;
/// `NegativeImbalance` indicates that some value has been removed from circulation
/// leading to a decrease in the total supply of the underlying value.
type NegativeImbalance: Imbalance<Self::Balance, Opposite = Self::PositiveImbalance>;
/// The total amount of value currently in circulation.
fn total_supply() -> Self::Balance;
/// Increase the total issuance of the underlying value by creating some `amount` of it
/// and attributing it to the `origin`. The `key` identifies the created "bag" of value.
/// In case the `key` already indentifies some other piece of value an error is returned.
fn create(
origin: Self::ExternalOrigin,
key: Self::Key,
amount: Self::Balance,
) -> Result<Self::PositiveImbalance, DispatchError>;
/// Get value item by it's ID, if exists.
fn get_limit(key: Self::Key) -> Option<(Self::Balance, Self::ExternalOrigin)>;
/// Consume underlying value.
///
/// If `key` does not identify any value or the value can't be fully consumed due to
/// being a part of other value or itself having unconsumed parts, return None,
/// else the corresponding piece of value is destroyed and imbalance is created.
fn consume(key: Self::Key) -> Option<(Self::NegativeImbalance, Self::ExternalOrigin)>;
/// Burns underlying value.
///
/// This "spends" the specified amount of value thereby decreasing the overall supply of it.
/// In case of a success, this indicates the entire value supply becomes over-collateralized,
/// hence negative imbalance.
fn spend(
key: Self::Key,
amount: Self::Balance,
) -> Result<Self::NegativeImbalance, DispatchError>;
/// Split underlying value.
///
/// If `key` does not identify any value or the `amount` exceeds what's locked under that key,
/// an error is returned.
/// This can't create imbalance as no value is burned or created.
fn split_with_value(
key: Self::Key,
new_key: Self::Key,
amount: Self::Balance,
) -> DispatchResult;
/// Split underlying value.
///
/// If `key` does not identify any value an error is returned.
/// This can't create imbalance as no value is burned or created.
fn split(key: Self::Key, new_key: Self::Key) -> DispatchResult;
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct Dispatch {
pub kind: DispatchKind,
pub message: Message,
pub payload_store: Option<PayloadStore>,
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct QueuedDispatch {
pub kind: DispatchKind,
pub message: QueuedMessage,
pub payload_store: Option<PayloadStore>,
}
impl Dispatch {
pub fn new_init(message: Message) -> Self {
Self {
message,
kind: DispatchKind::Init,
payload_store: None,
}
}
pub fn new_handle(message: Message) -> Self {
Self {
message,
kind: DispatchKind::Handle,
payload_store: None,
}
}
pub fn new_reply(message: Message) -> Self {
Self {
message,
kind: DispatchKind::HandleReply,
payload_store: None,
}
}
}
impl QueuedDispatch {
pub fn new_init(message: QueuedMessage) -> Self {
Self {
message,
kind: DispatchKind::Init,
payload_store: None,
}
}
pub fn new_handle(message: QueuedMessage) -> Self {
Self {
message,
kind: DispatchKind::Handle,
payload_store: None,
}
}
pub fn new_reply(message: QueuedMessage) -> Self {
Self {
message,
kind: DispatchKind::HandleReply,
payload_store: None,
}
}
pub fn into_dispatch(self, gas_limit: u64) -> gear_core::message::Dispatch {
gear_core::message::Dispatch {
message: self.message.into_message(gas_limit),
kind: self.kind,
payload_store: self.payload_store,
}
}
pub fn without_gas_limit(dispatch: gear_core::message::Dispatch) -> (Option<u64>, Self) {
let (gas_limit, message) = QueuedMessage::without_gas_limit(dispatch.message);
let dispatch = Self {
message,
kind: dispatch.kind,
payload_store: dispatch.payload_store,
};
(gas_limit, dispatch)
}
pub fn message_id(&self) -> &H256 {
&self.message.id
}
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct Message {
pub id: H256,
pub source: H256,
pub dest: H256,
pub payload: Vec<u8>,
pub gas_limit: u64,
pub value: u128,
pub reply: Option<(H256, ExitCode)>,
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct QueuedMessage {
pub id: H256,
pub source: H256,
pub dest: H256,
pub payload: Vec<u8>,
pub value: u128,
pub reply: Option<(H256, ExitCode)>,
}
impl QueuedMessage {
pub fn into_message(self, gas_limit: u64) -> gear_core::message::Message {
gear_core::message::Message {
id: gear_core::message::MessageId::from_origin(self.id),
source: gear_core::program::ProgramId::from_origin(self.source),
dest: gear_core::program::ProgramId::from_origin(self.dest),
payload: self.payload.into(),
gas_limit: Some(gas_limit),
value: self.value,
reply: self.reply.map(|(message_id, exit_code)| {
(
gear_core::message::MessageId::from_origin(message_id),
exit_code,
)
}),
}
}
pub fn without_gas_limit(message: gear_core::message::Message) -> (Option<u64>, Self) {
let gear_core::message::Message {
id,
source,
dest,
payload,
gas_limit,
value,
reply,
} = message;
let new_message = Self {
id: id.into_origin(),
source: source.into_origin(),
dest: dest.into_origin(),
payload: payload.into_raw(),
value,
reply: reply.map(|(message_id, exit_code)| (message_id.into_origin(), exit_code)),
};
(gas_limit, new_message)
}
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub enum Program {
Active(ActiveProgram),
Terminated,
}
#[derive(Clone, Copy, Debug)]
pub enum ProgramError {
CodeHashNotFound,
IsTerminated,
DoesNotExist,
}
impl Program {
pub fn try_into_native(self, id: H256) -> Result<NativeProgram, ProgramError> {
let is_initialized = self.is_initialized();
let program: ActiveProgram = self.try_into()?;
let code = crate::get_code(program.code_hash).ok_or(ProgramError::CodeHashNotFound)?;
let native_program = NativeProgram::from_parts(
ProgramId::from_origin(id),
code,
program.static_pages,
program.nonce,
program.persistent_pages,
is_initialized,
);
Ok(native_program)
}
pub fn is_active(&self) -> bool {
matches!(self, Program::Active(_))
}
pub fn is_terminated(&self) -> bool {
matches!(self, Program::Terminated)
}
pub fn is_initialized(&self) -> bool {
matches!(
self,
Program::Active(ActiveProgram {
state: ProgramState::Initialized,
..
})
)
}
pub fn is_uninitialized(&self) -> bool {
matches!(
self,
Program::Active(ActiveProgram {
state: ProgramState::Uninitialized { .. },
..
})
)
}
}
impl TryFrom<Program> for ActiveProgram {
type Error = ProgramError;
fn try_from(prog_with_status: Program) -> Result<ActiveProgram, Self::Error> {
match prog_with_status {
Program::Active(p) => Ok(p),
Program::Terminated => Err(ProgramError::IsTerminated),
}
}
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct ActiveProgram {
pub static_pages: u32,
pub persistent_pages: BTreeSet<u32>,
pub code_hash: H256,
pub nonce: u64,
pub state: ProgramState,
}
/// Enumeration contains variants for program state.
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub enum ProgramState {
/// `init` method of a program has not yet finished its execution so
/// the program is not considered as initialized. All messages to such a
/// program go to the wait list.
/// `message_id` contains identifier of the initialization message.
Uninitialized { message_id: H256 },
/// Program has been successfully initialized and can process messages.
Initialized,
}
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
pub struct CodeMetadata {
pub author: H256,
pub block_number: u32,
}
impl CodeMetadata {
pub fn new(author: H256, block_number: u32) -> Self {
CodeMetadata {
author,
block_number,
}
}
}
// Inner enum used to "generalise" get/set of data under "g::code::*" prefixes
enum CodeKeyPrefixKind {
// "g::code::"
RawCode,
// "g::code::metadata::"
CodeMetadata,
}
pub fn program_key(id: H256) -> Vec<u8> {
let mut key = Vec::new();
key.extend(STORAGE_PROGRAM_PREFIX);
id.encode_to(&mut key);
key
}
fn code_key(code_hash: H256, kind: CodeKeyPrefixKind) -> Vec<u8> {
let prefix = match kind {
CodeKeyPrefixKind::RawCode => STORAGE_CODE_PREFIX,
CodeKeyPrefixKind::CodeMetadata => STORAGE_CODE_METADATA_PREFIX,
};
// key's length is N bytes of code hash + M bytes of prefix
// currently code hash is 32 bytes
let mut key = Vec::with_capacity(prefix.len() + code_hash.as_bytes().len());
key.extend(prefix);
code_hash.encode_to(&mut key);
key
}
pub fn pages_prefix(program_id: H256) -> Vec<u8> {
let mut key = Vec::new();
key.extend(STORAGE_PROGRAM_PAGES_PREFIX);
program_id.encode_to(&mut key);
key
}
fn page_key(id: H256, page: u32) -> Vec<u8> {
let mut key = pages_prefix(id);
key.extend(b"::");
page.encode_to(&mut key);
key
}
pub fn wait_prefix(prog_id: H256) -> Vec<u8> {
let mut key = Vec::new();
key.extend(STORAGE_WAITLIST_PREFIX);
prog_id.encode_to(&mut key);
key.extend(b"::");
key
}
pub fn wait_key(prog_id: H256, msg_id: H256) -> Vec<u8> {
let mut key = wait_prefix(prog_id);
msg_id.encode_to(&mut key);
key
}
pub fn get_code(code_hash: H256) -> Option<Vec<u8>> {
sp_io::storage::get(&code_key(code_hash, CodeKeyPrefixKind::RawCode))
}
pub fn set_code(code_hash: H256, code: &[u8]) {
sp_io::storage::set(&code_key(code_hash, CodeKeyPrefixKind::RawCode), code)
}
pub fn set_code_metadata(code_hash: H256, metadata: CodeMetadata) {
sp_io::storage::set(
&code_key(code_hash, CodeKeyPrefixKind::CodeMetadata),
&metadata.encode(),
)
}
pub fn get_code_metadata(code_hash: H256) -> Option<CodeMetadata> {
sp_io::storage::get(&code_key(code_hash, CodeKeyPrefixKind::CodeMetadata))
.map(|data| CodeMetadata::decode(&mut &data[..]).expect("data encoded correctly"))
}
pub fn set_program_initialized(id: H256) {
if let Some(Program::Active(mut p)) = get_program(id) {
if !matches!(p.state, ProgramState::Initialized) {
p.state = ProgramState::Initialized;
sp_io::storage::set(&program_key(id), &Program::Active(p).encode());
}
}
}
pub fn set_program_terminated_status(id: H256) -> Result<(), ProgramError> {
if let Some(program) = get_program(id) {
if program.is_terminated() {
return Err(ProgramError::IsTerminated);
}
sp_io::storage::clear_prefix(&pages_prefix(id), None);
sp_io::storage::set(&program_key(id), &Program::Terminated.encode());
Ok(())
} else {
Err(ProgramError::DoesNotExist)
}
}
pub fn get_program(id: H256) -> Option<Program> {
sp_io::storage::get(&program_key(id))
.map(|val| Program::decode(&mut &val[..]).expect("values encoded correctly"))
}
/// Returns mem page data from storage for program `id` and `page_idx`
pub fn get_program_page_data(id: H256, page_idx: u32) -> Option<Vec<u8>> {
let key = page_key(id, page_idx);
sp_io::storage::get(&key)
}
/// Save page data key in storage
pub fn save_page_lazy_info(id: H256, page_num: u32) {
let key = page_key(id, page_num);
gear_ri::gear_ri::save_page_lazy_info(page_num, &key);
}
pub fn get_program_pages(id: H256, pages: BTreeSet<u32>) -> Option<BTreeMap<u32, Vec<u8>>> {
let mut persistent_pages = BTreeMap::new();
for page_num in pages {
let key = page_key(id, page_num);
persistent_pages.insert(page_num, sp_io::storage::get(&key)?);
}
Some(persistent_pages)
}
pub fn set_program(id: H256, program: ActiveProgram, persistent_pages: BTreeMap<u32, Vec<u8>>) {
for (page_num, page_buf) in persistent_pages {
let key = page_key(id, page_num);
sp_io::storage::set(&key, &page_buf);
}
sp_io::storage::set(&program_key(id), &Program::Active(program).encode())
}
pub fn program_exists(id: H256) -> bool {
sp_io::storage::exists(&program_key(id))
}
pub fn dequeue_dispatch() -> Option<QueuedDispatch> {
let mut dispatch_queue = StorageQueue::get(STORAGE_MESSAGE_PREFIX);
dispatch_queue.dequeue()
}
pub fn queue_dispatch(dispatch: QueuedDispatch) {
let mut dispatch_queue = StorageQueue::get(STORAGE_MESSAGE_PREFIX);
let id = dispatch.message.id;
dispatch_queue.queue(dispatch, id);
}
pub fn dispatch_iter() -> Iterator<QueuedDispatch> {
StorageQueue::get(STORAGE_MESSAGE_PREFIX).into_iter()
}
pub fn nonce_fetch_inc() -> u128 {
let original_nonce = sp_io::storage::get(STORAGE_MESSAGE_NONCE_KEY)
.map(|val| u128::decode(&mut &val[..]).expect("nonce decode fail"))
.unwrap_or(0u128);
let new_nonce = original_nonce.wrapping_add(1);
sp_io::storage::set(STORAGE_MESSAGE_NONCE_KEY, &new_nonce.encode());
original_nonce
}
pub fn peek_last_message_id(payload: &[u8]) -> H256 {
let nonce = sp_io::storage::get(STORAGE_MESSAGE_NONCE_KEY)
.map(|val| u128::decode(&mut &val[..]).expect("nonce decode fail"))
.unwrap_or(0u128);
let mut data = payload.encode();
data.extend_from_slice(&(nonce.wrapping_sub(1)).to_le_bytes());
let message_id: H256 = sp_io::hashing::blake2_256(&data).into();
message_id
}
// WARN: Never call that in threads
pub fn next_message_id(payload: &[u8]) -> H256 {
let nonce = nonce_fetch_inc();
let mut data = payload.encode();
data.extend_from_slice(&nonce.to_le_bytes());
let message_id: H256 = sp_io::hashing::blake2_256(&data).into();
message_id
}
pub fn caller_nonce_fetch_inc(caller_id: H256) -> u64 {
let mut key_id = STORAGE_MESSAGE_USER_NONCE_KEY.to_vec();
key_id.extend(&caller_id[..]);
let original_nonce = sp_io::storage::get(&key_id)
.map(|val| u64::decode(&mut &val[..]).expect("nonce decode fail"))
.unwrap_or(0);
let new_nonce = original_nonce.wrapping_add(1);
sp_io::storage::set(&key_id, &new_nonce.encode());
original_nonce
}
pub fn set_program_nonce(id: H256, nonce: u64) {
if let Some(Program::Active(mut prog)) = get_program(id) {
prog.nonce = nonce;
sp_io::storage::set(&program_key(id), &Program::Active(prog).encode())
}
}
pub fn set_program_persistent_pages(id: H256, persistent_pages: BTreeSet<u32>) {
if let Some(Program::Active(mut prog)) = get_program(id) {
prog.persistent_pages = persistent_pages;
sp_io::storage::set(&program_key(id), &Program::Active(prog).encode())
}
}
pub fn set_program_page(program_id: H256, page_num: u32, page_buf: Vec<u8>) {
let page_key = page_key(program_id, page_num);
sp_io::storage::set(&page_key, &page_buf);
}
pub fn remove_program_page(program_id: H256, page_num: u32) {
let page_key = page_key(program_id, page_num);
sp_io::storage::clear(&page_key);
}
pub fn insert_waiting_message(dest_prog_id: H256, msg_id: H256, dispatch: QueuedDispatch, bn: u32) {
let payload = (dispatch, bn);
sp_io::storage::set(&wait_key(dest_prog_id, msg_id), &payload.encode());
}
pub fn remove_waiting_message(dest_prog_id: H256, msg_id: H256) -> Option<(QueuedDispatch, u32)> {
let id = wait_key(dest_prog_id, msg_id);
let msg = sp_io::storage::get(&id)
.and_then(|val| <(QueuedDispatch, u32)>::decode(&mut &val[..]).ok());
if msg.is_some() {
sp_io::storage::clear(&id);
}
msg
}
pub fn waiting_init_prefix(prog_id: H256) -> Vec<u8> {
let mut key = Vec::new();
key.extend(STORAGE_PROGRAM_STATE_WAIT_PREFIX);
prog_id.encode_to(&mut key);
key
}
fn program_waitlist_prefix(prog_id: H256) -> Vec<u8> {
let mut key = Vec::new();
key.extend(STORAGE_WAITLIST_PREFIX);
prog_id.encode_to(&mut key);
key
}
pub fn remove_program_waitlist(prog_id: H256) -> Vec<QueuedDispatch> {
let key = program_waitlist_prefix(prog_id);
let messages =
sp_io::storage::get(&key).and_then(|v| Vec::<QueuedDispatch>::decode(&mut &v[..]).ok());
sp_io::storage::clear(&key);
messages.unwrap_or_default()
}
pub fn waiting_init_append_message_id(dest_prog_id: H256, message_id: H256) {
let key = waiting_init_prefix(dest_prog_id);
sp_io::storage::append(&key, message_id.encode());
}
pub fn waiting_init_take_messages(dest_prog_id: H256) -> Vec<H256> {
let key = waiting_init_prefix(dest_prog_id);
let messages = sp_io::storage::get(&key).and_then(|v| Vec::<H256>::decode(&mut &v[..]).ok());
sp_io::storage::clear(&key);
messages.unwrap_or_default()
}
pub fn code_exists(code_hash: H256) -> bool {
sp_io::storage::exists(&code_key(code_hash, CodeKeyPrefixKind::RawCode))
}
pub fn reset_storage() {
sp_io::storage::clear_prefix(STORAGE_PROGRAM_PREFIX, None);
sp_io::storage::clear_prefix(STORAGE_PROGRAM_PAGES_PREFIX, None);
sp_io::storage::clear_prefix(STORAGE_MESSAGE_PREFIX, None);
sp_io::storage::clear_prefix(STORAGE_CODE_PREFIX, None);
sp_io::storage::clear_prefix(STORAGE_WAITLIST_PREFIX, None);
sp_io::storage::clear_prefix(GAS_VALUE_PREFIX, None);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nonce_incremented() {
sp_io::TestExternalities::new_empty().execute_with(|| {
assert_eq!(nonce_fetch_inc(), 0_u128);
assert_eq!(nonce_fetch_inc(), 1_u128);
assert_eq!(nonce_fetch_inc(), 2_u128);
});
}
fn get_active_program(id: H256) -> Option<ActiveProgram> {
get_program(id).and_then(|p| p.try_into().ok())
}
#[test]
fn program_decoded() {
sp_io::TestExternalities::new_empty().execute_with(|| {
let code = b"pretended wasm code".to_vec();
let code_hash: H256 = CodeHash::generate(&code).into_origin();
let program_id = H256::from_low_u64_be(1);
let program = ActiveProgram {
static_pages: 256,
persistent_pages: Default::default(),
code_hash,
nonce: 0,
state: ProgramState::Initialized,
};
set_code(code_hash, &code);
assert!(get_program(program_id).is_none());
set_program(program_id, program.clone(), Default::default());
assert_eq!(get_active_program(program_id).unwrap(), program);
assert_eq!(get_code(program.code_hash).unwrap(), code);
});
}
}