feat: Hook up home_le_arr_len host function

This commit is contained in:
TimothyBanks
2026-08-10 17:15:30 -04:00
parent 93db40a25e
commit c619ae0263
11 changed files with 105 additions and 1 deletions

View File

@@ -187,6 +187,12 @@ host_functions! {
#[wasm_name = "tx_arr_len"]
fn get_tx_array_len(&self, field: i32) -> HostResult<i32>;
/// 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<i32>;
/// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512.
#[gas = 2000]
#[wasm_name = "sha512_half"]

View File

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

View File

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

View File

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

@@ -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<i32, wasmi::Error> {
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(),

View File

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

View File

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

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

View File

@@ -150,6 +150,11 @@ pub struct FakeHost {
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 `get_current_ledger_obj_array_len` answers, by field selector. An
/// unlisted selector answers `NoArray`.
pub home_le_arr_lens: HashMap<i32, i32>,
/// Every field selector `get_current_ledger_obj_array_len` was asked for.
pub home_le_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.
@@ -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<i32> {
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<usize> {
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)))"#;

View File

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

View File

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