diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index 1b355b1127..5e1a541751 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -205,6 +205,12 @@ host_functions! { #[wasm_name = "tx_inner_arr_len"] fn get_tx_nested_array_len(&self, locator: &[u8]) -> HostResult; + /// The number of elements in a nested array field of the current (escrow) ledger + /// object, reached by a `locator`, as with [`Self::get_tx_nested_array_len`]. + #[gas = 70] + #[wasm_name = "home_le_inner_arr_len"] + fn get_current_ledger_obj_nested_array_len(&self, locator: &[u8]) -> 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 6b4c82e172..4806091d90 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -147,6 +147,14 @@ impl HostFunctions for FakeHost { Ok(locator.len() as i32) } + /// The same, over the current ledger object. + fn get_current_ledger_obj_nested_array_len(&self, locator: &[u8]) -> HostResult { + if locator.is_empty() { + return Err(HostError::LocatorMalformed); + } + Ok(locator.len() as i32 + 1) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -215,6 +223,14 @@ fn the_trait_is_implementable() { host.get_tx_nested_array_len(&[]), Err(HostError::LocatorMalformed) ); + assert_eq!( + host.get_current_ledger_obj_nested_array_len(&[9, 0, 0, 0]), + Ok(5) + ); + assert_eq!( + host.get_current_ledger_obj_nested_array_len(&[]), + Err(HostError::LocatorMalformed) + ); 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(())); @@ -298,6 +314,7 @@ fn the_spec_table_matches_the_declarations() { ("home_le_arr_len", 40), ("le_arr_len", 40), ("tx_inner_arr_len", 70), + ("home_le_inner_arr_len", 70), ("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 571728198c..317f4e932e 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -238,6 +238,10 @@ mod ffi { #[cxx_name = "getTxNestedArrayLen"] fn get_tx_nested_array_len(self: &HostContext, locator: &[u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getCurrentLedgerObjNestedArrayLen"] + fn get_current_ledger_obj_nested_array_len(self: &HostContext, locator: &[u8]) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -373,6 +377,10 @@ impl HostFunctions for CxxHost<'_> { scalar(self.ctx.get_tx_nested_array_len(locator)) } + fn get_current_ledger_obj_nested_array_len(&self, locator: &[u8]) -> HostResult { + scalar(self.ctx.get_current_ledger_obj_nested_array_len(locator)) + } + 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 63b8459759..d59766935b 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -247,6 +247,9 @@ mod tests { fn get_tx_nested_array_len(&self, _locator: &[u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_current_ledger_obj_nested_array_len(&self, _locator: &[u8]) -> 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 11be276a22..db40efda87 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -267,6 +267,24 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::GetCurrentLedgerObjNestedArrayLen => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + loc_ptr: i32, + loc_len: i32| + -> Result { + charged( + &mut caller, + HostFunctionSpec::GetCurrentLedgerObjNestedArrayLen, + |c| { + let host = c.data().host; + let locator = read_borrowed(c, Region::new(loc_ptr, loc_len))?; + host.get_current_ledger_obj_nested_array_len(locator) + }, + ) + }, + ), 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 4ae9b2b7cf..f6e19ff35f 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -129,6 +129,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $tx_inner_arr_len (i32.const 0) (i32.const 4))", 2, ), + HostFunctionSpec::GetCurrentLedgerObjNestedArrayLen => ( + import::HOME_LE_INNER_ARR_LEN, + "(call $home_le_inner_arr_len (i32.const 0) (i32.const 4))", + 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 08d631797b..30e0caa810 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -301,6 +301,22 @@ fn tx_inner_arr_len_reads_the_locator_and_returns_the_count() { assert_eq!(*host.tx_nested_arr_lens_asked.borrow(), vec![locator]); } +/// The same read-input, scalar-out count over the current object, with its own answer +/// set distinct from the transaction's. +#[test] +fn home_le_inner_arr_len_reads_the_locator_and_returns_the_count() { + let locator = vec![5u8, 0, 0, 0]; + let host = FakeHost::new().answering_home_le_nested_arr_len(locator.clone(), 7); + + let wat = module( + &[import::HOME_LE_INNER_ARR_LEN, ONE_PAGE], + "(i32.store (i32.const 0) (i32.const 5)) + (call $home_le_inner_arr_len (i32.const 0) (i32.const 4))", + ); + assert_eq!(status(&wat, &host), 7, "the array length"); + assert_eq!(*host.home_le_nested_arr_lens_asked.borrow(), vec![locator]); +} + /// 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 08578d172b..3bf2dcc9e0 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; 19] = [ +const ALL_IMPORTS: [&str; 20] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -115,6 +115,7 @@ const ALL_IMPORTS: [&str; 19] = [ import::HOME_LE_ARR_LEN, import::LE_ARR_LEN, import::TX_INNER_ARR_LEN, + import::HOME_LE_INNER_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 2bfc049644..0b151cca9c 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -165,6 +165,11 @@ pub struct FakeHost { pub tx_nested_arr_lens: HashMap, i32>, /// Every locator `get_tx_nested_array_len` was asked for. pub tx_nested_arr_lens_asked: RefCell>>, + /// What `get_current_ledger_obj_nested_array_len` answers, by locator bytes. An + /// unlisted locator answers `NoArray`. + pub home_le_nested_arr_lens: HashMap, i32>, + /// Every locator `get_current_ledger_obj_nested_array_len` was asked for. + pub home_le_nested_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. @@ -212,6 +217,8 @@ impl Default for FakeHost { le_arr_lens_asked: RefCell::new(Vec::new()), tx_nested_arr_lens: HashMap::new(), tx_nested_arr_lens_asked: RefCell::new(Vec::new()), + home_le_nested_arr_lens: HashMap::new(), + home_le_nested_arr_lens_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -310,6 +317,11 @@ impl FakeHost { self } + pub fn answering_home_le_nested_arr_len(mut self, locator: Vec, len: i32) -> FakeHost { + self.home_le_nested_arr_lens.insert(locator, len); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -447,6 +459,16 @@ impl HostFunctions for FakeHost { } } + fn get_current_ledger_obj_nested_array_len(&self, locator: &[u8]) -> HostResult { + self.home_le_nested_arr_lens_asked + .borrow_mut() + .push(locator.to_vec()); + match self.home_le_nested_arr_lens.get(locator) { + 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) @@ -503,6 +525,7 @@ pub mod import { pub const LE_ARR_LEN: &str = r#"(import "host_lib" "le_arr_len" (func $le_arr_len (param i32 i32) (result i32)))"#; pub const TX_INNER_ARR_LEN: &str = r#"(import "host_lib" "tx_inner_arr_len" (func $tx_inner_arr_len (param i32 i32) (result i32)))"#; + pub const HOME_LE_INNER_ARR_LEN: &str = r#"(import "host_lib" "home_le_inner_arr_len" (func $home_le_inner_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 b691a2b249..79ed5d5882 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -106,6 +106,9 @@ public: [[nodiscard]] std::int32_t getTxNestedArrayLen(rust::Slice locator) const noexcept; + [[nodiscard]] std::int32_t + getCurrentLedgerObjNestedArrayLen(rust::Slice locator) 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 fff8a56f11..535e4ce7bf 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -350,6 +350,27 @@ HostContext::getTxNestedArrayLen(rust::Slice locator) const }); } +std::int32_t +HostContext::getCurrentLedgerObjNestedArrayLen( + rust::Slice locator) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (locator.empty() || (locator.size() & 3) != 0) + return hfErrorToInt(HostFunctionError::LocatorMalformed); + + std::uint32_t const steps = locator.size() / sizeof(std::int32_t); + std::vector locBuf(steps); + std::memcpy(locBuf.data(), locator.data(), locator.size()); + FieldLocator const fl(std::move(locBuf)); + + auto const len = hostFunctions_.getCurrentLedgerObjNestedArrayLen(fl); + if (!len) + return hfErrorToInt(len.error()); + + return *len; + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept