feat: Hook up mptoken_id host function

This commit is contained in:
TimothyBanks
2026-08-10 21:08:26 -04:00
parent c1008c473c
commit 2821cc3e8e
11 changed files with 149 additions and 1 deletions

View File

@@ -328,6 +328,12 @@ host_functions! {
out: &mut [u8],
) -> HostResult<usize>;
/// The 32-byte keylet of an `MPToken`, computed from a 24-byte MPT issuance id and
/// the 20-byte holder account. Reads both regions and writes the keylet.
#[gas = 500]
#[wasm_name = "mptoken_id"]
fn mptoken_keylet(&self, mptid: &[u8], holder: &[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

@@ -296,6 +296,18 @@ impl HostFunctions for FakeHost {
put(out, &[issuer[0]; HASH_LEN])
}
/// A keylet from an MPT id and a holder; `InvalidParams` if the id is empty,
/// `InvalidAccount` if the holder is empty.
fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult<usize> {
if mptid.is_empty() {
return Err(HostError::InvalidParams);
}
if holder.is_empty() {
return Err(HostError::InvalidAccount);
}
put(out, &[mptid[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;
@@ -457,6 +469,15 @@ fn the_trait_is_implementable() {
host.mptoken_issuance_keylet(&[], 5, &mut out),
Err(HostError::InvalidAccount)
);
assert_eq!(
host.mptoken_keylet(&[9; 24], &[8; 20], &mut out),
Ok(HASH_LEN)
);
assert_eq!(out[0], 9);
assert_eq!(
host.mptoken_keylet(&[], &[8; 20], &mut out),
Err(HostError::InvalidParams)
);
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(()));
@@ -553,6 +574,7 @@ fn the_spec_table_matches_the_declarations() {
("escrow_id", 350),
("trustline_id", 400),
("mpt_issuance_id", 350),
("mptoken_id", 500),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -327,6 +327,10 @@ mod ffi {
out: &mut [u8],
) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "mptokenKeylet"]
fn mptoken_keylet(self: &HostContext, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "sha512Half"]
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
@@ -547,6 +551,10 @@ impl HostFunctions for CxxHost<'_> {
bytes_written(self.ctx.mptoken_issuance_keylet(issuer, seq, out))
}
fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.mptoken_keylet(mptid, holder, out))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -322,6 +322,14 @@ mod tests {
) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn mptoken_keylet(
&self,
_mptid: &[u8],
_holder: &[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

@@ -542,6 +542,27 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::MptokenKeylet => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),
|mut caller: Caller<'_, VmState<'_>>,
mpt_ptr: i32,
mpt_len: i32,
holder_ptr: i32,
holder_len: i32,
out_ptr: i32,
out_len: i32|
-> Result<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::MptokenKeylet, |c| {
let out = Region::new(out_ptr, out_len);
let mptid = Region::new(mpt_ptr, mpt_len);
let holder = Region::new(holder_ptr, holder_len);
write_buffered(c, out, |host, data, buf| {
host.mptoken_keylet(mptid.read(data)?, holder.read(data)?, buf)
})
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -194,6 +194,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $mpt_issuance_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))",
5,
),
HostFunctionSpec::MptokenKeylet => (
import::MPTOKEN_ID,
"(call $mptoken_id (i32.const 0) (i32.const 24) (i32.const 24) (i32.const 20) (i32.const 44) (i32.const 20))",
6,
),
HostFunctionSpec::Sha512Half => (
import::SHA512_HALF,
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",

View File

@@ -589,6 +589,29 @@ fn mpt_issuance_id_reads_the_issuer_and_seq() {
assert_eq!(*host.mpt_issuance_keylets_asked.borrow(), vec![(issuer, 5)]);
}
/// A keylet from a 24-byte MPT id and a 20-byte holder: both reach the host as a
/// pair, and the keylet lands where the guest asked.
#[test]
fn mptoken_id_reads_the_mptid_and_holder() {
let mptid = vec![0u8; 24];
let holder = vec![0u8; 20];
let host = FakeHost::new().answering_mptoken_keylet(
mptid.clone(),
holder.clone(),
support::Answer::filler(32),
);
let wat = module(
&[import::MPTOKEN_ID, ONE_PAGE],
"(call $mptoken_id
(i32.const 0) (i32.const 24)
(i32.const 24) (i32.const 20)
(i32.const 64) (i32.const 64))",
);
assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length");
assert_eq!(*host.mptoken_keylets_asked.borrow(), vec![(mptid, holder)]);
}
/// 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; 32] = [
const ALL_IMPORTS: [&str; 33] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -128,6 +128,7 @@ const ALL_IMPORTS: [&str; 32] = [
import::ESCROW_ID,
import::TRUSTLINE_ID,
import::MPT_ISSUANCE_ID,
import::MPTOKEN_ID,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -229,6 +229,11 @@ pub struct FakeHost {
pub mpt_issuance_keylets: HashMap<(Vec<u8>, i32), Answer>,
/// Every (issuer, seq) `mptoken_issuance_keylet` was asked for.
pub mpt_issuance_keylets_asked: RefCell<Vec<(Vec<u8>, i32)>>,
/// What `mptoken_keylet` answers, by (mptid, holder) bytes. An unlisted key answers
/// `InvalidParams`.
pub mptoken_keylets: HashMap<(Vec<u8>, Vec<u8>), Answer>,
/// Every (mptid, holder) `mptoken_keylet` was asked for.
pub mptoken_keylets_asked: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
/// What `sha512_half` answers, whatever it is given.
pub digest: Answer,
/// Every field selector `get_current_ledger_obj_field` was asked for.
@@ -303,6 +308,8 @@ impl Default for FakeHost {
trust_line_keylets_asked: RefCell::new(Vec::new()),
mpt_issuance_keylets: HashMap::new(),
mpt_issuance_keylets_asked: RefCell::new(Vec::new()),
mptoken_keylets: HashMap::new(),
mptoken_keylets_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -516,6 +523,16 @@ impl FakeHost {
self
}
pub fn answering_mptoken_keylet(
mut self,
mptid: Vec<u8>,
holder: Vec<u8>,
answer: Answer,
) -> FakeHost {
self.mptoken_keylets.insert((mptid, holder), answer);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -805,6 +822,15 @@ impl HostFunctions for FakeHost {
}
}
fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult<usize> {
let key = (mptid.to_vec(), holder.to_vec());
self.mptoken_keylets_asked.borrow_mut().push(key.clone());
match self.mptoken_keylets.get(&key) {
Some(answer) => answer.fill(out),
None => Err(HostError::InvalidParams),
}
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
self.digested.borrow_mut().push(data.to_vec());
self.digest.fill(out)
@@ -875,6 +901,7 @@ pub mod import {
pub const ESCROW_ID: &str = r#"(import "host_lib" "escrow_id" (func $escrow_id (param i32 i32 i32 i32 i32) (result i32)))"#;
pub const TRUSTLINE_ID: &str = r#"(import "host_lib" "trustline_id" (func $trustline_id (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))"#;
pub const MPT_ISSUANCE_ID: &str = r#"(import "host_lib" "mpt_issuance_id" (func $mpt_issuance_id (param i32 i32 i32 i32 i32) (result i32)))"#;
pub const MPTOKEN_ID: &str = r#"(import "host_lib" "mptoken_id" (func $mptoken_id (param i32 i32 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

@@ -194,6 +194,14 @@ public:
std::int32_t seq,
rust::Slice<std::uint8_t> out) const noexcept;
// The MPT id must be 24 bytes and the holder 20, else `InvalidParams`. Writes the
// 32-byte keylet.
[[nodiscard]] std::int32_t
mptokenKeylet(
rust::Slice<std::uint8_t const> mptid,
rust::Slice<std::uint8_t const> holder,
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

@@ -644,6 +644,25 @@ HostContext::mptokenIssuanceKeylet(
});
}
std::int32_t
HostContext::mptokenKeylet(
rust::Slice<std::uint8_t const> mptid,
rust::Slice<std::uint8_t const> holder,
rust::Slice<std::uint8_t> out) const noexcept
{
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
if (mptid.size() != MPTID::size() || holder.size() != AccountID::size())
return hfErrorToInt(HostFunctionError::InvalidParams);
auto const value = hostFunctions_.mptokenKeylet(
MPTID::fromVoid(mptid.data()), AccountID::fromVoid(holder.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