diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index de887aec9f..d8602d8127 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -193,6 +193,12 @@ host_functions! { #[wasm_name = "home_le_arr_len"] fn get_current_ledger_obj_array_len(&self, field: i32) -> HostResult; + /// The number of elements in an array field of a previously cached ledger object, + /// selected by its cache slot and `SField` code. + #[gas = 40] + #[wasm_name = "le_arr_len"] + fn get_ledger_obj_array_len(&self, cache_idx: i32, field: i32) -> HostResult; + /// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512. #[gas = 2000] #[wasm_name = "sha512_half"] diff --git a/crates/xrpl-host-functions/tests/generated_abi.rs b/crates/xrpl-host-functions/tests/generated_abi.rs index f3139674c3..93ae0338ad 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -131,6 +131,14 @@ impl HostFunctions for FakeHost { Ok(field + 1) } + /// The same, over a cached object keyed by slot. + fn get_ledger_obj_array_len(&self, cache_idx: i32, field: i32) -> HostResult { + if cache_idx <= 0 || field < 0 { + return Err(HostError::NoArray); + } + Ok(cache_idx + field) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -192,6 +200,8 @@ fn the_trait_is_implementable() { host.get_current_ledger_obj_array_len(-1), Err(HostError::NoArray) ); + assert_eq!(host.get_ledger_obj_array_len(2, 3), Ok(5)); + assert_eq!(host.get_ledger_obj_array_len(0, 3), Err(HostError::NoArray)); assert_eq!(host.sha512_half(b"abc", &mut out), Ok(HASH_LEN)); assert_eq!(out[0], 3); assert_eq!(host.trace("hello", b"xy", true), Ok(())); @@ -273,6 +283,7 @@ fn the_spec_table_matches_the_declarations() { ("le_inner", 110), ("tx_arr_len", 40), ("home_le_arr_len", 40), + ("le_arr_len", 40), ("sha512_half", 2000), ("trace", 500), ("trace_num", 500), diff --git a/crates/xrpl-wasm-vm-ffi/src/lib.rs b/crates/xrpl-wasm-vm-ffi/src/lib.rs index c35ddfc6f1..7f5f96222e 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -230,6 +230,10 @@ mod ffi { #[cxx_name = "getCurrentLedgerObjArrayLen"] fn get_current_ledger_obj_array_len(self: &HostContext, field: i32) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getLedgerObjArrayLen"] + fn get_ledger_obj_array_len(self: &HostContext, cache_idx: i32, field: i32) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -357,6 +361,10 @@ impl HostFunctions for CxxHost<'_> { scalar(self.ctx.get_current_ledger_obj_array_len(field)) } + fn get_ledger_obj_array_len(&self, cache_idx: i32, field: i32) -> HostResult { + scalar(self.ctx.get_ledger_obj_array_len(cache_idx, field)) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { bytes_written(self.ctx.sha512_half(data, out)) } diff --git a/crates/xrpl-wasm-vm/src/abi.rs b/crates/xrpl-wasm-vm/src/abi.rs index cf0ef59fcc..74894ea7c8 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -241,6 +241,9 @@ mod tests { fn get_current_ledger_obj_array_len(&self, _field: i32) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_ledger_obj_array_len(&self, _cache_idx: i32, _field: i32) -> HostResult { + unreachable!("no unit test in this module calls the host") + } fn sha512_half(&self, _data: &[u8], _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } diff --git a/crates/xrpl-wasm-vm/src/register.rs b/crates/xrpl-wasm-vm/src/register.rs index 2662e8c6dc..7772194a3d 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -241,6 +241,18 @@ pub(crate) fn register_host_functions( ) }, ), + HostFunctionSpec::GetLedgerObjArrayLen => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + cache_idx: i32, + field: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::GetLedgerObjArrayLen, |c| { + c.data().host.get_ledger_obj_array_len(cache_idx, field) + }) + }, + ), HostFunctionSpec::Sha512Half => linker.func_wrap( HOST_MODULE, op.wasm_name(), diff --git a/crates/xrpl-wasm-vm/tests/budgets.rs b/crates/xrpl-wasm-vm/tests/budgets.rs index a94425dde6..7ccf30ddc1 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -119,6 +119,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $home_le_arr_len (i32.const 1))", 1, ), + HostFunctionSpec::GetLedgerObjArrayLen => ( + import::LE_ARR_LEN, + "(call $le_arr_len (i32.const 1) (i32.const 1))", + 2, + ), HostFunctionSpec::Sha512Half => ( import::SHA512_HALF, "(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))", diff --git a/crates/xrpl-wasm-vm/tests/host_calls.rs b/crates/xrpl-wasm-vm/tests/host_calls.rs index 6a853afad0..c5a72ac6cd 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -271,6 +271,20 @@ fn home_le_arr_len_passes_the_selector_and_returns_the_count() { assert_eq!(*host.home_le_arr_lens_asked.borrow(), vec![17]); } +/// The scalar count over a cached object: the slot leads, and both it and the +/// selector reach the host keyed together. +#[test] +fn le_arr_len_passes_the_slot_and_selector_and_returns_the_count() { + let host = FakeHost::new().answering_le_arr_len(2, 17, 9); + + let wat = module( + &[import::LE_ARR_LEN, ONE_PAGE], + "(call $le_arr_len (i32.const 2) (i32.const 17))", + ); + assert_eq!(status(&wat, &host), 9, "the array length"); + assert_eq!(*host.le_arr_lens_asked.borrow(), vec![(2, 17)]); +} + /// A leading scalar parameter reaches the host as declared. #[test] fn home_le_field_passes_the_field_selector_through() { diff --git a/crates/xrpl-wasm-vm/tests/preflight.rs b/crates/xrpl-wasm-vm/tests/preflight.rs index 7290438539..19c39132f4 100644 --- a/crates/xrpl-wasm-vm/tests/preflight.rs +++ b/crates/xrpl-wasm-vm/tests/preflight.rs @@ -98,7 +98,7 @@ fn a_disabled_feature_does_not_pass() { /// Every host function the ABI declares, spelled as a guest imports it. The count /// is asserted against the ABI so a function added to it cannot be left out here. -const ALL_IMPORTS: [&str; 17] = [ +const ALL_IMPORTS: [&str; 18] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -113,6 +113,7 @@ const ALL_IMPORTS: [&str; 17] = [ import::LE_INNER, import::TX_ARR_LEN, import::HOME_LE_ARR_LEN, + import::LE_ARR_LEN, import::SHA512_HALF, import::TRACE, import::TRACE_NUM, diff --git a/crates/xrpl-wasm-vm/tests/support/mod.rs b/crates/xrpl-wasm-vm/tests/support/mod.rs index 8a9baa9f77..c4ce465631 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -155,6 +155,11 @@ pub struct FakeHost { pub home_le_arr_lens: HashMap, /// Every field selector `get_current_ledger_obj_array_len` was asked for. pub home_le_arr_lens_asked: RefCell>, + /// What `get_ledger_obj_array_len` answers, by (cache slot, field selector). An + /// unlisted key answers `NoArray`. + pub le_arr_lens: HashMap<(i32, i32), i32>, + /// Every (cache slot, field selector) `get_ledger_obj_array_len` was asked for. + pub le_arr_lens_asked: RefCell>, /// What `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -198,6 +203,8 @@ impl Default for FakeHost { tx_arr_lens_asked: RefCell::new(Vec::new()), home_le_arr_lens: HashMap::new(), home_le_arr_lens_asked: RefCell::new(Vec::new()), + le_arr_lens: HashMap::new(), + le_arr_lens_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -286,6 +293,11 @@ impl FakeHost { self } + pub fn answering_le_arr_len(mut self, cache_idx: i32, field: i32, len: i32) -> FakeHost { + self.le_arr_lens.insert((cache_idx, field), len); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -405,6 +417,14 @@ impl HostFunctions for FakeHost { } } + fn get_ledger_obj_array_len(&self, cache_idx: i32, field: i32) -> HostResult { + self.le_arr_lens_asked.borrow_mut().push((cache_idx, field)); + match self.le_arr_lens.get(&(cache_idx, field)) { + Some(&len) => Ok(len), + None => Err(HostError::NoArray), + } + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { self.digested.borrow_mut().push(data.to_vec()); self.digest.fill(out) @@ -458,6 +478,8 @@ pub mod import { r#"(import "host_lib" "tx_arr_len" (func $tx_arr_len (param i32) (result i32)))"#; pub const HOME_LE_ARR_LEN: &str = r#"(import "host_lib" "home_le_arr_len" (func $home_le_arr_len (param i32) (result i32)))"#; + pub const LE_ARR_LEN: &str = + r#"(import "host_lib" "le_arr_len" (func $le_arr_len (param i32 i32) (result i32)))"#; pub const SHA512_HALF: &str = r#"(import "host_lib" "sha512_half" (func $sha512_half (param i32 i32 i32 i32) (result i32)))"#; pub const TRACE: &str = r#"(import "host_lib" "trace" (func $trace (param i32 i32 i32 i32 i32) (result i32)))"#; diff --git a/include/xrpl/tx/wasm/HostContext.h b/include/xrpl/tx/wasm/HostContext.h index 692fff900c..84580cef85 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -100,6 +100,9 @@ public: [[nodiscard]] std::int32_t getCurrentLedgerObjArrayLen(std::int32_t field) const noexcept; + [[nodiscard]] std::int32_t + getLedgerObjArrayLen(std::int32_t cacheIdx, std::int32_t field) const noexcept; + [[nodiscard]] std::int32_t sha512Half(rust::Slice data, rust::Slice out) const noexcept; diff --git a/src/libxrpl/tx/wasm/HostContext.cpp b/src/libxrpl/tx/wasm/HostContext.cpp index 91358aef95..0b39e2bdc5 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -313,6 +313,23 @@ HostContext::getCurrentLedgerObjArrayLen(std::int32_t field) const noexcept }); } +std::int32_t +HostContext::getLedgerObjArrayLen(std::int32_t cacheIdx, std::int32_t field) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + auto const& knownSFields = SField::getKnownCodeToField(); + auto const it = knownSFields.find(field); + if (it == knownSFields.end()) + return hfErrorToInt(HostFunctionError::InvalidField); + + auto const len = hostFunctions_.getLedgerObjArrayLen(cacheIdx, *it->second); + if (!len) + return hfErrorToInt(len.error()); + + return *len; + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept