diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index ca1efb98fa..39a5f20662 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -247,6 +247,13 @@ host_functions! { #[wasm_name = "amm_id"] fn amm_keylet(&self, asset1: &[u8], asset2: &[u8], out: &mut [u8]) -> HostResult; + /// The 32-byte keylet of a `Check`, computed from a 20-byte account id 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 = "check_id"] + fn check_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 e5b99aa542..f1761235b3 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -190,6 +190,14 @@ impl HostFunctions for FakeHost { put(out, &[asset1.len() as u8; HASH_LEN]) } + /// A keylet from an account and a sequence; `InvalidAccount` on an empty account. + fn check_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; @@ -288,6 +296,12 @@ fn the_trait_is_implementable() { host.amm_keylet(&[1; 20], &[1; 20], &mut out), Err(HostError::InvalidParams) ); + assert_eq!(host.check_keylet(&[7; 20], 5, &mut out), Ok(HASH_LEN)); + assert_eq!(out[0], 7); + assert_eq!( + host.check_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(())); @@ -376,6 +390,7 @@ fn the_spec_table_matches_the_declarations() { ("check_sig", 300), ("accountroot_id", 350), ("amm_id", 450), + ("check_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 01776c6ed0..5db3eabaaf 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -268,6 +268,10 @@ mod ffi { #[cxx_name = "ammKeylet"] fn amm_keylet(self: &HostContext, asset1: &[u8], asset2: &[u8], out: &mut [u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "checkKeylet"] + fn check_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; @@ -423,6 +427,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.amm_keylet(asset1, asset2, out)) } + fn check_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.check_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 d9344095e6..78d8bfa523 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -271,6 +271,9 @@ mod tests { fn amm_keylet(&self, _asset1: &[u8], _asset2: &[u8], _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn check_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 6d276f3c4e..82544efaab 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -363,6 +363,25 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::CheckKeylet => 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::CheckKeylet, |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.check_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 13d95a9039..ab2a610ac9 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -154,6 +154,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $amm_id (i32.const 0) (i32.const 20) (i32.const 24) (i32.const 40) (i32.const 0) (i32.const 32))", 6, ), + HostFunctionSpec::CheckKeylet => ( + import::CHECK_ID, + "(call $check_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 d8000aa898..8ac05a152d 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -414,6 +414,23 @@ fn amm_id_reads_two_assets_and_writes_the_keylet() { assert_eq!(*host.amm_keylets_asked.borrow(), vec![(asset1, asset2)]); } +/// A keylet getter that reads an account region and also takes a scalar seq: both +/// reach the host keyed together, and the keylet lands where the guest asked. +#[test] +fn check_id_reads_the_account_and_seq_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_check_keylet(account.clone(), 5, support::Answer::filler(32)); + + let wat = module( + &[import::CHECK_ID, ONE_PAGE], + "(call $check_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.check_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 285c5d016b..c494de5630 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; 24] = [ +const ALL_IMPORTS: [&str; 25] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, @@ -120,6 +120,7 @@ const ALL_IMPORTS: [&str; 24] = [ import::CHECK_SIG, import::ACCOUNTROOT_ID, import::AMM_ID, + import::CHECK_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 43c0bff329..9ef3aade86 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -189,6 +189,11 @@ pub struct FakeHost { pub amm_keylets: HashMap<(Vec, Vec), Answer>, /// Every (asset1, asset2) pair `amm_keylet` was asked for. pub amm_keylets_asked: RefCell, Vec)>>, + /// What `check_keylet` answers, by (account bytes, seq). An unlisted key answers + /// `InvalidAccount`. + pub check_keylets: HashMap<(Vec, i32), Answer>, + /// Every (account, seq) `check_keylet` was asked for. + pub check_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. @@ -247,6 +252,8 @@ impl Default for FakeHost { account_keylets_asked: RefCell::new(Vec::new()), amm_keylets: HashMap::new(), amm_keylets_asked: RefCell::new(Vec::new()), + check_keylets: HashMap::new(), + check_keylets_asked: RefCell::new(Vec::new()), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), digested: RefCell::new(Vec::new()), @@ -380,6 +387,16 @@ impl FakeHost { self } + pub fn answering_check_keylet( + mut self, + account: Vec, + seq: i32, + answer: Answer, + ) -> FakeHost { + self.check_keylets.insert((account, seq), answer); + self + } + pub fn answering_digest(mut self, answer: Answer) -> FakeHost { self.digest = answer; self @@ -566,6 +583,16 @@ impl HostFunctions for FakeHost { } } + fn check_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult { + self.check_keylets_asked + .borrow_mut() + .push((account.to_vec(), seq)); + match self.check_keylets.get(&(account.to_vec(), seq)) { + 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) @@ -627,6 +654,7 @@ pub mod import { 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 AMM_ID: &str = r#"(import "host_lib" "amm_id" (func $amm_id (param i32 i32 i32 i32 i32 i32) (result i32)))"#; + pub const CHECK_ID: &str = r#"(import "host_lib" "check_id" (func $check_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 5af72a16bf..8e0b3c5a11 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -133,6 +133,14 @@ public: rust::Slice asset2, 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 + checkKeylet( + 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 1bc7005fa3..a739bb9e54 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -485,6 +485,26 @@ HostContext::ammKeylet( }); } +std::int32_t +HostContext::checkKeylet( + 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_.checkKeylet( + 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