mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-15 19:20:25 +00:00
Compare commits
3 Commits
smart-escr
...
smart-escr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1825f38a10 | ||
|
|
e70623d0cb | ||
|
|
19631a6d92 |
@@ -799,98 +799,6 @@ WasmiEngine::run(
|
||||
return Unexpected<TER>(tecFAILED_PROCESSING);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
struct CustomSection
|
||||
{
|
||||
std::string_view name;
|
||||
std::span<char const> payload;
|
||||
};
|
||||
|
||||
struct Version
|
||||
{
|
||||
std::string_view name;
|
||||
std::string_view version;
|
||||
};
|
||||
|
||||
uint32_t
|
||||
readLEB128(Bytes const& wasmCode, size_t& offset)
|
||||
{
|
||||
auto result = uint32_t{};
|
||||
auto shift = uint32_t{};
|
||||
while (true)
|
||||
{
|
||||
auto byte = wasmCode[offset++];
|
||||
result |= (byte & 0x7F) << shift;
|
||||
if ((byte & 0x80) == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Filter>
|
||||
void
|
||||
filterCustomSections(Bytes const& wasmCode, Filter&& filter)
|
||||
{
|
||||
auto offset = size_t{8}; // Skip Magic number and Version
|
||||
|
||||
while (offset < wasmCode.size())
|
||||
{
|
||||
auto sectionId = wasmCode[offset++];
|
||||
auto sectionSize = readLEB128(wasmCode, offset);
|
||||
auto nextSection = offset + sectionSize;
|
||||
|
||||
if (sectionId == 0)
|
||||
{
|
||||
auto customSection = CustomSection{};
|
||||
|
||||
auto size = readLEB128(wasmCode, offset);
|
||||
customSection.name =
|
||||
std::string_view{reinterpret_cast<char const*>(wasmCode.data()) + offset, size};
|
||||
offset += size;
|
||||
|
||||
size = nextSection - offset;
|
||||
customSection.payload = std::span<char const>{
|
||||
reinterpret_cast<char const*>(wasmCode.data()) + offset, size};
|
||||
|
||||
if (filter(customSection))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
offset = nextSection;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Version>
|
||||
extractVersionInfo(Bytes const& wasmCode)
|
||||
{
|
||||
static constexpr auto kCommonLib = "xrpl-common-stdlib";
|
||||
static constexpr auto kEscrowLib = "xrpl-escrow-stdlib";
|
||||
|
||||
auto versions = std::vector<Version>{};
|
||||
filterCustomSections(wasmCode, [&](auto const& section) {
|
||||
if (section.name == kCommonLib || section.name == kEscrowLib)
|
||||
{
|
||||
versions.emplace_back(
|
||||
Version{
|
||||
.name = section.name,
|
||||
.version = std::string_view{
|
||||
section.payload.data(),
|
||||
section.payload.size()}});
|
||||
}
|
||||
|
||||
// Just read until we have found all the information we are looking for.
|
||||
return versions.size() == 2;
|
||||
});
|
||||
return versions;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Expected<WasmResult<int32_t>, TER>
|
||||
WasmiEngine::runHlp(
|
||||
Bytes const& wasmCode,
|
||||
@@ -910,13 +818,6 @@ WasmiEngine::runHlp(
|
||||
if (!hfs.checkSelf())
|
||||
throw std::runtime_error("hfs isn't clean");
|
||||
|
||||
auto versionInfo = extractVersionInfo(wasmCode);
|
||||
|
||||
for (auto const& version : versionInfo)
|
||||
{
|
||||
std::cout << version.name << " " << version.version << "\n";
|
||||
}
|
||||
|
||||
// Create and instantiate the module.
|
||||
[[maybe_unused]] int const m = addModule(wasmCode, true, imports, gas);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -227,28 +228,6 @@ struct Wasm_test : public beast::unit_test::Suite
|
||||
checkResult(re, 6'912, 59);
|
||||
}
|
||||
|
||||
void
|
||||
testVersion()
|
||||
{
|
||||
testcase("wasm lib test");
|
||||
// wat2wasm --enable-annotations mymodule.wat -o mymodule.wasm
|
||||
// xxd -p mymodule.wasm | tr -d '\n'
|
||||
static auto const kWasmModule = hexToBytes(
|
||||
"0061736d010000000105016000017f03020100040401700000070a010666696e69736800000a0601040041"
|
||||
"010b0018127872706c2d657363726f772d7374646c6962342e352e360018127872706c2d636f6d6d6f6e2d"
|
||||
"7374646c6962312e322e33");
|
||||
|
||||
auto& vm = WasmEngine::instance();
|
||||
|
||||
HostFunctions hfs;
|
||||
ImportVec imports;
|
||||
WasmImpFunc<Add_proto>(imports, "func-add", reinterpret_cast<void*>(&add), &hfs);
|
||||
|
||||
auto re = vm.run(kWasmModule, hfs, 10'000'000, "finish", wasmParams(1234), imports);
|
||||
|
||||
checkResult(re, 1, 59);
|
||||
}
|
||||
|
||||
void
|
||||
testBadWasm()
|
||||
{
|
||||
@@ -1566,12 +1545,72 @@ struct Wasm_test : public beast::unit_test::Suite
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Functor>
|
||||
void
|
||||
perf(size_t runs, Functor&& f)
|
||||
{
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::steady_clock;
|
||||
|
||||
// Warm up first.
|
||||
for (auto i = size_t{}; i < 10; ++i)
|
||||
{
|
||||
BEAST_EXPECT(f());
|
||||
}
|
||||
|
||||
auto totalTime = uint64_t{};
|
||||
for (auto i = size_t{}; i < runs; ++i)
|
||||
{
|
||||
auto const start = steady_clock::now();
|
||||
auto result = f();
|
||||
auto const end = steady_clock::now();
|
||||
totalTime += duration_cast<std::chrono::nanoseconds>(end - start).count();
|
||||
BEAST_EXPECT(result);
|
||||
}
|
||||
log << "Average time for " << runs << " runs: " << (totalTime / runs) << " ns\n";
|
||||
}
|
||||
|
||||
void
|
||||
perfEscrowFinish()
|
||||
{
|
||||
testcase("perf escrow finish");
|
||||
using namespace test::jtx;
|
||||
|
||||
static constexpr auto kRuns = 1000;
|
||||
|
||||
auto const wasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
Env env{*this};
|
||||
auto hfns = TestHostFunctions{env, 0};
|
||||
|
||||
perf(kRuns, [&] {
|
||||
return runEscrowWasm(wasm, hfns, 1'000'000, escrowFunctionName, {}).has_value();
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
perfEscrowCreate()
|
||||
{
|
||||
testcase("perf escrow create");
|
||||
using namespace test::jtx;
|
||||
|
||||
static constexpr auto kRuns = 1000;
|
||||
|
||||
auto const wasm = hexToBytes(kAllHostFunctionsWasmHex);
|
||||
|
||||
Env const env{*this};
|
||||
auto mock = HostFunctions{env.journal};
|
||||
|
||||
perf(kRuns, [&] { return !preflightEscrowWasm(wasm, mock, escrowFunctionName); });
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
using namespace test::jtx;
|
||||
|
||||
testVersion();
|
||||
perfEscrowFinish();
|
||||
perfEscrowCreate();
|
||||
|
||||
testGetDataHelperFunctions();
|
||||
testWasmLib();
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
(module
|
||||
;; Define a table with exactly 0 entries
|
||||
(table 0 funcref)
|
||||
|
||||
;; Standard finish function
|
||||
(func $finish (result i32)
|
||||
i32.const 1
|
||||
)
|
||||
(export "finish" (func $finish))
|
||||
|
||||
(@custom "xrpl-escrow-stdlib" "4.5.6")
|
||||
(@custom "xrpl-common-stdlib" "1.2.3")
|
||||
)
|
||||
Reference in New Issue
Block a user