feat: Hook up trustline_id host function

This commit is contained in:
TimothyBanks
2026-08-10 21:00:11 -04:00
parent 78c8128c98
commit 50623665c7
11 changed files with 205 additions and 1 deletions

View File

@@ -303,6 +303,19 @@ host_functions! {
#[wasm_name = "escrow_id"]
fn escrow_keylet(&self, account: &[u8], seq: i32, out: &mut [u8]) -> HostResult<usize>;
/// The 32-byte keylet of a `RippleState` (trust line), computed from two 20-byte
/// account ids and a 20-byte currency. Reads all three regions and writes the
/// keylet.
#[gas = 400]
#[wasm_name = "trustline_id"]
fn trust_line_keylet(
&self,
account1: &[u8],
account2: &[u8],
currency: &[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

@@ -265,6 +265,24 @@ impl HostFunctions for FakeHost {
put(out, &[account[0]; HASH_LEN])
}
/// A keylet from two accounts and a currency; `InvalidAccount` if either account
/// is empty, `InvalidParams` if they are equal or the currency is empty.
fn trust_line_keylet(
&self,
account1: &[u8],
account2: &[u8],
currency: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
if account1.is_empty() || account2.is_empty() {
return Err(HostError::InvalidAccount);
}
if account1 == account2 || currency.is_empty() {
return Err(HostError::InvalidParams);
}
put(out, &[account1[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;
@@ -408,6 +426,15 @@ fn the_trait_is_implementable() {
host.escrow_keylet(&[], 5, &mut out),
Err(HostError::InvalidAccount)
);
assert_eq!(
host.trust_line_keylet(&[7; 20], &[8; 20], &[1; 20], &mut out),
Ok(HASH_LEN)
);
assert_eq!(out[0], 7);
assert_eq!(
host.trust_line_keylet(&[7; 20], &[7; 20], &[1; 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(()));
@@ -502,6 +529,7 @@ fn the_spec_table_matches_the_declarations() {
("deposit_preauth_id", 350),
("did_id", 350),
("escrow_id", 350),
("trustline_id", 400),
("sha512_half", 2000),
("trace", 500),
("trace_num", 500),

View File

@@ -308,6 +308,16 @@ mod ffi {
#[cxx_name = "escrowKeylet"]
fn escrow_keylet(self: &HostContext, account: &[u8], seq: i32, out: &mut [u8]) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "trustLineKeylet"]
fn trust_line_keylet(
self: &HostContext,
account1: &[u8],
account2: &[u8],
currency: &[u8],
out: &mut [u8],
) -> i32;
#[namespace = "xrpl"]
#[cxx_name = "sha512Half"]
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
@@ -506,6 +516,19 @@ impl HostFunctions for CxxHost<'_> {
bytes_written(self.ctx.escrow_keylet(account, seq, out))
}
fn trust_line_keylet(
&self,
account1: &[u8],
account2: &[u8],
currency: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
bytes_written(
self.ctx
.trust_line_keylet(account1, account2, currency, out),
)
}
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
bytes_written(self.ctx.sha512_half(data, out))
}

View File

@@ -305,6 +305,15 @@ mod tests {
fn escrow_keylet(&self, _account: &[u8], _seq: i32, _out: &mut [u8]) -> HostResult<usize> {
unreachable!("no unit test in this module calls the host")
}
fn trust_line_keylet(
&self,
_account1: &[u8],
_account2: &[u8],
_currency: &[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

@@ -494,6 +494,35 @@ pub(crate) fn register_host_functions(
})
},
),
HostFunctionSpec::TrustLineKeylet => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),
|mut caller: Caller<'_, VmState<'_>>,
a1_ptr: i32,
a1_len: i32,
a2_ptr: i32,
a2_len: i32,
cur_ptr: i32,
cur_len: i32,
out_ptr: i32,
out_len: i32|
-> Result<i32, wasmi::Error> {
charged(&mut caller, HostFunctionSpec::TrustLineKeylet, |c| {
let out = Region::new(out_ptr, out_len);
let account1 = Region::new(a1_ptr, a1_len);
let account2 = Region::new(a2_ptr, a2_len);
let currency = Region::new(cur_ptr, cur_len);
write_buffered(c, out, |host, data, buf| {
host.trust_line_keylet(
account1.read(data)?,
account2.read(data)?,
currency.read(data)?,
buf,
)
})
})
},
),
HostFunctionSpec::Sha512Half => linker.func_wrap(
HOST_MODULE,
op.wasm_name(),

View File

@@ -184,6 +184,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
"(call $escrow_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))",
5,
),
HostFunctionSpec::TrustLineKeylet => (
import::TRUSTLINE_ID,
"(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::Sha512Half => (
import::SHA512_HALF,
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",

View File

@@ -542,6 +542,35 @@ fn escrow_id_reads_the_account_and_seq_and_writes_the_keylet() {
assert_eq!(*host.escrow_keylets_asked.borrow(), vec![(account, 5)]);
}
/// A keylet getter reading three regions — two accounts and a currency: all three
/// reach the host as a triple, and the keylet lands where the guest asked.
#[test]
fn trustline_id_reads_two_accounts_and_a_currency() {
let account1 = vec![0u8; 20];
let account2 = vec![0u8; 20];
let currency = vec![0u8; 20];
let host = FakeHost::new().answering_trust_line_keylet(
account1.clone(),
account2.clone(),
currency.clone(),
support::Answer::filler(32),
);
let wat = module(
&[import::TRUSTLINE_ID, ONE_PAGE],
"(call $trustline_id
(i32.const 0) (i32.const 20)
(i32.const 20) (i32.const 20)
(i32.const 40) (i32.const 20)
(i32.const 64) (i32.const 64))",
);
assert_eq!(status(&wat, &host), 32, "the 32-byte keylet length");
assert_eq!(
*host.trust_line_keylets_asked.borrow(),
vec![(account1, account2, currency)]
);
}
/// 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; 30] = [
const ALL_IMPORTS: [&str; 31] = [
import::LDGR_INDEX,
import::PARENT_LDGR_TIME,
import::PARENT_LDGR_HASH,
@@ -126,6 +126,7 @@ const ALL_IMPORTS: [&str; 30] = [
import::DEPOSIT_PREAUTH_ID,
import::DID_ID,
import::ESCROW_ID,
import::TRUSTLINE_ID,
import::SHA512_HALF,
import::TRACE,
import::TRACE_NUM,

View File

@@ -219,6 +219,11 @@ pub struct FakeHost {
pub escrow_keylets: HashMap<(Vec<u8>, i32), Answer>,
/// Every (account, seq) `escrow_keylet` was asked for.
pub escrow_keylets_asked: RefCell<Vec<(Vec<u8>, i32)>>,
/// What `trust_line_keylet` answers, by (account1, account2, currency) bytes. An
/// unlisted key answers `InvalidAccount`.
pub trust_line_keylets: HashMap<(Vec<u8>, Vec<u8>, Vec<u8>), Answer>,
/// Every (account1, account2, currency) `trust_line_keylet` was asked for.
pub trust_line_keylets_asked: RefCell<Vec<(Vec<u8>, 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.
@@ -289,6 +294,8 @@ impl Default for FakeHost {
did_keylets_asked: RefCell::new(Vec::new()),
escrow_keylets: HashMap::new(),
escrow_keylets_asked: RefCell::new(Vec::new()),
trust_line_keylets: HashMap::new(),
trust_line_keylets_asked: RefCell::new(Vec::new()),
digest: Answer::filler(32),
fields_asked: RefCell::new(Vec::new()),
digested: RefCell::new(Vec::new()),
@@ -480,6 +487,18 @@ impl FakeHost {
self
}
pub fn answering_trust_line_keylet(
mut self,
account1: Vec<u8>,
account2: Vec<u8>,
currency: Vec<u8>,
answer: Answer,
) -> FakeHost {
self.trust_line_keylets
.insert((account1, account2, currency), answer);
self
}
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
self.digest = answer;
self
@@ -738,6 +757,21 @@ impl HostFunctions for FakeHost {
}
}
fn trust_line_keylet(
&self,
account1: &[u8],
account2: &[u8],
currency: &[u8],
out: &mut [u8],
) -> HostResult<usize> {
let key = (account1.to_vec(), account2.to_vec(), currency.to_vec());
self.trust_line_keylets_asked.borrow_mut().push(key.clone());
match self.trust_line_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)
@@ -806,6 +840,7 @@ pub mod import {
pub const DID_ID: &str =
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 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

@@ -177,6 +177,15 @@ public:
std::int32_t seq,
rust::Slice<std::uint8_t> out) const noexcept;
// Both accounts and the currency must each be 20 bytes, else `InvalidParams`.
// Writes the 32-byte keylet.
[[nodiscard]] std::int32_t
trustLineKeylet(
rust::Slice<std::uint8_t const> account1,
rust::Slice<std::uint8_t const> account2,
rust::Slice<std::uint8_t const> currency,
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

@@ -601,6 +601,29 @@ HostContext::escrowKeylet(
});
}
std::int32_t
HostContext::trustLineKeylet(
rust::Slice<std::uint8_t const> account1,
rust::Slice<std::uint8_t const> account2,
rust::Slice<std::uint8_t const> currency,
rust::Slice<std::uint8_t> out) const noexcept
{
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
if (account1.size() != AccountID::size() || account2.size() != AccountID::size() ||
currency.size() != Currency::size())
return hfErrorToInt(HostFunctionError::InvalidParams);
auto const value = hostFunctions_.trustLineKeylet(
AccountID::fromVoid(account1.data()),
AccountID::fromVoid(account2.data()),
Currency::fromVoid(currency.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