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
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate bitflags;

use ceres_executor::Memory;
use ceres_std::{vec, BTreeMap, Vec};

/// Custom storage key
pub type StorageKey = [u8; 32];

mod chain;
mod contract;
mod instantiate;
mod memory;
mod restore;
mod schedule;
mod storage;
mod termination;
mod transfer;
mod tx;
mod util;

use self::{
    contract::{GasMeter, RentParams},
    schedule::Schedule,
};
use parity_scale_codec::{Decode, Encode};
pub use tx::Transaction;

bitflags! {
    /// Flags used by a contract to customize exit behaviour.
    #[derive(Encode, Decode)]
    pub struct ReturnFlags: u32 {
        /// If this bit is set all changes made by the contract execution are rolled back.
        const REVERT = 0x0000_0001;
    }
}

/// Return flags
pub struct ExecReturnValue {
    pub flags: ReturnFlags,
    pub data: Vec<u8>,
}

/// Extend data
pub struct Ext {
    pub instantiates: Vec<instantiate::InstantiateEntry>,
    pub restores: Vec<restore::RestoreEntry>,
    pub rent_allowance: [u8; 32],
    pub terminations: Vec<termination::TerminationEntry>,
    pub transfers: Vec<transfer::TransferEntry>,
    pub schedule: Schedule,
    pub rent_params: RentParams,
    pub gas_meter: GasMeter,
}

/// The runtime of ink! machine
pub struct Sandbox {
    pub input: Option<Vec<u8>>,
    pub ret: Option<Vec<u8>>,
    pub ext: Ext,
    pub tx: tx::Transaction,
    pub state: BTreeMap<StorageKey, Vec<u8>>,
    memory: Memory,
    pub events: Vec<(Vec<[u8; 32]>, Vec<u8>)>,
}

impl Sandbox {
    /// New sandbox
    pub fn new(memory: Memory, state: BTreeMap<StorageKey, Vec<u8>>) -> Sandbox {
        Sandbox {
            input: None,
            ret: None,
            ext: Ext {
                instantiates: vec![],
                restores: vec![],
                rent_allowance: [0; 32],
                terminations: vec![],
                transfers: vec![],
                schedule: Default::default(),
                rent_params: Default::default(),
                gas_meter: Default::default(),
            },
            events: vec![],
            tx: Default::default(),
            state,
            memory,
        }
    }
}