Skip to content

Commit eaae48d

Browse files
committed
runtime: Make DeterministicHostError field private
1 parent 2e94a2a commit eaae48d

File tree

14 files changed

+78
-60
lines changed

14 files changed

+78
-60
lines changed

chain/near/src/runtime/abi.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl ToAscObj<AscActionReceipt> for codec::Receipt {
119119
let action = match self.receipt.as_ref().unwrap() {
120120
codec::receipt::Receipt::Action(action) => action,
121121
codec::receipt::Receipt::Data(_) => {
122-
return Err(DeterministicHostError(anyhow!(
122+
return Err(DeterministicHostError::from(anyhow!(
123123
"Data receipt are now allowed"
124124
)));
125125
}
@@ -413,12 +413,12 @@ impl ToAscObj<AscSuccessStatusEnum> for codec::execution_outcome::Status {
413413
asc_new(heap, receipt_id.id.as_ref().unwrap())?.to_payload(),
414414
),
415415
codec::execution_outcome::Status::Failure(_) => {
416-
return Err(DeterministicHostError(anyhow!(
416+
return Err(DeterministicHostError::from(anyhow!(
417417
"Failure execution status are not allowed"
418418
)));
419419
}
420420
codec::execution_outcome::Status::Unknown(_) => {
421-
return Err(DeterministicHostError(anyhow!(
421+
return Err(DeterministicHostError::from(anyhow!(
422422
"Unknown execution status are not allowed"
423423
)));
424424
}
@@ -443,7 +443,7 @@ impl ToAscObj<AscMerklePathItem> for codec::MerklePathItem {
443443
0 => AscDirection::Left,
444444
1 => AscDirection::Right,
445445
x => {
446-
return Err(DeterministicHostError(anyhow!(
446+
return Err(DeterministicHostError::from(anyhow!(
447447
"Invalid direction value {}",
448448
x
449449
)))
@@ -474,7 +474,7 @@ impl ToAscObj<AscSignature> for codec::Signature {
474474
0 => 0,
475475
1 => 1,
476476
value => {
477-
return Err(DeterministicHostError(anyhow!(
477+
return Err(DeterministicHostError::from(anyhow!(
478478
"Invalid signature type {}",
479479
value,
480480
)))
@@ -506,7 +506,7 @@ impl ToAscObj<AscPublicKey> for codec::PublicKey {
506506
0 => 0,
507507
1 => 1,
508508
value => {
509-
return Err(DeterministicHostError(anyhow!(
509+
return Err(DeterministicHostError::from(anyhow!(
510510
"Invalid public key type {}",
511511
value,
512512
)))

chain/near/src/trigger.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,22 +439,24 @@ mod tests {
439439
fn get(&self, offset: u32, size: u32) -> Result<Vec<u8>, DeterministicHostError> {
440440
let memory_byte_count = self.memory.len();
441441
if memory_byte_count == 0 {
442-
return Err(DeterministicHostError(anyhow!("No memory is allocated")));
442+
return Err(DeterministicHostError::from(anyhow!(
443+
"No memory is allocated"
444+
)));
443445
}
444446

445447
let start_offset = offset as usize;
446448
let end_offset_exclusive = start_offset + size as usize;
447449

448450
if start_offset >= memory_byte_count {
449-
return Err(DeterministicHostError(anyhow!(
451+
return Err(DeterministicHostError::from(anyhow!(
450452
"Start offset {} is outside of allocated memory, max offset is {}",
451453
start_offset,
452454
memory_byte_count - 1
453455
)));
454456
}
455457

456458
if end_offset_exclusive > memory_byte_count {
457-
return Err(DeterministicHostError(anyhow!(
459+
return Err(DeterministicHostError::from(anyhow!(
458460
"End of offset {} is outside of allocated memory, max offset is {}",
459461
end_offset_exclusive,
460462
memory_byte_count - 1

graph/src/components/subgraph/host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl From<anyhow::Error> for MappingError {
2626

2727
impl From<DeterministicHostError> for MappingError {
2828
fn from(value: DeterministicHostError) -> MappingError {
29-
MappingError::Unknown(value.0)
29+
MappingError::Unknown(value.inner())
3030
}
3131
}
3232

graph/src/runtime/asc_ptr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ impl<C: AscType> AscPtr<C> {
158158
self.check_is_not_null()?;
159159

160160
let start_of_rt_size = self.0.checked_sub(SIZE_OF_RT_SIZE).ok_or_else(|| {
161-
DeterministicHostError(anyhow::anyhow!("Subtract overflow on pointer: {}", self.0))
161+
DeterministicHostError::from(anyhow::anyhow!(
162+
"Subtract overflow on pointer: {}",
163+
self.0
164+
))
162165
})?;
163166
let raw_bytes = heap.get(start_of_rt_size, size_of::<u32>() as u32)?;
164167
let mut u32_bytes: [u8; size_of::<u32>()] = [0; size_of::<u32>()];
@@ -183,7 +186,7 @@ impl<C: AscType> AscPtr<C> {
183186
/// Summary: ALWAYS call this before reading an AscPtr.
184187
pub fn check_is_not_null(&self) -> Result<(), DeterministicHostError> {
185188
if self.is_null() {
186-
return Err(DeterministicHostError(anyhow::anyhow!(
189+
return Err(DeterministicHostError::from(anyhow::anyhow!(
187190
"Tried to read AssemblyScript value that is 'null'. Suggestion: look into the function that the error happened and add 'log' calls till you find where a 'null' value is being used as non-nullable. It's likely that you're calling a 'graph-ts' function (or operator) with a 'null' value when it doesn't support it."
188191
)));
189192
}

graph/src/runtime/gas/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl GasCounter {
9494
.unwrap();
9595
let new = old.saturating_add(amount.0);
9696
if new >= *MAX_GAS_PER_HANDLER {
97-
Err(DeterministicHostError(anyhow::anyhow!(
97+
Err(DeterministicHostError::from(anyhow::anyhow!(
9898
"Gas limit exceeded. Used: {}",
9999
new
100100
)))

graph/src/runtime/mod.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl AscType for bool {
9494
_api_version: &Version,
9595
) -> Result<Self, DeterministicHostError> {
9696
if asc_obj.len() != 1 {
97-
Err(DeterministicHostError(anyhow::anyhow!(
97+
Err(DeterministicHostError::from(anyhow::anyhow!(
9898
"Incorrect size for bool. Expected 1, got {},",
9999
asc_obj.len()
100100
)))
@@ -117,7 +117,7 @@ macro_rules! impl_asc_type {
117117

118118
fn from_asc_bytes(asc_obj: &[u8], _api_version: &Version) -> Result<Self, DeterministicHostError> {
119119
let bytes = asc_obj.try_into().map_err(|_| {
120-
DeterministicHostError(anyhow::anyhow!(
120+
DeterministicHostError::from(anyhow::anyhow!(
121121
"Incorrect size for {}. Expected {}, got {},",
122122
stringify!($T),
123123
size_of::<Self>(),
@@ -252,14 +252,26 @@ impl ToAscObj<u32> for IndexForAscTypeId {
252252
}
253253

254254
#[derive(Debug)]
255-
pub struct DeterministicHostError(pub Error);
255+
pub struct DeterministicHostError(Error);
256+
257+
impl DeterministicHostError {
258+
pub fn inner(self) -> Error {
259+
self.0
260+
}
261+
}
256262

257263
impl fmt::Display for DeterministicHostError {
258264
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259265
self.0.fmt(f)
260266
}
261267
}
262268

269+
impl From<Error> for DeterministicHostError {
270+
fn from(e: Error) -> DeterministicHostError {
271+
DeterministicHostError(e)
272+
}
273+
}
274+
263275
impl std::error::Error for DeterministicHostError {}
264276

265277
#[derive(thiserror::Error, Debug)]

runtime/derive/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ fn asc_type_derive_struct(item_struct: ItemStruct) -> TokenStream {
102102
// that contains both versions (each in a variant), and that increased
103103
// the memory size, so that's why we use less than.
104104
if asc_obj.len() < std::mem::size_of::<Self>() {
105-
return Err(graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("Size does not match")));
105+
return Err(graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("Size does not match")));
106106
}
107107
}
108108
_ => {
109109
let content_size = std::mem::size_of::<Self>();
110110
let aligned_size = graph::runtime::padding_to_16(content_size);
111111

112112
if graph::runtime::HEADER_SIZE + asc_obj.len() == aligned_size + content_size {
113-
return Err(graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("Size does not match")));
113+
return Err(graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("Size does not match")));
114114
}
115115
},
116116
};
@@ -120,7 +120,7 @@ fn asc_type_derive_struct(item_struct: ItemStruct) -> TokenStream {
120120
#(
121121
let field_size = std::mem::size_of::<#field_types>();
122122
let field_data = asc_obj.get(offset..(offset + field_size)).ok_or_else(|| {
123-
graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("Attempted to read past end of array"))
123+
graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("Attempted to read past end of array"))
124124
})?;
125125
let #field_names2 = graph::runtime::AscType::from_asc_bytes(&field_data, api_version)?;
126126
offset += field_size;
@@ -163,7 +163,7 @@ fn asc_type_derive_struct(item_struct: ItemStruct) -> TokenStream {
163163
// fn from_asc_bytes(asc_obj: &[u8], _api_version: graph::semver::Version) -> Result<Self, graph::runtime::DeterministicHostError> {
164164
// let mut u32_bytes: [u8; size_of::<u32>()] = [0; size_of::<u32>()];
165165
// if std::mem::size_of_val(&u32_bytes) != std::mem::size_of_val(&asc_obj) {
166-
// return Err(graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("Invalid asc bytes size")));
166+
// return Err(graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("Invalid asc bytes size")));
167167
// }
168168
// u32_bytes.copy_from_slice(&asc_obj);
169169
// let discr = u32::from_le_bytes(u32_bytes);
@@ -174,7 +174,7 @@ fn asc_type_derive_struct(item_struct: ItemStruct) -> TokenStream {
174174
// 3u32 => JsonValueKind::String,
175175
// 4u32 => JsonValueKind::Array,
176176
// 5u32 => JsonValueKind::Object,
177-
// _ => Err(graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("value {} is out of range for {}", discr, "JsonValueKind"))),
177+
// _ => Err(graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("value {} is out of range for {}", discr, "JsonValueKind"))),
178178
// }
179179
// }
180180
// }
@@ -206,11 +206,11 @@ fn asc_type_derive_enum(item_enum: ItemEnum) -> TokenStream {
206206

207207
fn from_asc_bytes(asc_obj: &[u8], _api_version: &graph::semver::Version) -> Result<Self, graph::runtime::DeterministicHostError> {
208208
let u32_bytes = ::std::convert::TryFrom::try_from(asc_obj)
209-
.map_err(|_| graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("Invalid asc bytes size")))?;
209+
.map_err(|_| graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("Invalid asc bytes size")))?;
210210
let discr = u32::from_le_bytes(u32_bytes);
211211
match discr {
212212
#(#variant_discriminant2 => Ok(#enum_name_iter2::#variant_paths2),)*
213-
_ => Err(graph::runtime::DeterministicHostError(graph::prelude::anyhow::anyhow!("value {} is out of range for {}", discr, stringify!(#enum_name))))
213+
_ => Err(graph::runtime::DeterministicHostError::from(graph::prelude::anyhow::anyhow!("value {} is out of range for {}", discr, stringify!(#enum_name))))
214214
}
215215
}
216216
}

runtime/wasm/src/asc_abi/v0_0_4.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl ArrayBuffer {
3232
}
3333

3434
if content.len() > u32::max_value() as usize {
35-
return Err(DeterministicHostError(anyhow::anyhow!(
35+
return Err(DeterministicHostError::from(anyhow::anyhow!(
3636
"slice cannot fit in WASM memory"
3737
)));
3838
}
@@ -69,7 +69,7 @@ impl ArrayBuffer {
6969
self.content
7070
.get(range)
7171
.ok_or_else(|| {
72-
DeterministicHostError(anyhow::anyhow!("Attempted to read past end of array"))
72+
DeterministicHostError::from(anyhow::anyhow!("Attempted to read past end of array"))
7373
})?
7474
.chunks_exact(size_of::<T>())
7575
.map(|bytes| T::from_asc_bytes(bytes))
@@ -106,10 +106,10 @@ impl AscType for ArrayBuffer {
106106
// Skip `byte_length` and the padding.
107107
let content_offset = size_of::<u32>() + 4;
108108
let byte_length = asc_obj.get(..size_of::<u32>()).ok_or_else(|| {
109-
DeterministicHostError(anyhow!("Attempted to read past end of array"))
109+
DeterministicHostError::from(anyhow!("Attempted to read past end of array"))
110110
})?;
111111
let content = asc_obj.get(content_offset..).ok_or_else(|| {
112-
DeterministicHostError(anyhow!("Attempted to read past end of array"))
112+
DeterministicHostError::from(anyhow!("Attempted to read past end of array"))
113113
})?;
114114
Ok(ArrayBuffer {
115115
byte_length: u32::from_asc_bytes(&byte_length, api_version)?,
@@ -188,7 +188,7 @@ pub struct AscString {
188188
impl AscString {
189189
pub fn new(content: &[u16]) -> Result<Self, DeterministicHostError> {
190190
if size_of_val(content) > u32::max_value() as usize {
191-
return Err(DeterministicHostError(anyhow!(
191+
return Err(DeterministicHostError::from(anyhow!(
192192
"string cannot fit in WASM memory"
193193
)));
194194
}
@@ -229,21 +229,21 @@ impl AscType for AscString {
229229

230230
let length = asc_obj
231231
.get(..offset)
232-
.ok_or(DeterministicHostError(anyhow::anyhow!(
232+
.ok_or(DeterministicHostError::from(anyhow::anyhow!(
233233
"String bytes not long enough to contain length"
234234
)))?;
235235

236236
// Does not panic - already validated slice length == size_of::<i32>.
237237
let length = i32::from_le_bytes(length.try_into().unwrap());
238238
if length.checked_mul(2).and_then(|l| l.checked_add(4)) != asc_obj.len().try_into().ok() {
239-
return Err(DeterministicHostError(anyhow::anyhow!(
239+
return Err(DeterministicHostError::from(anyhow::anyhow!(
240240
"String length header does not equal byte length"
241241
)));
242242
}
243243

244244
// Prevents panic when accessing offset + 1 in the loop
245245
if asc_obj.len() % 2 != 0 {
246-
return Err(DeterministicHostError(anyhow::anyhow!(
246+
return Err(DeterministicHostError::from(anyhow::anyhow!(
247247
"Invalid string length"
248248
)));
249249
}
@@ -279,7 +279,7 @@ impl AscType for AscString {
279279
let data_size = code_point_size.checked_mul(length);
280280
let total_size = data_size.and_then(|d| d.checked_add(length_size));
281281
total_size.ok_or_else(|| {
282-
DeterministicHostError(anyhow::anyhow!("Overflowed when getting size of string"))
282+
DeterministicHostError::from(anyhow::anyhow!("Overflowed when getting size of string"))
283283
})
284284
}
285285
}

runtime/wasm/src/asc_abi/v0_0_5.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ArrayBuffer {
2929
}
3030

3131
if content.len() > u32::max_value() as usize {
32-
return Err(DeterministicHostError(anyhow::anyhow!(
32+
return Err(DeterministicHostError::from(anyhow::anyhow!(
3333
"slice cannot fit in WASM memory"
3434
)));
3535
}
@@ -141,7 +141,7 @@ impl<T: AscValue> TypedArray<T> {
141141
.data_start
142142
.checked_sub(self.buffer.wasm_ptr())
143143
.ok_or_else(|| {
144-
DeterministicHostError(anyhow::anyhow!(
144+
DeterministicHostError::from(anyhow::anyhow!(
145145
"Subtract overflow on pointer: {}",
146146
self.data_start
147147
))
@@ -169,7 +169,7 @@ pub struct AscString {
169169
impl AscString {
170170
pub fn new(content: &[u16]) -> Result<Self, DeterministicHostError> {
171171
if size_of_val(content) > u32::max_value() as usize {
172-
return Err(DeterministicHostError(anyhow!(
172+
return Err(DeterministicHostError::from(anyhow!(
173173
"string cannot fit in WASM memory"
174174
)));
175175
}
@@ -222,7 +222,7 @@ impl AscType for AscString {
222222
let code_point_bytes = [
223223
pair[0],
224224
*pair.get(1).ok_or_else(|| {
225-
DeterministicHostError(anyhow!(
225+
DeterministicHostError::from(anyhow!(
226226
"Attempted to read past end of string content bytes chunk"
227227
))
228228
})?,
@@ -290,7 +290,7 @@ impl<T: AscValue> Array<T> {
290290
.buffer_data_start
291291
.checked_sub(self.buffer.wasm_ptr())
292292
.ok_or_else(|| {
293-
DeterministicHostError(anyhow::anyhow!(
293+
DeterministicHostError::from(anyhow::anyhow!(
294294
"Subtract overflow on pointer: {}",
295295
self.buffer_data_start
296296
))

runtime/wasm/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ impl IntoTrap for DeterministicHostError {
2626
DeterminismLevel::Deterministic
2727
}
2828
fn into_trap(self) -> Trap {
29-
Trap::from(self.0)
29+
Trap::from(self.inner())
3030
}
3131
}

0 commit comments

Comments
 (0)