From fe325ea96a4376c6de39dd29db881d2260c3ff97 Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 17:01:09 -0400 Subject: [PATCH] feat: Hook up home_le_inner host function --- crates/xrpl-host-functions/src/lib.rs | 10 +++++++ .../tests/generated_abi.rs | 18 +++++++++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 16 +++++++++++ crates/xrpl-wasm-vm/src/abi.rs | 7 +++++ crates/xrpl-wasm-vm/src/register.rs | 22 +++++++++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 5 ++++ crates/xrpl-wasm-vm/tests/host_calls.rs | 17 ++++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 ++- crates/xrpl-wasm-vm/tests/support/mod.rs | 27 +++++++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 5 ++++ src/libxrpl/tx/wasm/HostContext.cpp | 22 +++++++++++++++ 11 files changed, 151 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index c714f447c3..e31e3f9c4f 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -159,6 +159,16 @@ host_functions! { #[wasm_name = "tx_inner"] fn get_tx_nested_field(&self, locator: &[u8], out: &mut [u8]) -> HostResult; + /// The serialized bytes of a nested field of the current (escrow) ledger object, + /// reached by a `locator`, as with [`Self::get_tx_nested_field`]. + #[gas = 110] + #[wasm_name = "home_le_inner"] + fn get_current_ledger_obj_nested_field( + &self, + locator: &[u8], + out: &mut [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 21839c05b5..96ab5b7dce 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -90,6 +90,18 @@ impl HostFunctions for FakeHost { put(out, &[locator[0], locator.len() as u8]) } + /// The same, over the current ledger object. + fn get_current_ledger_obj_nested_field( + &self, + locator: &[u8], + out: &mut [u8], + ) -> HostResult { + if locator.is_empty() { + return Err(HostError::LocatorMalformed); + } + put(out, &[locator.len() as u8, locator[0]]) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -134,6 +146,11 @@ fn the_trait_is_implementable() { assert_eq!(out[..2], [2, 4]); assert_eq!(host.get_tx_nested_field(&[9, 0, 0, 0], &mut out), Ok(2)); assert_eq!(out[..2], [9, 4]); + assert_eq!( + host.get_current_ledger_obj_nested_field(&[9, 0, 0, 0], &mut out), + Ok(2) + ); + assert_eq!(out[..2], [4, 9]); 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(())); @@ -211,6 +228,7 @@ fn the_spec_table_matches_the_declarations() { ("home_le_field", 70), ("le_field", 70), ("tx_inner", 110), + ("home_le_inner", 110), ("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 9d68635395..8f7ee09c10 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -204,6 +204,14 @@ mod ffi { #[cxx_name = "getTxNestedField"] fn get_tx_nested_field(self: &HostContext, locator: &[u8], out: &mut [u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getCurrentLedgerObjNestedField"] + fn get_current_ledger_obj_nested_field( + self: &HostContext, + locator: &[u8], + out: &mut [u8], + ) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -303,6 +311,14 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.get_tx_nested_field(locator, out)) } + fn get_current_ledger_obj_nested_field( + &self, + locator: &[u8], + out: &mut [u8], + ) -> HostResult { + bytes_written(self.ctx.get_current_ledger_obj_nested_field(locator, out)) + } + 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 ed159d4c5a..ec0ef2cca4 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -220,6 +220,13 @@ mod tests { fn get_tx_nested_field(&self, _locator: &[u8], _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_current_ledger_obj_nested_field( + &self, + _locator: &[u8], + _out: &mut [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 5540360d95..a08bd0d2d1 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -172,6 +172,28 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::GetCurrentLedgerObjNestedField => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + loc_ptr: i32, + loc_len: i32, + out_ptr: i32, + out_len: i32| + -> Result { + charged( + &mut caller, + HostFunctionSpec::GetCurrentLedgerObjNestedField, + |c| { + let out = Region::new(out_ptr, out_len); + let locator = Region::new(loc_ptr, loc_len); + write_buffered(c, out, |host, data, buf| { + host.get_current_ledger_obj_nested_field(locator.read(data)?, buf) + }) + }, + ) + }, + ), 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 ad4d4e3abe..447caa424b 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -101,6 +101,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $tx_inner (i32.const 0) (i32.const 4) (i32.const 8) (i32.const 4))", 4, ), + HostFunctionSpec::GetCurrentLedgerObjNestedField => ( + import::HOME_LE_INNER, + "(call $home_le_inner (i32.const 0) (i32.const 4) (i32.const 8) (i32.const 4))", + 4, + ), 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 5732752cea..5187694a2f 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -206,6 +206,23 @@ fn tx_inner_reads_the_locator_and_writes_the_field() { assert_eq!(*host.tx_nested_asked.borrow(), vec![locator]); } +/// The same read-input-write-output path over the current object, with its own +/// answer set distinct from the transaction's nested getter. +#[test] +fn home_le_inner_reads_the_locator_and_writes_the_field() { + let locator = vec![5u8, 0, 0, 0]; + let host = FakeHost::new() + .answering_home_le_nested(locator.clone(), support::Answer::bytes([0xcc, 0xdd, 0xee])); + + let wat = module( + &[import::HOME_LE_INNER, ONE_PAGE], + "(i32.store (i32.const 0) (i32.const 5)) + (call $home_le_inner (i32.const 0) (i32.const 4) (i32.const 64) (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 3, "the field bytes the host wrote"); + assert_eq!(*host.home_le_nested_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 4725baf5bc..972a0fa080 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; 13] = [ +const ALL_IMPORTS: [&str; 14] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -109,6 +109,7 @@ const ALL_IMPORTS: [&str; 13] = [ import::HOME_LE_FIELD, import::LE_FIELD, import::TX_INNER, + import::HOME_LE_INNER, 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 908a965057..28b8427971 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -135,6 +135,11 @@ pub struct FakeHost { pub tx_nested: HashMap, Answer>, /// Every locator `get_tx_nested_field` was asked for. pub tx_nested_asked: RefCell>>, + /// What `get_current_ledger_obj_nested_field` answers, by locator bytes. An + /// unlisted locator answers `FieldNotFound`. + pub home_le_nested: HashMap, Answer>, + /// Every locator `get_current_ledger_obj_nested_field` was asked for. + pub home_le_nested_asked: RefCell>>, /// What `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -170,6 +175,8 @@ impl Default for FakeHost { le_fields_asked: RefCell::new(Vec::new()), tx_nested: HashMap::new(), tx_nested_asked: RefCell::new(Vec::new()), + home_le_nested: HashMap::new(), + home_le_nested_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -233,6 +240,11 @@ impl FakeHost { self } + pub fn answering_home_le_nested(mut self, locator: Vec, answer: Answer) -> FakeHost { + self.home_le_nested.insert(locator, answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -307,6 +319,20 @@ impl HostFunctions for FakeHost { } } + fn get_current_ledger_obj_nested_field( + &self, + locator: &[u8], + out: &mut [u8], + ) -> HostResult { + self.home_le_nested_asked + .borrow_mut() + .push(locator.to_vec()); + match self.home_le_nested.get(locator) { + Some(answer) => answer.fill(out), + None => Err(HostError::FieldNotFound), + } + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { self.digested.borrow_mut().push(data.to_vec()); self.digest.fill(out) @@ -354,6 +380,7 @@ pub mod import { r#"(import "host_lib" "le_field" (func $le_field (param i32 i32 i32 i32) (result i32)))"#; pub const TX_INNER: &str = 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 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 6083a93920..2b1c5e3418 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -81,6 +81,11 @@ public: getTxNestedField(rust::Slice locator, rust::Slice out) const noexcept; + [[nodiscard]] std::int32_t + getCurrentLedgerObjNestedField( + rust::Slice locator, + rust::Slice out) 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 2a1d2f37f4..a36d04e094 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -234,6 +234,28 @@ HostContext::getTxNestedField( }); } +std::int32_t +HostContext::getCurrentLedgerObjNestedField( + rust::Slice locator, + rust::Slice out) 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 value = hostFunctions_.getCurrentLedgerObjNestedField(fl); + if (!value) + return hfErrorToInt(value.error()); + + return answer(out, value->data(), value->size()); + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept