mirror of
https://github.com/XRPLF/rippled.git
synced 2026-08-21 14:20:56 +00:00
feat: Hook up le_field host function
This commit is contained in:
@@ -146,6 +146,12 @@ host_functions! {
|
||||
#[wasm_name = "home_le_field"]
|
||||
fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult<usize>;
|
||||
|
||||
/// The serialized bytes of one field of a previously cached ledger object,
|
||||
/// selected by its cache slot and the field's `SField` code.
|
||||
#[gas = 70]
|
||||
#[wasm_name = "le_field"]
|
||||
fn get_ledger_obj_field(&self, cache_idx: i32, field: 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"]
|
||||
|
||||
@@ -69,6 +69,19 @@ impl HostFunctions for FakeHost {
|
||||
put(out, &[field as u8])
|
||||
}
|
||||
|
||||
/// A field getter over a cached object, keyed by slot and selector.
|
||||
fn get_ledger_obj_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
field: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
if cache_idx <= 0 || field < 0 {
|
||||
return Err(HostError::FieldNotFound);
|
||||
}
|
||||
put(out, &[cache_idx as u8, field as u8])
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
let mut digest = [0; HASH_LEN];
|
||||
digest[0] = data.len() as u8;
|
||||
@@ -109,6 +122,8 @@ fn the_trait_is_implementable() {
|
||||
assert_eq!(out[0], 5);
|
||||
assert_eq!(host.get_current_ledger_obj_field(3, &mut out), Ok(1));
|
||||
assert_eq!(out[0], 3);
|
||||
assert_eq!(host.get_ledger_obj_field(2, 4, &mut out), Ok(2));
|
||||
assert_eq!(out[..2], [2, 4]);
|
||||
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(()));
|
||||
@@ -184,6 +199,7 @@ fn the_spec_table_matches_the_declarations() {
|
||||
("cache_le", 5000),
|
||||
("tx_field", 70),
|
||||
("home_le_field", 70),
|
||||
("le_field", 70),
|
||||
("sha512_half", 2000),
|
||||
("trace", 500),
|
||||
("trace_num", 500),
|
||||
|
||||
@@ -191,6 +191,15 @@ mod ffi {
|
||||
#[cxx_name = "getCurrentLedgerObjField"]
|
||||
fn get_current_ledger_obj_field(self: &HostContext, field: i32, out: &mut [u8]) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "getLedgerObjField"]
|
||||
fn get_ledger_obj_field(
|
||||
self: &HostContext,
|
||||
cache_idx: i32,
|
||||
field: i32,
|
||||
out: &mut [u8],
|
||||
) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "sha512Half"]
|
||||
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
|
||||
@@ -277,6 +286,15 @@ impl HostFunctions for CxxHost<'_> {
|
||||
bytes_written(self.ctx.get_current_ledger_obj_field(field, out))
|
||||
}
|
||||
|
||||
fn get_ledger_obj_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
field: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
bytes_written(self.ctx.get_ledger_obj_field(cache_idx, field, out))
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
bytes_written(self.ctx.sha512_half(data, out))
|
||||
}
|
||||
|
||||
@@ -209,6 +209,14 @@ mod tests {
|
||||
fn get_current_ledger_obj_field(&self, _field: i32, _out: &mut [u8]) -> HostResult<usize> {
|
||||
unreachable!("no unit test in this module calls the host")
|
||||
}
|
||||
fn get_ledger_obj_field(
|
||||
&self,
|
||||
_cache_idx: i32,
|
||||
_field: 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")
|
||||
}
|
||||
|
||||
@@ -137,6 +137,23 @@ pub(crate) fn register_host_functions(
|
||||
)
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::GetLedgerObjField => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|mut caller: Caller<'_, VmState<'_>>,
|
||||
cache_idx: i32,
|
||||
field: i32,
|
||||
out_ptr: i32,
|
||||
out_len: i32|
|
||||
-> Result<i32, wasmi::Error> {
|
||||
charged(&mut caller, HostFunctionSpec::GetLedgerObjField, |c| {
|
||||
let out = Region::new(out_ptr, out_len);
|
||||
write_into(c, out, |host, out| {
|
||||
host.get_ledger_obj_field(cache_idx, field, out)
|
||||
})
|
||||
})
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|
||||
@@ -91,6 +91,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
|
||||
"(call $home_le_field (i32.const 1) (i32.const 0) (i32.const 4))",
|
||||
3,
|
||||
),
|
||||
HostFunctionSpec::GetLedgerObjField => (
|
||||
import::LE_FIELD,
|
||||
"(call $le_field (i32.const 1) (i32.const 1) (i32.const 0) (i32.const 4))",
|
||||
4,
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => (
|
||||
import::SHA512_HALF,
|
||||
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",
|
||||
|
||||
@@ -171,6 +171,21 @@ fn tx_field_passes_the_selector_and_writes_the_field() {
|
||||
assert_eq!(*host.tx_fields_asked.borrow(), vec![17]);
|
||||
}
|
||||
|
||||
/// A field getter over a cached object: both the slot and the selector reach the
|
||||
/// host, keyed together, and the answered bytes land where the guest asked.
|
||||
#[test]
|
||||
fn le_field_passes_the_slot_and_selector_through() {
|
||||
let host =
|
||||
FakeHost::new().answering_le_field(2, 17, support::Answer::bytes([0xab, 0xcd, 0xef]));
|
||||
|
||||
let wat = module(
|
||||
&[import::LE_FIELD, ONE_PAGE],
|
||||
"(call $le_field (i32.const 2) (i32.const 17) (i32.const 0) (i32.const 64))",
|
||||
);
|
||||
assert_eq!(status(&wat, &host), 3);
|
||||
assert_eq!(*host.le_fields_asked.borrow(), vec![(2, 17)]);
|
||||
}
|
||||
|
||||
/// 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; 11] = [
|
||||
const ALL_IMPORTS: [&str; 12] = [
|
||||
import::LDGR_INDEX,
|
||||
import::PARENT_LDGR_TIME,
|
||||
import::PARENT_LDGR_HASH,
|
||||
@@ -107,6 +107,7 @@ const ALL_IMPORTS: [&str; 11] = [
|
||||
import::CACHE_LE,
|
||||
import::TX_FIELD,
|
||||
import::HOME_LE_FIELD,
|
||||
import::LE_FIELD,
|
||||
import::SHA512_HALF,
|
||||
import::TRACE,
|
||||
import::TRACE_NUM,
|
||||
|
||||
@@ -125,6 +125,11 @@ pub struct FakeHost {
|
||||
/// What `get_current_ledger_obj_field` answers, by field selector. An
|
||||
/// unlisted selector answers `FieldNotFound`.
|
||||
pub fields: HashMap<i32, Answer>,
|
||||
/// What `get_ledger_obj_field` answers, by (cache slot, field selector). An
|
||||
/// unlisted key answers `FieldNotFound`.
|
||||
pub le_fields: HashMap<(i32, i32), Answer>,
|
||||
/// Every (cache slot, field selector) `get_ledger_obj_field` was asked for.
|
||||
pub le_fields_asked: RefCell<Vec<(i32, i32)>>,
|
||||
/// What `sha512_half` answers, whatever it is given.
|
||||
pub digest: Answer,
|
||||
/// Every field selector `get_current_ledger_obj_field` was asked for.
|
||||
@@ -156,6 +161,8 @@ impl Default for FakeHost {
|
||||
tx_fields: HashMap::new(),
|
||||
tx_fields_asked: RefCell::new(Vec::new()),
|
||||
fields: HashMap::new(),
|
||||
le_fields: HashMap::new(),
|
||||
le_fields_asked: RefCell::new(Vec::new()),
|
||||
digest: Answer::filler(32),
|
||||
fields_asked: RefCell::new(Vec::new()),
|
||||
digested: RefCell::new(Vec::new()),
|
||||
@@ -209,6 +216,11 @@ impl FakeHost {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_le_field(mut self, cache_idx: i32, field: i32, answer: Answer) -> FakeHost {
|
||||
self.le_fields.insert((cache_idx, field), answer);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
|
||||
self.digest = answer;
|
||||
self
|
||||
@@ -262,6 +274,19 @@ impl HostFunctions for FakeHost {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_ledger_obj_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
field: i32,
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
self.le_fields_asked.borrow_mut().push((cache_idx, field));
|
||||
match self.le_fields.get(&(cache_idx, field)) {
|
||||
Some(answer) => answer.fill(out),
|
||||
None => Err(HostError::FieldNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
self.digested.borrow_mut().push(data.to_vec());
|
||||
self.digest.fill(out)
|
||||
@@ -305,6 +330,8 @@ pub mod import {
|
||||
pub const TX_FIELD: &str =
|
||||
r#"(import "host_lib" "tx_field" (func $tx_field (param i32 i32 i32) (result i32)))"#;
|
||||
pub const HOME_LE_FIELD: &str = r#"(import "host_lib" "home_le_field" (func $home_le_field (param i32 i32 i32) (result i32)))"#;
|
||||
pub const LE_FIELD: &str =
|
||||
r#"(import "host_lib" "le_field" (func $le_field (param 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)))"#;
|
||||
|
||||
@@ -71,6 +71,10 @@ public:
|
||||
[[nodiscard]] std::int32_t
|
||||
getCurrentLedgerObjField(std::int32_t field, rust::Slice<std::uint8_t> out) const noexcept;
|
||||
|
||||
[[nodiscard]] std::int32_t
|
||||
getLedgerObjField(std::int32_t cacheIdx, std::int32_t field, 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;
|
||||
|
||||
|
||||
@@ -186,6 +186,26 @@ HostContext::getCurrentLedgerObjField(std::int32_t field, rust::Slice<std::uint8
|
||||
});
|
||||
}
|
||||
|
||||
std::int32_t
|
||||
HostContext::getLedgerObjField(
|
||||
std::int32_t cacheIdx,
|
||||
std::int32_t field,
|
||||
rust::Slice<std::uint8_t> out) const noexcept
|
||||
{
|
||||
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
|
||||
auto const& knownSFields = SField::getKnownCodeToField();
|
||||
auto const it = knownSFields.find(field);
|
||||
if (it == knownSFields.end())
|
||||
return hfErrorToInt(HostFunctionError::InvalidField);
|
||||
|
||||
auto const value = hostFunctions_.getLedgerObjField(cacheIdx, *it->second);
|
||||
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