From 98abdef208441a3af710dd2007bcd9923d16409f Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 21:49:46 -0400 Subject: [PATCH] feat: Hook up vault_id host function --- crates/xrpl-host-functions/src/lib.rs | 7 +++++ .../tests/generated_abi.rs | 15 +++++++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 8 ++++++ crates/xrpl-wasm-vm/src/abi.rs | 3 +++ crates/xrpl-wasm-vm/src/register.rs | 19 +++++++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 5 ++++ crates/xrpl-wasm-vm/tests/host_calls.rs | 15 +++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 ++- crates/xrpl-wasm-vm/tests/support/mod.rs | 27 +++++++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 8 ++++++ src/libxrpl/tx/wasm/HostContext.cpp | 20 ++++++++++++++ 11 files changed, 129 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index 8954a91e64..7f51efd52c 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -399,6 +399,13 @@ host_functions! { #[wasm_name = "ticket_id"] fn ticket_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult; + /// The 32-byte keylet of a `Vault`, computed from the 20-byte owner 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 = "vault_id"] + fn vault_keylet(&self, account: &[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 a3df0f5c56..4bf9c7dddb 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -376,6 +376,14 @@ impl HostFunctions for FakeHost { put(out, &[account[0]; HASH_LEN]) } + /// The same account-and-sequence shape, for a `Vault`. + fn vault_keylet(&self, account: &[u8], _seq: i32, out: &mut [u8]) -> HostResult { + if account.is_empty() { + return Err(HostError::InvalidAccount); + } + put(out, &[account[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; @@ -597,6 +605,12 @@ fn the_trait_is_implementable() { host.ticket_keylet(&[], 5, &mut out), Err(HostError::InvalidAccount) ); + assert_eq!(host.vault_keylet(&[7; 20], 5, &mut out), Ok(HASH_LEN)); + assert_eq!(out[0], 7); + assert_eq!( + host.vault_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(())); @@ -701,6 +715,7 @@ fn the_spec_table_matches_the_declarations() { ("permissioned_domain_id", 350), ("signers_id", 350), ("ticket_id", 350), + ("vault_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 7d1d7ace21..5eac97b36b 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -375,6 +375,10 @@ mod ffi { #[cxx_name = "ticketKeylet"] fn ticket_keylet(self: &HostContext, account: &[u8], seq: i32, out: &mut [u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "vaultKeylet"] + fn vault_keylet(self: &HostContext, account: &[u8], seq: i32, out: &mut [u8]) -> i32; + #[namespace = "xrpl"] #[cxx_name = "sha512Half"] fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32; @@ -638,6 +642,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.ticket_keylet(account, seq, out)) } + fn vault_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.vault_keylet(account, 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 ca2bce12e6..2ecdbeb816 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -372,6 +372,9 @@ mod tests { fn ticket_keylet(&self, _account: &[u8], _seq: i32, _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn vault_keylet(&self, _account: &[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 55734898f5..aaea45ac46 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -707,6 +707,25 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::VaultKeylet => 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::VaultKeylet, |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.vault_keylet(account.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 35379440ab..a7a38f1e15 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -234,6 +234,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $ticket_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))", 5, ), + HostFunctionSpec::VaultKeylet => ( + import::VAULT_ID, + "(call $vault_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 105a483d71..d8c2fe1b16 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -733,6 +733,21 @@ fn ticket_id_reads_the_account_and_seq() { assert_eq!(*host.ticket_keylets_asked.borrow(), vec![(account, 5)]); } +/// The last account-and-sequence keylet, with its own answer set, for a vault. +#[test] +fn vault_id_reads_the_account_and_seq() { + let account = vec![0u8; 20]; + let host = + FakeHost::new().answering_vault_keylet(account.clone(), 5, support::Answer::filler(32)); + + let wat = module( + &[import::VAULT_ID, ONE_PAGE], + "(call $vault_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.vault_keylets_asked.borrow(), vec![(account, 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 450436dd52..912b0eed7e 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; 40] = [ +const ALL_IMPORTS: [&str; 41] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -136,6 +136,7 @@ const ALL_IMPORTS: [&str; 40] = [ import::PERMISSIONED_DOMAIN_ID, import::SIGNERS_ID, import::TICKET_ID, + import::VAULT_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 c8d09d923b..861019940b 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -269,6 +269,11 @@ pub struct FakeHost { pub ticket_keylets: HashMap<(Vec, i32), Answer>, /// Every (account, seq) `ticket_keylet` was asked for. pub ticket_keylets_asked: RefCell, i32)>>, + /// What `vault_keylet` answers, by (account bytes, seq). An unlisted key answers + /// `InvalidAccount`. + pub vault_keylets: HashMap<(Vec, i32), Answer>, + /// Every (account, seq) `vault_keylet` was asked for. + pub vault_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. @@ -359,6 +364,8 @@ impl Default for FakeHost { signer_list_keylets_asked: RefCell::new(Vec::new()), ticket_keylets: HashMap::new(), ticket_keylets_asked: RefCell::new(Vec::new()), + vault_keylets: HashMap::new(), + vault_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -649,6 +656,16 @@ impl FakeHost { self } + pub fn answering_vault_keylet( + mut self, + account: Vec, + seq: i32, + answer: Answer, + ) -> FakeHost { + self.vault_keylets.insert((account, seq), answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -1022,6 +1039,15 @@ impl HostFunctions for FakeHost { } } + fn vault_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult { + let key = (account.to_vec(), seq); + self.vault_keylets_asked.borrow_mut().push(key.clone()); + match self.vault_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) @@ -1100,6 +1126,7 @@ pub mod import { pub const PERMISSIONED_DOMAIN_ID: &str = r#"(import "host_lib" "permissioned_domain_id" (func $permissioned_domain_id (param i32 i32 i32 i32 i32) (result i32)))"#; pub const SIGNERS_ID: &str = r#"(import "host_lib" "signers_id" (func $signers_id (param i32 i32 i32 i32) (result i32)))"#; pub const TICKET_ID: &str = r#"(import "host_lib" "ticket_id" (func $ticket_id (param i32 i32 i32 i32 i32) (result i32)))"#; + pub const VAULT_ID: &str = r#"(import "host_lib" "vault_id" (func $vault_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 d2d4120935..8b82c83c9a 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -256,6 +256,14 @@ public: std::int32_t seq, rust::Slice out) const noexcept; + // The account 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 + vaultKeylet( + rust::Slice account, + 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 0a4fe25a83..5291fed8f2 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -803,6 +803,26 @@ HostContext::ticketKeylet( }); } +std::int32_t +HostContext::vaultKeylet( + rust::Slice account, + std::int32_t seq, + rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + if (account.size() != AccountID::size()) + return hfErrorToInt(HostFunctionError::InvalidParams); + + // The guest's u32 seq arrives as its i32 bit pattern; recover it. + auto const value = hostFunctions_.vaultKeylet( + AccountID::fromVoid(account.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