Compare commits

..

4 Commits

Author SHA1 Message Date
TimothyBanks
1556bb7795 feat: smart-escrow/versioning 2026-07-14 15:57:18 -04:00
TimothyBanks
6082e0d9cd feat: smart-escrow/versioning 2026-07-14 15:54:48 -04:00
TimothyBanks
72b9918168 feat: smart-escrow/versioning 2026-07-14 15:51:31 -04:00
TimothyBanks
730061f69a feat: Encode version in custom secton 2026-07-13 14:27:44 -04:00
4 changed files with 137 additions and 1 deletions

View File

@@ -15,7 +15,7 @@
// Add new amendments to the top of this list.
// Keep it sorted in reverse chronological order.
XRPL_FEATURE(SmartEscrow, Supported::Yes, VoteBehavior::DefaultNo)
XRPL_FEATURE(SmartEscrow, Supported::No, VoteBehavior::DefaultNo)
XRPL_FIX (Cleanup3_2_0, Supported::No, VoteBehavior::DefaultNo)
XRPL_FEATURE(MPTokensV2, Supported::No, VoteBehavior::DefaultNo)
XRPL_FIX (Cleanup3_1_3, Supported::Yes, VoteBehavior::DefaultYes)

View File

@@ -799,6 +799,98 @@ 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,
@@ -818,6 +910,13 @@ 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);

View File

@@ -227,6 +227,28 @@ 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()
{
@@ -1549,6 +1571,8 @@ struct Wasm_test : public beast::unit_test::Suite
{
using namespace test::jtx;
testVersion();
testGetDataHelperFunctions();
testWasmLib();
testBadWasm();

View File

@@ -0,0 +1,13 @@
(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")
)