diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index c2082a9d0f..c2e5de4827 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -106,6 +106,11 @@ host_functions! { #[wasm_name = "ldgr_index"] fn get_ledger_sqn(&self, out: &mut [u8]) -> HostResult; + /// The close time of the parent (last-closed) ledger, as 4 little-endian bytes. + #[gas = 60] + #[wasm_name = "parent_ldgr_time"] + fn get_parent_ledger_time(&self, out: &mut [u8]) -> HostResult; + /// The serialized bytes of one field of the current (escrow) ledger object. #[gas = 70] #[wasm_name = "home_le_field"] diff --git a/crates/xrpl-host-functions/tests/generated_abi.rs b/crates/xrpl-host-functions/tests/generated_abi.rs index a760a2b3af..16e8336f39 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -31,6 +31,10 @@ impl HostFunctions for FakeHost { put(out, &7u32.to_le_bytes()) } + fn get_parent_ledger_time(&self, out: &mut [u8]) -> HostResult { + put(out, &9u32.to_le_bytes()) + } + /// Fails on a field it doesn't know, so the error channel is exercised too. fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult { if field < 0 { @@ -65,6 +69,8 @@ fn the_trait_is_implementable() { assert_eq!(host.get_ledger_sqn(&mut out), Ok(4)); assert_eq!(out[..4], [7, 0, 0, 0]); + assert_eq!(host.get_parent_ledger_time(&mut out), Ok(4)); + assert_eq!(out[..4], [9, 0, 0, 0]); assert_eq!(host.get_current_ledger_obj_field(3, &mut out), Ok(1)); assert_eq!(out[0], 3); assert_eq!(host.sha512_half(b"abc", &mut out), Ok(HASH_LEN)); @@ -135,6 +141,7 @@ fn the_spec_table_matches_the_declarations() { table, [ ("ldgr_index", 60), + ("parent_ldgr_time", 60), ("home_le_field", 70), ("sha512_half", 2000), ("trace", 500), diff --git a/crates/xrpl-wasm-vm-ffi/src/lib.rs b/crates/xrpl-wasm-vm-ffi/src/lib.rs index 5a4c77048e..9a68c16c9c 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -159,6 +159,10 @@ mod ffi { #[cxx_name = "getLedgerSqn"] fn get_ledger_sqn(self: &HostContext, out: &mut [u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getParentLedgerTime"] + fn get_parent_ledger_time(self: &HostContext, out: &mut [u8]) -> i32; + #[namespace = "xrpl"] #[cxx_name = "getCurrentLedgerObjField"] fn get_current_ledger_obj_field(self: &HostContext, field: i32, out: &mut [u8]) -> i32; @@ -212,6 +216,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.get_ledger_sqn(out)) } + fn get_parent_ledger_time(&self, out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.get_parent_ledger_time(out)) + } + fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult { bytes_written(self.ctx.get_current_ledger_obj_field(field, out)) } diff --git a/crates/xrpl-wasm-vm/src/abi.rs b/crates/xrpl-wasm-vm/src/abi.rs index db6db25fda..b6c828598a 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -188,6 +188,9 @@ mod tests { fn get_ledger_sqn(&self, _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_parent_ledger_time(&self, _out: &mut [u8]) -> HostResult { + unreachable!("no unit test in this module calls the host") + } fn get_current_ledger_obj_field(&self, _field: i32, _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } diff --git a/crates/xrpl-wasm-vm/src/register.rs b/crates/xrpl-wasm-vm/src/register.rs index ef933f1bf9..28a7c5830d 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -35,6 +35,19 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::GetParentLedgerTime => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::GetParentLedgerTime, |c| { + let out = Region::new(out_ptr, out_len); + write_into(c, out, |host, out| host.get_parent_ledger_time(out)) + }) + }, + ), HostFunctionSpec::GetCurrentLedgerObjField => linker.func_wrap( HOST_MODULE, op.wasm_name(), diff --git a/crates/xrpl-wasm-vm/tests/budgets.rs b/crates/xrpl-wasm-vm/tests/budgets.rs index 9b7ac613ed..fabe7c86f6 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -56,6 +56,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $ldgr_index (i32.const 0) (i32.const 4))", 2, ), + HostFunctionSpec::GetParentLedgerTime => ( + import::PARENT_LDGR_TIME, + "(call $parent_ldgr_time (i32.const 0) (i32.const 4))", + 2, + ), HostFunctionSpec::GetCurrentLedgerObjField => ( import::HOME_LE_FIELD, "(call $home_le_field (i32.const 1) (i32.const 0) (i32.const 4))", diff --git a/crates/xrpl-wasm-vm/tests/host_calls.rs b/crates/xrpl-wasm-vm/tests/host_calls.rs index d9987d58ea..c6981e1f12 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -27,6 +27,28 @@ fn ldgr_index_writes_the_sequence_number_where_the_guest_asked() { assert_eq!(status(&wat, &host), 4, "the byte count"); } +/// A second scalar getter travels the same path: the value the host supplies lands +/// where the guest asked, and the status is the byte count. The default parent +/// ledger time is distinct from the sequence number, so this cannot pass by reading +/// the wrong one. +#[test] +fn parent_ldgr_time_writes_the_close_time_where_the_guest_asked() { + let host = FakeHost::new(); + + let wat = module( + &[import::PARENT_LDGR_TIME, ONE_PAGE], + "(drop (call $parent_ldgr_time (i32.const 64) (i32.const 4))) + (i32.load (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 9, "the 4 LE bytes the host wrote"); + + let wat = module( + &[import::PARENT_LDGR_TIME, ONE_PAGE], + "(call $parent_ldgr_time (i32.const 64) (i32.const 4))", + ); + assert_eq!(status(&wat, &host), 4, "the byte count"); +} + /// The output region is wherever the guest points, not a fixed address. #[test] fn the_output_region_is_the_pointer_the_guest_gave() { diff --git a/crates/xrpl-wasm-vm/tests/preflight.rs b/crates/xrpl-wasm-vm/tests/preflight.rs index cfa29883da..99b115ee29 100644 --- a/crates/xrpl-wasm-vm/tests/preflight.rs +++ b/crates/xrpl-wasm-vm/tests/preflight.rs @@ -98,8 +98,9 @@ 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; 5] = [ +const ALL_IMPORTS: [&str; 6] = [ import::LDGR_INDEX, + import::PARENT_LDGR_TIME, import::HOME_LE_FIELD, import::SHA512_HALF, import::TRACE, diff --git a/crates/xrpl-wasm-vm/tests/support/mod.rs b/crates/xrpl-wasm-vm/tests/support/mod.rs index 80ab51395e..8f280596fe 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -103,6 +103,8 @@ pub enum Trace { pub struct FakeHost { /// What `get_ledger_sqn` answers. pub ledger_sqn: Answer, + /// What `get_parent_ledger_time` answers. + pub parent_ledger_time: Answer, /// What `get_current_ledger_obj_field` answers, by field selector. An /// unlisted selector answers `FieldNotFound`. pub fields: HashMap, @@ -121,6 +123,9 @@ impl Default for FakeHost { FakeHost { // 4 little-endian bytes, as the declaration's doc comment specifies. ledger_sqn: Answer::bytes(7u32.to_le_bytes()), + // A distinct value from the sequence number, so a test cannot pass by + // reading one where it meant the other. + parent_ledger_time: Answer::bytes(9u32.to_le_bytes()), fields: HashMap::new(), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), @@ -140,6 +145,11 @@ impl FakeHost { self } + pub fn answering_parent_ledger_time(mut self, answer: Answer) -> FakeHost { + self.parent_ledger_time = answer; + self + } + pub fn answering_field(mut self, field: i32, answer: Answer) -> FakeHost { self.fields.insert(field, answer); self @@ -160,6 +170,10 @@ impl HostFunctions for FakeHost { self.ledger_sqn.fill(out) } + fn get_parent_ledger_time(&self, out: &mut [u8]) -> HostResult { + self.parent_ledger_time.fill(out) + } + fn get_current_ledger_obj_field(&self, field: i32, out: &mut [u8]) -> HostResult { self.fields_asked.borrow_mut().push(field); match self.fields.get(&field) { @@ -201,6 +215,7 @@ impl HostFunctions for FakeHost { pub mod import { pub const LDGR_INDEX: &str = r#"(import "host_lib" "ldgr_index" (func $ldgr_index (param i32 i32) (result i32)))"#; + pub const PARENT_LDGR_TIME: &str = r#"(import "host_lib" "parent_ldgr_time" (func $parent_ldgr_time (param 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 SHA512_HALF: &str = r#"(import "host_lib" "sha512_half" (func $sha512_half (param i32 i32 i32 i32) (result i32)))"#; pub const TRACE: &str = diff --git a/include/xrpl/tx/wasm/HostContext.h b/include/xrpl/tx/wasm/HostContext.h index 7f0f71f26a..cebeb52e0c 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -44,6 +44,9 @@ public: [[nodiscard]] std::int32_t getLedgerSqn(rust::Slice out) const noexcept; + [[nodiscard]] std::int32_t + getParentLedgerTime(rust::Slice out) const noexcept; + [[nodiscard]] std::int32_t getCurrentLedgerObjField(std::int32_t field, rust::Slice out) const noexcept; diff --git a/src/libxrpl/tx/wasm/HostContext.cpp b/src/libxrpl/tx/wasm/HostContext.cpp index bdf22a802e..60dcb7ba2f 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -71,6 +71,18 @@ HostContext::getLedgerSqn(rust::Slice out) const noexcept }); } +std::int32_t +HostContext::getParentLedgerTime(rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + auto const time = hostFunctions_.getParentLedgerTime(); + if (!time) + return hfErrorToInt(time.error()); + + return answerScalar(out, *time); + }); +} + std::int32_t HostContext::getCurrentLedgerObjField(std::int32_t field, rust::Slice out) const noexcept