-
Notifications
You must be signed in to change notification settings - Fork 43
/
storage.rs
456 lines (423 loc) · 18.2 KB
/
storage.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
//! This module contains the [Storage] type and its supporting types, which
//! provide the [Host](crate::Host) with access to durable ledger entries.
//!
//! For more details, see the [Env](crate::Env) data access functions:
//! - [Env::has_contract_data](crate::Env::has_contract_data)
//! - [Env::get_contract_data](crate::Env::get_contract_data)
//! - [Env::put_contract_data](crate::Env::put_contract_data)
//! - [Env::del_contract_data](crate::Env::del_contract_data)
use std::rc::Rc;
use soroban_env_common::xdr::{ScErrorCode, ScErrorType};
use soroban_env_common::{Env, Val};
use crate::budget::Budget;
use crate::xdr::{LedgerEntry, LedgerKey};
use crate::Host;
use crate::{host::metered_map::MeteredOrdMap, HostError};
pub type FootprintMap = MeteredOrdMap<Rc<LedgerKey>, AccessType, Budget>;
pub type EntryWithLiveUntil = (Rc<LedgerEntry>, Option<u32>);
pub type StorageMap = MeteredOrdMap<Rc<LedgerKey>, Option<EntryWithLiveUntil>, Budget>;
/// The in-memory instance storage of the current running contract. Initially
/// contains entries from the `ScMap` of the corresponding `ScContractInstance`
/// contract data entry.
#[derive(Clone)]
pub(crate) struct InstanceStorageMap {
pub(crate) map: MeteredOrdMap<Val, Val, Host>,
pub(crate) is_modified: bool,
}
impl InstanceStorageMap {
pub(crate) fn from_map(map: Vec<(Val, Val)>, host: &Host) -> Result<Self, HostError> {
Ok(Self {
map: MeteredOrdMap::from_map(map, host)?,
is_modified: false,
})
}
}
/// A helper type used by [Footprint] to designate which ways
/// a given [LedgerKey] is accessed, or is allowed to be accessed,
/// in a given transaction.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum AccessType {
/// When in [FootprintMode::Recording], indicates that the [LedgerKey] is only read.
/// When in [FootprintMode::Enforcing], indicates that the [LedgerKey] is only _allowed_ to be read.
ReadOnly,
/// When in [FootprintMode::Recording], indicates that the [LedgerKey] is written (and also possibly read)
/// When in [FootprintMode::Enforcing], indicates that the [LedgerKey] is _allowed_ to be written (and also allowed to be read).
ReadWrite,
}
/// A helper type used by [FootprintMode::Recording] to provide access
/// to a stable read-snapshot of a ledger.
pub trait SnapshotSource {
// Returns the ledger entry for the key and its live_until ledger.
fn get(&self, key: &Rc<LedgerKey>) -> Result<EntryWithLiveUntil, HostError>;
fn has(&self, key: &Rc<LedgerKey>) -> Result<bool, HostError>;
}
/// Describes the total set of [LedgerKey]s that a given transaction
/// will access, as well as the [AccessType] governing each key.
///
/// A [Footprint] must be provided in order to run a transaction that
/// accesses any [LedgerKey]s in [FootprintMode::Enforcing]. If a
/// transaction has an unknown [Footprint] it can be calculated by
/// running a "preflight" execution in [FootprintMode::Recording],
/// against a suitably fresh [SnapshotSource].
// Notes on metering: covered by the underneath `MeteredOrdMap`.
#[derive(Clone, Default)]
pub struct Footprint(pub FootprintMap);
impl Footprint {
pub fn record_access(
&mut self,
key: &Rc<LedgerKey>,
ty: AccessType,
budget: &Budget,
) -> Result<(), HostError> {
if let Some(existing) = self.0.get::<Rc<LedgerKey>>(key, budget)? {
match (existing, ty) {
(AccessType::ReadOnly, AccessType::ReadOnly) => Ok(()),
(AccessType::ReadOnly, AccessType::ReadWrite) => {
// The only interesting case is an upgrade
// from previously-read-only to read-write.
self.0 = self.0.insert(Rc::clone(key), ty, budget)?;
Ok(())
}
(AccessType::ReadWrite, AccessType::ReadOnly) => Ok(()),
(AccessType::ReadWrite, AccessType::ReadWrite) => Ok(()),
}
} else {
self.0 = self.0.insert(Rc::clone(key), ty, budget)?;
Ok(())
}
}
pub fn enforce_access(
&mut self,
key: &Rc<LedgerKey>,
ty: AccessType,
budget: &Budget,
) -> Result<(), HostError> {
// `ExceededLimit` is not the most precise term here, but footprint has
// to be externally supplied in a similar fashion to budget and it's
// also representing an execution resource limit (number of ledger
// entries to access), so it might be considered 'exceeded'.
// This also helps distinguish access errors from the values simply
// being missing from storage (but with a valid footprint).
if let Some(existing) = self.0.get::<Rc<LedgerKey>>(key, budget)? {
match (existing, ty) {
(AccessType::ReadOnly, AccessType::ReadOnly) => Ok(()),
(AccessType::ReadOnly, AccessType::ReadWrite) => {
Err((ScErrorType::Storage, ScErrorCode::ExceededLimit).into())
}
(AccessType::ReadWrite, AccessType::ReadOnly) => Ok(()),
(AccessType::ReadWrite, AccessType::ReadWrite) => Ok(()),
}
} else {
Err((ScErrorType::Storage, ScErrorCode::ExceededLimit).into())
}
}
}
#[derive(Clone, Default)]
pub enum FootprintMode {
Recording(Rc<dyn SnapshotSource>),
#[default]
Enforcing,
}
/// A special-purpose map from [LedgerKey]s to [LedgerEntry]s. Represents a
/// transactional batch of contract IO from and to durable storage, while
/// partitioning that IO between concurrently executing groups of contracts
/// through the use of IO [Footprint]s.
///
/// Specifically: access to each [LedgerKey] is mediated by the [Footprint],
/// which may be in either [FootprintMode::Recording] or
/// [FootprintMode::Enforcing] mode.
///
/// [FootprintMode::Recording] mode is used to calculate [Footprint]s during
/// "preflight" execution of a contract. Once calculated, a recorded [Footprint]
/// can be provided to "real" execution, which always runs in
/// [FootprintMode::Enforcing] mode and enforces partitioned access.
#[derive(Clone, Default)]
pub struct Storage {
pub footprint: Footprint,
pub mode: FootprintMode,
pub map: StorageMap,
}
// Notes on metering: all storage operations: `put`, `get`, `del`, `has` are
// covered by the underlying [MeteredOrdMap] and the [Footprint]'s own map.
impl Storage {
/// Only a subset of Stellar's XDR ledger key or entry types are supported
/// by Soroban: accounts, trustlines, contract code and data. The rest are
/// never used by stellar-core when interacting with the Soroban host, nor
/// does the Soroban host ever generate any. Therefore the storage system
/// will reject them with [ScErrorCode::InternalError] if they ever occur.
pub fn check_supported_ledger_entry_type(le: &LedgerEntry) -> Result<(), HostError> {
use crate::xdr::LedgerEntryData::*;
match le.data {
Account(_) | Trustline(_) | ContractData(_) | ContractCode(_) => Ok(()),
Offer(_) | Data(_) | ClaimableBalance(_) | LiquidityPool(_) | ConfigSetting(_)
| Ttl(_) => Err((ScErrorType::Storage, ScErrorCode::InternalError).into()),
}
}
/// Only a subset of Stellar's XDR ledger key or entry types are supported
/// by Soroban: accounts, trustlines, contract code and data. The rest are
/// never used by stellar-core when interacting with the Soroban host, nor
/// does the Soroban host ever generate any. Therefore the storage system
/// will reject them with [ScErrorCode::InternalError] if they ever occur.
pub fn check_supported_ledger_key_type(lk: &LedgerKey) -> Result<(), HostError> {
use LedgerKey::*;
match lk {
Account(_) | Trustline(_) | ContractData(_) | ContractCode(_) => Ok(()),
Offer(_) | Data(_) | ClaimableBalance(_) | LiquidityPool(_) | ConfigSetting(_)
| Ttl(_) => Err((ScErrorType::Storage, ScErrorCode::InternalError).into()),
}
}
/// Constructs a new [Storage] in [FootprintMode::Enforcing] using a
/// given [Footprint] and a storage map populated with all the keys
/// listed in the [Footprint].
pub fn with_enforcing_footprint_and_map(footprint: Footprint, map: StorageMap) -> Self {
Self {
mode: FootprintMode::Enforcing,
footprint,
map,
}
}
/// Constructs a new [Storage] in [FootprintMode::Recording] using a
/// given [SnapshotSource].
pub fn with_recording_footprint(src: Rc<dyn SnapshotSource>) -> Self {
Self {
mode: FootprintMode::Recording(src),
footprint: Footprint::default(),
map: Default::default(),
}
}
// Helper function the next 3 `get`-variants funnel into.
fn try_get_full(
&mut self,
key: &Rc<LedgerKey>,
budget: &Budget,
) -> Result<Option<EntryWithLiveUntil>, HostError> {
let _span = tracy_span!("storage get");
Self::check_supported_ledger_key_type(key)?;
self.prepare_read_only_access(key, budget)?;
match self.map.get::<Rc<LedgerKey>>(key, budget)? {
// Key has to be in the storage map at this point due to
// `prepare_read_only_access`.
None => Err((ScErrorType::Storage, ScErrorCode::InternalError).into()),
Some(pair_option) => Ok(pair_option.clone()),
}
}
/// Attempts to retrieve the [LedgerEntry] associated with a given
/// [LedgerKey] in the [Storage], returning an error if the key is not
/// found.
///
/// In [FootprintMode::Recording] mode, records the read [LedgerKey] in the
/// [Footprint] as [AccessType::ReadOnly] (unless already recorded as
/// [AccessType::ReadWrite]) and reads through to the underlying
/// [SnapshotSource], if the [LedgerKey] has not yet been loaded.
///
/// In [FootprintMode::Enforcing] mode, succeeds only if the read
/// [LedgerKey] has been declared in the [Footprint].
pub fn get(
&mut self,
key: &Rc<LedgerKey>,
budget: &Budget,
) -> Result<Rc<LedgerEntry>, HostError> {
self.try_get(key, budget)?
.ok_or_else(|| (ScErrorType::Storage, ScErrorCode::MissingValue).into())
}
// Like `get`, but distinguishes between missing values (return `Ok(None)`)
// and out-of-footprint values or errors (`Err(...)`).
pub(crate) fn try_get(
&mut self,
key: &Rc<LedgerKey>,
budget: &Budget,
) -> Result<Option<Rc<LedgerEntry>>, HostError> {
self.try_get_full(key, budget)
.map(|ok| ok.map(|pair| pair.0))
}
/// Attempts to retrieve the [LedgerEntry] associated with a given
/// [LedgerKey] and its live until ledger (if applicable) in the [Storage],
/// returning an error if the key is not found.
///
/// Live until ledgers only exist for `ContractData` and `ContractCode`
/// ledger entries and are `None` for all the other entry kinds.
///
/// In [FootprintMode::Recording] mode, records the read [LedgerKey] in the
/// [Footprint] as [AccessType::ReadOnly] (unless already recorded as
/// [AccessType::ReadWrite]) and reads through to the underlying
/// [SnapshotSource], if the [LedgerKey] has not yet been loaded.
///
/// In [FootprintMode::Enforcing] mode, succeeds only if the read
/// [LedgerKey] has been declared in the [Footprint].
pub(crate) fn get_with_live_until_ledger(
&mut self,
key: &Rc<LedgerKey>,
budget: &Budget,
) -> Result<EntryWithLiveUntil, HostError> {
self.try_get_full(key, budget)?
.ok_or_else(|| (ScErrorType::Storage, ScErrorCode::MissingValue).into())
}
// Helper function `put` and `del` funnel into.
fn put_opt(
&mut self,
key: &Rc<LedgerKey>,
val: Option<EntryWithLiveUntil>,
budget: &Budget,
) -> Result<(), HostError> {
Self::check_supported_ledger_key_type(key)?;
if let Some(le) = &val {
Self::check_supported_ledger_entry_type(&le.0)?;
}
let ty = AccessType::ReadWrite;
match self.mode {
FootprintMode::Recording(_) => {
self.footprint.record_access(key, ty, budget)?;
}
FootprintMode::Enforcing => {
self.footprint.enforce_access(key, ty, budget)?;
}
};
self.map = self.map.insert(Rc::clone(key), val, budget)?;
Ok(())
}
/// Attempts to write to the [LedgerEntry] associated with a given
/// [LedgerKey] in the [Storage].
///
/// In [FootprintMode::Recording] mode, records the written [LedgerKey] in
/// the [Footprint] as [AccessType::ReadWrite].
///
/// In [FootprintMode::Enforcing] mode, succeeds only if the written
/// [LedgerKey] has been declared in the [Footprint] as
/// [AccessType::ReadWrite].
pub fn put(
&mut self,
key: &Rc<LedgerKey>,
val: &Rc<LedgerEntry>,
live_until_ledger: Option<u32>,
budget: &Budget,
) -> Result<(), HostError> {
let _span = tracy_span!("storage put");
self.put_opt(key, Some((val.clone(), live_until_ledger)), budget)
}
/// Attempts to delete the [LedgerEntry] associated with a given [LedgerKey]
/// in the [Storage].
///
/// In [FootprintMode::Recording] mode, records the deleted [LedgerKey] in
/// the [Footprint] as [AccessType::ReadWrite].
///
/// In [FootprintMode::Enforcing] mode, succeeds only if the deleted
/// [LedgerKey] has been declared in the [Footprint] as
/// [AccessType::ReadWrite].
pub fn del(&mut self, key: &Rc<LedgerKey>, budget: &Budget) -> Result<(), HostError> {
let _span = tracy_span!("storage del");
self.put_opt(key, None, budget)
}
/// Attempts to determine the presence of a [LedgerEntry] associated with a
/// given [LedgerKey] in the [Storage], returning `Ok(true)` if an entry
/// with the key exists and `Ok(false)` if it does not.
///
/// In [FootprintMode::Recording] mode, records the access and reads-through
/// to the underlying [SnapshotSource].
///
/// In [FootprintMode::Enforcing] mode, succeeds only if the access has been
/// declared in the [Footprint].
pub fn has(&mut self, key: &Rc<LedgerKey>, budget: &Budget) -> Result<bool, HostError> {
let _span = tracy_span!("storage has");
Self::check_supported_ledger_key_type(key)?;
self.prepare_read_only_access(key, budget)?;
Ok(self
.map
.get::<Rc<LedgerKey>>(key, budget)?
// Key has to be present in storage at this point, so not having it
// would be an internal error.
.ok_or_else(|| HostError::from((ScErrorType::Storage, ScErrorCode::InternalError)))?
.is_some())
}
/// Extends `key` to live `extend_to` ledgers from now (not counting the
/// current ledger) if the current live_until_ledger for the entry is
/// `threshold` ledgers or less away from the current ledger.
///
/// This operation is only defined within a host as it relies on ledger
/// state.
///
/// This operation does not modify any ledger entries, but does change the
/// internal storage
pub(crate) fn extend(
&mut self,
host: &Host,
key: Rc<LedgerKey>,
threshold: u32,
extend_to: u32,
) -> Result<(), HostError> {
let _span = tracy_span!("extend key");
Self::check_supported_ledger_key_type(&key)?;
if threshold > extend_to {
return Err(host.err(
ScErrorType::Storage,
ScErrorCode::InvalidInput,
"threshold must be <= extend_to",
&[threshold.into(), extend_to.into()],
));
}
// Extending deleted/non-existing/out-of-footprint entries will result in
// an error.
let (entry, old_live_until) = self.get_with_live_until_ledger(&key, host.budget_ref())?;
let old_live_until = old_live_until.ok_or_else(|| {
host.err(
ScErrorType::Storage,
ScErrorCode::InternalError,
"trying to extend invalid entry",
&[],
)
})?;
let ledger_seq: u32 = host.get_ledger_sequence()?.into();
if old_live_until < ledger_seq {
return Err(host.err(
ScErrorType::Storage,
ScErrorCode::InternalError,
"accessing no-longer-live entry",
&[old_live_until.into(), ledger_seq.into()],
));
}
let new_live_until =
host.with_ledger_info(|li| Ok(li.sequence_number.saturating_add(extend_to)))?;
if new_live_until > host.max_live_until_ledger()? {
return Err(host.err(
ScErrorType::Storage,
ScErrorCode::InvalidAction,
"trying to extend past max live_until ledger",
&[new_live_until.into()],
));
}
if new_live_until > old_live_until && old_live_until.saturating_sub(ledger_seq) <= threshold
{
self.map = self.map.insert(
key,
Some((entry.clone(), Some(new_live_until))),
host.budget_ref(),
)?;
}
Ok(())
}
fn prepare_read_only_access(
&mut self,
key: &Rc<LedgerKey>,
budget: &Budget,
) -> Result<(), HostError> {
let ty = AccessType::ReadOnly;
match self.mode {
FootprintMode::Recording(ref src) => {
self.footprint.record_access(key, ty, budget)?;
// In recording mode we treat the map as a cache
// that misses read-through to the underlying src.
if !self.map.contains_key::<Rc<LedgerKey>>(key, budget)? {
let value = if src.has(&key)? {
Some(src.get(key)?)
} else {
None
};
self.map = self.map.insert(key.clone(), value, budget)?;
}
}
FootprintMode::Enforcing => {
self.footprint.enforce_access(key, ty, budget)?;
}
};
Ok(())
}
}