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
use std::collections::BTreeSet;
use casper_types::{
account::{self, AccountHash},
bytesrepr::{FromBytes, ToBytes},
contracts::NamedKeys,
system::{
auction::{
AccountProvider, Auction, Bid, EraInfo, Error, MintProvider, RuntimeProvider,
StorageProvider, UnbondingPurse,
},
mint, MINT,
},
CLTyped, CLValue, EraId, Key, KeyTag, PublicKey, RuntimeArgs, StoredValue, URef,
BLAKE2B_DIGEST_LENGTH, U512,
};
use super::Runtime;
use crate::{core::execution, storage::global_state::StateReader};
impl From<execution::Error> for Option<Error> {
fn from(exec_error: execution::Error) -> Self {
match exec_error {
execution::Error::GasLimit => Some(Error::GasLimit),
_ => None,
}
}
}
impl<'a, R> StorageProvider for Runtime<'a, R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
fn read<T: FromBytes + CLTyped>(&mut self, uref: URef) -> Result<Option<T>, Error> {
match self.context.read_gs(&uref.into()) {
Ok(Some(StoredValue::CLValue(cl_value))) => {
Ok(Some(cl_value.into_t().map_err(|_| Error::CLValue)?))
}
Ok(Some(_)) => Err(Error::Storage),
Ok(None) => Ok(None),
Err(execution::Error::BytesRepr(_)) => Err(Error::Serialization),
Err(execution::Error::GasLimit) => Err(Error::GasLimit),
Err(_) => Err(Error::Storage),
}
}
fn write<T: ToBytes + CLTyped>(&mut self, uref: URef, value: T) -> Result<(), Error> {
let cl_value = CLValue::from_t(value).map_err(|_| Error::CLValue)?;
self.context
.metered_write_gs(uref.into(), StoredValue::CLValue(cl_value))
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::Storage))
}
fn read_bid(&mut self, account_hash: &AccountHash) -> Result<Option<Bid>, Error> {
match self.context.read_gs(&Key::Bid(*account_hash)) {
Ok(Some(StoredValue::Bid(bid))) => Ok(Some(*bid)),
Ok(Some(_)) => Err(Error::Storage),
Ok(None) => Ok(None),
Err(execution::Error::BytesRepr(_)) => Err(Error::Serialization),
Err(execution::Error::GasLimit) => Err(Error::GasLimit),
Err(_) => Err(Error::Storage),
}
}
fn write_bid(&mut self, account_hash: AccountHash, bid: Bid) -> Result<(), Error> {
self.context
.metered_write_gs_unsafe(Key::Bid(account_hash), StoredValue::Bid(Box::new(bid)))
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::Storage))
}
fn read_withdraw(&mut self, account_hash: &AccountHash) -> Result<Vec<UnbondingPurse>, Error> {
match self.context.read_gs(&Key::Withdraw(*account_hash)) {
Ok(Some(StoredValue::Withdraw(unbonding_purses))) => Ok(unbonding_purses),
Ok(Some(_)) => Err(Error::Storage),
Ok(None) => Ok(Vec::new()),
Err(execution::Error::BytesRepr(_)) => Err(Error::Serialization),
Err(execution::Error::GasLimit) => Err(Error::GasLimit),
Err(_) => Err(Error::Storage),
}
}
fn write_withdraw(
&mut self,
account_hash: AccountHash,
unbonding_purses: Vec<UnbondingPurse>,
) -> Result<(), Error> {
self.context
.metered_write_gs_unsafe(
Key::Withdraw(account_hash),
StoredValue::Withdraw(unbonding_purses),
)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::Storage))
}
fn record_era_info(&mut self, era_id: EraId, era_info: EraInfo) -> Result<(), Error> {
Runtime::record_era_info(self, era_id, era_info)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::RecordEraInfo))
}
}
impl<'a, R> RuntimeProvider for Runtime<'a, R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
fn get_caller(&self) -> AccountHash {
self.context.get_caller()
}
fn is_allowed_session_caller(&self, account_hash: &AccountHash) -> bool {
Runtime::is_allowed_session_caller(self, account_hash)
}
fn named_keys_get(&self, name: &str) -> Option<Key> {
self.context.named_keys_get(name).cloned()
}
fn get_keys(&mut self, key_tag: &KeyTag) -> Result<BTreeSet<Key>, Error> {
self.context.get_keys(key_tag).map_err(|_| Error::Storage)
}
fn blake2b<T: AsRef<[u8]>>(&self, data: T) -> [u8; BLAKE2B_DIGEST_LENGTH] {
account::blake2b(data)
}
}
impl<'a, R> MintProvider for Runtime<'a, R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
fn unbond(&mut self, unbonding_purse: &UnbondingPurse) -> Result<(), Error> {
let account_hash =
AccountHash::from_public_key(unbonding_purse.unbonder_public_key(), account::blake2b);
let maybe_value = self
.context
.read_gs_direct(&Key::Account(account_hash))
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::Storage))?;
match maybe_value {
Some(StoredValue::Account(account)) => {
self.mint_transfer_direct(
Some(account_hash),
*unbonding_purse.bonding_purse(),
account.main_purse(),
*unbonding_purse.amount(),
None,
)
.map_err(|_| Error::Transfer)?
.map_err(|_| Error::Transfer)?;
Ok(())
}
Some(_cl_value) => Err(Error::CLValue),
None => Err(Error::InvalidPublicKey),
}
}
fn mint_transfer_direct(
&mut self,
to: Option<AccountHash>,
source: URef,
target: URef,
amount: U512,
id: Option<u64>,
) -> Result<Result<(), mint::Error>, Error> {
if self.context.get_caller() != PublicKey::System.to_account_hash()
&& self.context.validate_uref(&source).is_err()
{
return Err(Error::InvalidCaller);
}
let args_values = RuntimeArgs::try_new(|args| {
args.insert(mint::ARG_TO, to)?;
args.insert(mint::ARG_SOURCE, source)?;
args.insert(mint::ARG_TARGET, target)?;
args.insert(mint::ARG_AMOUNT, amount)?;
args.insert(mint::ARG_ID, id)?;
Ok(())
})
.map_err(|_| Error::CLValue)?;
let gas_counter = self.gas_counter();
let mut stack = self.stack().clone();
stack
.push(
self.get_system_contract_stack_frame(MINT)
.map_err(|_| Error::Storage)?,
)
.map_err(|_| Error::RuntimeStackOverflow)?;
let cl_value = self
.call_host_mint(
self.context.protocol_version(),
mint::METHOD_TRANSFER,
&mut NamedKeys::default(),
&args_values,
&[],
stack,
)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::Transfer))?;
self.set_gas_counter(gas_counter);
cl_value.into_t().map_err(|_| Error::CLValue)
}
fn create_purse(&mut self) -> Result<URef, Error> {
Runtime::create_purse(self).map_err(|exec_error| {
<Option<Error>>::from(exec_error).unwrap_or(Error::CreatePurseFailed)
})
}
fn get_balance(&mut self, purse: URef) -> Result<Option<U512>, Error> {
Runtime::get_balance(self, purse)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::GetBalance))
}
fn read_base_round_reward(&mut self) -> Result<U512, Error> {
let mint_contract = self.get_mint_contract().map_err(|exec_error| {
<Option<Error>>::from(exec_error).unwrap_or(Error::MissingValue)
})?;
self.mint_read_base_round_reward(mint_contract)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::MissingValue))
}
fn mint(&mut self, amount: U512) -> Result<URef, Error> {
let mint_contract = self
.get_mint_contract()
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::MintReward))?;
self.mint_mint(mint_contract, amount)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::MintReward))
}
fn reduce_total_supply(&mut self, amount: U512) -> Result<(), Error> {
let mint_contract = self
.get_mint_contract()
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::MintReward))?;
self.mint_reduce_total_supply(mint_contract, amount)
.map_err(|exec_error| <Option<Error>>::from(exec_error).unwrap_or(Error::MintReward))
}
}
impl<'a, R> AccountProvider for Runtime<'a, R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
fn get_main_purse(&self) -> Result<URef, Error> {
Ok(Runtime::context(self).account().main_purse())
}
}
impl<'a, R> Auction for Runtime<'a, R>
where
R: StateReader<Key, StoredValue>,
R::Error: Into<execution::Error>,
{
}