From c1008c473c6db4b75ab671bb012bd6ab8832f218 Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 21:04:38 -0400 Subject: [PATCH] feat: Hook up mpt_issuance_id host function --- crates/xrpl-host-functions/src/lib.rs | 12 +++++++ .../tests/generated_abi.rs | 23 +++++++++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 18 ++++++++++ crates/xrpl-wasm-vm/src/abi.rs | 8 +++++ crates/xrpl-wasm-vm/src/register.rs | 19 +++++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 5 +++ crates/xrpl-wasm-vm/tests/host_calls.rs | 18 ++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 +- crates/xrpl-wasm-vm/tests/support/mod.rs | 34 +++++++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 8 +++++ src/libxrpl/tx/wasm/HostContext.cpp | 20 +++++++++++ 11 files changed, 167 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index b356cf50ee..8458fa4a5d 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -316,6 +316,18 @@ host_functions! { out: &mut [u8], ) -> HostResult; + /// The 32-byte keylet of an `MPTokenIssuance`, computed from the 20-byte issuer + /// account and its sequence number. `seq` is the guest's `u32` carried as its + /// `i32` bit pattern. Reads the account region and writes the keylet. + #[gas = 350] + #[wasm_name = "mpt_issuance_id"] + fn mptoken_issuance_keylet( + &self, + issuer: &[u8], + seq: i32, + out: &mut [u8], + ) -> HostResult; + /// The XRPL `sha512Half` of `data`: the first [`HASH_LEN`] bytes of its SHA-512. #[gas = 2000] #[wasm_name = "sha512_half"] diff --git a/crates/xrpl-host-functions/tests/generated_abi.rs b/crates/xrpl-host-functions/tests/generated_abi.rs index 8b7d17f4fa..0b04c597cd 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -283,6 +283,19 @@ impl HostFunctions for FakeHost { put(out, &[account1[0]; HASH_LEN]) } + /// The issuer-and-sequence shape, for an `MPTokenIssuance`. + fn mptoken_issuance_keylet( + &self, + issuer: &[u8], + _seq: i32, + out: &mut [u8], + ) -> HostResult { + if issuer.is_empty() { + return Err(HostError::InvalidAccount); + } + put(out, &[issuer[0]; HASH_LEN]) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { let mut digest = [0; HASH_LEN]; digest[0] = data.len() as u8; @@ -435,6 +448,15 @@ fn the_trait_is_implementable() { host.trust_line_keylet(&[7; 20], &[7; 20], &[1; 20], &mut out), Err(HostError::InvalidParams) ); + assert_eq!( + host.mptoken_issuance_keylet(&[7; 20], 5, &mut out), + Ok(HASH_LEN) + ); + assert_eq!(out[0], 7); + assert_eq!( + host.mptoken_issuance_keylet(&[], 5, &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(())); @@ -530,6 +552,7 @@ fn the_spec_table_matches_the_declarations() { ("did_id", 350), ("escrow_id", 350), ("trustline_id", 400), + ("mpt_issuance_id", 350), ("sha512_half", 2000), ("trace", 500), ("trace_num", 500), diff --git a/crates/xrpl-wasm-vm-ffi/src/lib.rs b/crates/xrpl-wasm-vm-ffi/src/lib.rs index e7d95ad035..8210042e49 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -318,6 +318,15 @@ mod ffi { out: &mut [u8], ) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "mptokenIssuanceKeylet"] + fn mptoken_issuance_keylet( + self: &HostContext, + issuer: &[u8], + seq: i32, + out: &mut [u8], + ) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -529,6 +538,15 @@ impl HostFunctions for CxxHost<'_> { ) } + fn mptoken_issuance_keylet( + &self, + issuer: &[u8], + seq: i32, + out: &mut [u8], + ) -> HostResult { + bytes_written(self.ctx.mptoken_issuance_keylet(issuer, seq, out)) + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { bytes_written(self.ctx.sha512_half(data, out)) } diff --git a/crates/xrpl-wasm-vm/src/abi.rs b/crates/xrpl-wasm-vm/src/abi.rs index cf943942b1..614d0216cd 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -314,6 +314,14 @@ mod tests { ) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn mptoken_issuance_keylet( + &self, + _issuer: &[u8], + _seq: i32, + _out: &mut [u8], + ) -> HostResult { + unreachable!("no unit test in this module calls the host") + } fn sha512_half(&self, _data: &[u8], _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } diff --git a/crates/xrpl-wasm-vm/src/register.rs b/crates/xrpl-wasm-vm/src/register.rs index 1bf9f738d1..fcea025a70 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -523,6 +523,25 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::MptokenIssuanceKeylet => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + acc_ptr: i32, + acc_len: i32, + seq: i32, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::MptokenIssuanceKeylet, |c| { + let out = Region::new(out_ptr, out_len); + let issuer = Region::new(acc_ptr, acc_len); + write_buffered(c, out, |host, data, buf| { + host.mptoken_issuance_keylet(issuer.read(data)?, seq, buf) + }) + }) + }, + ), HostFunctionSpec::Sha512Half => linker.func_wrap( HOST_MODULE, op.wasm_name(), diff --git a/crates/xrpl-wasm-vm/tests/budgets.rs b/crates/xrpl-wasm-vm/tests/budgets.rs index cd1c00bb1e..43accf5194 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -189,6 +189,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $trustline_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 40) (i32.const 20) (i32.const 60) (i32.const 32))", 8, ), + HostFunctionSpec::MptokenIssuanceKeylet => ( + import::MPT_ISSUANCE_ID, + "(call $mpt_issuance_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))", + 5, + ), HostFunctionSpec::Sha512Half => ( import::SHA512_HALF, "(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))", diff --git a/crates/xrpl-wasm-vm/tests/host_calls.rs b/crates/xrpl-wasm-vm/tests/host_calls.rs index 027ef8c8eb..456bcbccb5 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -571,6 +571,24 @@ fn trustline_id_reads_two_accounts_and_a_currency() { ); } +/// The issuer-and-sequence keylet shape (like escrow), with its own answer set. +#[test] +fn mpt_issuance_id_reads_the_issuer_and_seq() { + let issuer = vec![0u8; 20]; + let host = FakeHost::new().answering_mpt_issuance_keylet( + issuer.clone(), + 5, + support::Answer::filler(32), + ); + + let wat = module( + &[import::MPT_ISSUANCE_ID, ONE_PAGE], + "(call $mpt_issuance_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 64) (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length"); + assert_eq!(*host.mpt_issuance_keylets_asked.borrow(), vec![(issuer, 5)]); +} + /// A leading scalar parameter reaches the host as declared. #[test] fn home_le_field_passes_the_field_selector_through() { diff --git a/crates/xrpl-wasm-vm/tests/preflight.rs b/crates/xrpl-wasm-vm/tests/preflight.rs index 7a9b09f4f1..01d167b607 100644 --- a/crates/xrpl-wasm-vm/tests/preflight.rs +++ b/crates/xrpl-wasm-vm/tests/preflight.rs @@ -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; 31] = [ +const ALL_IMPORTS: [&str; 32] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -127,6 +127,7 @@ const ALL_IMPORTS: [&str; 31] = [ import::DID_ID, import::ESCROW_ID, import::TRUSTLINE_ID, + import::MPT_ISSUANCE_ID, import::SHA512_HALF, import::TRACE, import::TRACE_NUM, diff --git a/crates/xrpl-wasm-vm/tests/support/mod.rs b/crates/xrpl-wasm-vm/tests/support/mod.rs index 15a1a1acdf..52000126bb 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -224,6 +224,11 @@ pub struct FakeHost { pub trust_line_keylets: HashMap<(Vec, Vec, Vec), Answer>, /// Every (account1, account2, currency) `trust_line_keylet` was asked for. pub trust_line_keylets_asked: RefCell, Vec, Vec)>>, + /// What `mptoken_issuance_keylet` answers, by (issuer bytes, seq). An unlisted key + /// answers `InvalidAccount`. + pub mpt_issuance_keylets: HashMap<(Vec, i32), Answer>, + /// Every (issuer, seq) `mptoken_issuance_keylet` was asked for. + pub mpt_issuance_keylets_asked: RefCell, i32)>>, /// What `sha512_half` answers, whatever it is given. pub digest: Answer, /// Every field selector `get_current_ledger_obj_field` was asked for. @@ -296,6 +301,8 @@ impl Default for FakeHost { escrow_keylets_asked: RefCell::new(Vec::new()), trust_line_keylets: HashMap::new(), trust_line_keylets_asked: RefCell::new(Vec::new()), + mpt_issuance_keylets: HashMap::new(), + mpt_issuance_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -499,6 +506,16 @@ impl FakeHost { self } + pub fn answering_mpt_issuance_keylet( + mut self, + issuer: Vec, + seq: i32, + answer: Answer, + ) -> FakeHost { + self.mpt_issuance_keylets.insert((issuer, seq), answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -772,6 +789,22 @@ impl HostFunctions for FakeHost { } } + fn mptoken_issuance_keylet( + &self, + issuer: &[u8], + seq: i32, + out: &mut [u8], + ) -> HostResult { + let key = (issuer.to_vec(), seq); + self.mpt_issuance_keylets_asked + .borrow_mut() + .push(key.clone()); + match self.mpt_issuance_keylets.get(&key) { + Some(answer) => answer.fill(out), + None => Err(HostError::InvalidAccount), + } + } + fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult { self.digested.borrow_mut().push(data.to_vec()); self.digest.fill(out) @@ -841,6 +874,7 @@ pub mod import { r#"(import "host_lib" "did_id" (func $did_id (param i32 i32 i32 i32) (result i32)))"#; 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 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)))"#; diff --git a/include/xrpl/tx/wasm/HostContext.h b/include/xrpl/tx/wasm/HostContext.h index 324f849f1c..0d430a425a 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -186,6 +186,14 @@ public: rust::Slice currency, rust::Slice out) const noexcept; + // The issuer id must be 20 bytes, else `InvalidParams`. `seq` carries the guest's + // u32 as its i32 bit pattern. Writes the 32-byte keylet. + [[nodiscard]] std::int32_t + mptokenIssuanceKeylet( + rust::Slice issuer, + std::int32_t seq, + rust::Slice out) const noexcept; + [[nodiscard]] std::int32_t sha512Half(rust::Slice data, rust::Slice out) const noexcept; diff --git a/src/libxrpl/tx/wasm/HostContext.cpp b/src/libxrpl/tx/wasm/HostContext.cpp index fea825b173..ba9bf3846f 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -624,6 +624,26 @@ HostContext::trustLineKeylet( }); } +std::int32_t +HostContext::mptokenIssuanceKeylet( + rust::Slice issuer, + std::int32_t seq, + rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (issuer.size() != AccountID::size()) + return hfErrorToInt(HostFunctionError::InvalidParams); + + // The guest's u32 seq arrives as its i32 bit pattern; recover it. + auto const value = hostFunctions_.mptokenIssuanceKeylet( + AccountID::fromVoid(issuer.data()), static_cast(seq)); + if (!value) + return hfErrorToInt(value.error()); + + return answer(out, value->data(), value->size()); + }); +} + std::int32_t HostContext::sha512Half(rust::Slice data, rust::Slice out) const noexcept