mirror of
https://github.com/XRPLF/rippled.git
synced 2026-08-21 14:20:56 +00:00
feat: Hook up paychan_id host function
This commit is contained in:
@@ -360,6 +360,20 @@ host_functions! {
|
||||
#[wasm_name = "oracle_id"]
|
||||
fn oracle_keylet(&self, account: &[u8], doc_id: i32, out: &mut [u8]) -> HostResult<usize>;
|
||||
|
||||
/// The 32-byte keylet of a `PayChannel`, computed from the 20-byte source account,
|
||||
/// the 20-byte destination account, and the channel's sequence number. `seq` is the
|
||||
/// guest's `u32` carried as its `i32` bit pattern. Reads both account regions and
|
||||
/// writes the keylet.
|
||||
#[gas = 350]
|
||||
#[wasm_name = "paychan_id"]
|
||||
fn paychannel_keylet(
|
||||
&self,
|
||||
account: &[u8],
|
||||
destination: &[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"]
|
||||
|
||||
@@ -332,6 +332,21 @@ impl HostFunctions for FakeHost {
|
||||
put(out, &[account[0]; HASH_LEN])
|
||||
}
|
||||
|
||||
/// A two-account-and-sequence shape, for a `PayChannel`; `InvalidAccount` if
|
||||
/// either account is empty.
|
||||
fn paychannel_keylet(
|
||||
&self,
|
||||
account: &[u8],
|
||||
destination: &[u8],
|
||||
_seq: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
if account.is_empty() || destination.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;
|
||||
@@ -523,6 +538,15 @@ fn the_trait_is_implementable() {
|
||||
host.oracle_keylet(&[], 5, &mut out),
|
||||
Err(HostError::InvalidAccount)
|
||||
);
|
||||
assert_eq!(
|
||||
host.paychannel_keylet(&[7; 20], &[8; 20], 5, &mut out),
|
||||
Ok(HASH_LEN)
|
||||
);
|
||||
assert_eq!(out[0], 7);
|
||||
assert_eq!(
|
||||
host.paychannel_keylet(&[7; 20], &[], 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(()));
|
||||
@@ -623,6 +647,7 @@ fn the_spec_table_matches_the_declarations() {
|
||||
("nft_offer_id", 350),
|
||||
("offer_id", 350),
|
||||
("oracle_id", 350),
|
||||
("paychan_id", 350),
|
||||
("sha512_half", 2000),
|
||||
("trace", 500),
|
||||
("trace_num", 500),
|
||||
|
||||
@@ -348,6 +348,16 @@ mod ffi {
|
||||
#[cxx_name = "oracleKeylet"]
|
||||
fn oracle_keylet(self: &HostContext, account: &[u8], doc_id: i32, out: &mut [u8]) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "paychannelKeylet"]
|
||||
fn paychannel_keylet(
|
||||
self: &HostContext,
|
||||
account: &[u8],
|
||||
destination: &[u8],
|
||||
seq: i32,
|
||||
out: &mut [u8],
|
||||
) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "sha512Half"]
|
||||
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
|
||||
@@ -584,6 +594,16 @@ impl HostFunctions for CxxHost<'_> {
|
||||
bytes_written(self.ctx.oracle_keylet(account, doc_id, out))
|
||||
}
|
||||
|
||||
fn paychannel_keylet(
|
||||
&self,
|
||||
account: &[u8],
|
||||
destination: &[u8],
|
||||
seq: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
bytes_written(self.ctx.paychannel_keylet(account, destination, seq, out))
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
bytes_written(self.ctx.sha512_half(data, out))
|
||||
}
|
||||
|
||||
@@ -349,6 +349,15 @@ mod tests {
|
||||
) -> HostResult<usize> {
|
||||
unreachable!("no unit test in this module calls the host")
|
||||
}
|
||||
fn paychannel_keylet(
|
||||
&self,
|
||||
_account: &[u8],
|
||||
_destination: &[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")
|
||||
}
|
||||
|
||||
@@ -620,6 +620,33 @@ pub(crate) fn register_host_functions(
|
||||
})
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::PaychannelKeylet => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|mut caller: Caller<'_, VmState<'_>>,
|
||||
acc_ptr: i32,
|
||||
acc_len: i32,
|
||||
dst_ptr: i32,
|
||||
dst_len: i32,
|
||||
seq: i32,
|
||||
out_ptr: i32,
|
||||
out_len: i32|
|
||||
-> Result<i32, wasmi::Error> {
|
||||
charged(&mut caller, HostFunctionSpec::PaychannelKeylet, |c| {
|
||||
let out = Region::new(out_ptr, out_len);
|
||||
let account = Region::new(acc_ptr, acc_len);
|
||||
let destination = Region::new(dst_ptr, dst_len);
|
||||
write_buffered(c, out, |host, data, buf| {
|
||||
host.paychannel_keylet(
|
||||
account.read(data)?,
|
||||
destination.read(data)?,
|
||||
seq,
|
||||
buf,
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|
||||
@@ -214,6 +214,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
|
||||
"(call $oracle_id (i32.const 0) (i32.const 20) (i32.const 5) (i32.const 32) (i32.const 32))",
|
||||
5,
|
||||
),
|
||||
HostFunctionSpec::PaychannelKeylet => (
|
||||
import::PAYCHAN_ID,
|
||||
"(call $paychan_id (i32.const 0) (i32.const 20) (i32.const 20) (i32.const 20) (i32.const 5) (i32.const 40) (i32.const 20))",
|
||||
7,
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => (
|
||||
import::SHA512_HALF,
|
||||
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",
|
||||
|
||||
@@ -659,6 +659,30 @@ fn oracle_id_reads_the_account_and_doc_id() {
|
||||
assert_eq!(*host.oracle_keylets_asked.borrow(), vec![(account, 5)]);
|
||||
}
|
||||
|
||||
/// A keylet that reads two account regions and a scalar: both accounts and the
|
||||
/// sequence reach the host, keyed together, and the answered bytes land where asked.
|
||||
#[test]
|
||||
fn paychan_id_reads_both_accounts_and_the_seq() {
|
||||
let account = vec![0u8; 20];
|
||||
let destination = vec![0u8; 20];
|
||||
let host = FakeHost::new().answering_paychannel_keylet(
|
||||
account.clone(),
|
||||
destination.clone(),
|
||||
5,
|
||||
support::Answer::filler(32),
|
||||
);
|
||||
|
||||
let wat = module(
|
||||
&[import::PAYCHAN_ID, ONE_PAGE],
|
||||
"(call $paychan_id (i32.const 0) (i32.const 20) (i32.const 32) (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.paychannel_keylets_asked.borrow(),
|
||||
vec![(account, destination, 5)]
|
||||
);
|
||||
}
|
||||
|
||||
/// A leading scalar parameter reaches the host as declared.
|
||||
#[test]
|
||||
fn home_le_field_passes_the_field_selector_through() {
|
||||
|
||||
@@ -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; 36] = [
|
||||
const ALL_IMPORTS: [&str; 37] = [
|
||||
import::LDGR_INDEX,
|
||||
import::PARENT_LDGR_TIME,
|
||||
import::PARENT_LDGR_HASH,
|
||||
@@ -132,6 +132,7 @@ const ALL_IMPORTS: [&str; 36] = [
|
||||
import::NFT_OFFER_ID,
|
||||
import::OFFER_ID,
|
||||
import::ORACLE_ID,
|
||||
import::PAYCHAN_ID,
|
||||
import::SHA512_HALF,
|
||||
import::TRACE,
|
||||
import::TRACE_NUM,
|
||||
|
||||
@@ -249,6 +249,11 @@ pub struct FakeHost {
|
||||
pub oracle_keylets: HashMap<(Vec<u8>, i32), Answer>,
|
||||
/// Every (account, doc id) `oracle_keylet` was asked for.
|
||||
pub oracle_keylets_asked: RefCell<Vec<(Vec<u8>, i32)>>,
|
||||
/// What `paychannel_keylet` answers, by (account, destination, seq). An unlisted
|
||||
/// key answers `InvalidAccount`.
|
||||
pub paychannel_keylets: HashMap<(Vec<u8>, Vec<u8>, i32), Answer>,
|
||||
/// Every (account, destination, seq) `paychannel_keylet` was asked for.
|
||||
pub paychannel_keylets_asked: RefCell<Vec<(Vec<u8>, 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.
|
||||
@@ -331,6 +336,8 @@ impl Default for FakeHost {
|
||||
offer_keylets_asked: RefCell::new(Vec::new()),
|
||||
oracle_keylets: HashMap::new(),
|
||||
oracle_keylets_asked: RefCell::new(Vec::new()),
|
||||
paychannel_keylets: HashMap::new(),
|
||||
paychannel_keylets_asked: RefCell::new(Vec::new()),
|
||||
digest: Answer::filler(32),
|
||||
fields_asked: RefCell::new(Vec::new()),
|
||||
digested: RefCell::new(Vec::new()),
|
||||
@@ -584,6 +591,18 @@ impl FakeHost {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_paychannel_keylet(
|
||||
mut self,
|
||||
account: Vec<u8>,
|
||||
destination: Vec<u8>,
|
||||
seq: i32,
|
||||
answer: Answer,
|
||||
) -> FakeHost {
|
||||
self.paychannel_keylets
|
||||
.insert((account, destination, seq), answer);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
|
||||
self.digest = answer;
|
||||
self
|
||||
@@ -909,6 +928,21 @@ impl HostFunctions for FakeHost {
|
||||
}
|
||||
}
|
||||
|
||||
fn paychannel_keylet(
|
||||
&self,
|
||||
account: &[u8],
|
||||
destination: &[u8],
|
||||
seq: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
let key = (account.to_vec(), destination.to_vec(), seq);
|
||||
self.paychannel_keylets_asked.borrow_mut().push(key.clone());
|
||||
match self.paychannel_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)
|
||||
@@ -983,6 +1017,7 @@ pub mod import {
|
||||
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 OFFER_ID: &str = r#"(import "host_lib" "offer_id" (func $offer_id (param i32 i32 i32 i32 i32) (result i32)))"#;
|
||||
pub const ORACLE_ID: &str = r#"(import "host_lib" "oracle_id" (func $oracle_id (param i32 i32 i32 i32 i32) (result i32)))"#;
|
||||
pub const PAYCHAN_ID: &str = r#"(import "host_lib" "paychan_id" (func $paychan_id (param 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)))"#;
|
||||
|
||||
@@ -226,6 +226,15 @@ public:
|
||||
std::int32_t docId,
|
||||
rust::Slice<std::uint8_t> out) const noexcept;
|
||||
|
||||
// Both account ids 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
|
||||
paychannelKeylet(
|
||||
rust::Slice<std::uint8_t const> account,
|
||||
rust::Slice<std::uint8_t const> destination,
|
||||
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;
|
||||
|
||||
|
||||
@@ -723,6 +723,29 @@ HostContext::oracleKeylet(
|
||||
});
|
||||
}
|
||||
|
||||
std::int32_t
|
||||
HostContext::paychannelKeylet(
|
||||
rust::Slice<std::uint8_t const> account,
|
||||
rust::Slice<std::uint8_t const> destination,
|
||||
std::int32_t seq,
|
||||
rust::Slice<std::uint8_t> out) const noexcept
|
||||
{
|
||||
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
|
||||
if (account.size() != AccountID::size() || destination.size() != AccountID::size())
|
||||
return hfErrorToInt(HostFunctionError::InvalidParams);
|
||||
|
||||
// The guest's u32 seq arrives as its i32 bit pattern; recover it.
|
||||
auto const value = hostFunctions_.paychannelKeylet(
|
||||
AccountID::fromVoid(account.data()),
|
||||
AccountID::fromVoid(destination.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
|
||||
|
||||
Reference in New Issue
Block a user