From 9a6efde771295f50335388fa1d24cf9d1f7fd9ca Mon Sep 17 00:00:00 2001 From: TimothyBanks Date: Mon, 10 Aug 2026 15:19:52 -0400 Subject: [PATCH] feat: Hook up base_fee host function --- crates/xrpl-host-functions/src/lib.rs | 5 +++++ .../tests/generated_abi.rs | 7 +++++++ crates/xrpl-wasm-vm-ffi/src/lib.rs | 8 ++++++++ crates/xrpl-wasm-vm/src/abi.rs | 3 +++ crates/xrpl-wasm-vm/src/register.rs | 13 +++++++++++++ crates/xrpl-wasm-vm/tests/budgets.rs | 5 +++++ crates/xrpl-wasm-vm/tests/host_calls.rs | 19 +++++++++++++++++++ crates/xrpl-wasm-vm/tests/preflight.rs | 3 ++- crates/xrpl-wasm-vm/tests/support/mod.rs | 15 +++++++++++++++ include/xrpl/tx/wasm/HostContext.h | 3 +++ src/libxrpl/tx/wasm/HostContext.cpp | 12 ++++++++++++ 11 files changed, 92 insertions(+), 1 deletion(-) diff --git a/crates/xrpl-host-functions/src/lib.rs b/crates/xrpl-host-functions/src/lib.rs index a9ff193d3f..6eb070bf09 100644 --- a/crates/xrpl-host-functions/src/lib.rs +++ b/crates/xrpl-host-functions/src/lib.rs @@ -116,6 +116,11 @@ host_functions! { #[wasm_name = "parent_ldgr_hash"] fn get_parent_ledger_hash(&self, out: &mut [u8]) -> HostResult; + /// The base fee of the ledger being built, in drops, as 4 little-endian bytes. + #[gas = 60] + #[wasm_name = "base_fee"] + fn get_base_fee(&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 ce306e9093..f026c31001 100644 --- a/crates/xrpl-host-functions/tests/generated_abi.rs +++ b/crates/xrpl-host-functions/tests/generated_abi.rs @@ -39,6 +39,10 @@ impl HostFunctions for FakeHost { put(out, &[0xab; HASH_LEN]) } + fn get_base_fee(&self, out: &mut [u8]) -> HostResult { + put(out, &10u32.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 { @@ -77,6 +81,8 @@ fn the_trait_is_implementable() { assert_eq!(out[..4], [9, 0, 0, 0]); assert_eq!(host.get_parent_ledger_hash(&mut out), Ok(HASH_LEN)); assert_eq!(out[0], 0xab); + assert_eq!(host.get_base_fee(&mut out), Ok(4)); + assert_eq!(out[..4], [10, 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)); @@ -149,6 +155,7 @@ fn the_spec_table_matches_the_declarations() { ("ldgr_index", 60), ("parent_ldgr_time", 60), ("parent_ldgr_hash", 60), + ("base_fee", 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 00939b6e26..999515c918 100644 --- a/crates/xrpl-wasm-vm-ffi/src/lib.rs +++ b/crates/xrpl-wasm-vm-ffi/src/lib.rs @@ -167,6 +167,10 @@ mod ffi { #[cxx_name = "getParentLedgerHash"] fn get_parent_ledger_hash(self: &HostContext, out: &mut [u8]) -> i32; + #[namespace = "xrpl"] + #[cxx_name = "getBaseFee"] + fn get_base_fee(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; @@ -228,6 +232,10 @@ impl HostFunctions for CxxHost<'_> { bytes_written(self.ctx.get_parent_ledger_hash(out)) } + fn get_base_fee(&self, out: &mut [u8]) -> HostResult { + bytes_written(self.ctx.get_base_fee(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 8d018454e5..f5998af76c 100644 --- a/crates/xrpl-wasm-vm/src/abi.rs +++ b/crates/xrpl-wasm-vm/src/abi.rs @@ -194,6 +194,9 @@ mod tests { fn get_parent_ledger_hash(&self, _out: &mut [u8]) -> HostResult { unreachable!("no unit test in this module calls the host") } + fn get_base_fee(&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 89678afe26..6dad076d90 100644 --- a/crates/xrpl-wasm-vm/src/register.rs +++ b/crates/xrpl-wasm-vm/src/register.rs @@ -61,6 +61,19 @@ pub(crate) fn register_host_functions( }) }, ), + HostFunctionSpec::GetBaseFee => linker.func_wrap( + HOST_MODULE, + op.wasm_name(), + |mut caller: Caller<'_, VmState<'_>>, + out_ptr: i32, + out_len: i32| + -> Result { + charged(&mut caller, HostFunctionSpec::GetBaseFee, |c| { + let out = Region::new(out_ptr, out_len); + write_into(c, out, |host, out| host.get_base_fee(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 ff732879b4..3896edaa10 100644 --- a/crates/xrpl-wasm-vm/tests/budgets.rs +++ b/crates/xrpl-wasm-vm/tests/budgets.rs @@ -66,6 +66,11 @@ fn call_for(op: HostFunctionSpec) -> Call { "(call $parent_ldgr_hash (i32.const 0) (i32.const 32))", 2, ), + HostFunctionSpec::GetBaseFee => ( + import::BASE_FEE, + "(call $base_fee (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 1b1956e4a9..45ed5c68c7 100644 --- a/crates/xrpl-wasm-vm/tests/host_calls.rs +++ b/crates/xrpl-wasm-vm/tests/host_calls.rs @@ -74,6 +74,25 @@ fn parent_ldgr_hash_writes_all_32_bytes_where_the_guest_asked() { ); } +/// A third scalar getter, to pin the pattern rather than a single instance of it. +#[test] +fn base_fee_writes_the_fee_where_the_guest_asked() { + let host = FakeHost::new(); + + let wat = module( + &[import::BASE_FEE, ONE_PAGE], + "(drop (call $base_fee (i32.const 64) (i32.const 4))) + (i32.load (i32.const 64))", + ); + assert_eq!(status(&wat, &host), 10, "the 4 LE bytes the host wrote"); + + let wat = module( + &[import::BASE_FEE, ONE_PAGE], + "(call $base_fee (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 c097c0a3bc..f8eb7bca4a 100644 --- a/crates/xrpl-wasm-vm/tests/preflight.rs +++ b/crates/xrpl-wasm-vm/tests/preflight.rs @@ -98,10 +98,11 @@ 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; 7] = [ +const ALL_IMPORTS: [&str; 8] = [ import::LDGR_INDEX, import::PARENT_LDGR_TIME, import::PARENT_LDGR_HASH, + import::BASE_FEE, 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 3d063b61b1..a4da822a6b 100644 --- a/crates/xrpl-wasm-vm/tests/support/mod.rs +++ b/crates/xrpl-wasm-vm/tests/support/mod.rs @@ -107,6 +107,8 @@ pub struct FakeHost { pub parent_ledger_time: Answer, /// What `get_parent_ledger_hash` answers. pub parent_ledger_hash: Answer, + /// What `get_base_fee` answers. + pub base_fee: Answer, /// What `get_current_ledger_obj_field` answers, by field selector. An /// unlisted selector answers `FieldNotFound`. pub fields: HashMap, @@ -130,6 +132,8 @@ impl Default for FakeHost { parent_ledger_time: Answer::bytes(9u32.to_le_bytes()), // 32 bytes counting up from 0, the length of a real ledger hash. parent_ledger_hash: Answer::filler(32), + // A distinct value again, so no getter can pass by reading another's answer. + base_fee: Answer::bytes(10u32.to_le_bytes()), fields: HashMap::new(), digest: Answer::filler(32), fields_asked: RefCell::new(Vec::new()), @@ -159,6 +163,11 @@ impl FakeHost { self } + pub fn answering_base_fee(mut self, answer: Answer) -> FakeHost { + self.base_fee = answer; + self + } + pub fn answering_field(mut self, field: i32, answer: Answer) -> FakeHost { self.fields.insert(field, answer); self @@ -187,6 +196,10 @@ impl HostFunctions for FakeHost { self.parent_ledger_hash.fill(out) } + fn get_base_fee(&self, out: &mut [u8]) -> HostResult { + self.base_fee.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) { @@ -230,6 +243,8 @@ pub mod import { 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 PARENT_LDGR_HASH: &str = r#"(import "host_lib" "parent_ldgr_hash" (func $parent_ldgr_hash (param i32 i32) (result i32)))"#; + pub const BASE_FEE: &str = + r#"(import "host_lib" "base_fee" (func $base_fee (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 ca287762e5..7f3767dd41 100644 --- a/include/xrpl/tx/wasm/HostContext.h +++ b/include/xrpl/tx/wasm/HostContext.h @@ -50,6 +50,9 @@ public: [[nodiscard]] std::int32_t getParentLedgerHash(rust::Slice out) const noexcept; + [[nodiscard]] std::int32_t + getBaseFee(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 9bdac61f35..c67387024f 100644 --- a/src/libxrpl/tx/wasm/HostContext.cpp +++ b/src/libxrpl/tx/wasm/HostContext.cpp @@ -95,6 +95,18 @@ HostContext::getParentLedgerHash(rust::Slice out) const noexcept }); } +std::int32_t +HostContext::getBaseFee(rust::Slice out) const noexcept +{ + return guarded(hostFunctions_.getJournal(), kHostInternal, [&] { + auto const fee = hostFunctions_.getBaseFee(); + if (!fee) + return hfErrorToInt(fee.error()); + + return answerScalar(out, *fee); + }); +} + std::int32_t HostContext::getCurrentLedgerObjField(std::int32_t field, rust::Slice out) const noexcept