feat: Hook up nft_offer_id host function

This commit is contained in:
TimothyBanks
2026-08-10 21:12:57 -04:00
parent 2821cc3e8e
commit f52eb08d8a
11 changed files with 147 additions and 1 deletions

View File

@@ -334,6 +334,18 @@ host_functions! {
#[wasm_name = "mptoken_id"]
fn mptoken_keylet(&self, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> HostResult<usize>;
/// The 32-byte keylet of an `NFTokenOffer`, 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 = "nft_offer_id"]
fn nftoken_offer_keylet(
&self,
account: &[u8],
seq: i32,
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

@@ -308,6 +308,14 @@ impl HostFunctions for FakeHost {
put(out, &[mptid[0]; HASH_LEN])
}
/// The account-and-sequence shape, for an `NFTokenOffer`.
fn nftoken_offer_keylet(&self, account: &[u8], _seq: i32, 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;
@@ -478,6 +486,15 @@ fn the_trait_is_implementable() {
host.mptoken_keylet(&[], &[8; 20], &mut out),
Err(HostError::InvalidParams)
);
assert_eq!(
host.nftoken_offer_keylet(&[7; 20], 5, &mut out),
Ok(HASH_LEN)
);
assert_eq!(out[0], 7);
assert_eq!(
host.nftoken_offer_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(()));
@@ -575,6 +592,7 @@ fn the_spec_table_matches_the_declarations() {
("trustline_id", 400),
("mpt_issuance_id", 350),
("mptoken_id", 500),
("nft_offer_id", 350),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -331,6 +331,15 @@ mod ffi {
#[cxx_name = "mptokenKeylet"]
fn mptoken_keylet(self: &HostContext, mptid: &[u8], holder: &[u8], out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "nftokenOfferKeylet"]
fn nftoken_offer_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;
@@ -555,6 +564,10 @@ impl HostFunctions for CxxHost<'_> {
bytes_written(self.ctx.mptoken_keylet(mptid, holder, out))
}
fn nftoken_offer_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.nftoken_offer_keylet(account, seq, out))
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

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

@@ -563,6 +563,25 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::NftokenOfferKeylet => 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<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::NftokenOfferKeylet, |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.nftoken_offer_keylet(account.read(data)?, seq, buf)
})
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -199,6 +199,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $mptoken_id (i32.const 0) (i32.const 24) (i32.const 24) (i32.const 20) (i32.const 44) (i32.const 20))",
6,
),
HostFunctionSpec::NftokenOfferKeylet => (
import::NFT_OFFER_ID,
"(call $nft_offer_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))",

View File

@@ -612,6 +612,21 @@ fn mptoken_id_reads_the_mptid_and_holder() {
assert_eq!(*host.mptoken_keylets_asked.borrow(), vec![(mptid, holder)]);
}
/// Another account-and-sequence keylet, with its own answer set.
#[test]
fn nft_offer_id_reads_the_account_and_seq() {
let account = vec![0u8; 20];
let host =
FakeHost::new().answering_nft_offer_keylet(account.clone(), 5, support::Answer::filler(32));
let wat = module(
&[import::NFT_OFFER_ID, ONE_PAGE],
"(call $nft_offer_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.nft_offer_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() {

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; 33] = [
const ALL_IMPORTS: [&str; 34] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -129,6 +129,7 @@ const ALL_IMPORTS: [&str; 33] = [
import::TRUSTLINE_ID,
import::MPT_ISSUANCE_ID,
import::MPTOKEN_ID,
import::NFT_OFFER_ID,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -234,6 +234,11 @@ pub struct FakeHost {
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 `nftoken_offer_keylet` answers, by (account bytes, seq). An unlisted key
/// answers `InvalidAccount`.
pub nft_offer_keylets: HashMap<(Vec<u8>, i32), Answer>,
/// Every (account, seq) `nftoken_offer_keylet` was asked for.
pub nft_offer_keylets_asked: RefCell<Vec<(Vec<u8>, i32)>>,
/// What `sha512_half` answers, whatever it is given.
pub digest: Answer,
/// Every field selector `get_current_ledger_obj_field` was asked for.
@@ -310,6 +315,8 @@ impl Default for FakeHost {
mpt_issuance_keylets_asked: RefCell::new(Vec::new()),
mptoken_keylets: HashMap::new(),
mptoken_keylets_asked: RefCell::new(Vec::new()),
nft_offer_keylets: HashMap::new(),
nft_offer_keylets_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -533,6 +540,16 @@ impl FakeHost {
self
}
pub fn answering_nft_offer_keylet(
mut self,
account: Vec<u8>,
seq: i32,
answer: Answer,
) -> FakeHost {
self.nft_offer_keylets.insert((account, seq), answer);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -831,6 +848,15 @@ impl HostFunctions for FakeHost {
}
}
fn nftoken_offer_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult<usize> {
let key = (account.to_vec(), seq);
self.nft_offer_keylets_asked.borrow_mut().push(key.clone());
match self.nft_offer_keylets.get(&key) {
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)
@@ -902,6 +928,7 @@ pub mod import {
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 NFT_OFFER_ID: &str = r#"(import "host_lib" "nft_offer_id" (func $nft_offer_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)))"#;

View File

@@ -202,6 +202,14 @@ public:
rust::Slice<std::uint8_t const> holder,
rust::Slice<std::uint8_t> 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
nftokenOfferKeylet(
rust::Slice<std::uint8_t const> account,
std::int32_t seq,
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

@@ -663,6 +663,26 @@ HostContext::mptokenKeylet(
});
}
std::int32_t
HostContext::nftokenOfferKeylet(
rust::Slice<std::uint8_t const> account,
std::int32_t seq,
rust::Slice<std::uint8_t> 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_.nftokenOfferKeylet(
AccountID::fromVoid(account.data()), static_cast<std::uint32_t>(seq));
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