feat: Hook up tx_inner_arr_len host function

This commit is contained in:
TimothyBanks
2026-08-10 17:23:06 -04:00
parent 930ba88921
commit 1d9485c926
11 changed files with 114 additions and 1 deletions

View File

@@ -199,6 +199,12 @@ host_functions! {
#[wasm_name = "le_arr_len"]
fn get_ledger_obj_array_len(&self, cache_idx: i32, field: i32) -> HostResult<i32>;
/// The number of elements in a nested array field of the transaction, reached by a
/// `locator`. Reads the locator region and answers the count directly.
#[gas = 70]
#[wasm_name = "tx_inner_arr_len"]
fn get_tx_nested_array_len(&self, locator: &[u8]) -> HostResult<i32>;
/// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512.
#[gas = 2000]
#[wasm_name = "sha512_half"]

View File

@@ -139,6 +139,14 @@ impl HostFunctions for FakeHost {
Ok(cache_idx + field)
}
/// A nested array-length getter, keyed by the locator bytes.
fn get_tx_nested_array_len(&self, locator: &[u8]) -> HostResult<i32> {
if locator.is_empty() {
return Err(HostError::LocatorMalformed);
}
Ok(locator.len() as i32)
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
let mut digest = [0; HASH_LEN];
digest[0] = data.len() as u8;
@@ -202,6 +210,11 @@ fn the_trait_is_implementable() {
);
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.get_tx_nested_array_len(&[9, 0, 0, 0]), Ok(4));
assert_eq!(
host.get_tx_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(()));
@@ -284,6 +297,7 @@ fn the_spec_table_matches_the_declarations() {
("tx_arr_len", 40),
("home_le_arr_len", 40),
("le_arr_len", 40),
("tx_inner_arr_len", 70),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -234,6 +234,10 @@ mod ffi {
#[cxx_name = "getLedgerObjArrayLen"]
fn get_ledger_obj_array_len(self: &HostContext, cache_idx: i32, field: i32) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "getTxNestedArrayLen"]
fn get_tx_nested_array_len(self: &HostContext, locator: &[u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "sha512Half"]
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
@@ -365,6 +369,10 @@ impl HostFunctions for CxxHost<'_> {
scalar(self.ctx.get_ledger_obj_array_len(cache_idx, field))
}
fn get_tx_nested_array_len(&self, locator: &[u8]) -> HostResult<i32> {
scalar(self.ctx.get_tx_nested_array_len(locator))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -244,6 +244,9 @@ mod tests {
fn get_ledger_obj_array_len(&self, _cache_idx: i32, _field: i32) -> HostResult<i32> {
unreachable!("no unit test in this module calls the host")
}
fn get_tx_nested_array_len(&self, _locator: &[u8]) -> HostResult<i32> {
unreachable!("no unit test in this module calls the host")
}
fn sha512_half(&self, _data: &[u8], _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}

View File

@@ -253,6 +253,20 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::GetTxNestedArrayLen => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),
|mut caller: Caller<'_, VmState<'_>>,
loc_ptr: i32,
loc_len: i32|
-> Result<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::GetTxNestedArrayLen, |c| {
let host = c.data().host;
let locator = read_borrowed(c, Region::new(loc_ptr, loc_len))?;
host.get_tx_nested_array_len(locator)
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -124,6 +124,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $le_arr_len (i32.const 1) (i32.const 1))",
2,
),
HostFunctionSpec::GetTxNestedArrayLen => (
import::TX_INNER_ARR_LEN,
"(call $tx_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))",

View File

@@ -285,6 +285,22 @@ fn le_arr_len_passes_the_slot_and_selector_and_returns_the_count() {
assert_eq!(*host.le_arr_lens_asked.borrow(), vec![(2, 17)]);
}
/// A nested array-length getter: the locator is read from memory and the count comes
/// back as the status — read-input, scalar-out, no output buffer.
#[test]
fn tx_inner_arr_len_reads_the_locator_and_returns_the_count() {
let locator = vec![5u8, 0, 0, 0];
let host = FakeHost::new().answering_tx_nested_arr_len(locator.clone(), 6);
let wat = module(
&[import::TX_INNER_ARR_LEN, ONE_PAGE],
"(i32.store (i32.const 0) (i32.const 5))
(call $tx_inner_arr_len (i32.const 0) (i32.const 4))",
);
assert_eq!(status(&wat, &host), 6, "the array length");
assert_eq!(*host.tx_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() {

View File

@@ -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; 18] = [
const ALL_IMPORTS: [&str; 19] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -114,6 +114,7 @@ const ALL_IMPORTS: [&str; 18] = [
import::TX_ARR_LEN,
import::HOME_LE_ARR_LEN,
import::LE_ARR_LEN,
import::TX_INNER_ARR_LEN,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -160,6 +160,11 @@ pub struct FakeHost {
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<Vec<(i32, i32)>>,
/// What `get_tx_nested_array_len` answers, by locator bytes. An unlisted locator
/// answers `NoArray`.
pub tx_nested_arr_lens: HashMap<Vec<u8>, i32>,
/// Every locator `get_tx_nested_array_len` was asked for.
pub tx_nested_arr_lens_asked: RefCell<Vec<Vec<u8>>>,
/// What `sha512_half` answers, whatever it is given.
pub digest: Answer,
/// Every field selector `get_current_ledger_obj_field` was asked for.
@@ -205,6 +210,8 @@ impl Default for FakeHost {
home_le_arr_lens_asked: RefCell::new(Vec::new()),
le_arr_lens: HashMap::new(),
le_arr_lens_asked: RefCell::new(Vec::new()),
tx_nested_arr_lens: HashMap::new(),
tx_nested_arr_lens_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -298,6 +305,11 @@ impl FakeHost {
self
}
pub fn answering_tx_nested_arr_len(mut self, locator: Vec<u8>, len: i32) -> FakeHost {
self.tx_nested_arr_lens.insert(locator, len);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -425,6 +437,16 @@ impl HostFunctions for FakeHost {
}
}
fn get_tx_nested_array_len(&self, locator: &[u8]) -> HostResult<i32> {
self.tx_nested_arr_lens_asked
.borrow_mut()
.push(locator.to_vec());
match self.tx_nested_arr_lens.get(locator) {
Some(&len) => Ok(len),
None => Err(HostError::NoArray),
}
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
self.digested.borrow_mut().push(data.to_vec());
self.digest.fill(out)
@@ -480,6 +502,7 @@ pub mod import {
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 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 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)))"#;

View File

@@ -103,6 +103,9 @@ public:
[[nodiscard]] std::int32_t
getLedgerObjArrayLen(std::int32_t cacheIdx, std::int32_t field) const noexcept;
[[nodiscard]] std::int32_t
getTxNestedArrayLen(rust::Slice<std::uint8_t const> locator) const noexcept;
[[nodiscard]] std::int32_t
sha512Half(rust::Slice<std::uint8_t const> data, rust::Slice<std::uint8_t> out) const noexcept;

View File

@@ -330,6 +330,26 @@ HostContext::getLedgerObjArrayLen(std::int32_t cacheIdx, std::int32_t field) con
});
}
std::int32_t
HostContext::getTxNestedArrayLen(rust::Slice<std::uint8_t const> 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<std::int32_t> locBuf(steps);
std::memcpy(locBuf.data(), locator.data(), locator.size());
FieldLocator const fl(std::move(locBuf));
auto const len = hostFunctions_.getTxNestedArrayLen(fl);
if (!len)
return hfErrorToInt(len.error());
return *len;
});
}
std::int32_t
HostContext::sha512Half(rust::Slice<std::uint8_t const> data, rust::Slice<std::uint8_t> out)
const noexcept