mirror of
https://github.com/XRPLF/rippled.git
synced 2026-08-21 22:30:57 +00:00
feat: Hook up le_inner host function
This commit is contained in:
@@ -169,6 +169,17 @@ host_functions! {
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize>;
|
||||
|
||||
/// The serialized bytes of a nested field of a previously cached ledger object,
|
||||
/// selected by its cache slot and reached by a `locator`.
|
||||
#[gas = 110]
|
||||
#[wasm_name = "le_inner"]
|
||||
fn get_ledger_obj_nested_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
locator: &[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"]
|
||||
|
||||
@@ -102,6 +102,19 @@ impl HostFunctions for FakeHost {
|
||||
put(out, &[locator.len() as u8, locator[0]])
|
||||
}
|
||||
|
||||
/// The same, over a cached object keyed by slot.
|
||||
fn get_ledger_obj_nested_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
locator: &[u8],
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
if cache_idx <= 0 || locator.is_empty() {
|
||||
return Err(HostError::LocatorMalformed);
|
||||
}
|
||||
put(out, &[cache_idx as u8, locator[0]])
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
let mut digest = [0; HASH_LEN];
|
||||
digest[0] = data.len() as u8;
|
||||
@@ -151,6 +164,11 @@ fn the_trait_is_implementable() {
|
||||
Ok(2)
|
||||
);
|
||||
assert_eq!(out[..2], [4, 9]);
|
||||
assert_eq!(
|
||||
host.get_ledger_obj_nested_field(3, &[9, 0, 0, 0], &mut out),
|
||||
Ok(2)
|
||||
);
|
||||
assert_eq!(out[..2], [3, 9]);
|
||||
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(()));
|
||||
@@ -229,6 +247,7 @@ fn the_spec_table_matches_the_declarations() {
|
||||
("le_field", 70),
|
||||
("tx_inner", 110),
|
||||
("home_le_inner", 110),
|
||||
("le_inner", 110),
|
||||
("sha512_half", 2000),
|
||||
("trace", 500),
|
||||
("trace_num", 500),
|
||||
|
||||
@@ -212,6 +212,15 @@ mod ffi {
|
||||
out: &mut [u8],
|
||||
) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "getLedgerObjNestedField"]
|
||||
fn get_ledger_obj_nested_field(
|
||||
self: &HostContext,
|
||||
cache_idx: i32,
|
||||
locator: &[u8],
|
||||
out: &mut [u8],
|
||||
) -> i32;
|
||||
|
||||
#[namespace = "xrpl"]
|
||||
#[cxx_name = "sha512Half"]
|
||||
fn sha512_half(self: &HostContext, data: &[u8], out: &mut [u8]) -> i32;
|
||||
@@ -319,6 +328,18 @@ impl HostFunctions for CxxHost<'_> {
|
||||
bytes_written(self.ctx.get_current_ledger_obj_nested_field(locator, out))
|
||||
}
|
||||
|
||||
fn get_ledger_obj_nested_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
locator: &[u8],
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
bytes_written(
|
||||
self.ctx
|
||||
.get_ledger_obj_nested_field(cache_idx, locator, out),
|
||||
)
|
||||
}
|
||||
|
||||
fn sha512_half(&self, data: &[u8], out: &mut [u8]) -> HostResult<usize> {
|
||||
bytes_written(self.ctx.sha512_half(data, out))
|
||||
}
|
||||
|
||||
@@ -227,6 +227,14 @@ mod tests {
|
||||
) -> HostResult<usize> {
|
||||
unreachable!("no unit test in this module calls the host")
|
||||
}
|
||||
fn get_ledger_obj_nested_field(
|
||||
&self,
|
||||
_cache_idx: i32,
|
||||
_locator: &[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")
|
||||
}
|
||||
|
||||
@@ -194,6 +194,33 @@ pub(crate) fn register_host_functions(
|
||||
)
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::GetLedgerObjNestedField => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|mut caller: Caller<'_, VmState<'_>>,
|
||||
cache_idx: i32,
|
||||
loc_ptr: i32,
|
||||
loc_len: i32,
|
||||
out_ptr: i32,
|
||||
out_len: i32|
|
||||
-> Result<i32, wasmi::Error> {
|
||||
charged(
|
||||
&mut caller,
|
||||
HostFunctionSpec::GetLedgerObjNestedField,
|
||||
|c| {
|
||||
let out = Region::new(out_ptr, out_len);
|
||||
let locator = Region::new(loc_ptr, loc_len);
|
||||
write_buffered(c, out, |host, data, buf| {
|
||||
host.get_ledger_obj_nested_field(
|
||||
cache_idx,
|
||||
locator.read(data)?,
|
||||
buf,
|
||||
)
|
||||
})
|
||||
},
|
||||
)
|
||||
},
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => linker.func_wrap(
|
||||
HOST_MODULE,
|
||||
op.wasm_name(),
|
||||
|
||||
@@ -106,6 +106,11 @@ fn call_for(op: HostFunctionSpec) -> Call {
|
||||
"(call $home_le_inner (i32.const 0) (i32.const 4) (i32.const 8) (i32.const 4))",
|
||||
4,
|
||||
),
|
||||
HostFunctionSpec::GetLedgerObjNestedField => (
|
||||
import::LE_INNER,
|
||||
"(call $le_inner (i32.const 1) (i32.const 0) (i32.const 4) (i32.const 8) (i32.const 4))",
|
||||
5,
|
||||
),
|
||||
HostFunctionSpec::Sha512Half => (
|
||||
import::SHA512_HALF,
|
||||
"(call $sha512_half (i32.const 0) (i32.const 4) (i32.const 0) (i32.const 32))",
|
||||
|
||||
@@ -223,6 +223,26 @@ fn home_le_inner_reads_the_locator_and_writes_the_field() {
|
||||
assert_eq!(*host.home_le_nested_asked.borrow(), vec![locator]);
|
||||
}
|
||||
|
||||
/// The nested getter over a cached object: the slot leads, the locator is read from
|
||||
/// memory, and the two reach the host keyed together.
|
||||
#[test]
|
||||
fn le_inner_reads_the_slot_and_locator_and_writes_the_field() {
|
||||
let locator = vec![5u8, 0, 0, 0];
|
||||
let host = FakeHost::new().answering_le_nested(
|
||||
3,
|
||||
locator.clone(),
|
||||
support::Answer::bytes([0x11, 0x22]),
|
||||
);
|
||||
|
||||
let wat = module(
|
||||
&[import::LE_INNER, ONE_PAGE],
|
||||
"(i32.store (i32.const 0) (i32.const 5))
|
||||
(call $le_inner (i32.const 3) (i32.const 0) (i32.const 4) (i32.const 64) (i32.const 64))",
|
||||
);
|
||||
assert_eq!(status(&wat, &host), 2, "the field bytes the host wrote");
|
||||
assert_eq!(*host.le_nested_asked.borrow(), vec![(3, locator)]);
|
||||
}
|
||||
|
||||
/// 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; 14] = [
|
||||
const ALL_IMPORTS: [&str; 15] = [
|
||||
import::LDGR_INDEX,
|
||||
import::PARENT_LDGR_TIME,
|
||||
import::PARENT_LDGR_HASH,
|
||||
@@ -110,6 +110,7 @@ const ALL_IMPORTS: [&str; 14] = [
|
||||
import::LE_FIELD,
|
||||
import::TX_INNER,
|
||||
import::HOME_LE_INNER,
|
||||
import::LE_INNER,
|
||||
import::SHA512_HALF,
|
||||
import::TRACE,
|
||||
import::TRACE_NUM,
|
||||
|
||||
@@ -140,6 +140,11 @@ pub struct FakeHost {
|
||||
pub home_le_nested: HashMap<Vec<u8>, Answer>,
|
||||
/// Every locator `get_current_ledger_obj_nested_field` was asked for.
|
||||
pub home_le_nested_asked: RefCell<Vec<Vec<u8>>>,
|
||||
/// What `get_ledger_obj_nested_field` answers, by (cache slot, locator bytes). An
|
||||
/// unlisted key answers `FieldNotFound`.
|
||||
pub le_nested: HashMap<(i32, Vec<u8>), Answer>,
|
||||
/// Every (cache slot, locator) `get_ledger_obj_nested_field` was asked for.
|
||||
pub le_nested_asked: RefCell<Vec<(i32, Vec<u8>)>>,
|
||||
/// What `sha512_half` answers, whatever it is given.
|
||||
pub digest: Answer,
|
||||
/// Every field selector `get_current_ledger_obj_field` was asked for.
|
||||
@@ -177,6 +182,8 @@ impl Default for FakeHost {
|
||||
tx_nested_asked: RefCell::new(Vec::new()),
|
||||
home_le_nested: HashMap::new(),
|
||||
home_le_nested_asked: RefCell::new(Vec::new()),
|
||||
le_nested: HashMap::new(),
|
||||
le_nested_asked: RefCell::new(Vec::new()),
|
||||
digest: Answer::filler(32),
|
||||
fields_asked: RefCell::new(Vec::new()),
|
||||
digested: RefCell::new(Vec::new()),
|
||||
@@ -245,6 +252,16 @@ impl FakeHost {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_le_nested(
|
||||
mut self,
|
||||
cache_idx: i32,
|
||||
locator: Vec<u8>,
|
||||
answer: Answer,
|
||||
) -> FakeHost {
|
||||
self.le_nested.insert((cache_idx, locator), answer);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn answering_digest(mut self, answer: Answer) -> FakeHost {
|
||||
self.digest = answer;
|
||||
self
|
||||
@@ -333,6 +350,21 @@ impl HostFunctions for FakeHost {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_ledger_obj_nested_field(
|
||||
&self,
|
||||
cache_idx: i32,
|
||||
locator: &[u8],
|
||||
out: &mut [u8],
|
||||
) -> HostResult<usize> {
|
||||
self.le_nested_asked
|
||||
.borrow_mut()
|
||||
.push((cache_idx, locator.to_vec()));
|
||||
match self.le_nested.get(&(cache_idx, locator.to_vec())) {
|
||||
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)
|
||||
@@ -381,6 +413,7 @@ pub mod import {
|
||||
pub const TX_INNER: &str =
|
||||
r#"(import "host_lib" "tx_inner" (func $tx_inner (param i32 i32 i32 i32) (result i32)))"#;
|
||||
pub const HOME_LE_INNER: &str = r#"(import "host_lib" "home_le_inner" (func $home_le_inner (param i32 i32 i32 i32) (result i32)))"#;
|
||||
pub const LE_INNER: &str = r#"(import "host_lib" "le_inner" (func $le_inner (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)))"#;
|
||||
|
||||
@@ -86,6 +86,12 @@ public:
|
||||
rust::Slice<std::uint8_t const> locator,
|
||||
rust::Slice<std::uint8_t> out) const noexcept;
|
||||
|
||||
[[nodiscard]] std::int32_t
|
||||
getLedgerObjNestedField(
|
||||
std::int32_t cacheIdx,
|
||||
rust::Slice<std::uint8_t const> locator,
|
||||
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;
|
||||
|
||||
|
||||
@@ -256,6 +256,29 @@ HostContext::getCurrentLedgerObjNestedField(
|
||||
});
|
||||
}
|
||||
|
||||
std::int32_t
|
||||
HostContext::getLedgerObjNestedField(
|
||||
std::int32_t cacheIdx,
|
||||
rust::Slice<std::uint8_t const> locator,
|
||||
rust::Slice<std::uint8_t> out) const noexcept
|
||||
{
|
||||
return guarded(hostFunctions_.getJournal(), kHostInternal, [&] {
|
||||
if (locator.empty() || (locator.size() & 3) != 0)
|
||||
return hfErrorToInt(HostFunctionError::LocatorMalformed);
|
||||
|
||||
std::uint32_t const steps = locator.size() / sizeof(std::int32_t);
|
||||
std::vector<std::int32_t> locBuf(steps);
|
||||
std::memcpy(locBuf.data(), locator.data(), locator.size());
|
||||
FieldLocator const fl(std::move(locBuf));
|
||||
|
||||
auto const value = hostFunctions_.getLedgerObjNestedField(cacheIdx, fl);
|
||||
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