feat: Hook up parent_ldgr_hash host function

This commit is contained in:
TimothyBanks
2026-08-10 15:04:35 -04:00
parent 8da36db515
commit 6abd492ebb
11 changed files with 97 additions and 1 deletions

View File

@@ -111,6 +111,11 @@ host_functions! {
#[wasm_name = "parent_ldgr_time"]
fn get_parent_ledger_time(&self, out: &mut [u8]) -> HostResult<usize>;
/// The hash of the parent (last-closed) ledger, as 32 bytes.
#[gas = 60]
#[wasm_name = "parent_ldgr_hash"]
fn get_parent_ledger_hash(&self, out: &mut [u8]) -> HostResult<usize>;
/// The serialized bytes of one field of the current (escrow) ledger object.
#[gas = 70]
#[wasm_name = "home_le_field"]

View File

@@ -35,6 +35,10 @@ impl HostFunctions for FakeHost {
put(out, &9u32.to_le_bytes())
}
fn get_parent_ledger_hash(&self, out: &mut [u8]) -> HostResult<usize> {
put(out, &[0xab; HASH_LEN])
}
/// Fails on a field it doesn't know, so the error channel is exercised too.
fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult<usize> {
if field < 0 {
@@ -71,6 +75,8 @@ fn the_trait_is_implementable() {
assert_eq!(out[..4], [7, 0, 0, 0]);
assert_eq!(host.get_parent_ledger_time(&mut out), Ok(4));
assert_eq!(out[..4], [9, 0, 0, 0]);
assert_eq!(host.get_parent_ledger_hash(&mut out), Ok(HASH_LEN));
assert_eq!(out[0], 0xab);
assert_eq!(host.get_current_ledger_obj_field(3, &mut out), Ok(1));
assert_eq!(out[0], 3);
assert_eq!(host.sha512_half(b"abc", &mut out), Ok(HASH_LEN));
@@ -142,6 +148,7 @@ fn the_spec_table_matches_the_declarations() {
[
("ldgr_index", 60),
("parent_ldgr_time", 60),
("parent_ldgr_hash", 60),
("home_le_field", 70),
("sha512_half", 2000),
("trace", 500),

View File

@@ -163,6 +163,10 @@ mod ffi {
#[cxx_name = "getParentLedgerTime"]
fn get_parent_ledger_time(self: &HostContext, out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "getParentLedgerHash"]
fn get_parent_ledger_hash(self: &HostContext, out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "getCurrentLedgerObjField"]
fn get_current_ledger_obj_field(self: &HostContext, field: i32, out: &mut [u8]) -> i32;
@@ -220,6 +224,10 @@ impl HostFunctions for CxxHost<'_> {
bytes_written(self.ctx.get_parent_ledger_time(out))
}
fn get_parent_ledger_hash(&self, out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.get_parent_ledger_hash(out))
}
fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.get_current_ledger_obj_field(field, out))
}

View File

@@ -191,6 +191,9 @@ mod tests {
fn get_parent_ledger_time(&self, _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn get_parent_ledger_hash(&self, _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn get_current_ledger_obj_field(&self, _field: i32, _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}

View File

@@ -48,6 +48,19 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::GetParentLedgerHash => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),
|mut caller: Caller<'_, VmState<'_>>,
out_ptr: i32,
out_len: i32|
-> Result<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::GetParentLedgerHash, |c| {
let out = Region::new(out_ptr, out_len);
write_into(c, out, |host, out| host.get_parent_ledger_hash(out))
})
},
),
HostFunctionSpec::GetCurrentLedgerObjField => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -61,6 +61,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $parent_ldgr_time (i32.const 0) (i32.const 4))",
2,
),
HostFunctionSpec::GetParentLedgerHash => (
import::PARENT_LDGR_HASH,
"(call $parent_ldgr_hash (i32.const 0) (i32.const 32))",
2,
),
HostFunctionSpec::GetCurrentLedgerObjField => (
import::HOME_LE_FIELD,
"(call $home_le_field (i32.const 1) (i32.const 0) (i32.const 4))",

View File

@@ -49,6 +49,31 @@ fn parent_ldgr_time_writes_the_close_time_where_the_guest_asked() {
assert_eq!(status(&wat, &host), 4, "the byte count");
}
/// A 32-byte value (a ledger hash) travels the same getter path as the 4-byte
/// scalars: every byte lands where the guest asked, and the status is the length.
#[test]
fn parent_ldgr_hash_writes_all_32_bytes_where_the_guest_asked() {
let host = FakeHost::new();
let wat = module(
&[import::PARENT_LDGR_HASH, ONE_PAGE],
"(call $parent_ldgr_hash (i32.const 64) (i32.const 32))",
);
assert_eq!(status(&wat, &host), 32, "the byte count");
// The default hash is 0, 1, 2, ..., so its first four bytes load as 0x03020100.
let wat = module(
&[import::PARENT_LDGR_HASH, ONE_PAGE],
"(drop (call $parent_ldgr_hash (i32.const 64) (i32.const 32)))
(i32.load (i32.const 64))",
);
assert_eq!(
status(&wat, &host),
0x03020100,
"the first four bytes the host wrote"
);
}
/// The output region is wherever the guest points, not a fixed address.
#[test]
fn the_output_region_is_the_pointer_the_guest_gave() {

View File

@@ -98,9 +98,10 @@ 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; 6] = [
const ALL_IMPORTS: [&str; 7] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
import::HOME_LE_FIELD,
import::SHA512_HALF,
import::TRACE,

View File

@@ -105,6 +105,8 @@ pub struct FakeHost {
pub ledger_sqn: Answer,
/// What `get_parent_ledger_time` answers.
pub parent_ledger_time: Answer,
/// What `get_parent_ledger_hash` answers.
pub parent_ledger_hash: Answer,
/// What `get_current_ledger_obj_field` answers, by field selector. An
/// unlisted selector answers `FieldNotFound`.
pub fields: HashMap<i32, Answer>,
@@ -126,6 +128,8 @@ impl Default for FakeHost {
// A distinct value from the sequence number, so a test cannot pass by
// reading one where it meant the other.
parent_ledger_time: Answer::bytes(9u32.to_le_bytes()),
// 32 bytes counting up from 0, the length of a real ledger hash.
parent_ledger_hash: Answer::filler(32),
fields: HashMap::new(),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
@@ -150,6 +154,11 @@ impl FakeHost {
self
}
pub fn answering_parent_ledger_hash(mut self, answer: Answer) -> FakeHost {
self.parent_ledger_hash = answer;
self
}
pub fn answering_field(mut self, field: i32, answer: Answer) -> FakeHost {
self.fields.insert(field, answer);
self
@@ -174,6 +183,10 @@ impl HostFunctions for FakeHost {
self.parent_ledger_time.fill(out)
}
fn get_parent_ledger_hash(&self, out: &mut [u8]) -> HostResult<usize> {
self.parent_ledger_hash.fill(out)
}
fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult<usize> {
self.fields_asked.borrow_mut().push(field);
match self.fields.get(&field) {
@@ -216,6 +229,7 @@ pub mod import {
pub const LDGR_INDEX: &str =
r#"(import "host_lib" "ldgr_index" (func $ldgr_index (param i32 i32) (result i32)))"#;
pub const PARENT_LDGR_TIME: &str = r#"(import "host_lib" "parent_ldgr_time" (func $parent_ldgr_time (param i32 i32) (result i32)))"#;
pub const PARENT_LDGR_HASH: &str = r#"(import "host_lib" "parent_ldgr_hash" (func $parent_ldgr_hash (param i32 i32) (result i32)))"#;
pub const HOME_LE_FIELD: &str = r#"(import "host_lib" "home_le_field" (func $home_le_field (param 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 =

View File

@@ -47,6 +47,9 @@ public:
[[nodiscard]] std::int32_t
getParentLedgerTime(rust::Slice<std::uint8_t> out) const noexcept;
[[nodiscard]] std::int32_t
getParentLedgerHash(rust::Slice<std::uint8_t> out) const noexcept;
[[nodiscard]] std::int32_t
getCurrentLedgerObjField(std::int32_t field, rust::Slice<std::uint8_t> out) const noexcept;

View File

@@ -83,6 +83,18 @@ HostContext::getParentLedgerTime(rust::Slice<std::uint8_t> out) const noexcept
});
}
std::int32_t
HostContext::getParentLedgerHash(rust::Slice<std::uint8_t> out) const noexcept
{
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
auto const hash = hostFunctions_.getParentLedgerHash();
if (!hash)
return hfErrorToInt(hash.error());
return answer(out, hash->data(), hash->size());
});
}
std::int32_t
HostContext::getCurrentLedgerObjField(std::int32_t field, rust::Slice<std::uint8_t> out)
const noexcept