Fixed review comments

This commit is contained in:
Sergey Kuznetsov
2026-08-06 14:40:53 +01:00
parent ffb2d37d17
commit 7f9ece3891
16 changed files with 443 additions and 506 deletions

View File

@@ -18,10 +18,11 @@ bridge.
`ApplyContext&`. Bodies are split across `HostFuncImpl*.cpp` by category.
- **`HostContext.h`** — the bridge's C++ half: an ABI-shaped, `noexcept` view of
`HostFunctions` that the engine calls back into. Nothing may unwind into Rust, so every
method routes through one `guarded()`.
method routes through `guarded()`.
- **`WasmCommon.h`** — the shared vocabulary: `HostFunctionError` (the codes a contract
sees), `Bytes`, `FieldLocator`, `WasmTER`, and `adjustWasmEndianess`, which is where the
boundary's byte order is decided.
sees), `Bytes`, `FieldLocator`, `WasmTER`, `adjustWasmEndianess`, which is where the
boundary's byte order is decided, and `guarded()`, the one catch every crossing of the
bridge's C++ half goes through.
## Host functions

View File

@@ -1,13 +1,17 @@
#pragma once
#include <xrpl/basics/Log.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/contract.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/protocol/TER.h>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <optional>
#include <source_location>
#include <stdexcept>
#include <type_traits>
#include <utility>
@@ -146,4 +150,28 @@ hfErrorToInt(HostFunctionError e)
return static_cast<int32_t>(e);
}
template <class Body>
std::invoke_result_t<Body>
guarded(
beast::Journal journal,
std::invoke_result_t<Body> onThrow,
Body&& body,
std::source_location const location = std::source_location::current()) noexcept
{
try
{
return body();
}
catch (std::exception const& e)
{
JLOG(journal.error()) << "wasm: " << location.function_name() << " threw: " << e.what();
}
catch (...)
{
JLOG(journal.error()) << "wasm: " << location.function_name() << " threw";
}
return onThrow;
}
} // namespace xrpl

View File

@@ -22,15 +22,12 @@ std::string_view inline constexpr escrowFunctionName = "escrow_finish";
// when the number means anything, the gas to write to transaction metadata: a contract
// that traps or exhausts its budget is charged for what it burned, while a `tecINTERNAL`
// reports no cost because the fault is the node's rather than the transaction's.
//
// Does not throw. Every way a run can end - including a Rust panic inside the engine or
// a C++ exception thrown by a host function - arrives as one of those two answers.
std::expected<EscrowResult, WasmTER>
runEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::int64_t gasLimit,
std::string_view funcName = escrowFunctionName);
std::string_view funcName = escrowFunctionName) noexcept;
// Screen `wasmCode`: whether `runEscrowWasm` would refuse it before the contract's
// first instruction. Compiles the module and reads its imports and exports; runs
@@ -44,12 +41,10 @@ runEscrowWasm(
// engine cannot run, so it is refused before it can reach the ledger.
// `telFAILED_PROCESSING` if the engine itself failed: nothing was learned about the
// module, and a defect here is not evidence that the transaction is malformed.
//
// Does not throw.
NotTEC
preflightEscrowWasm(
Bytes const& wasmCode,
beast::Journal j,
std::string_view funcName = escrowFunctionName);
std::string_view funcName = escrowFunctionName) noexcept;
} // namespace xrpl