From 93db40a25ee56d275191176bad7db392fc65c97c Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 17:11:36 -0400 Subject: [PATCH] feat: Hook up tx_arr_len host function --- crates/xrpl-host-functions/src/lib.rs | 7 ++++++ .../tests/generated_abi.rs | 11 ++++++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 9 ++++++++ crates/xrpl-wasm-vm/src/abi.rs | 3 +++ crates/xrpl-wasm-vm/src/register.rs | 9 ++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 3 +++ crates/xrpl-wasm-vm/tests/host_calls.rs | 14 ++++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 ++- crates/xrpl-wasm-vm/tests/support/mod.rs | 22 +++++++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 5 +++++ src/libxrpl/tx/wasm/HostContext.cpp | 17 ++++++++++++++ 11 files changed, 102 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index adfccb0af1..0e2a7b447b 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -180,6 +180,13 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The number of elements in an array field of the transaction, selected by its + /// `SField` code. Answers the count directly, or a negative error (`NoArray` if + /// the field is not an array). Reads and writes no memory. + #[gas = 40] + #[wasm_name = "tx_arr_len"] + fn get_tx_array_len(&self, 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 0dc50c5a3b..11f2dcddd3 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -115,6 +115,14 @@ impl HostFunctions for FakeHost { put(out, &[cache_idx as u8, locator[0]]) } + /// A scalar-in, scalar-out count; `NoArray` on a negative selector. + fn get_tx_array_len(&self, field: i32) -> HostResult { + if field < 0 { + return Err(HostError::NoArray); + } + Ok(field) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -169,6 +177,8 @@ fn the_trait_is_implementable() { Ok(2) ); assert_eq!(out[..2], [3, 9]); + assert_eq!(host.get_tx_array_len(3), Ok(3)); + assert_eq!(host.get_tx_array_len(-1), 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(())); @@ -248,6 +258,7 @@ fn the_spec_table_matches_the_declarations() { ("tx_inner", 110), ("home_le_inner", 110), ("le_inner", 110), + ("tx_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 fca7d57683..5dc06cfea5 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -221,6 +221,11 @@ mod ffi { out: &mut [u8], ) -> i32; + /// Answers the array's element count directly, or a negative `HostError` code. + #[namespace = "xrpl"] + #[cxx_name = "getTxArrayLen"] + fn get_tx_array_len(self: &HostContext, field: i32) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -340,6 +345,10 @@ impl HostFunctions for CxxHost<'_> { ) } + fn get_tx_array_len(&self, field: i32) -> HostResult { + scalar(self.ctx.get_tx_array_len(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 daa8cf9f17..638c119412 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -235,6 +235,9 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_tx_array_len(&self, _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 1dc57f0751..99c86b86b0 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -221,6 +221,15 @@ pub(crate) fn register_host_functions( ) }, ), + HostFunctionSpec::GetTxArrayLen => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, field: i32| -> Result { + charged(&mut caller, HostFunctionSpec::GetTxArrayLen, |c| { + c.data().host.get_tx_array_len(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 f415169c6b..7181ac4ae8 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -111,6 +111,9 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $le_inner (i32.const 1) (i32.const 0) (i32.const 4) (i32.const 8) (i32.const 4))", 5, ), + HostFunctionSpec::GetTxArrayLen => { + (import::TX_ARR_LEN, "(call $tx_arr_len (i32.const 1))", 1) + } 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 77e1c4205a..660b50c4ff 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -243,6 +243,20 @@ fn le_inner_reads_the_slot_and_locator_and_writes_the_field() { assert_eq!(*host.le_nested_asked.borrow(), vec![(3, locator)]); } +/// A scalar-in, scalar-out call — no memory regions at all: the field selector +/// reaches the host and the array length comes back as the status. +#[test] +fn tx_arr_len_passes_the_selector_and_returns_the_count() { + let host = FakeHost::new().answering_tx_arr_len(17, 5); + + let wat = module( + &[import::TX_ARR_LEN, ONE_PAGE], + "(call $tx_arr_len (i32.const 17))", + ); + assert_eq!(status(&wat, &host), 5, "the array length"); + assert_eq!(*host.tx_arr_lens_asked.borrow(), vec![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 70ab994903..ab166d8b21 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; 15] = [ +const ALL_IMPORTS: [&str; 16] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -111,6 +111,7 @@ const ALL_IMPORTS: [&str; 15] = [ import::TX_INNER, import::HOME_LE_INNER, import::LE_INNER, + import::TX_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 e93d33c1a0..af022d52b1 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -145,6 +145,11 @@ pub struct FakeHost { pub le_nested: HashMap<(i32, Vec), Answer>, /// Every (cache slot, locator) `get_ledger_obj_nested_field` was asked for. pub le_nested_asked: RefCell)>>, + /// What `get_tx_array_len` answers, by field selector. An unlisted selector + /// answers `NoArray`. + pub tx_arr_lens: HashMap, + /// Every field selector `get_tx_array_len` was asked for. + pub tx_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. @@ -184,6 +189,8 @@ impl Default for FakeHost { home_le_nested_asked: RefCell::new(Vec::new()), le_nested: HashMap::new(), le_nested_asked: RefCell::new(Vec::new()), + tx_arr_lens: HashMap::new(), + tx_arr_lens_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -262,6 +269,11 @@ impl FakeHost { self } + pub fn answering_tx_arr_len(mut self, field: i32, len: i32) -> FakeHost { + self.tx_arr_lens.insert(field, len); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -365,6 +377,14 @@ impl HostFunctions for FakeHost { } } + fn get_tx_array_len(&self, field: i32) -> HostResult { + self.tx_arr_lens_asked.borrow_mut().push(field); + match self.tx_arr_lens.get(&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) @@ -414,6 +434,8 @@ pub mod import { r#"(import "host_lib" "tx_inner" (func $tx_inner (param i32 i32 i32 i32) (result i32)))"#; pub const HOME_LE_INNER: &str = r#"(import "host_lib" "home_le_inner" (func $home_le_inner (param i32 i32 i32 i32) (result i32)))"#; pub const LE_INNER: &str = r#"(import "host_lib" "le_inner" (func $le_inner (param i32 i32 i32 i32 i32) (result i32)))"#; + pub const TX_ARR_LEN: &str = + r#"(import "host_lib" "tx_arr_len" (func $tx_arr_len (param 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 a4bd47c26c..08fc35deac 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -92,6 +92,11 @@ public: rust::Slice locator, rust::Slice out) const noexcept; + // Answers the array's element count directly, or a negative `HostFunctionError` + // code (`NoArray` if the field is not an array). + [[nodiscard]] std::int32_t + getTxArrayLen(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 49d4e641b0..b03e0d1e07 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -279,6 +279,23 @@ HostContext::getLedgerObjNestedField( }); } +std::int32_t +HostContext::getTxArrayLen(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_.getTxArrayLen(*it->second); + if (!len) + return hfErrorToInt(len.error()); + + return *len; + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept