-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathasc_heap.rs
More file actions
161 lines (142 loc) · 4.54 KB
/
asc_heap.rs
File metadata and controls
161 lines (142 loc) · 4.54 KB
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
use std::mem::MaybeUninit;
use semver::Version;
use super::{
gas::GasCounter, AscIndexId, AscPtr, AscType, DeterministicHostError, HostExportError,
IndexForAscTypeId,
};
// A 128 limit is plenty for any subgraph, while the `fn recursion_limit` test ensures it is not
// large enough to cause stack overflows.
const MAX_RECURSION_DEPTH: usize = 128;
/// A type that can read and write to the Asc heap. Call `asc_new` and `asc_get`
/// for reading and writing Rust structs from and to Asc.
///
/// The implementor must provide the direct Asc interface with `raw_new` and `get`.
pub trait AscHeap {
/// Allocate new space and write `bytes`, return the allocated address.
fn raw_new(&mut self, bytes: &[u8], gas: &GasCounter) -> Result<u32, DeterministicHostError>;
fn read<'a>(
&self,
offset: u32,
buffer: &'a mut [MaybeUninit<u8>],
gas: &GasCounter,
) -> Result<&'a mut [u8], DeterministicHostError>;
fn read_u32(&self, offset: u32, gas: &GasCounter) -> Result<u32, DeterministicHostError>;
fn api_version(&self) -> &Version;
fn asc_type_id(&mut self, type_id_index: IndexForAscTypeId) -> Result<u32, HostExportError>;
}
/// Instantiate `rust_obj` as an Asc object of class `C`.
/// Returns a pointer to the Asc heap.
///
/// This operation is expensive as it requires a call to `raw_new` for every
/// nested object.
pub fn asc_new<C, T: ?Sized, H: AscHeap + ?Sized>(
heap: &mut H,
rust_obj: &T,
gas: &GasCounter,
) -> Result<AscPtr<C>, HostExportError>
where
C: AscType + AscIndexId,
T: ToAscObj<C>,
{
let obj = rust_obj.to_asc_obj(heap, gas)?;
AscPtr::alloc_obj(obj, heap, gas)
}
/// Map an optional object to its Asc equivalent if Some, otherwise return a missing field error.
pub fn asc_new_or_missing<H, O, A>(
heap: &mut H,
object: &Option<O>,
gas: &GasCounter,
type_name: &str,
field_name: &str,
) -> Result<AscPtr<A>, HostExportError>
where
H: AscHeap + ?Sized,
O: ToAscObj<A>,
A: AscType + AscIndexId,
{
match object {
Some(o) => asc_new(heap, o, gas),
None => Err(missing_field_error(type_name, field_name)),
}
}
/// Map an optional object to its Asc equivalent if Some, otherwise return null.
pub fn asc_new_or_null<H, O, A>(
heap: &mut H,
object: &Option<O>,
gas: &GasCounter,
) -> Result<AscPtr<A>, HostExportError>
where
H: AscHeap + ?Sized,
O: ToAscObj<A>,
A: AscType + AscIndexId,
{
match object {
Some(o) => asc_new(heap, o, gas),
None => Ok(AscPtr::null()),
}
}
/// Create an error for a missing field in a type.
fn missing_field_error(type_name: &str, field_name: &str) -> HostExportError {
DeterministicHostError::from(anyhow::anyhow!("{} missing {}", type_name, field_name)).into()
}
/// Read the rust representation of an Asc object of class `C`.
///
/// This operation is expensive as it requires a call to `get` for every
/// nested object.
pub fn asc_get<T, C, H: AscHeap + ?Sized>(
heap: &H,
asc_ptr: AscPtr<C>,
gas: &GasCounter,
mut depth: usize,
) -> Result<T, DeterministicHostError>
where
C: AscType + AscIndexId,
T: FromAscObj<C>,
{
depth += 1;
if depth > MAX_RECURSION_DEPTH {
return Err(DeterministicHostError::Other(anyhow::anyhow!(
"recursion limit reached"
)));
}
T::from_asc_obj(asc_ptr.read_ptr(heap, gas)?, heap, gas, depth)
}
/// Type that can be converted to an Asc object of class `C`.
pub trait ToAscObj<C: AscType> {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<C, HostExportError>;
}
impl<C: AscType, T: ToAscObj<C>> ToAscObj<C> for &T {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<C, HostExportError> {
(*self).to_asc_obj(heap, gas)
}
}
impl ToAscObj<bool> for bool {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
_heap: &mut H,
_gas: &GasCounter,
) -> Result<bool, HostExportError> {
Ok(*self)
}
}
/// Type that can be converted from an Asc object of class `C`.
///
/// ### Overflow protection
/// The `depth` parameter is used to prevent stack overflows, it measures how many `asc_get` calls
/// have been made. `from_asc_obj` does not need to increment the depth, only pass it through.
pub trait FromAscObj<C: AscType>: Sized {
fn from_asc_obj<H: AscHeap + ?Sized>(
obj: C,
heap: &H,
gas: &GasCounter,
depth: usize,
) -> Result<Self, DeterministicHostError>;
}