feat: Hook up tx_arr_len host function

This commit is contained in:
TimothyBanks
2026-08-10 17:11:36 -04:00
parent 8cae773691
commit 93db40a25e
11 changed files with 102 additions and 1 deletions

View File

@@ -180,6 +180,13 @@ host_functions! {
out: &mut [u8],
) -> HostResult<usize>;
/// 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<i32>;
/// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512.
#[gas = 2000]
#[wasm_name = "sha512_half"]

View File

@@ -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<i32> {
if field < 0 {
return Err(HostError::NoArray);
}
Ok(field)
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
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),

View File

@@ -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<i32> {
scalar(self.ctx.get_tx_array_len(field))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -235,6 +235,9 @@ mod tests {
) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn get_tx_array_len(&self, _field: i32) -> 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

@@ -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<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::GetTxArrayLen, |c| {
c.data().host.get_tx_array_len(field)
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -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))",

View File

@@ -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() {

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; 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,

View File

@@ -145,6 +145,11 @@ pub struct FakeHost {
pub le_nested: HashMap<(i32, Vec<u8>), Answer>,
/// Every (cache slot, locator) `get_ledger_obj_nested_field` was asked for.
pub le_nested_asked: RefCell<Vec<(i32, Vec<u8>)>>,
/// What `get_tx_array_len` answers, by field selector. An unlisted selector
/// answers `NoArray`.
pub tx_arr_lens: HashMap<i32, i32>,
/// Every field selector `get_tx_array_len` was asked for.
pub tx_arr_lens_asked: RefCell<Vec<i32>>,
/// 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<i32> {
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<usize> {
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)))"#;

View File

@@ -92,6 +92,11 @@ public:
rust::Slice<std::uint8_t const> locator,
rust::Slice<std::uint8_t> 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<std::uint8_t const> data, rust::Slice<std::uint8_t> out) const noexcept;

View File

@@ -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<std::uint8_t const> data, rust::Slice<std::uint8_t> out)
const noexcept