feat: Hook up accountroot_id host function

This commit is contained in:
TimothyBanks
2026-08-10 17:46:07 -04:00
parent 3caaecff07
commit 17fb37871c
11 changed files with 134 additions and 1 deletions

View File

@@ -234,6 +234,12 @@ host_functions! {
pubkey: &[u8],
) -> HostResult<i32>;
/// The 32-byte ledger key (keylet) of an account's `AccountRoot`, computed from a
/// 20-byte account id. Reads the account region and writes the keylet.
#[gas = 350]
#[wasm_name = "accountroot_id"]
fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult<usize>;
/// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512.
#[gas = 2000]
#[wasm_name = "sha512_half"]

View File

@@ -173,6 +173,15 @@ impl HostFunctions for FakeHost {
Ok(i32::from(!signature.is_empty()))
}
/// A keylet getter: reads an account, writes a 32-byte keylet; `InvalidAccount`
/// on an empty account.
fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult<usize> {
if account.is_empty() {
return Err(HostError::InvalidAccount);
}
put(out, &[account[0]; HASH_LEN])
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
let mut digest = [0; HASH_LEN];
digest[0] = data.len() as u8;
@@ -259,6 +268,12 @@ fn the_trait_is_implementable() {
);
assert_eq!(host.check_signature(b"msg", b"sig", b"pk"), Ok(1));
assert_eq!(host.check_signature(b"msg", b"", b"pk"), Ok(0));
assert_eq!(host.account_keylet(&[7; 20], &mut out), Ok(HASH_LEN));
assert_eq!(out[0], 7);
assert_eq!(
host.account_keylet(&[], &mut out),
Err(HostError::InvalidAccount)
);
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(()));
@@ -345,6 +360,7 @@ fn the_spec_table_matches_the_declarations() {
("home_le_inner_arr_len", 70),
("le_inner_arr_len", 70),
("check_sig", 300),
("accountroot_id", 350),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -260,6 +260,10 @@ mod ffi {
pubkey: &[u8],
) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "accountKeylet"]
fn account_keylet(self: &HostContext, account: &[u8], out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "sha512Half"]
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
@@ -407,6 +411,10 @@ impl HostFunctions for CxxHost<'_> {
scalar(self.ctx.check_signature(message, signature, pubkey))
}
fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.account_keylet(account, out))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -265,6 +265,9 @@ mod tests {
) -> HostResult<i32> {
unreachable!("no unit test in this module calls the host")
}
fn account_keylet(&self, _account: &[u8], _out: &mut [u8]) -> HostResult<usize> {
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

@@ -324,6 +324,24 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::AccountKeylet => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),
|mut caller: Caller<'_, VmState<'_>>,
acc_ptr: i32,
acc_len: i32,
out_ptr: i32,
out_len: i32|
-> Result<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::AccountKeylet, |c| {
let out = Region::new(out_ptr, out_len);
let account = Region::new(acc_ptr, acc_len);
write_buffered(c, out, |host, data, buf| {
host.account_keylet(account.read(data)?, buf)
})
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -144,6 +144,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $check_sig (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0) (i32.const 0))",
6,
),
HostFunctionSpec::AccountKeylet => (
import::ACCOUNTROOT_ID,
"(call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 32) (i32.const 32))",
4,
),
HostFunctionSpec::Sha512Half => (
import::SHA512_HALF,
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",

View File

@@ -359,6 +359,37 @@ fn check_sig_reads_all_three_regions_and_returns_the_verdict() {
assert_eq!(status(&wat, &host), 0, "the invalid verdict");
}
/// A keylet getter: reads an account region and writes a 32-byte keylet back — the
/// read-input-write-output path. The account reaches the host and the keylet lands
/// where the guest asked.
#[test]
fn accountroot_id_reads_the_account_and_writes_the_keylet() {
// Guest memory is zeroed, so a 20-byte account read is all zeros.
let account = vec![0u8; 20];
let host =
FakeHost::new().answering_account_keylet(account.clone(), support::Answer::filler(32));
let wat = module(
&[import::ACCOUNTROOT_ID, ONE_PAGE],
"(call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 64) (i32.const 64))",
);
assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length");
assert_eq!(*host.account_keylets_asked.borrow(), vec![account]);
// The keylet bytes land at the output pointer: filler is 0, 1, 2, ..., so the
// first four load as 0x03020100.
let wat = module(
&[import::ACCOUNTROOT_ID, ONE_PAGE],
"(drop (call $accountroot_id (i32.const 0) (i32.const 20) (i32.const 64) (i32.const 64)))
(i32.load (i32.const 64))",
);
assert_eq!(
status(&wat, &host),
0x03020100,
"the first four keylet bytes"
);
}
/// 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; 22] = [
const ALL_IMPORTS: [&str; 23] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -118,6 +118,7 @@ const ALL_IMPORTS: [&str; 22] = [
import::HOME_LE_INNER_ARR_LEN,
import::LE_INNER_ARR_LEN,
import::CHECK_SIG,
import::ACCOUNTROOT_ID,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -179,6 +179,11 @@ pub struct FakeHost {
pub sig_valid: HostResult<i32>,
/// Every (message, signature, pubkey) `check_signature` was asked to verify.
pub sigs_checked: RefCell<Vec<(Vec<u8>, Vec<u8>, Vec<u8>)>>,
/// What `account_keylet` answers, by account bytes. An unlisted account answers
/// `InvalidAccount`.
pub account_keylets: HashMap<Vec<u8>, Answer>,
/// Every account `account_keylet` was asked for.
pub account_keylets_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.
@@ -233,6 +238,8 @@ impl Default for FakeHost {
// Valid by default; the verification itself is the host's job, not the ABI's.
sig_valid: Ok(1),
sigs_checked: RefCell::new(Vec::new()),
account_keylets: HashMap::new(),
account_keylets_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -351,6 +358,11 @@ impl FakeHost {
self
}
pub fn answering_account_keylet(mut self, account: Vec<u8>, answer: Answer) -> FakeHost {
self.account_keylets.insert(account, answer);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -517,6 +529,16 @@ impl HostFunctions for FakeHost {
self.sig_valid
}
fn account_keylet(&self, account: &[u8], out: &mut [u8]) -> HostResult<usize> {
self.account_keylets_asked
.borrow_mut()
.push(account.to_vec());
match self.account_keylets.get(account) {
Some(answer) => answer.fill(out),
None => Err(HostError::InvalidAccount),
}
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
self.digested.borrow_mut().push(data.to_vec());
self.digest.fill(out)
@@ -576,6 +598,7 @@ pub mod import {
pub const HOME_LE_INNER_ARR_LEN: &str = r#"(import "host_lib" "home_le_inner_arr_len" (func $home_le_inner_arr_len (param i32 i32) (result i32)))"#;
pub const LE_INNER_ARR_LEN: &str = r#"(import "host_lib" "le_inner_arr_len" (func $le_inner_arr_len (param i32 i32 i32) (result i32)))"#;
pub const CHECK_SIG: &str = r#"(import "host_lib" "check_sig" (func $check_sig (param i32 i32 i32 i32 i32 i32) (result i32)))"#;
pub const ACCOUNTROOT_ID: &str = r#"(import "host_lib" "accountroot_id" (func $accountroot_id (param i32 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 =
r#"(import "host_lib" "trace" (func $trace (param i32 i32 i32 i32 i32) (result i32)))"#;

View File

@@ -120,6 +120,11 @@ public:
rust::Slice<std::uint8_t const> signature,
rust::Slice<std::uint8_t const> pubkey) const noexcept;
// The account id must be 20 bytes, else `InvalidParams`. Writes the 32-byte keylet.
[[nodiscard]] std::int32_t
accountKeylet(rust::Slice<std::uint8_t const> account, rust::Slice<std::uint8_t> out)
const noexcept;
[[nodiscard]] std::int32_t
sha512Half(rust::Slice<std::uint8_t const> data, rust::Slice<std::uint8_t> out) const noexcept;

View File

@@ -2,6 +2,7 @@
#include <xrpl/basics/Slice.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/tx/wasm/HostFunc.h>
#include <xrpl/tx/wasm/WasmCommon.h>
@@ -411,6 +412,22 @@ HostContext::checkSignature(
});
}
std::int32_t
HostContext::accountKeylet(rust::Slice<std::uint8_t const> account, rust::Slice<std::uint8_t> out)
const noexcept
{
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
if (account.size() != AccountID::size())
return hfErrorToInt(HostFunctionError::InvalidParams);
auto const value = hostFunctions_.accountKeylet(AccountID::fromVoid(account.data()));
if (!value)
return hfErrorToInt(value.error());
return answer(out, value->data(), value->size());
});
}
std::int32_t
HostContext::sha512Half(rust::Slice<std::uint8_t const> data, rust::Slice<std::uint8_t> out)
const noexcept