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
#![feature(non_exhaustive)]
use oasis_types::{AccountMeta, Address, Event};
pub trait Blockchain {
fn name(&self) -> &str;
fn block(&self, height: usize) -> Option<&dyn Block>;
fn last_block(&self) -> &dyn Block;
fn last_block_mut(&mut self) -> &mut dyn Block;
}
pub trait Block {
fn height(&self) -> u64;
#[allow(clippy::too_many_arguments)]
fn transact(
&mut self,
caller: Address,
callee: Address,
payer: Address,
value: u128,
input: &[u8],
gas: u64,
gas_price: u64,
) -> Box<dyn Receipt>;
fn code_at(&self, addr: &Address) -> Option<&[u8]>;
fn account_meta_at(&self, addr: &Address) -> Option<AccountMeta>;
fn state_at(&self, addr: &Address) -> Option<&dyn KVStore>;
fn events(&self) -> Vec<&Event>;
fn receipts(&self) -> Vec<&dyn Receipt>;
}
pub trait PendingTransaction {
fn address(&self) -> &Address;
fn sender(&self) -> &Address;
fn value(&self) -> u128;
fn input(&self) -> &[u8];
fn transact(&mut self, callee: Address, value: u128, input: &[u8]) -> Box<dyn Receipt>;
fn ret(&mut self, data: &[u8]);
fn err(&mut self, data: &[u8]);
fn emit(&mut self, topics: &[&[u8]], data: &[u8]);
fn state(&self) -> &dyn KVStore;
fn state_mut(&mut self) -> &mut dyn KVStoreMut;
fn code_at(&self, addr: &Address) -> Option<&[u8]>;
fn account_meta_at(&self, addr: &Address) -> Option<AccountMeta>;
}
pub trait KVStore {
fn contains(&self, key: &[u8]) -> bool;
fn get(&self, key: &[u8]) -> Option<Vec<u8>>;
}
pub trait KVStoreMut: KVStore {
fn set(&mut self, key: &[u8], value: &[u8]);
fn remove(&mut self, key: &[u8]);
}
pub trait Receipt {
fn caller(&self) -> &Address;
fn callee(&self) -> &Address;
fn gas_used(&self) -> u64;
fn events(&self) -> Vec<&Event>;
fn outcome(&self) -> TransactionOutcome;
fn output(&self) -> &[u8];
fn reverted(&self) -> bool {
match self.outcome() {
TransactionOutcome::Success => false,
_ => true,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
#[repr(u16)]
pub enum TransactionOutcome {
Success,
InsufficientFunds,
InsufficientGas,
InvalidInput,
InvalidCallee,
Aborted,
Fatal,
}
impl TransactionOutcome {
pub fn reverted(self) -> bool {
match self {
TransactionOutcome::Success => false,
_ => true,
}
}
}