Skip to content

Commit

Permalink
Add wasmtime
Browse files Browse the repository at this point in the history
  • Loading branch information
oleks-rip committed Feb 11, 2025
1 parent 4bdf1cd commit 660fc05
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 10 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ find_package(nudb REQUIRED)
find_package(date REQUIRED)
find_package(xxHash REQUIRED)
find_package(wasmedge REQUIRED)
find_package(wasmtime REQUIRED)

target_link_libraries(ripple_libs INTERFACE
ed25519::ed25519
Expand Down
1 change: 1 addition & 0 deletions cmake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ target_link_libraries(xrpl.imports.main
xxHash::xxhash
$<$<BOOL:${voidstar}>:antithesis-sdk-cpp>
wasmedge::wasmedge
wasmtime::wasmtime
)

include(add_module)
Expand Down
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Xrpl(ConanFile):
'xxhash/0.8.2',
'zlib/1.3.1',
'wasmedge/0.14.1',
'wasmtime/21.0.0',
]

tool_requires = [
Expand Down
10 changes: 10 additions & 0 deletions src/test/app/Wasm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,23 @@ struct Wasm_test : public beast::unit_test::suite
auto wasmStr = boost::algorithm::unhex(std::string(wasmHex));
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
std::string funcName("mock_escrow");

auto re = runEscrowWasm(wasm, funcName, 15);
if (BEAST_EXPECT(re.has_value()))
BEAST_EXPECT(re.value());

re = runEscrowWasm(wasm, funcName, 11);
if (BEAST_EXPECT(re.has_value()))
BEAST_EXPECT(!re.value());

testcase("escrow wasmTime P0 test");
re = runEscrowWasmWTime(wasm, funcName, 15);
if (BEAST_EXPECT(re.has_value()))
BEAST_EXPECT(re.value());

re = runEscrowWasmWTime(wasm, funcName, 11);
if (BEAST_EXPECT(re.has_value()))
BEAST_EXPECT(!re.value());
}

void
Expand Down
116 changes: 108 additions & 8 deletions src/xrpld/app/misc/WasmVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

#include <xrpld/app/misc/WasmVM.h>

// WasmVM::WasmVM(beast::Journal j)
// : j_(j)
//{
// }
#include <memory>

namespace ripple {

Expected<bool, TER>
runEscrowWasm(
std::vector<uint8_t> const& wasmCode,
Expand Down Expand Up @@ -67,6 +65,102 @@ runEscrowWasm(
return Unexpected<TER>(tecFAILED_PROCESSING);
}

static void
print_wasmi_error(
const char* message,
wasmtime_error_t* error,
wasm_trap_t* trap)
{
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL)
{
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
}
else
{
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
}

Expected<bool, TER>
runEscrowWasmWTime(
std::vector<uint8_t> const& wasmCode,
std::string const& funcName,
int32_t input)
{
wasmtime_error_t* error = nullptr;

std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)> engine(
wasm_engine_new(), &wasm_engine_delete);
std::unique_ptr<wasmtime_store_t, decltype(&wasmtime_store_delete)> store(
wasmtime_store_new(engine.get(), NULL, NULL), &wasmtime_store_delete);
wasmtime_context_t* context = wasmtime_store_context(store.get());

// Now that we've got our binary webassembly we can compile our module.
std::unique_ptr<wasmtime_module_t, decltype(&wasmtime_module_delete)>
module(nullptr, &wasmtime_module_delete);

{
wasmtime_module_t* m = NULL;
error = wasmtime_module_new(
engine.get(), wasmCode.data(), wasmCode.size(), &m);
if (error != NULL)
{
print_wasmi_error("failed to compile module", error, NULL);
return Unexpected<TER>(tecFAILED_PROCESSING);
}

module = decltype(module)(m, &wasmtime_module_delete);
}

// instantiate our module
wasm_trap_t* trap = NULL;
wasmtime_instance_t instance;
error =
wasmtime_instance_new(context, module.get(), NULL, 0, &instance, &trap);
if (error != NULL || trap != NULL)
{
print_wasmi_error("failed to instantiate", error, trap);
return Unexpected<TER>(tecFAILED_PROCESSING);
}

// Lookup our export function
wasmtime_extern_t wasmFunc;
if (!wasmtime_instance_export_get(
context, &instance, funcName.c_str(), funcName.size(), &wasmFunc))
{
printf("Can't find: %s\n", funcName.c_str());
return Unexpected<TER>(tecFAILED_PROCESSING);
}
assert(wasmFunc.kind == WASMTIME_EXTERN_FUNC);

// And call it!
wasmtime_val_t params[1];
params[0].kind = WASMTIME_I32;
params[0].of.i32 = input;
wasmtime_val_t results[1];
error = wasmtime_func_call(
context, &wasmFunc.of.func, params, 1, results, 1, &trap);
if (error != NULL || trap != NULL)
{
print_wasmi_error("failed to call func", error, trap);
return Unexpected<TER>(tecFAILED_PROCESSING);
}
assert(results[0].kind == WASMTIME_I32);
// printf("Result: %d\n", results[0].of.i32);

bool re = false;
if (results[0].of.i32 != 0)
re = true;

return re;
}

Expected<bool, TER>
runEscrowWasm(
std::vector<uint8_t> const& wasmCode,
Expand Down Expand Up @@ -395,9 +489,15 @@ runEscrowWasmP4(
}
}

WasmEdge_Result get_ledger_sqn(void * data, const WasmEdge_CallingFrameContext *,
const WasmEdge_Value *In, WasmEdge_Value *Out) {
Out[0] = WasmEdge_ValueGenI32(((LedgerDataProvider *)data)->get_ledger_sqn());
WasmEdge_Result
get_ledger_sqn(
void* data,
const WasmEdge_CallingFrameContext*,
const WasmEdge_Value* In,
WasmEdge_Value* Out)
{
Out[0] =
WasmEdge_ValueGenI32(((LedgerDataProvider*)data)->get_ledger_sqn());
return WasmEdge_Result_Success;
}

Expand All @@ -408,7 +508,7 @@ runEscrowWasm(
LedgerDataProvider* ledgerDataProvider)
{
WasmEdge_VMContext* VMCxt = WasmEdge_VMCreate(NULL, NULL);
{//register host function
{ // register host function
WasmEdge_ValType ReturnList[1] = {WasmEdge_ValTypeGenI32()};
WasmEdge_FunctionTypeContext* HostFType =
WasmEdge_FunctionTypeCreate(NULL, 0, ReturnList, 1);
Expand Down
15 changes: 13 additions & 2 deletions src/xrpld/app/misc/WasmVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#define RIPPLE_APP_MISC_WASMVM_H_INLCUDED

#include <xrpl/basics/Expected.h>
// #include <xrpl/beast/utility/Journal.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/protocol/TER.h>

#include <wasmedge/wasmedge.h>

#include <wasm.h>
#include <wasmtime.h>

namespace ripple {

Expected<bool, TER>
Expand All @@ -33,6 +36,14 @@ runEscrowWasm(
std::string const& funcName,
int32_t input);

Expected<bool, TER>
runEscrowWasmWTime(
std::vector<uint8_t> const& wasmCode,
std::string const& funcName,
int32_t input);



Expected<bool, TER>
runEscrowWasm(
std::vector<uint8_t> const& wasmCode,
Expand Down Expand Up @@ -81,4 +92,4 @@ runEscrowWasm(
//
// private:
// beast::Journal j_;
// };
// };

0 comments on commit 660fc05

Please sign in to comment.