From b6c0a78a4a3651317dd6793c48576a05bd82c71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wo=C5=9B?= Date: Wed, 13 Sep 2023 12:06:15 +0200 Subject: [PATCH] improve ealy_exit function --- packages/fuel-indexer-macros/src/wasm.rs | 3 +-- packages/fuel-indexer-plugin/src/lib.rs | 3 +++ packages/fuel-indexer-plugin/src/wasm.rs | 13 ++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/fuel-indexer-macros/src/wasm.rs b/packages/fuel-indexer-macros/src/wasm.rs index 8d3a0ed7b..95838c13b 100644 --- a/packages/fuel-indexer-macros/src/wasm.rs +++ b/packages/fuel-indexer-macros/src/wasm.rs @@ -17,10 +17,9 @@ pub fn handler_block_wasm( let blocks: Vec = match deserialize(&bytes) { Ok(blocks) => blocks, Err(msg) => { - // TODO: probably need some error codes to send back to runtime. core::mem::forget(bytes); Logger::error(&msg); - return; + early_exit(WasmIndexerError::DeserializationError) } }; core::mem::forget(bytes); diff --git a/packages/fuel-indexer-plugin/src/lib.rs b/packages/fuel-indexer-plugin/src/lib.rs index 07127fa3a..2bbfadad8 100644 --- a/packages/fuel-indexer-plugin/src/lib.rs +++ b/packages/fuel-indexer-plugin/src/lib.rs @@ -15,6 +15,9 @@ pub mod types { pub use fuel_indexer_types::fuel::field::*; pub use fuel_indexer_types::{fuel, prelude::*}; + // For use with `early_exit` function to terminate execution on error. + pub use fuel_indexer_lib::WasmIndexerError; + // These imports are used in the indexer.rs module when iterating over // block transactions, in order to cache contract IDs. pub use std::collections::{HashMap, HashSet}; diff --git a/packages/fuel-indexer-plugin/src/wasm.rs b/packages/fuel-indexer-plugin/src/wasm.rs index a42a18b24..d0e8970c5 100644 --- a/packages/fuel-indexer-plugin/src/wasm.rs +++ b/packages/fuel-indexer-plugin/src/wasm.rs @@ -25,7 +25,7 @@ extern "C" { fn ff_log_data(ptr: *const u8, len: u32, log_level: u32); fn ff_put_object(type_id: i64, ptr: *const u8, len: u32); fn ff_put_many_to_many_record(ptr: *const u8, len: u32); - fn ff_early_exit(err_code: i32); + fn ff_early_exit(err_code: u32); } // TODO: more to do here, hook up to 'impl log::Log for Logger' @@ -102,8 +102,7 @@ pub trait Entity<'a>: Sized + PartialEq + Eq + std::fmt::Debug { let buff = if let Ok(bytes) = bincode::serialize(&id.to_string()) { bytes } else { - ff_early_exit(WasmIndexerError::SerializationError as i32); - unreachable!(); + early_exit(WasmIndexerError::SerializationError); }; let mut bufflen = (buff.len() as u32).to_le_bytes(); @@ -116,8 +115,7 @@ pub trait Entity<'a>: Sized + PartialEq + Eq + std::fmt::Debug { match deserialize(&bytes) { Ok(vec) => Some(Self::from_row(vec)), Err(_) => { - ff_early_exit(WasmIndexerError::DeserializationError as i32); - unreachable!() + early_exit(WasmIndexerError::DeserializationError); } }; } @@ -158,3 +156,8 @@ fn alloc_fn(size: u32) -> *const u8 { fn dealloc_fn(ptr: *mut u8, len: usize) { let _vec = unsafe { Vec::from_raw_parts(ptr, len, len) }; } + +pub fn early_exit(err_code: WasmIndexerError) -> ! { + unsafe { ff_early_exit(err_code as u32) } + unreachable!("Expected termination of WASM exetution after a call to ff_early_exit.") +}