diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index 0e2a7b447b..de887aec9f 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -187,6 +187,12 @@ host_functions! { #[wasm_name = "tx_arr_len"] fn get_tx_array_len(&self, field: i32) -> HostResult; + /// The number of elements in an array field of the current (escrow) ledger + /// object, as with [`Self::get_tx_array_len`]. + #[gas = 40] + #[wasm_name = "home_le_arr_len"] + fn get_current_ledger_obj_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 11f2dcddd3..f3139674c3 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -123,6 +123,14 @@ impl HostFunctions for FakeHost { Ok(field) } + /// The same, over the current ledger object. + fn get_current_ledger_obj_array_len(&self, field: i32) -> HostResult { + if field < 0 { + return Err(HostError::NoArray); + } + Ok(field + 1) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -179,6 +187,11 @@ fn the_trait_is_implementable() { 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.get_current_ledger_obj_array_len(3), Ok(4)); + assert_eq!( + host.get_current_ledger_obj_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(())); @@ -259,6 +272,7 @@ fn the_spec_table_matches_the_declarations() { ("home_le_inner", 110), ("le_inner", 110), ("tx_arr_len", 40), + ("home_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 5dc06cfea5..c35ddfc6f1 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -226,6 +226,10 @@ mod ffi { #[cxx_name = "getTxArrayLen"] fn get_tx_array_len(self: &HostContext, field: i32) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getCurrentLedgerObjArrayLen"] + fn get_current_ledger_obj_array_len(self: &HostContext, field: i32) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -349,6 +353,10 @@ impl HostFunctions for CxxHost<'_> { scalar(self.ctx.get_tx_array_len(field)) } + fn get_current_ledger_obj_array_len(&self, field: i32) -> HostResult { + scalar(self.ctx.get_current_ledger_obj_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 638c119412..cf0ef59fcc 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -238,6 +238,9 @@ mod tests { fn get_tx_array_len(&self, _field: i32) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_current_ledger_obj_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 99c86b86b0..2662e8c6dc 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -230,6 +230,17 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::GetCurrentLedgerObjArrayLen => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, field: i32| -> Result { + charged( + &mut caller, + HostFunctionSpec::GetCurrentLedgerObjArrayLen, + |c| c.data().host.get_current_ledger_obj_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 7181ac4ae8..a94425dde6 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -114,6 +114,11 @@ fn call_for(op: HostFunctionSpec) -> Call { HostFunctionSpec::GetTxArrayLen => { (import::TX_ARR_LEN, "(call $tx_arr_len (i32.const 1))", 1) } + HostFunctionSpec::GetCurrentLedgerObjArrayLen => ( + import::HOME_LE_ARR_LEN, + "(call $home_le_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 660b50c4ff..6a853afad0 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -257,6 +257,20 @@ fn tx_arr_len_passes_the_selector_and_returns_the_count() { assert_eq!(*host.tx_arr_lens_asked.borrow(), vec![17]); } +/// The same scalar-in, scalar-out count over the current object, with its own answer +/// set distinct from the transaction's. +#[test] +fn home_le_arr_len_passes_the_selector_and_returns_the_count() { + let host = FakeHost::new().answering_home_le_arr_len(17, 8); + + let wat = module( + &[import::HOME_LE_ARR_LEN, ONE_PAGE], + "(call $home_le_arr_len (i32.const 17))", + ); + assert_eq!(status(&wat, &host), 8, "the array length"); + assert_eq!(*host.home_le_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 ab166d8b21..7290438539 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; 16] = [ +const ALL_IMPORTS: [&str; 17] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -112,6 +112,7 @@ const ALL_IMPORTS: [&str; 16] = [ import::HOME_LE_INNER, import::LE_INNER, import::TX_ARR_LEN, + import::HOME_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 af022d52b1..8a9baa9f77 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -150,6 +150,11 @@ pub struct FakeHost { pub tx_arr_lens: HashMap, /// Every field selector `get_tx_array_len` was asked for. pub tx_arr_lens_asked: RefCell>, + /// What `get_current_ledger_obj_array_len` answers, by field selector. An + /// unlisted selector answers `NoArray`. + 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 `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -191,6 +196,8 @@ impl Default for FakeHost { le_nested_asked: RefCell::new(Vec::new()), tx_arr_lens: HashMap::new(), tx_arr_lens_asked: RefCell::new(Vec::new()), + home_le_arr_lens: HashMap::new(), + home_le_arr_lens_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -274,6 +281,11 @@ impl FakeHost { self } + pub fn answering_home_le_arr_len(mut self, field: i32, len: i32) -> FakeHost { + self.home_le_arr_lens.insert(field, len); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -385,6 +397,14 @@ impl HostFunctions for FakeHost { } } + fn get_current_ledger_obj_array_len(&self, field: i32) -> HostResult { + self.home_le_arr_lens_asked.borrow_mut().push(field); + match self.home_le_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) @@ -436,6 +456,8 @@ pub mod import { 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 HOME_LE_ARR_LEN: &str = + r#"(import "host_lib" "home_le_arr_len" (func $home_le_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 08fc35deac..692fff900c 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -97,6 +97,9 @@ public: [[nodiscard]] std::int32_t getTxArrayLen(std::int32_t field) const noexcept; + [[nodiscard]] std::int32_t + getCurrentLedgerObjArrayLen(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 b03e0d1e07..91358aef95 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -296,6 +296,23 @@ HostContext::getTxArrayLen(std::int32_t field) const noexcept }); } +std::int32_t +HostContext::getCurrentLedgerObjArrayLen(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_.getCurrentLedgerObjArrayLen(*it->second); + if (!len) + return hfErrorToInt(len.error()); + + return *len; + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept