From 83418644f7bcba2ee2a084401268d6ab68653610 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Wed, 10 Sep 2025 14:56:21 -0400 Subject: [PATCH] add host functions --- src/xrpld/app/wasm/HostFunc.h | 467 ++++ src/xrpld/app/wasm/HostFuncImpl.h | 297 +++ src/xrpld/app/wasm/HostFuncWrapper.h | 567 +++++ src/xrpld/app/wasm/detail/HostFuncImpl.cpp | 1347 +++++++++++ src/xrpld/app/wasm/detail/HostFuncWrapper.cpp | 2065 +++++++++++++++++ src/xrpld/app/wasm/detail/WasmVM.cpp | 82 +- 6 files changed, 4824 insertions(+), 1 deletion(-) create mode 100644 src/xrpld/app/wasm/HostFuncImpl.h create mode 100644 src/xrpld/app/wasm/HostFuncWrapper.h create mode 100644 src/xrpld/app/wasm/detail/HostFuncImpl.cpp create mode 100644 src/xrpld/app/wasm/detail/HostFuncWrapper.cpp diff --git a/src/xrpld/app/wasm/HostFunc.h b/src/xrpld/app/wasm/HostFunc.h index b7ea5c7e85..f1332462a4 100644 --- a/src/xrpld/app/wasm/HostFunc.h +++ b/src/xrpld/app/wasm/HostFunc.h @@ -32,6 +32,71 @@ namespace ripple { +enum class HostFunctionError : int32_t { + INTERNAL = -1, + FIELD_NOT_FOUND = -2, + BUFFER_TOO_SMALL = -3, + NO_ARRAY = -4, + NOT_LEAF_FIELD = -5, + LOCATOR_MALFORMED = -6, + SLOT_OUT_RANGE = -7, + SLOTS_FULL = -8, + EMPTY_SLOT = -9, + LEDGER_OBJ_NOT_FOUND = -10, + DECODING = -11, + DATA_FIELD_TOO_LARGE = -12, + POINTER_OUT_OF_BOUNDS = -13, + NO_MEM_EXPORTED = -14, + INVALID_PARAMS = -15, + INVALID_ACCOUNT = -16, + INVALID_FIELD = -17, + INDEX_OUT_OF_BOUNDS = -18, + FLOAT_INPUT_MALFORMED = -19, + FLOAT_COMPUTATION_ERROR = -20, +}; + +inline int32_t +HfErrorToInt(HostFunctionError e) +{ + return static_cast(e); +} + +std::string +floatToString(Slice const& data); + +Expected +floatFromIntImpl(int64_t x, int32_t mode); + +Expected +floatFromUintImpl(uint64_t x, int32_t mode); + +Expected +floatSetImpl(int64_t mantissa, int32_t exponent, int32_t mode); + +Expected +floatCompareImpl(Slice const& x, Slice const& y); + +Expected +floatAddImpl(Slice const& x, Slice const& y, int32_t mode); + +Expected +floatSubtractImpl(Slice const& x, Slice const& y, int32_t mode); + +Expected +floatMultiplyImpl(Slice const& x, Slice const& y, int32_t mode); + +Expected +floatDivideImpl(Slice const& x, Slice const& y, int32_t mode); + +Expected +floatRootImpl(Slice const& x, int32_t n, int32_t mode); + +Expected +floatPowerImpl(Slice const& x, int32_t n, int32_t mode); + +Expected +floatLogImpl(Slice const& x, int32_t mode); + struct HostFunctions { // LCOV_EXCL_START @@ -52,6 +117,408 @@ struct HostFunctions return beast::Journal{beast::Journal::getNullSink()}; } + virtual Expected + getLedgerSqn() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getParentLedgerTime() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getParentLedgerHash() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerAccountHash() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerTransactionHash() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getBaseFee() + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + isAmendmentEnabled(uint256 const& amendmentId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + isAmendmentEnabled(std::string_view const& amendmentName) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + cacheLedgerObj(uint256 const& objId, int32_t cacheIdx) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getTxField(SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getCurrentLedgerObjField(SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerObjField(int32_t cacheIdx, SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getTxNestedField(Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getCurrentLedgerObjNestedField(Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getTxArrayLen(SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getCurrentLedgerObjArrayLen(SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getTxNestedArrayLen(Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getCurrentLedgerObjNestedArrayLen(Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + updateData(Slice const& data) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + checkSignature( + Slice const& message, + Slice const& signature, + Slice const& pubkey) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + computeSha512HalfHash(Slice const& data) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + accountKeylet(AccountID const& account) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + ammKeylet(Asset const& issue1, Asset const& issue2) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + checkKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + credentialKeylet( + AccountID const& subject, + AccountID const& issuer, + Slice const& credentialType) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + didKeylet(AccountID const& account) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + delegateKeylet(AccountID const& account, AccountID const& authorize) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + depositPreauthKeylet(AccountID const& account, AccountID const& authorize) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + escrowKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + lineKeylet( + AccountID const& account1, + AccountID const& account2, + Currency const& currency) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + mptokenKeylet(MPTID const& mptid, AccountID const& holder) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + nftOfferKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + offerKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + oracleKeylet(AccountID const& account, std::uint32_t docId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + paychanKeylet( + AccountID const& account, + AccountID const& destination, + std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + signersKeylet(AccountID const& account) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + ticketKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + vaultKeylet(AccountID const& account, std::uint32_t seq) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFT(AccountID const& account, uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFTIssuer(uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFTTaxon(uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFTFlags(uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFTTransferFee(uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + getNFTSerial(uint256 const& nftId) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + trace(std::string_view const& msg, Slice const& data, bool asHex) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + traceNum(std::string_view const& msg, int64_t data) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + traceAccount(std::string_view const& msg, AccountID const& account) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + traceFloat(std::string_view const& msg, Slice const& data) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + traceAmount(std::string_view const& msg, STAmount const& amount) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatFromInt(int64_t x, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatFromUint(uint64_t x, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatSet(int64_t mantissa, int32_t exponent, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatCompare(Slice const& x, Slice const& y) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatAdd(Slice const& x, Slice const& y, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatSubtract(Slice const& x, Slice const& y, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatMultiply(Slice const& x, Slice const& y, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatDivide(Slice const& x, Slice const& y, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatRoot(Slice const& x, int32_t n, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatPower(Slice const& x, int32_t n, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + + virtual Expected + floatLog(Slice const& x, int32_t mode) + { + return Unexpected(HostFunctionError::INTERNAL); + } + virtual ~HostFunctions() = default; // LCOV_EXCL_STOP }; diff --git a/src/xrpld/app/wasm/HostFuncImpl.h b/src/xrpld/app/wasm/HostFuncImpl.h new file mode 100644 index 0000000000..5539a5ef40 --- /dev/null +++ b/src/xrpld/app/wasm/HostFuncImpl.h @@ -0,0 +1,297 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2023 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#pragma once + +#include +#include + +namespace ripple { +class WasmHostFunctionsImpl : public HostFunctions +{ + ApplyContext& ctx; + Keylet leKey; + std::shared_ptr currentLedgerObj = nullptr; + bool isLedgerObjCached = false; + + static int constexpr MAX_CACHE = 256; + std::array, MAX_CACHE> cache; + std::optional data_; + + void const* rt_ = nullptr; + + Expected, HostFunctionError> + getCurrentLedgerObj() + { + if (!isLedgerObjCached) + { + isLedgerObjCached = true; + currentLedgerObj = ctx.view().read(leKey); + } + if (currentLedgerObj) + return currentLedgerObj; + return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND); + } + + Expected + normalizeCacheIndex(int32_t cacheIdx); + +public: + WasmHostFunctionsImpl(ApplyContext& ctx, Keylet const& leKey) + : ctx(ctx), leKey(leKey) + { + } + + virtual void + setRT(void const* rt) override + { + rt_ = rt; + } + + virtual void const* + getRT() const override + { + return rt_; + } + + beast::Journal + getJournal() override + { + return ctx.journal; + } + + std::optional const& + getData() const + { + return data_; + } + + Expected + getLedgerSqn() override; + + Expected + getParentLedgerTime() override; + + Expected + getParentLedgerHash() override; + + Expected + getLedgerAccountHash() override; + + Expected + getLedgerTransactionHash() override; + + Expected + getBaseFee() override; + + Expected + isAmendmentEnabled(uint256 const& amendmentId) override; + + Expected + isAmendmentEnabled(std::string_view const& amendmentName) override; + + Expected + cacheLedgerObj(uint256 const& objId, int32_t cacheIdx) override; + + Expected + getTxField(SField const& fname) override; + + Expected + getCurrentLedgerObjField(SField const& fname) override; + + Expected + getLedgerObjField(int32_t cacheIdx, SField const& fname) override; + + Expected + getTxNestedField(Slice const& locator) override; + + Expected + getCurrentLedgerObjNestedField(Slice const& locator) override; + + Expected + getLedgerObjNestedField(int32_t cacheIdx, Slice const& locator) override; + + Expected + getTxArrayLen(SField const& fname) override; + + Expected + getCurrentLedgerObjArrayLen(SField const& fname) override; + + Expected + getLedgerObjArrayLen(int32_t cacheIdx, SField const& fname) override; + + Expected + getTxNestedArrayLen(Slice const& locator) override; + + Expected + getCurrentLedgerObjNestedArrayLen(Slice const& locator) override; + + Expected + getLedgerObjNestedArrayLen(int32_t cacheIdx, Slice const& locator) override; + + Expected + updateData(Slice const& data) override; + + Expected + checkSignature( + Slice const& message, + Slice const& signature, + Slice const& pubkey) override; + + Expected + computeSha512HalfHash(Slice const& data) override; + + Expected + accountKeylet(AccountID const& account) override; + + Expected + ammKeylet(Asset const& issue1, Asset const& issue2) override; + + Expected + checkKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + credentialKeylet( + AccountID const& subject, + AccountID const& issuer, + Slice const& credentialType) override; + + Expected + didKeylet(AccountID const& account) override; + + Expected + delegateKeylet(AccountID const& account, AccountID const& authorize) + override; + + Expected + depositPreauthKeylet(AccountID const& account, AccountID const& authorize) + override; + + Expected + escrowKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + lineKeylet( + AccountID const& account1, + AccountID const& account2, + Currency const& currency) override; + + Expected + mptIssuanceKeylet(AccountID const& issuer, std::uint32_t seq) override; + + Expected + mptokenKeylet(MPTID const& mptid, AccountID const& holder) override; + + Expected + nftOfferKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + offerKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + oracleKeylet(AccountID const& account, std::uint32_t docId) override; + + Expected + paychanKeylet( + AccountID const& account, + AccountID const& destination, + std::uint32_t seq) override; + + Expected + permissionedDomainKeylet(AccountID const& account, std::uint32_t seq) + override; + + Expected + signersKeylet(AccountID const& account) override; + + Expected + ticketKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + vaultKeylet(AccountID const& account, std::uint32_t seq) override; + + Expected + getNFT(AccountID const& account, uint256 const& nftId) override; + + Expected + getNFTIssuer(uint256 const& nftId) override; + + Expected + getNFTTaxon(uint256 const& nftId) override; + + Expected + getNFTFlags(uint256 const& nftId) override; + + Expected + getNFTTransferFee(uint256 const& nftId) override; + + Expected + getNFTSerial(uint256 const& nftId) override; + + Expected + trace(std::string_view const& msg, Slice const& data, bool asHex) override; + + Expected + traceNum(std::string_view const& msg, int64_t data) override; + + Expected + traceAccount(std::string_view const& msg, AccountID const& account) + override; + + Expected + traceFloat(std::string_view const& msg, Slice const& data) override; + + Expected + traceAmount(std::string_view const& msg, STAmount const& amount) override; + + Expected + floatFromInt(int64_t x, int32_t mode) override; + + Expected + floatFromUint(uint64_t x, int32_t mode) override; + + Expected + floatSet(int64_t mantissa, int32_t exponent, int32_t mode) override; + + Expected + floatCompare(Slice const& x, Slice const& y) override; + + Expected + floatAdd(Slice const& x, Slice const& y, int32_t mode) override; + + Expected + floatSubtract(Slice const& x, Slice const& y, int32_t mode) override; + + Expected + floatMultiply(Slice const& x, Slice const& y, int32_t mode) override; + + Expected + floatDivide(Slice const& x, Slice const& y, int32_t mode) override; + + Expected + floatRoot(Slice const& x, int32_t n, int32_t mode) override; + + Expected + floatPower(Slice const& x, int32_t n, int32_t mode) override; + + Expected + floatLog(Slice const& x, int32_t mode) override; +}; + +} // namespace ripple diff --git a/src/xrpld/app/wasm/HostFuncWrapper.h b/src/xrpld/app/wasm/HostFuncWrapper.h new file mode 100644 index 0000000000..dc0524d611 --- /dev/null +++ b/src/xrpld/app/wasm/HostFuncWrapper.h @@ -0,0 +1,567 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2025 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#pragma once + +#include + +namespace ripple { + +using getLedgerSqn_proto = int32_t(); +wasm_trap_t* +getLedgerSqn_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getParentLedgerTime_proto = int32_t(); +wasm_trap_t* +getParentLedgerTime_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getParentLedgerHash_proto = int32_t(uint8_t*, int32_t); +wasm_trap_t* +getParentLedgerHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerAccountHash_proto = int32_t(uint8_t*, int32_t); +wasm_trap_t* +getLedgerAccountHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerTransactionHash_proto = int32_t(uint8_t*, int32_t); +wasm_trap_t* +getLedgerTransactionHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getBaseFee_proto = int32_t(); +wasm_trap_t* +getBaseFee_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using isAmendmentEnabled_proto = int32_t(uint8_t const*, int32_t); +wasm_trap_t* +isAmendmentEnabled_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using cacheLedgerObj_proto = int32_t(uint8_t const*, int32_t, int32_t); +wasm_trap_t* +cacheLedgerObj_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getTxField_proto = int32_t(int32_t, uint8_t*, int32_t); +wasm_trap_t* +getTxField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getCurrentLedgerObjField_proto = int32_t(int32_t, uint8_t*, int32_t); +wasm_trap_t* +getCurrentLedgerObjField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerObjField_proto = int32_t(int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getLedgerObjField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getTxNestedField_proto = + int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getTxNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getCurrentLedgerObjNestedField_proto = + int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getCurrentLedgerObjNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerObjNestedField_proto = + int32_t(int32_t, uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getLedgerObjNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getTxArrayLen_proto = int32_t(int32_t); +wasm_trap_t* +getTxArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getCurrentLedgerObjArrayLen_proto = int32_t(int32_t); +wasm_trap_t* +getCurrentLedgerObjArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerObjArrayLen_proto = int32_t(int32_t, int32_t); +wasm_trap_t* +getLedgerObjArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getTxNestedArrayLen_proto = int32_t(uint8_t const*, int32_t); +wasm_trap_t* +getTxNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getCurrentLedgerObjNestedArrayLen_proto = + int32_t(uint8_t const*, int32_t); +wasm_trap_t* +getCurrentLedgerObjNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getLedgerObjNestedArrayLen_proto = + int32_t(int32_t, uint8_t const*, int32_t); +wasm_trap_t* +getLedgerObjNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using updateData_proto = int32_t(uint8_t const*, int32_t); +wasm_trap_t* +updateData_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using checkSignature_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t const*, + int32_t); +wasm_trap_t* +checkSignature_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using computeSha512HalfHash_proto = + int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +computeSha512HalfHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using accountKeylet_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +accountKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using ammKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +ammKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using checkKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +checkKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using credentialKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +credentialKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using delegateKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +delegateKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using depositPreauthKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +depositPreauthKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using didKeylet_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +didKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using escrowKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +escrowKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using lineKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +lineKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using mptIssuanceKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +mptIssuanceKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using mptokenKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +mptokenKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using nftOfferKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +nftOfferKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using offerKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +offerKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using oracleKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +oracleKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using paychanKeylet_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +paychanKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using permissionedDomainKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +permissionedDomainKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using signersKeylet_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +signersKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using ticketKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +ticketKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using vaultKeylet_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t); +wasm_trap_t* +vaultKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getNFT_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t); +wasm_trap_t* +getNFT_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +using getNFTIssuer_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getNFTIssuer_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getNFTTaxon_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getNFTTaxon_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getNFTFlags_proto = int32_t(uint8_t const*, int32_t); +wasm_trap_t* +getNFTFlags_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getNFTTransferFee_proto = int32_t(uint8_t const*, int32_t); +wasm_trap_t* +getNFTTransferFee_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using getNFTSerial_proto = int32_t(uint8_t const*, int32_t, uint8_t*, int32_t); +wasm_trap_t* +getNFTSerial_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using trace_proto = + int32_t(uint8_t const*, int32_t, uint8_t const*, int32_t, int32_t); +wasm_trap_t* +trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +using traceNum_proto = int32_t(uint8_t const*, int32_t, int64_t); +wasm_trap_t* +traceNum_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +using traceAccount_proto = + int32_t(uint8_t const*, int32_t, uint8_t const*, int32_t); +wasm_trap_t* +traceAccount_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using traceFloat_proto = + int32_t(uint8_t const*, int32_t, uint8_t const*, int32_t); +wasm_trap_t* +traceFloat_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using traceAmount_proto = + int32_t(uint8_t const*, int32_t, uint8_t const*, int32_t); +wasm_trap_t* +traceAmount_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatFromInt_proto = int32_t(int64_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatFromInt_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatFromUint_proto = + int32_t(uint8_t const*, int32_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatFromUint_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatSet_proto = int32_t(int32_t, int64_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatSet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +using floatCompare_proto = + int32_t(uint8_t const*, int32_t, uint8_t const*, int32_t); +wasm_trap_t* +floatCompare_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatAdd_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t, + int32_t); +wasm_trap_t* +floatAdd_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +using floatSubtract_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t, + int32_t); +wasm_trap_t* +floatSubtract_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatMultiply_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t, + int32_t); +wasm_trap_t* +floatMultiply_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatDivide_proto = int32_t( + uint8_t const*, + int32_t, + uint8_t const*, + int32_t, + uint8_t*, + int32_t, + int32_t); +wasm_trap_t* +floatDivide_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatRoot_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatRoot_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatPower_proto = + int32_t(uint8_t const*, int32_t, int32_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatPower_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results); + +using floatLog_proto = + int32_t(uint8_t const*, int32_t, uint8_t*, int32_t, int32_t); +wasm_trap_t* +floatLog_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results); + +} // namespace ripple diff --git a/src/xrpld/app/wasm/detail/HostFuncImpl.cpp b/src/xrpld/app/wasm/detail/HostFuncImpl.cpp new file mode 100644 index 0000000000..4b585acdcd --- /dev/null +++ b/src/xrpld/app/wasm/detail/HostFuncImpl.cpp @@ -0,0 +1,1347 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include + +#include +#include + +#ifdef _DEBUG +// #define DEBUG_OUTPUT 1 +// #define DEBUG_OUTPUT_WAMR 1 +#endif + +namespace ripple { + +Expected +WasmHostFunctionsImpl::getLedgerSqn() +{ + auto seq = ctx.view().seq(); + if (seq > std::numeric_limits::max()) + return Unexpected(HostFunctionError::INTERNAL); // LCOV_EXCL_LINE + return static_cast(seq); +} + +Expected +WasmHostFunctionsImpl::getParentLedgerTime() +{ + auto time = ctx.view().parentCloseTime().time_since_epoch().count(); + if (time > std::numeric_limits::max()) + return Unexpected(HostFunctionError::INTERNAL); + return static_cast(time); +} + +Expected +WasmHostFunctionsImpl::getParentLedgerHash() +{ + return ctx.view().info().parentHash; +} + +Expected +WasmHostFunctionsImpl::getLedgerAccountHash() +{ + return ctx.view().info().accountHash; +} + +Expected +WasmHostFunctionsImpl::getLedgerTransactionHash() +{ + return ctx.view().info().txHash; +} + +Expected +WasmHostFunctionsImpl::getBaseFee() +{ + auto fee = ctx.view().fees().base.drops(); + if (fee > std::numeric_limits::max()) + return Unexpected(HostFunctionError::INTERNAL); + return static_cast(fee); +} + +Expected +WasmHostFunctionsImpl::isAmendmentEnabled(uint256 const& amendmentId) +{ + return ctx.view().rules().enabled(amendmentId); +} + +Expected +WasmHostFunctionsImpl::isAmendmentEnabled(std::string_view const& amendmentName) +{ + auto const& table = ctx.app.getAmendmentTable(); + auto const amendment = table.find(std::string(amendmentName)); + return ctx.view().rules().enabled(amendment); +} + +Expected +WasmHostFunctionsImpl::normalizeCacheIndex(int32_t cacheIdx) +{ + --cacheIdx; + if (cacheIdx < 0 || cacheIdx >= MAX_CACHE) + return Unexpected(HostFunctionError::SLOT_OUT_RANGE); + if (!cache[cacheIdx]) + return Unexpected(HostFunctionError::EMPTY_SLOT); + return cacheIdx; +} + +Expected +WasmHostFunctionsImpl::cacheLedgerObj(uint256 const& objId, int32_t cacheIdx) +{ + auto const& keylet = keylet::unchecked(objId); + if (cacheIdx < 0 || cacheIdx > MAX_CACHE) + return Unexpected(HostFunctionError::SLOT_OUT_RANGE); + + if (cacheIdx == 0) + { + for (cacheIdx = 0; cacheIdx < MAX_CACHE; ++cacheIdx) + if (!cache[cacheIdx]) + break; + } + else + { + cacheIdx--; // convert to 0-based index + } + + if (cacheIdx >= MAX_CACHE) + return Unexpected(HostFunctionError::SLOTS_FULL); + + cache[cacheIdx] = ctx.view().read(keylet); + if (!cache[cacheIdx]) + return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND); + return cacheIdx + 1; // return 1-based index +} + +static Expected +getAnyFieldData(STBase const* obj) +{ + // auto const& fname = obj.getFName(); + if (!obj) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + + auto const stype = obj->getSType(); + switch (stype) + { + // LCOV_EXCL_START + case STI_UNKNOWN: + case STI_NOTPRESENT: + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + break; + // LCOV_EXCL_STOP + case STI_OBJECT: + case STI_ARRAY: + return Unexpected(HostFunctionError::NOT_LEAF_FIELD); + break; + case STI_ACCOUNT: { + auto const* account(static_cast(obj)); + auto const& data = account->value(); + return Bytes{data.begin(), data.end()}; + } + break; + case STI_AMOUNT: + // will be processed by serializer + break; + case STI_ISSUE: { + auto const* issue(static_cast(obj)); + Asset const& asset(issue->value()); + // XRP and IOU will be processed by serializer + if (asset.holds()) + { + // MPT + auto const& mptIssue = asset.get(); + auto const& mptID = mptIssue.getMptID(); + return Bytes{mptID.cbegin(), mptID.cend()}; + } + } + break; + case STI_VL: { + auto const* vl(static_cast(obj)); + auto const& data = vl->value(); + return Bytes{data.begin(), data.end()}; + } + break; + case STI_UINT16: { + auto const& num(static_cast const*>(obj)); + std::uint16_t const data = num->value(); + auto const* b = reinterpret_cast(&data); + auto const* e = reinterpret_cast(&data + 1); + return Bytes{b, e}; + } + case STI_UINT32: { + auto const* num(static_cast const*>(obj)); + std::uint32_t const data = num->value(); + auto const* b = reinterpret_cast(&data); + auto const* e = reinterpret_cast(&data + 1); + return Bytes{b, e}; + } + break; + default: + break; // default to serializer + } + + Serializer msg; + obj->add(msg); + auto const data = msg.getData(); + + return data; +} + +Expected +WasmHostFunctionsImpl::getTxField(SField const& fname) +{ + return getAnyFieldData(ctx.tx.peekAtPField(fname)); +} + +Expected +WasmHostFunctionsImpl::getCurrentLedgerObjField(SField const& fname) +{ + auto const sle = getCurrentLedgerObj(); + if (!sle.has_value()) + return Unexpected(sle.error()); + return getAnyFieldData(sle.value()->peekAtPField(fname)); +} + +Expected +WasmHostFunctionsImpl::getLedgerObjField(int32_t cacheIdx, SField const& fname) +{ + auto const normalizedIdx = normalizeCacheIndex(cacheIdx); + if (!normalizedIdx.has_value()) + return Unexpected(normalizedIdx.error()); + return getAnyFieldData(cache[normalizedIdx.value()]->peekAtPField(fname)); +} + +static inline bool +noField(STBase const* field) +{ + return !field || (STI_NOTPRESENT == field->getSType()) || + (STI_UNKNOWN == field->getSType()); +} + +static Expected +locateField(STObject const& obj, Slice const& locator) +{ + if (locator.empty() || (locator.size() & 3)) // must be multiple of 4 + return Unexpected(HostFunctionError::LOCATOR_MALFORMED); + + int32_t const* locPtr = reinterpret_cast(locator.data()); + int32_t const locSize = locator.size() / 4; + STBase const* field = nullptr; + auto const& knownSFields = SField::getKnownCodeToField(); + + { + int32_t const sfieldCode = locPtr[0]; + auto const it = knownSFields.find(sfieldCode); + if (it == knownSFields.end()) + return Unexpected(HostFunctionError::INVALID_FIELD); + + auto const& fname(*it->second); + field = obj.peekAtPField(fname); + if (noField(field)) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + } + + for (int i = 1; i < locSize; ++i) + { + int32_t const sfieldCode = locPtr[i]; + + if (STI_ARRAY == field->getSType()) + { + auto const* arr = static_cast(field); + if (sfieldCode >= arr->size()) + return Unexpected(HostFunctionError::INDEX_OUT_OF_BOUNDS); + field = &(arr->operator[](sfieldCode)); + } + else if (STI_OBJECT == field->getSType()) + { + auto const* o = static_cast(field); + + auto const it = knownSFields.find(sfieldCode); + if (it == knownSFields.end()) + return Unexpected(HostFunctionError::INVALID_FIELD); + + auto const& fname(*it->second); + field = o->peekAtPField(fname); + } + else // simple field must be the last one + { + return Unexpected(HostFunctionError::LOCATOR_MALFORMED); + } + + if (noField(field)) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + } + + return field; +} + +Expected +WasmHostFunctionsImpl::getTxNestedField(Slice const& locator) +{ + auto const r = locateField(ctx.tx, locator); + if (!r) + return Unexpected(r.error()); + + return getAnyFieldData(r.value()); +} + +Expected +WasmHostFunctionsImpl::getCurrentLedgerObjNestedField(Slice const& locator) +{ + auto const sle = getCurrentLedgerObj(); + if (!sle.has_value()) + return Unexpected(sle.error()); + + auto const r = locateField(*sle.value(), locator); + if (!r) + return Unexpected(r.error()); + + return getAnyFieldData(r.value()); +} + +Expected +WasmHostFunctionsImpl::getLedgerObjNestedField( + int32_t cacheIdx, + Slice const& locator) +{ + auto const normalizedIdx = normalizeCacheIndex(cacheIdx); + if (!normalizedIdx.has_value()) + return Unexpected(normalizedIdx.error()); + + auto const r = locateField(*cache[normalizedIdx.value()], locator); + if (!r) + return Unexpected(r.error()); + + return getAnyFieldData(r.value()); +} + +Expected +WasmHostFunctionsImpl::getTxArrayLen(SField const& fname) +{ + if (fname.fieldType != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + + auto const* field = ctx.tx.peekAtPField(fname); + if (noField(field)) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); // LCOV_EXCL_LINE + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::getCurrentLedgerObjArrayLen(SField const& fname) +{ + if (fname.fieldType != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + + auto const sle = getCurrentLedgerObj(); + if (!sle.has_value()) + return Unexpected(sle.error()); + + auto const* field = sle.value()->peekAtPField(fname); + if (noField(field)) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); // LCOV_EXCL_LINE + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::getLedgerObjArrayLen( + int32_t cacheIdx, + SField const& fname) +{ + if (fname.fieldType != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + + auto const normalizedIdx = normalizeCacheIndex(cacheIdx); + if (!normalizedIdx.has_value()) + return Unexpected(normalizedIdx.error()); + + auto const* field = cache[normalizedIdx.value()]->peekAtPField(fname); + if (noField(field)) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); // LCOV_EXCL_LINE + + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::getTxNestedArrayLen(Slice const& locator) +{ + auto const r = locateField(ctx.tx, locator); + if (!r) + return Unexpected(r.error()); + + auto const* field = r.value(); + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::getCurrentLedgerObjNestedArrayLen(Slice const& locator) +{ + auto const sle = getCurrentLedgerObj(); + if (!sle.has_value()) + return Unexpected(sle.error()); + auto const r = locateField(*sle.value(), locator); + if (!r) + return Unexpected(r.error()); + + auto const* field = r.value(); + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::getLedgerObjNestedArrayLen( + int32_t cacheIdx, + Slice const& locator) +{ + auto const normalizedIdx = normalizeCacheIndex(cacheIdx); + if (!normalizedIdx.has_value()) + return Unexpected(normalizedIdx.error()); + + auto const r = locateField(*cache[normalizedIdx.value()], locator); + if (!r) + return Unexpected(r.error()); + + auto const* field = r.value(); + if (field->getSType() != STI_ARRAY) + return Unexpected(HostFunctionError::NO_ARRAY); + int32_t const sz = static_cast(field)->size(); + + return sz; +} + +Expected +WasmHostFunctionsImpl::updateData(Slice const& data) +{ + if (data.size() > maxWasmDataLength) + { + return Unexpected(HostFunctionError::DATA_FIELD_TOO_LARGE); + } + data_ = Bytes(data.begin(), data.end()); + return 0; +} + +Expected +WasmHostFunctionsImpl::checkSignature( + Slice const& message, + Slice const& signature, + Slice const& pubkey) +{ + if (!publicKeyType(pubkey)) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + PublicKey const pk(pubkey); + return verify(pk, message, signature, /*canonical*/ true); +} + +Expected +WasmHostFunctionsImpl::computeSha512HalfHash(Slice const& data) +{ + auto const hash = sha512Half(data); + return hash; +} + +Expected +WasmHostFunctionsImpl::accountKeylet(AccountID const& account) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::account(account); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::ammKeylet(Asset const& issue1, Asset const& issue2) +{ + if (issue1 == issue2) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + // note: this should be removed with the MPT DEX amendment + if (issue1.holds() || issue2.holds()) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + auto const keylet = keylet::amm(issue1, issue2); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::checkKeylet(AccountID const& account, std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::check(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::credentialKeylet( + AccountID const& subject, + AccountID const& issuer, + Slice const& credentialType) +{ + if (!subject || !issuer) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + + if (credentialType.empty() || + credentialType.size() > maxCredentialTypeLength) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + auto const keylet = keylet::credential(subject, issuer, credentialType); + + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::didKeylet(AccountID const& account) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::did(account); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::delegateKeylet( + AccountID const& account, + AccountID const& authorize) +{ + if (!account || !authorize) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + if (account == authorize) + return Unexpected(HostFunctionError::INVALID_PARAMS); + auto const keylet = keylet::delegate(account, authorize); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::depositPreauthKeylet( + AccountID const& account, + AccountID const& authorize) +{ + if (!account || !authorize) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + if (account == authorize) + return Unexpected(HostFunctionError::INVALID_PARAMS); + auto const keylet = keylet::depositPreauth(account, authorize); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::escrowKeylet(AccountID const& account, std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::escrow(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::lineKeylet( + AccountID const& account1, + AccountID const& account2, + Currency const& currency) +{ + if (!account1 || !account2) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + if (account1 == account2) + return Unexpected(HostFunctionError::INVALID_PARAMS); + if (currency.isZero()) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + auto const keylet = keylet::line(account1, account2, currency); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::mptIssuanceKeylet( + AccountID const& issuer, + std::uint32_t seq) +{ + if (!issuer) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + + auto const keylet = keylet::mptIssuance(seq, issuer); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::mptokenKeylet( + MPTID const& mptid, + AccountID const& holder) +{ + if (!mptid) + return Unexpected(HostFunctionError::INVALID_PARAMS); + if (!holder) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + + auto const keylet = keylet::mptoken(mptid, holder); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::nftOfferKeylet( + AccountID const& account, + std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::nftoffer(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::offerKeylet(AccountID const& account, std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::offer(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::oracleKeylet( + AccountID const& account, + std::uint32_t documentId) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::oracle(account, documentId); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::paychanKeylet( + AccountID const& account, + AccountID const& destination, + std::uint32_t seq) +{ + if (!account || !destination) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + if (account == destination) + return Unexpected(HostFunctionError::INVALID_PARAMS); + auto const keylet = keylet::payChan(account, destination, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::permissionedDomainKeylet( + AccountID const& account, + std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::permissionedDomain(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::signersKeylet(AccountID const& account) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::signers(account); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::ticketKeylet(AccountID const& account, std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::ticket(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::vaultKeylet(AccountID const& account, std::uint32_t seq) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + auto const keylet = keylet::vault(account, seq); + return Bytes{keylet.key.begin(), keylet.key.end()}; +} + +Expected +WasmHostFunctionsImpl::getNFT(AccountID const& account, uint256 const& nftId) +{ + if (!account) + return Unexpected(HostFunctionError::INVALID_ACCOUNT); + + if (!nftId) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + auto obj = nft::findToken(ctx.view(), account, nftId); + if (!obj) + return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND); + + auto ouri = obj->at(~sfURI); + if (!ouri) + return Unexpected(HostFunctionError::FIELD_NOT_FOUND); + + Slice const s = ouri->value(); + return Bytes(s.begin(), s.end()); +} + +Expected +WasmHostFunctionsImpl::getNFTIssuer(uint256 const& nftId) +{ + auto const issuer = nft::getIssuer(nftId); + if (!issuer) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + return Bytes{issuer.begin(), issuer.end()}; +} + +Expected +WasmHostFunctionsImpl::getNFTTaxon(uint256 const& nftId) +{ + return nft::toUInt32(nft::getTaxon(nftId)); +} + +Expected +WasmHostFunctionsImpl::getNFTFlags(uint256 const& nftId) +{ + return nft::getFlags(nftId); +} + +Expected +WasmHostFunctionsImpl::getNFTTransferFee(uint256 const& nftId) +{ + return nft::getTransferFee(nftId); +} + +Expected +WasmHostFunctionsImpl::getNFTSerial(uint256 const& nftId) +{ + return nft::getSerial(nftId); +} + +Expected +WasmHostFunctionsImpl::trace( + std::string_view const& msg, + Slice const& data, + bool asHex) +{ +#ifdef DEBUG_OUTPUT + auto j = getJournal().error(); +#else + auto j = getJournal().trace(); +#endif + if (!asHex) + { + j << "WAMR TRACE (" << leKey.key << "): " << msg << " " + << std::string_view( + reinterpret_cast(data.data()), data.size()); + } + else + { + std::string hex; + hex.reserve(data.size() * 2); + boost::algorithm::hex( + data.begin(), data.end(), std::back_inserter(hex)); + j << "WAMR DEV TRACE (" << leKey.key << "): " << msg << " " << hex; + } + + return msg.size() + data.size() * (asHex ? 2 : 1); +} + +Expected +WasmHostFunctionsImpl::traceNum(std::string_view const& msg, int64_t data) +{ +#ifdef DEBUG_OUTPUT + auto j = getJournal().error(); +#else + auto j = getJournal().trace(); +#endif + j << "WAMR TRACE NUM(" << leKey.key << "): " << msg << " " << data; + return msg.size() + sizeof(data); +} + +Expected +WasmHostFunctionsImpl::traceAccount( + std::string_view const& msg, + AccountID const& account) +{ +#ifdef DEBUG_OUTPUT + auto j = getJournal().error(); +#else + auto j = getJournal().trace(); +#endif + + auto const accountStr = toBase58(account); + + j << "WAMR TRACE ACCOUNT(" << leKey.key << "): " << msg << " " + << accountStr; + return msg.size() + accountStr.size(); +} + +Expected +WasmHostFunctionsImpl::traceFloat( + std::string_view const& msg, + Slice const& data) +{ +#ifdef DEBUG_OUTPUT + auto j = getJournal().error(); +#else + auto j = getJournal().trace(); +#endif + auto const s = floatToString(data); + j << "WAMR TRACE FLOAT(" << leKey.key << "): " << msg << " " << s; + return msg.size() + s.size(); +} + +Expected +WasmHostFunctionsImpl::traceAmount( + std::string_view const& msg, + STAmount const& amount) +{ +#ifdef DEBUG_OUTPUT + auto j = getJournal().error(); +#else + auto j = getJournal().trace(); +#endif + auto const amountStr = amount.getFullText(); + j << "WAMR TRACE AMOUNT(" << leKey.key << "): " << msg << " " << amountStr; + return msg.size() + amountStr.size(); +} + +Expected +WasmHostFunctionsImpl::floatFromInt(int64_t x, int32_t mode) +{ + return floatFromIntImpl(x, mode); +} + +Expected +WasmHostFunctionsImpl::floatFromUint(uint64_t x, int32_t mode) +{ + return floatFromUintImpl(x, mode); +} + +Expected +WasmHostFunctionsImpl::floatSet( + int64_t mantissa, + int32_t exponent, + int32_t mode) +{ + return floatSetImpl(mantissa, exponent, mode); +} + +Expected +WasmHostFunctionsImpl::floatCompare(Slice const& x, Slice const& y) +{ + return floatCompareImpl(x, y); +} + +Expected +WasmHostFunctionsImpl::floatAdd(Slice const& x, Slice const& y, int32_t mode) +{ + return floatAddImpl(x, y, mode); +} + +Expected +WasmHostFunctionsImpl::floatSubtract( + Slice const& x, + Slice const& y, + int32_t mode) +{ + return floatSubtractImpl(x, y, mode); +} + +Expected +WasmHostFunctionsImpl::floatMultiply( + Slice const& x, + Slice const& y, + int32_t mode) +{ + return floatMultiplyImpl(x, y, mode); +} + +Expected +WasmHostFunctionsImpl::floatDivide(Slice const& x, Slice const& y, int32_t mode) +{ + return floatDivideImpl(x, y, mode); +} + +Expected +WasmHostFunctionsImpl::floatRoot(Slice const& x, int32_t n, int32_t mode) +{ + return floatRootImpl(x, n, mode); +} + +Expected +WasmHostFunctionsImpl::floatPower(Slice const& x, int32_t n, int32_t mode) +{ + return floatPowerImpl(x, n, mode); +} + +Expected +WasmHostFunctionsImpl::floatLog(Slice const& x, int32_t mode) +{ + return floatLogImpl(x, mode); +} + +class Number2 : public Number +{ +protected: + static Bytes const FLOAT_NULL; + + bool good_; + +public: + Number2(Slice const& data) : Number(), good_(false) + { + if (data.size() != 8) + return; + + if (std::ranges::equal(FLOAT_NULL, data)) + { + good_ = true; + return; + } + + uint64_t const v = SerialIter(data).get64(); + if (!(v & STAmount::cIssuedCurrency)) + return; + + int64_t const neg = (v & STAmount::cPositive) ? 1 : -1; + int32_t const e = static_cast((v >> (64 - 10)) & 0xFFull); + if (e < 1 || e > 177) + return; + + int64_t const m = neg * static_cast(v & ((1ull << 54) - 1)); + if (!m) + return; + + Number x(m, e + IOUAmount::minExponent - 1); + *static_cast(this) = x; + good_ = true; + } + + Number2() : Number(), good_(true) + { + } + + Number2(int64_t x) : Number(x), good_(true) + { + } + + Number2(uint64_t x) : Number(0), good_(false) + { + using mtype = std::invoke_result_t; + if (x <= std::numeric_limits::max()) + *this = Number(x); + else + *this = Number(x / 10, 1) + Number(x % 10); + + good_ = true; + } + + Number2(int64_t mantissa, int32_t exponent) + : Number(mantissa, exponent), good_(true) + { + } + + Number2(Number const& n) : Number(n), good_(true) + { + } + + operator bool() const + { + return good_; + } + + Expected + toBytes() const + { + uint64_t v = mantissa() >= 0 ? STAmount::cPositive : 0; + v |= STAmount::cIssuedCurrency; + + uint64_t const absM = mantissa() >= 0 ? mantissa() : -mantissa(); + if (!absM) + { + using etype = + std::invoke_result_t; + if (exponent() != std::numeric_limits::lowest()) + { + return Unexpected( + HostFunctionError:: + FLOAT_COMPUTATION_ERROR); // LCOV_EXCL_LINE + } + return FLOAT_NULL; + } + else if (absM > ((1ull << 54) - 1)) + { + return Unexpected( + HostFunctionError::FLOAT_COMPUTATION_ERROR); // LCOV_EXCL_LINE + } + else if (exponent() > IOUAmount::maxExponent) + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + else if (exponent() < IOUAmount::minExponent) + return FLOAT_NULL; + + int const e = exponent() - IOUAmount::minExponent + 1; //+97 + v |= absM; + v |= ((uint64_t)e) << 54; + + Serializer msg; + msg.add64(v); + auto const data = msg.getData(); + +#ifdef DEBUG_OUTPUT + std::cout << "m: " << std::setw(20) << mantissa() + << ", e: " << std::setw(12) << exponent() << ", hex: "; + std::cout << std::hex << std::uppercase << std::setfill('0'); + for (auto const& c : data) + std::cout << std::setw(2) << (unsigned)c << " "; + std::cout << std::dec << std::setfill(' ') << std::endl; +#endif + + return data; + } +}; + +Bytes const Number2::FLOAT_NULL = + {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +struct SetRound +{ + Number::rounding_mode oldMode_; + bool good_; + + SetRound(int32_t mode) : oldMode_(Number::getround()), good_(false) + { + if (mode < Number::rounding_mode::to_nearest || + mode > Number::rounding_mode::upward) + return; + + Number::setround(static_cast(mode)); + good_ = true; + } + + ~SetRound() + { + Number::setround(oldMode_); + } + + operator bool() const + { + return good_; + } +}; + +std::string +floatToString(Slice const& data) +{ + Number2 const num(data); + if (!num) + { + std::string hex; + hex.reserve(data.size() * 2); + boost::algorithm::hex( + data.begin(), data.end(), std::back_inserter(hex)); + return "Invalid data: " + hex; + } + + auto const s = to_string(num); + return s; +} + +Expected +floatFromIntImpl(int64_t x, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 num(x); + return num.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatFromUintImpl(uint64_t x, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 num(x); + return num.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatSetImpl(int64_t mantissa, int32_t exponent, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 num(mantissa, exponent); + return num.toBytes(); + } + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); +} + +Expected +floatCompareImpl(Slice const& x, Slice const& y) +{ + try + { + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 yy(y); + if (!yy) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + return xx < yy ? 2 : (xx == yy ? 0 : 1); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatAddImpl(Slice const& x, Slice const& y, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 yy(y); + if (!yy) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 res = xx + yy; + + return res.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatSubtractImpl(Slice const& x, Slice const& y, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 yy(y); + if (!yy) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 res = xx - yy; + + return res.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatMultiplyImpl(Slice const& x, Slice const& y, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 yy(y); + if (!yy) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 res = xx * yy; + + return res.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatDivideImpl(Slice const& x, Slice const& y, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 yy(y); + if (!yy) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + Number2 res = xx / yy; + + return res.toBytes(); + } + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); +} + +Expected +floatRootImpl(Slice const& x, int32_t n, int32_t mode) +{ + try + { + if (n < 1) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 res(root(xx, n)); + + return res.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +Expected +floatPowerImpl(Slice const& x, int32_t n, int32_t mode) +{ + try + { + if (n < 0) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + if (xx == Number() && !n) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + Number2 res(power(xx, n, 1)); + + return res.toBytes(); + } + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); +} + +Expected +floatLogImpl(Slice const& x, int32_t mode) +{ + try + { + SetRound rm(mode); + if (!rm) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 xx(x); + if (!xx) + return Unexpected(HostFunctionError::FLOAT_INPUT_MALFORMED); + + Number2 res(lg(xx)); + + return res.toBytes(); + } + // LCOV_EXCL_START + catch (...) + { + } + return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR); + // LCOV_EXCL_STOP +} + +} // namespace ripple diff --git a/src/xrpld/app/wasm/detail/HostFuncWrapper.cpp b/src/xrpld/app/wasm/detail/HostFuncWrapper.cpp new file mode 100644 index 0000000000..1bd9622a4c --- /dev/null +++ b/src/xrpld/app/wasm/detail/HostFuncWrapper.cpp @@ -0,0 +1,2065 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include +#include + +#include +#include +#include + +namespace ripple { + +using SFieldCRef = std::reference_wrapper; + +static int32_t +setData( + InstanceWrapper const* runtime, + int32_t dst, + int32_t dstSize, + uint8_t const* src, + int32_t srcSize) +{ + if (!srcSize) + return 0; // LCOV_EXCL_LINE + + if (dst < 0 || dstSize < 0 || !src || srcSize < 0) + return HfErrorToInt(HostFunctionError::INVALID_PARAMS); + + auto memory = runtime ? runtime->getMem() : wmem(); + + // LCOV_EXCL_START + if (!memory.s) + return HfErrorToInt(HostFunctionError::NO_MEM_EXPORTED); + // LCOV_EXCL_STOP + if (dst + dstSize > memory.s) + return HfErrorToInt(HostFunctionError::POINTER_OUT_OF_BOUNDS); + if (srcSize > dstSize) + return HfErrorToInt(HostFunctionError::BUFFER_TOO_SMALL); + + memcpy(memory.p + dst, src, srcSize); + + return srcSize; +} + +template +Expected +getDataInt32(IW const* _runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const result = params->data[i].of.i32; + i++; + return result; +} + +template +Expected +getDataInt64(IW const* _runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const result = params->data[i].of.i64; + i++; + return result; +} + +template +Expected +getDataUInt64(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const r = getDataSlice(runtime, params, i); + if (!r) + return Unexpected(r.error()); + if (r->size() != sizeof(uint64_t)) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + return *reinterpret_cast(r->data()); +} + +template +Expected +getDataSField(IW const* _runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const& m = SField::getKnownCodeToField(); + auto const it = m.find(params->data[i].of.i32); + if (it == m.end()) + { + return Unexpected(HostFunctionError::INVALID_FIELD); + } + i++; + return *it->second; +} + +template +Expected +getDataSlice(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const ptr = params->data[i].of.i32; + auto const size = params->data[i + 1].of.i32; + if (ptr < 0 || size < 0) + return Unexpected(HostFunctionError::INVALID_PARAMS); + + if (!size) + return Slice(); + + if (size > maxWasmDataLength) + return Unexpected(HostFunctionError::DATA_FIELD_TOO_LARGE); + + auto memory = runtime ? runtime->getMem() : wmem(); + // LCOV_EXCL_START + if (!memory.s) + return Unexpected(HostFunctionError::NO_MEM_EXPORTED); + // LCOV_EXCL_STOP + + if (ptr + size > memory.s) + return Unexpected(HostFunctionError::POINTER_OUT_OF_BOUNDS); + + Slice data(memory.p + ptr, size); + i += 2; + return data; +} + +template +Expected +getDataUInt256(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const slice = getDataSlice(runtime, params, i); + if (!slice) + { + return Unexpected(slice.error()); + } + + if (slice->size() != uint256::bytes) + { + return Unexpected(HostFunctionError::INVALID_PARAMS); + } + return uint256::fromVoid(slice->data()); +} + +template +Expected +getDataAccountID(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const slice = getDataSlice(runtime, params, i); + if (!slice) + { + return Unexpected(slice.error()); + } + + if (slice->size() != AccountID::bytes) + { + return Unexpected(HostFunctionError::INVALID_PARAMS); + } + + return AccountID::fromVoid(slice->data()); +} + +template +static Expected +getDataCurrency(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const slice = getDataSlice(runtime, params, i); + if (!slice) + { + return Unexpected(slice.error()); + } + + if (slice->size() != Currency::bytes) + { + return Unexpected(HostFunctionError::INVALID_PARAMS); + } + + return Currency::fromVoid(slice->data()); +} + +template +static Expected +getDataAsset(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const slice = getDataSlice(runtime, params, i); + if (!slice) + { + return Unexpected(slice.error()); + } + + if (slice->size() == MPTID::bytes) + { + auto const mptid = MPTID::fromVoid(slice->data()); + return Asset{mptid}; + } + + if (slice->size() == Currency::bytes) + { + auto const currency = Currency::fromVoid(slice->data()); + auto const issue = Issue{currency, xrpAccount()}; + if (!issue.native()) + return Unexpected(HostFunctionError::INVALID_PARAMS); + return Asset{issue}; + } + + if (slice->size() == (AccountID::bytes + Currency::bytes)) + { + auto const issue = Issue( + Currency::fromVoid(slice->data()), + AccountID::fromVoid(slice->data() + Currency::bytes)); + + if (issue.native()) + return Unexpected(HostFunctionError::INVALID_PARAMS); + return Asset{issue}; + } + + return Unexpected(HostFunctionError::INVALID_PARAMS); +} + +template +Expected +getDataString(IW const* runtime, wasm_val_vec_t const* params, int32_t& i) +{ + auto const slice = getDataSlice(runtime, params, i); + if (!slice) + return Unexpected(slice.error()); + return std::string_view( + reinterpret_cast(slice->data()), slice->size()); +} + +std::nullptr_t +hfResult(wasm_val_vec_t* results, int32_t value) +{ + results->data[0] = WASM_I32_VAL(value); + results->num_elems = 1; + return nullptr; +} + +std::nullptr_t +hfResult(wasm_val_vec_t* results, HostFunctionError value) +{ + results->data[0] = WASM_I32_VAL(HfErrorToInt(value)); + results->num_elems = 1; + return nullptr; +} + +template +std::nullptr_t +returnResult( + InstanceWrapper const* runtime, + wasm_val_vec_t const* params, + wasm_val_vec_t* results, + Expected const& res, + int32_t index) +{ + if (!res) + { + return hfResult(results, res.error()); + } + + using t = std::decay_t; + if constexpr (std::is_same_v) + { + return hfResult( + results, + setData( + runtime, + params->data[index].of.i32, + params->data[index + 1].of.i32, + res->data(), + res->size())); + } + else if constexpr (std::is_same_v) + { + return hfResult( + results, + setData( + runtime, + params->data[index].of.i32, + params->data[index + 1].of.i32, + res->data(), + res->size())); + } + else if constexpr (std::is_same_v) + { + return hfResult(results, res.value()); + } + else if constexpr (std::is_same_v) + { + auto const resultValue = res.value(); + return hfResult( + results, + setData( + runtime, + params->data[index].of.i32, + params->data[index + 1].of.i32, + reinterpret_cast(&resultValue), + static_cast(sizeof(resultValue)))); + } + else + { + static_assert( + [] { return false; }(), "Unhandled return type in returnResult"); + } +} + +wasm_trap_t* +getLedgerSqn_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult(runtime, params, results, hf->getLedgerSqn(), index); +} + +wasm_trap_t* +getParentLedgerTime_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult( + runtime, params, results, hf->getParentLedgerTime(), index); +} + +wasm_trap_t* +getParentLedgerHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult( + runtime, params, results, hf->getParentLedgerHash(), index); +} + +wasm_trap_t* +getLedgerAccountHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult( + runtime, params, results, hf->getLedgerAccountHash(), index); +} + +wasm_trap_t* +getLedgerTransactionHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult( + runtime, params, results, hf->getLedgerTransactionHash(), index); +} + +wasm_trap_t* +getBaseFee_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + return returnResult(runtime, params, results, hf->getBaseFee(), index); +} + +wasm_trap_t* +isAmendmentEnabled_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const slice = getDataSlice(runtime, params, index); + if (!slice) + { + return hfResult(results, slice.error()); + } + + if (slice->size() == uint256::bytes) + { + if (auto ret = hf->isAmendmentEnabled(uint256::fromVoid(slice->data())); + *ret == 1) + { + return returnResult(runtime, params, results, ret, index); + } + } + + if (slice->size() > 64) + { + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + } + + auto const str = std::string_view( + reinterpret_cast(slice->data()), slice->size()); + return returnResult( + runtime, params, results, hf->isAmendmentEnabled(str), index); +} + +wasm_trap_t* +cacheLedgerObj_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const id = getDataUInt256(runtime, params, index); + if (!id) + { + return hfResult(results, id.error()); + } + + auto const cache = getDataInt32(runtime, params, index); + if (!cache) + { + return hfResult(results, cache.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, params, results, hf->cacheLedgerObj(*id, *cache), index); +} + +wasm_trap_t* +getTxField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + return returnResult( + runtime, params, results, hf->getTxField(*fname), index); +} + +wasm_trap_t* +getCurrentLedgerObjField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + + return returnResult( + runtime, params, results, hf->getCurrentLedgerObjField(*fname), index); +} + +wasm_trap_t* +getLedgerObjField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const cache = getDataInt32(runtime, params, index); + if (!cache) + { + return hfResult(results, cache.error()); // LCOV_EXCL_LINE + } + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + + return returnResult( + runtime, params, results, hf->getLedgerObjField(*cache, *fname), index); +} + +wasm_trap_t* +getTxNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + + return returnResult( + runtime, params, results, hf->getTxNestedField(*bytes), index); +} + +wasm_trap_t* +getCurrentLedgerObjNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + return returnResult( + runtime, + params, + results, + hf->getCurrentLedgerObjNestedField(*bytes), + index); +} + +wasm_trap_t* +getLedgerObjNestedField_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const cache = getDataInt32(runtime, params, index); + if (!cache) + { + return hfResult(results, cache.error()); // LCOV_EXCL_LINE + } + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + + return returnResult( + runtime, + params, + results, + hf->getLedgerObjNestedField(*cache, *bytes), + index); +} + +wasm_trap_t* +getTxArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + + return returnResult( + runtime, params, results, hf->getTxArrayLen(*fname), index); +} + +wasm_trap_t* +getCurrentLedgerObjArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + + return returnResult( + runtime, + params, + results, + hf->getCurrentLedgerObjArrayLen(*fname), + index); +} + +wasm_trap_t* +getLedgerObjArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const cache = getDataInt32(runtime, params, index); + if (!cache) + { + return hfResult(results, cache.error()); // LCOV_EXCL_LINE + } + + auto const fname = getDataSField(runtime, params, index); + if (!fname) + { + return hfResult(results, fname.error()); + } + + return returnResult( + runtime, + params, + results, + hf->getLedgerObjArrayLen(*cache, *fname), + index); +} + +wasm_trap_t* +getTxNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + + return returnResult( + runtime, params, results, hf->getTxNestedArrayLen(*bytes), index); +} + +wasm_trap_t* +getCurrentLedgerObjNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + + return returnResult( + runtime, + params, + results, + hf->getCurrentLedgerObjNestedArrayLen(*bytes), + index); +} +wasm_trap_t* +getLedgerObjNestedArrayLen_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const cache = getDataInt32(runtime, params, index); + if (!cache) + { + return hfResult(results, cache.error()); // LCOV_EXCL_LINE + } + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + return returnResult( + runtime, + params, + results, + hf->getLedgerObjNestedArrayLen(*cache, *bytes), + index); +} + +wasm_trap_t* +updateData_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + + return returnResult( + runtime, params, results, hf->updateData(*bytes), index); +} + +wasm_trap_t* +checkSignature_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const message = getDataSlice(runtime, params, index); + if (!message) + { + return hfResult(results, message.error()); + } + + auto const signature = getDataSlice(runtime, params, index); + if (!signature) + { + return hfResult(results, signature.error()); + } + + auto const pubkey = getDataSlice(runtime, params, index); + if (!pubkey) + { + return hfResult(results, pubkey.error()); + } + + return returnResult( + runtime, + params, + results, + hf->checkSignature(*message, *signature, *pubkey), + index); +} + +wasm_trap_t* +computeSha512HalfHash_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const bytes = getDataSlice(runtime, params, index); + if (!bytes) + { + return hfResult(results, bytes.error()); + } + return returnResult( + runtime, params, results, hf->computeSha512HalfHash(*bytes), index); +} + +wasm_trap_t* +accountKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + return returnResult( + runtime, params, results, hf->accountKeylet(*acc), index); +} + +wasm_trap_t* +ammKeylet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const issue1 = getDataAsset(runtime, params, index); + if (!issue1) + { + return hfResult(results, issue1.error()); + } + + auto const issue2 = getDataAsset(runtime, params, index); + if (!issue2) + { + return hfResult(results, issue2.error()); + } + + return returnResult( + runtime, + params, + results, + hf->ammKeylet(issue1.value(), issue2.value()), + index); +} + +wasm_trap_t* +checkKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, params, results, hf->checkKeylet(acc.value(), *seq), index); +} + +wasm_trap_t* +credentialKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const subj = getDataAccountID(runtime, params, index); + if (!subj) + { + return hfResult(results, subj.error()); + } + + auto const iss = getDataAccountID(runtime, params, index); + if (!iss) + { + return hfResult(results, iss.error()); + } + + auto const credType = getDataSlice(runtime, params, index); + if (!credType) + { + return hfResult(results, credType.error()); + } + + return returnResult( + runtime, + params, + results, + hf->credentialKeylet(*subj, *iss, *credType), + index); +} + +wasm_trap_t* +delegateKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const authorize = getDataAccountID(runtime, params, index); + if (!authorize) + { + return hfResult(results, authorize.error()); + } + + return returnResult( + runtime, + params, + results, + hf->delegateKeylet(acc.value(), authorize.value()), + index); +} + +wasm_trap_t* +depositPreauthKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const authorize = getDataAccountID(runtime, params, index); + if (!authorize) + { + return hfResult(results, authorize.error()); + } + + return returnResult( + runtime, + params, + results, + hf->depositPreauthKeylet(acc.value(), authorize.value()), + index); +} + +wasm_trap_t* +didKeylet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + return returnResult( + runtime, params, results, hf->didKeylet(acc.value()), index); +} + +wasm_trap_t* +escrowKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, params, results, hf->escrowKeylet(*acc, *seq), index); +} + +wasm_trap_t* +lineKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc1 = getDataAccountID(runtime, params, index); + if (!acc1) + { + return hfResult(results, acc1.error()); + } + + auto const acc2 = getDataAccountID(runtime, params, index); + if (!acc2) + { + return hfResult(results, acc2.error()); + } + + auto const currency = getDataCurrency(runtime, params, index); + if (!currency) + { + return hfResult(results, currency.error()); + } + + return returnResult( + runtime, + params, + results, + hf->lineKeylet(acc1.value(), acc2.value(), currency.value()), + index); +} + +wasm_trap_t* +mptIssuanceKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->mptIssuanceKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +mptokenKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const slice = getDataSlice(runtime, params, index); + if (!slice) + { + return hfResult(results, slice.error()); + } + + if (slice->size() != MPTID::bytes) + { + return hfResult(results, HostFunctionError::INVALID_PARAMS); + } + auto const mptid = MPTID::fromVoid(slice->data()); + + auto const holder = getDataAccountID(runtime, params, index); + if (!holder) + { + return hfResult(results, holder.error()); + } + + return returnResult( + runtime, + params, + results, + hf->mptokenKeylet(mptid, holder.value()), + index); +} + +wasm_trap_t* +nftOfferKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->nftOfferKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +offerKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->offerKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +oracleKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const documentId = getDataInt32(runtime, params, index); + if (!documentId) + { + return hfResult(results, documentId.error()); // LCOV_EXCL_LINE + } + return returnResult( + runtime, params, results, hf->oracleKeylet(*acc, *documentId), index); +} + +wasm_trap_t* +paychanKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const dest = getDataAccountID(runtime, params, index); + if (!dest) + { + return hfResult(results, dest.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->paychanKeylet(acc.value(), dest.value(), seq.value()), + index); +} + +wasm_trap_t* +permissionedDomainKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->permissionedDomainKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +signersKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + return returnResult( + runtime, params, results, hf->signersKeylet(acc.value()), index); +} + +wasm_trap_t* +ticketKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->ticketKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +vaultKeylet_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const seq = getDataInt32(runtime, params, index); + if (!seq) + { + return hfResult(results, seq.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, + params, + results, + hf->vaultKeylet(acc.value(), seq.value()), + index); +} + +wasm_trap_t* +getNFT_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const acc = getDataAccountID(runtime, params, index); + if (!acc) + { + return hfResult(results, acc.error()); + } + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFT(*acc, *nftId), index); +} + +wasm_trap_t* +getNFTIssuer_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFTIssuer(*nftId), index); +} + +wasm_trap_t* +getNFTTaxon_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFTTaxon(*nftId), index); +} + +wasm_trap_t* +getNFTFlags_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFTFlags(*nftId), index); +} + +wasm_trap_t* +getNFTTransferFee_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFTTransferFee(*nftId), index); +} + +wasm_trap_t* +getNFTSerial_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + auto const nftId = getDataUInt256(runtime, params, index); + if (!nftId) + { + return hfResult(results, nftId.error()); + } + + return returnResult( + runtime, params, results, hf->getNFTSerial(*nftId), index); +} + +wasm_trap_t* +trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + + if (params->data[1].of.i32 + params->data[3].of.i32 > maxWasmDataLength) + { + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + } + + auto const msg = getDataString(runtime, params, index); + if (!msg) + { + return hfResult(results, msg.error()); + } + + auto const data = getDataSlice(runtime, params, index); + if (!data) + { + return hfResult(results, data.error()); + } + + auto const asHex = getDataInt32(runtime, params, index); + if (!asHex) + { + return hfResult(results, asHex.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, params, results, hf->trace(*msg, *data, *asHex), index); +} + +wasm_trap_t* +traceNum_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + int index = 0; + if (params->data[1].of.i32 > maxWasmDataLength) + { + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + } + + auto const msg = getDataString(runtime, params, index); + if (!msg) + { + return hfResult(results, msg.error()); + } + + auto const number = getDataInt64(runtime, params, index); + if (!number) + { + return hfResult(results, number.error()); // LCOV_EXCL_LINE + } + + return returnResult( + runtime, params, results, hf->traceNum(*msg, *number), index); +} + +wasm_trap_t* +traceAccount_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + if (params->data[1].of.i32 > maxWasmDataLength) + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + + int i = 0; + auto const msg = getDataString(runtime, params, i); + if (!msg) + return hfResult(results, msg.error()); + + auto const account = getDataAccountID(runtime, params, i); + if (!account) + return hfResult(results, account.error()); + + return returnResult( + runtime, params, results, hf->traceAccount(*msg, *account), i); +} + +wasm_trap_t* +traceFloat_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + if (params->data[1].of.i32 > maxWasmDataLength) + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + + int i = 0; + auto const msg = getDataString(runtime, params, i); + if (!msg) + return hfResult(results, msg.error()); + + auto const number = getDataSlice(runtime, params, i); + if (!number) + return hfResult(results, number.error()); + + return returnResult( + runtime, params, results, hf->traceFloat(*msg, *number), i); +} + +wasm_trap_t* +traceAmount_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + if (params->data[1].of.i32 > maxWasmDataLength) + return hfResult(results, HostFunctionError::DATA_FIELD_TOO_LARGE); + + int i = 0; + auto const msg = getDataString(runtime, params, i); + if (!msg) + return hfResult(results, msg.error()); + + auto const amountSliceOpt = getDataSlice(runtime, params, i); + if (!amountSliceOpt) + return hfResult(results, amountSliceOpt.error()); + + auto const amountSlice = amountSliceOpt.value(); + auto serialIter = SerialIter(amountSlice); + + std::optional amount; + try + { + amount = STAmount(serialIter, sfGeneric); + } + catch (std::exception const&) + { + amount = std::nullopt; + } + if (!amount || !amount.value()) + return hfResult(results, HostFunctionError::INVALID_PARAMS); + + return returnResult( + runtime, params, results, hf->traceAmount(*msg, *amount), i); +} + +wasm_trap_t* +floatFromInt_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataInt64(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + i = 3; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 1; + return returnResult( + runtime, params, results, hf->floatFromInt(*x, *rounding), i); +} + +wasm_trap_t* +floatFromUint_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataUInt64(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + i = 4; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 2; + return returnResult( + runtime, params, results, hf->floatFromUint(*x, *rounding), i); +} + +wasm_trap_t* +floatSet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const exp = getDataInt32(runtime, params, i); + if (!exp) + return hfResult(results, exp.error()); + + auto const mant = getDataInt64(runtime, params, i); + if (!mant) + return hfResult(results, mant.error()); + + i = 4; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 2; + return returnResult( + runtime, params, results, hf->floatSet(*mant, *exp, *rounding), i); +} + +wasm_trap_t* +floatCompare_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const y = getDataSlice(runtime, params, i); + if (!y) + return hfResult(results, y.error()); + + return returnResult(runtime, params, results, hf->floatCompare(*x, *y), i); +} + +wasm_trap_t* +floatAdd_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const y = getDataSlice(runtime, params, i); + if (!y) + return hfResult(results, y.error()); + + i = 6; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 4; + return returnResult( + runtime, params, results, hf->floatAdd(*x, *y, *rounding), i); +} + +wasm_trap_t* +floatSubtract_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const y = getDataSlice(runtime, params, i); + if (!y) + return hfResult(results, y.error()); + + i = 6; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 4; + return returnResult( + runtime, params, results, hf->floatSubtract(*x, *y, *rounding), i); +} + +wasm_trap_t* +floatMultiply_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const y = getDataSlice(runtime, params, i); + if (!y) + return hfResult(results, y.error()); + + i = 6; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 4; + return returnResult( + runtime, params, results, hf->floatMultiply(*x, *y, *rounding), i); +} + +wasm_trap_t* +floatDivide_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const y = getDataSlice(runtime, params, i); + if (!y) + return hfResult(results, y.error()); + + i = 6; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 4; + return returnResult( + runtime, params, results, hf->floatDivide(*x, *y, *rounding), i); +} + +wasm_trap_t* +floatRoot_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const n = getDataInt32(runtime, params, i); + if (!n) + return hfResult(results, n.error()); + + i = 5; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 3; + return returnResult( + runtime, params, results, hf->floatRoot(*x, *n, *rounding), i); +} + +wasm_trap_t* +floatPower_wrap( + void* env, + wasm_val_vec_t const* params, + wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + auto const n = getDataInt32(runtime, params, i); + if (!n) + return hfResult(results, n.error()); + + i = 5; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 3; + return returnResult( + runtime, params, results, hf->floatPower(*x, *n, *rounding), i); +} + +wasm_trap_t* +floatLog_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results) +{ + auto* hf = reinterpret_cast(env); + auto const* runtime = reinterpret_cast(hf->getRT()); + + int i = 0; + auto const x = getDataSlice(runtime, params, i); + if (!x) + return hfResult(results, x.error()); + + i = 4; + auto const rounding = getDataInt32(runtime, params, i); + if (!rounding) + return hfResult(results, rounding.error()); + + i = 2; + return returnResult( + runtime, params, results, hf->floatLog(*x, *rounding), i); +} + +// LCOV_EXCL_START +namespace test { + +class MockInstanceWrapper +{ + wmem mem_; + +public: + MockInstanceWrapper(wmem memory) : mem_(memory) + { + } + + // Mock methods to simulate the behavior of InstanceWrapper + wmem + getMem() const + { + return mem_; + } +}; + +bool +testGetDataIncrement() +{ + wasm_val_t values[4]; + + std::array buffer = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + MockInstanceWrapper runtime(wmem{buffer.data(), buffer.size()}); + + { + // test int32_t + wasm_val_vec_t params = {1, &values[0], 1, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(42); + + int index = 0; + auto const result = getDataInt32(&runtime, ¶ms, index); + if (!result || result.value() != 42 || index != 1) + return false; + } + + { + // test int64_t + wasm_val_vec_t params = {1, &values[0], 1, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I64_VAL(1234); + + int index = 0; + auto const result = getDataInt64(&runtime, ¶ms, index); + if (!result || result.value() != 1234 || index != 1) + return false; + } + + { + // test SFieldCRef + wasm_val_vec_t params = {1, &values[0], 1, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(sfAccount.fieldCode); + + int index = 0; + auto const result = getDataSField(&runtime, ¶ms, index); + if (!result || result.value().get() != sfAccount || index != 1) + return false; + } + + { + // test Slice + wasm_val_vec_t params = {2, &values[0], 2, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(0); + values[1] = WASM_I32_VAL(3); + + int index = 0; + auto const result = getDataSlice(&runtime, ¶ms, index); + if (!result || result.value() != Slice(buffer.data(), 3) || index != 2) + return false; + } + + { + // test string + wasm_val_vec_t params = {2, &values[0], 2, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(0); + values[1] = WASM_I32_VAL(5); + + int index = 0; + auto const result = getDataString(&runtime, ¶ms, index); + if (!result || + result.value() != + std::string_view( + reinterpret_cast(buffer.data()), 5) || + index != 2) + return false; + } + + { + // test account + AccountID const id(calcAccountID( + generateKeyPair(KeyType::secp256k1, generateSeed("alice")).first)); + + wasm_val_vec_t params = {2, &values[0], 2, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(0); + values[1] = WASM_I32_VAL(id.bytes); + memcpy(&buffer[0], id.data(), id.bytes); + + int index = 0; + auto const result = getDataAccountID(&runtime, ¶ms, index); + if (!result || result.value() != id || index != 2) + return false; + } + + { + // test uint256 + + Hash h1 = sha512Half(Slice(buffer.data(), 8)); + wasm_val_vec_t params = {2, &values[0], 2, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(0); + values[1] = WASM_I32_VAL(h1.bytes); + memcpy(&buffer[0], h1.data(), h1.bytes); + + int index = 0; + auto const result = getDataUInt256(&runtime, ¶ms, index); + if (!result || result.value() != h1 || index != 2) + return false; + } + + { + // test Currency + + Currency const c = xrpCurrency(); + wasm_val_vec_t params = {2, &values[0], 2, sizeof(wasm_val_t), nullptr}; + + values[0] = WASM_I32_VAL(0); + values[1] = WASM_I32_VAL(c.bytes); + memcpy(&buffer[0], c.data(), c.bytes); + + int index = 0; + auto const result = getDataCurrency(&runtime, ¶ms, index); + if (!result || result.value() != c || index != 2) + return false; + } + + return true; +} + +} // namespace test +// LCOV_EXCL_STOP + +} // namespace ripple diff --git a/src/xrpld/app/wasm/detail/WasmVM.cpp b/src/xrpld/app/wasm/detail/WasmVM.cpp index e13b5885cf..d9dd67860d 100644 --- a/src/xrpld/app/wasm/detail/WasmVM.cpp +++ b/src/xrpld/app/wasm/detail/WasmVM.cpp @@ -34,12 +34,92 @@ namespace ripple { +static void +setCommonHostFunctions(HostFunctions* hfs, std::vector& i) +{ + // clang-format off + WASM_IMPORT_FUNC2(i, getLedgerSqn, "get_ledger_sqn", hfs, 60); + WASM_IMPORT_FUNC2(i, getParentLedgerTime, "get_parent_ledger_time", hfs, 60); + WASM_IMPORT_FUNC2(i, getParentLedgerHash, "get_parent_ledger_hash", hfs, 60); + WASM_IMPORT_FUNC2(i, getLedgerAccountHash, "get_ledger_account_hash", hfs, 60); + WASM_IMPORT_FUNC2(i, getLedgerTransactionHash, "get_ledger_tx_hash", hfs, 60); + WASM_IMPORT_FUNC2(i, getBaseFee, "get_base_fee", hfs, 60); + WASM_IMPORT_FUNC2(i, isAmendmentEnabled, "amendment_enabled", hfs, 60); + + WASM_IMPORT_FUNC2(i, cacheLedgerObj, "cache_ledger_obj", hfs, 5000); + WASM_IMPORT_FUNC2(i, getTxField, "get_tx_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjField, "get_current_ledger_obj_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getLedgerObjField, "get_ledger_obj_field", hfs, 70); + WASM_IMPORT_FUNC2(i, getTxNestedField, "get_tx_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedField, "get_current_ledger_obj_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getLedgerObjNestedField, "get_ledger_obj_nested_field", hfs, 110); + WASM_IMPORT_FUNC2(i, getTxArrayLen, "get_tx_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjArrayLen, "get_current_ledger_obj_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getLedgerObjArrayLen, "get_ledger_obj_array_len", hfs, 40); + WASM_IMPORT_FUNC2(i, getTxNestedArrayLen, "get_tx_nested_array_len", hfs, 70); + WASM_IMPORT_FUNC2(i, getCurrentLedgerObjNestedArrayLen, "get_current_ledger_obj_nested_array_len", hfs, 70); + WASM_IMPORT_FUNC2(i, getLedgerObjNestedArrayLen, "get_ledger_obj_nested_array_len", hfs, 70); + + WASM_IMPORT_FUNC2(i, checkSignature, "check_sig", hfs, 2000); + WASM_IMPORT_FUNC2(i, computeSha512HalfHash, "compute_sha512_half", hfs, 2000); + + WASM_IMPORT_FUNC2(i, accountKeylet, "account_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, ammKeylet, "amm_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, checkKeylet, "check_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, credentialKeylet, "credential_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, delegateKeylet, "delegate_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, depositPreauthKeylet, "deposit_preauth_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, didKeylet, "did_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, escrowKeylet, "escrow_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, lineKeylet, "line_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, mptIssuanceKeylet, "mpt_issuance_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, mptokenKeylet, "mptoken_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, nftOfferKeylet, "nft_offer_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, offerKeylet, "offer_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, oracleKeylet, "oracle_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, paychanKeylet, "paychan_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, permissionedDomainKeylet, "permissioned_domain_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, signersKeylet, "signers_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, ticketKeylet, "ticket_keylet", hfs, 350); + WASM_IMPORT_FUNC2(i, vaultKeylet, "vault_keylet", hfs, 350); + + WASM_IMPORT_FUNC2(i, getNFT, "get_nft", hfs, 1000); + WASM_IMPORT_FUNC2(i, getNFTIssuer, "get_nft_issuer", hfs, 60); + WASM_IMPORT_FUNC2(i, getNFTTaxon, "get_nft_taxon", hfs, 60); + WASM_IMPORT_FUNC2(i, getNFTFlags, "get_nft_flags", hfs, 60); + WASM_IMPORT_FUNC2(i, getNFTTransferFee, "get_nft_transfer_fee", hfs, 60); + WASM_IMPORT_FUNC2(i, getNFTSerial, "get_nft_serial", hfs, 60); + + WASM_IMPORT_FUNC (i, trace, hfs, 500); + WASM_IMPORT_FUNC2(i, traceNum, "trace_num", hfs, 500); + WASM_IMPORT_FUNC2(i, traceAccount, "trace_account", hfs, 500); + WASM_IMPORT_FUNC2(i, traceFloat, "trace_opaque_float", hfs, 500); + WASM_IMPORT_FUNC2(i, traceAmount, "trace_amount", hfs, 500); + + WASM_IMPORT_FUNC2(i, floatFromInt, "float_from_int", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatFromUint, "float_from_uint", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatSet, "float_set", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatCompare, "float_compare", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatAdd, "float_add", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatSubtract, "float_subtract", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatMultiply, "float_multiply", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatDivide, "float_divide", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatRoot, "float_root", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatPower, "float_pow", hfs, 1000); + WASM_IMPORT_FUNC2(i, floatLog, "float_log", hfs, 1000); + // clang-format on +} + std::vector createWasmImport(HostFunctions* hfs) { std::vector i; - // Add host functions here + if (hfs) + { + setCommonHostFunctions(hfs, i); + WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000); + } return i; }