mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-05 03:05:49 +00:00
Compare commits
3 Commits
fix-online
...
func-hooks
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6a27e895e | ||
|
|
b56dc38c22 | ||
|
|
e654219ba5 |
@@ -764,6 +764,7 @@ if (tests)
|
||||
src/test/app/ValidatorSite_test.cpp
|
||||
src/test/app/SetHook_test.cpp
|
||||
src/test/app/SetHookTSH_test.cpp
|
||||
src/test/app/SetHookV3_test.cpp
|
||||
src/test/app/Wildcard_test.cpp
|
||||
src/test/app/XahauGenesis_test.cpp
|
||||
src/test/app/tx/apply_test.cpp
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
using GuardLog =
|
||||
std::optional<std::reference_wrapper<std::basic_ostream<char>>>;
|
||||
|
||||
#define DEBUG_GUARD 0
|
||||
#define DEBUG_GUARD_VERBOSE 0
|
||||
#define DEBUG_GUARD_VERY_VERBOSE 0
|
||||
#define DEBUG_GUARD 1
|
||||
#define DEBUG_GUARD_VERBOSE 1
|
||||
#define DEBUG_GUARD_VERY_VERBOSE 1
|
||||
|
||||
#define GUARDLOG(logCode) \
|
||||
if (!guardLog) \
|
||||
@@ -901,23 +901,23 @@ validateGuards(
|
||||
// followed by an leb128 length
|
||||
int section_type = wasm[i++];
|
||||
|
||||
if (section_type == 0)
|
||||
{
|
||||
GUARDLOG(hook::log::CUSTOM_SECTION_DISALLOWED)
|
||||
<< "Malformed transaction. "
|
||||
<< "Hook contained a custom section, which is not allowed. Use "
|
||||
"cleaner.\n";
|
||||
return {};
|
||||
}
|
||||
// if (section_type == 0)
|
||||
// {
|
||||
// GUARDLOG(hook::log::CUSTOM_SECTION_DISALLOWED)
|
||||
// << "Malformed transaction. "
|
||||
// << "Hook contained a custom section, which is not allowed. Use "
|
||||
// "cleaner.\n";
|
||||
// return {};
|
||||
// }
|
||||
|
||||
if (section_type <= last_section_type)
|
||||
{
|
||||
GUARDLOG(hook::log::SECTIONS_OUT_OF_SEQUENCE)
|
||||
<< "Malformed transcation. "
|
||||
<< "Hook contained wasm sections that were either repeated or "
|
||||
"were out of sequence.\n";
|
||||
return {};
|
||||
}
|
||||
// if (section_type <= last_section_type)
|
||||
// {
|
||||
// GUARDLOG(hook::log::SECTIONS_OUT_OF_SEQUENCE)
|
||||
// << "Malformed transcation. "
|
||||
// << "Hook contained wasm sections that were either repeated or "
|
||||
// "were out of sequence.\n";
|
||||
// return {};
|
||||
// }
|
||||
|
||||
last_section_type = section_type;
|
||||
|
||||
@@ -1139,21 +1139,32 @@ validateGuards(
|
||||
}
|
||||
}
|
||||
|
||||
if (wasm[i] == 'm' && wasm[i + 1] == 'e' && wasm[i + 2] == 't' && wasm[i + 3] == 'h' && wasm[i + 4] == 'o' && wasm[i + 5] == 'd')
|
||||
{
|
||||
i += name_len;
|
||||
CHECK_SHORT_HOOK();
|
||||
i++;
|
||||
CHECK_SHORT_HOOK();
|
||||
hook_func_idx = parseLeb128(wasm, i, &i);
|
||||
CHECK_SHORT_HOOK();
|
||||
continue;
|
||||
}
|
||||
|
||||
i += name_len + 1;
|
||||
parseLeb128(wasm, i, &i);
|
||||
CHECK_SHORT_HOOK();
|
||||
}
|
||||
|
||||
// execution to here means export section was parsed
|
||||
if (!hook_func_idx)
|
||||
{
|
||||
GUARDLOG(hook::log::EXPORT_MISSING)
|
||||
<< "Malformed transaction. "
|
||||
<< "Hook did not export: "
|
||||
<< (!hook_func_idx ? "int64_t hook(uint32_t); " : "")
|
||||
<< "\n";
|
||||
return {};
|
||||
}
|
||||
// if (!hook_func_idx)
|
||||
// {
|
||||
// GUARDLOG(hook::log::EXPORT_MISSING)
|
||||
// << "Malformed transaction. "
|
||||
// << "Hook did not export: "
|
||||
// << (!hook_func_idx ? "int64_t hook(uint32_t); " : "")
|
||||
// << "\n";
|
||||
// return {};
|
||||
// }
|
||||
}
|
||||
else if (section_type == 3) // function section
|
||||
{
|
||||
@@ -1295,13 +1306,13 @@ validateGuards(
|
||||
else
|
||||
{
|
||||
// fail
|
||||
GUARDLOG(hook::log::FUNC_TYPE_INVALID)
|
||||
<< "Invalid function type. Not used by any import or "
|
||||
"hook/cbak func. "
|
||||
<< "Codesec: " << section_type << " "
|
||||
<< "Local: " << j << " "
|
||||
<< "Offset: " << i << "\n";
|
||||
return {};
|
||||
// GUARDLOG(hook::log::FUNC_TYPE_INVALID)
|
||||
// << "Invalid function type. Not used by any import or "
|
||||
// "hook/cbak func. "
|
||||
// << "Codesec: " << section_type << " "
|
||||
// << "Local: " << j << " "
|
||||
// << "Offset: " << i << "\n";
|
||||
// return {};
|
||||
}
|
||||
|
||||
int param_count = parseLeb128(wasm, i, &i);
|
||||
@@ -1320,11 +1331,11 @@ validateGuards(
|
||||
}
|
||||
else if (param_count != (*first_signature).get().size() - 1)
|
||||
{
|
||||
GUARDLOG(hook::log::FUNC_TYPE_INVALID)
|
||||
<< "Malformed transaction. "
|
||||
<< "Hook API: " << *first_name
|
||||
<< " has the wrong number of parameters.\n";
|
||||
return {};
|
||||
// GUARDLOG(hook::log::FUNC_TYPE_INVALID)
|
||||
// << "Malformed transaction. "
|
||||
// << "Hook API: " << *first_name
|
||||
// << " has the wrong number of parameters.\n";
|
||||
// return {};
|
||||
}
|
||||
|
||||
for (int k = 0; k < param_count; ++k)
|
||||
@@ -1388,12 +1399,12 @@ validateGuards(
|
||||
// most compilers out
|
||||
if (result_count != 1)
|
||||
{
|
||||
GUARDLOG(hook::log::FUNC_RETURN_COUNT)
|
||||
<< "Malformed transaction. "
|
||||
<< "Hook declares a function type that returns fewer "
|
||||
"or more than one value. "
|
||||
<< "\n";
|
||||
return {};
|
||||
// GUARDLOG(hook::log::FUNC_RETURN_COUNT)
|
||||
// << "Malformed transaction. "
|
||||
// << "Hook declares a function type that returns fewer "
|
||||
// "or more than one value. "
|
||||
// << "\n";
|
||||
// return {};
|
||||
}
|
||||
|
||||
// this can only ever be 1 in production, but in testing it may
|
||||
|
||||
@@ -723,6 +723,9 @@ public:
|
||||
uint32_t wasmParam,
|
||||
beast::Journal const& j)
|
||||
{
|
||||
|
||||
static WasmEdge_String _hookFunctionName = WasmEdge_StringCreateByCString("methodCreate");
|
||||
|
||||
// HookExecutor can only execute once
|
||||
assert(!spent);
|
||||
|
||||
@@ -761,7 +764,7 @@ public:
|
||||
vm.ctx,
|
||||
reinterpret_cast<const uint8_t*>(wasm),
|
||||
len,
|
||||
callback ? cbakFunctionName : hookFunctionName,
|
||||
_hookFunctionName,
|
||||
params,
|
||||
1,
|
||||
returns,
|
||||
|
||||
@@ -1776,7 +1776,7 @@ Transactor::operator()()
|
||||
|
||||
// write state if all chains executed successfully
|
||||
if (isTesSuccess(result))
|
||||
hook::finalizeHookState(stateMap, ctx_, ctx_.tx.getTransactionID());
|
||||
result = hook::finalizeHookState(stateMap, ctx_, ctx_.tx.getTransactionID());
|
||||
|
||||
// write hook results
|
||||
// this happens irrespective of whether final result was a tesSUCCESS
|
||||
@@ -2031,8 +2031,9 @@ Transactor::operator()()
|
||||
for (auto const& [accID, hookHashes] : aawMap)
|
||||
doAgainAsWeak(accID, hookHashes, stateMap, weakResults, proMeta);
|
||||
|
||||
result = hook::finalizeHookState(stateMap, ctx_, ctx_.tx.getTransactionID());
|
||||
|
||||
// write hook results
|
||||
hook::finalizeHookState(stateMap, ctx_, ctx_.tx.getTransactionID());
|
||||
for (auto& weakResult : weakResults)
|
||||
hook::finalizeHookResult(weakResult, ctx_, isTesSuccess(result));
|
||||
|
||||
|
||||
1
src/test/app/.env
Normal file
1
src/test/app/.env
Normal file
@@ -0,0 +1 @@
|
||||
HOOKS_COMPILE_HOST=http://localhost:9000
|
||||
140
src/test/app/SetHookV3_test.cpp
Normal file
140
src/test/app/SetHookV3_test.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012-2016 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 <ripple/app/hook/Enum.h>
|
||||
#include <ripple/app/ledger/LedgerMaster.h>
|
||||
#include <ripple/app/tx/impl/SetHook.h>
|
||||
#include <ripple/protocol/TxFlags.h>
|
||||
#include <ripple/protocol/jss.h>
|
||||
#include <test/app/SetHook_wasm.h>
|
||||
#include <test/jtx.h>
|
||||
#include <test/jtx/hook.h>
|
||||
#include <unordered_map>
|
||||
#include <iostream>
|
||||
#include <ripple/json/json_reader.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
namespace test {
|
||||
|
||||
class SetHookV3_test : public beast::unit_test::suite
|
||||
{
|
||||
private:
|
||||
// helper
|
||||
void static overrideFlag(Json::Value& jv)
|
||||
{
|
||||
jv[jss::Flags] = hsfOVERRIDE;
|
||||
}
|
||||
|
||||
public:
|
||||
// This is a large fee, large enough that we can set most small test hooks
|
||||
// without running into fee issues we only want to test fee code specifically in
|
||||
// fee unit tests, the rest of the time we want to ignore it.
|
||||
#define HSFEE fee(100'000'000)
|
||||
#define M(m) memo(m, "", "")
|
||||
std::string
|
||||
loadHook()
|
||||
{
|
||||
std::string name = "/Users/darkmatter/projects/ledger-works/xahaud/src/test/app/wasm/custom.wasm";
|
||||
if (!std::filesystem::exists(name)) {
|
||||
std::cout << "File does not exist: " << name << "\n";
|
||||
return "";
|
||||
}
|
||||
|
||||
std::ifstream hookFile(name, std::ios::binary);
|
||||
|
||||
if (!hookFile)
|
||||
{
|
||||
std::cout << "Failed to open file: " << name << "\n";
|
||||
return "";
|
||||
}
|
||||
|
||||
// Read the file into a vector
|
||||
std::vector<char> buffer((std::istreambuf_iterator<char>(hookFile)), std::istreambuf_iterator<char>());
|
||||
|
||||
// Check if the buffer is empty
|
||||
if (buffer.empty()) {
|
||||
std::cout << "File is empty or could not be read properly.\n";
|
||||
return "";
|
||||
}
|
||||
|
||||
return strHex(buffer);
|
||||
}
|
||||
|
||||
void
|
||||
testSimple(FeatureBitset features)
|
||||
{
|
||||
testcase("Test simple");
|
||||
|
||||
using namespace jtx;
|
||||
|
||||
// Env env{*this, features};
|
||||
Env env{*this, envconfig(), features, nullptr,
|
||||
beast::severities::kTrace
|
||||
};
|
||||
|
||||
auto const alice = Account{"alice"};
|
||||
auto const gw = Account{"gateway"};
|
||||
auto const USD = gw["USD"];
|
||||
env.fund(XRP(10000), alice, gw);
|
||||
env.close();
|
||||
env.trust(USD(100000), alice);
|
||||
env.close();
|
||||
env(pay(gw, alice, USD(10000)));
|
||||
env.close();
|
||||
|
||||
std::string hook = loadHook();
|
||||
std::cout << "Hook: " << hook << "\n";
|
||||
|
||||
// install the hook on alice
|
||||
env(ripple::test::jtx::hook(alice, {{hso(hook, overrideFlag)}}, 0),
|
||||
M("set simple"),
|
||||
HSFEE);
|
||||
env.close();
|
||||
|
||||
// // invoke the hook
|
||||
// Json::Value jv = invoke::invoke(alice);
|
||||
// Json::Value params{Json::arrayValue};
|
||||
// Json::Value pv;
|
||||
// Json::Value piv;
|
||||
// piv[jss::HookParameterName] = "736F6D6566756E63";
|
||||
// piv[jss::HookParameterValue] = "736F6D6566756E63";
|
||||
// pv[jss::HookParameter] = piv;
|
||||
// params[0u] = pv;
|
||||
// jv[jss::HookParameters] = params;
|
||||
env(invoke::invoke(alice), M("test simple"), fee(XRP(1)));
|
||||
}
|
||||
|
||||
void
|
||||
testWithFeatures(FeatureBitset features)
|
||||
{
|
||||
testSimple(features);
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
using namespace test::jtx;
|
||||
auto const sa = supported_amendments();
|
||||
testWithFeatures(sa);
|
||||
}
|
||||
};
|
||||
BEAST_DEFINE_TESTSUITE(SetHookV3, app, ripple);
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
#undef M
|
||||
10
src/test/app/contracts/base.c
Normal file
10
src/test/app/contracts/base.c
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#include "hookapi.h"
|
||||
|
||||
int64_t hook(uint32_t reserved) {
|
||||
TRACESTR("Base.c: Called.");
|
||||
_g(1,1);
|
||||
return accept(SBUF("base: Finished."), __LINE__);;
|
||||
}
|
||||
20
src/test/app/contracts/custom.c
Normal file
20
src/test/app/contracts/custom.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#include "hookapi.h"
|
||||
// #define DEBUG 1
|
||||
// #define SBUF(str) (uint32_t)(str), sizeof(str)
|
||||
// #define TRACESTR(v) if (DEBUG) trace((uint32_t)(#v), (uint32_t)(sizeof(#v) - 1), (uint32_t)(v), sizeof(v), 0);
|
||||
// // hook developers should use this guard macro, simply GUARD(<maximum iterations>)
|
||||
// #define GUARD(maxiter) _g((1ULL << 31U) + __LINE__, (maxiter)+1)
|
||||
// #define GUARDM(maxiter, n) _g(( (1ULL << 31U) + (__LINE__ << 16) + n), (maxiter)+1)
|
||||
|
||||
// hooks-cli compile-c contracts/custom.c build
|
||||
|
||||
int64_t methodCreate(uint32_t reserved) {
|
||||
TRACESTR("somefunc.c: Called.");
|
||||
for (int i = 0; GUARD(10), i < 10; i++) {
|
||||
TRACESTR("somefunc.c: Iterating.");
|
||||
}
|
||||
return accept(SBUF("somefunc.c: Finished."), __LINE__);
|
||||
}
|
||||
Reference in New Issue
Block a user