Skip to content

Draft: Add support for Lua-based TX filtering #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 28.x-knots
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,9 @@ AM_CONDITIONAL([EMBEDDED_LIBSECP256K1],[test x$system_libsecp256k1 = xno])
AC_SUBST(libsecp256k1_CFLAGS)
AC_SUBST(libsecp256k1_LIBS)

CPPFLAGS="-I/opt/homebrew/opt/lua/include/lua $CPPFLAGS"
LIBS="-L/opt/homebrew/opt/lua/lib -llua -lm $LIBS"
Comment on lines +1468 to +1469
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This certainly isn't portable.


if test "$enable_wallet" != "no"; then
dnl Check for libdb_cxx only if wallet enabled
if test "$use_bdb" != "no"; then
Expand Down
1 change: 1 addition & 0 deletions scripts/demo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return true
1 change: 1 addition & 0 deletions scripts/demo2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two demos that do the same thing?

2 changes: 1 addition & 1 deletion src/kernel/checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool Clang_IndVarSimplify_Bug_SanityCheck() {
}
last = *it;
}
return false;
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the sanity check. If the test is failing for you, you should get a compiler that doesn't have the bug. It could very well impact other code!

}

util::Result<void> SanityChecks(const Context&)
Expand Down
38 changes: 38 additions & 0 deletions src/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@
#include <script/script.h>
#include <script/solver.h>
#include <serialize.h>
#include <sol/sol.hpp>
#include <span.h>

#include <algorithm>
#include <cstddef>
#include <filesystem>
#include <utility>
#include <vector>

unsigned int g_script_size_policy_limit{DEFAULT_SCRIPT_SIZE_POLICY_LIMIT};

bool lua_init;
sol::state lua;

void EnsureLuaInit()
{
if(lua_init) return;

lua.open_libraries(sol::lib::base, sol::lib::package);

lua_init = true;
}

CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
{
// "Dust" is defined in terms of dustRelayFee,
Expand Down Expand Up @@ -192,6 +206,30 @@ bool IsStandardTx(const CTransaction& tx, const kernel::MemPoolOptions& opts, st
MaybeReject("multi-op-return");
}

// TODO: TryAcceptTx(const CTransaction& tx, const kernel::MemPoolOptions& opts, std::string& out_reason)
// TODO: Call this after calling `IsStandardTx` (in validation.cpp)
{
EnsureLuaInit();

// TODO: Make script path configuration option
// TODO: Order scripts in some way (e.g., by name)
for (const auto& entry : std::filesystem::directory_iterator("/Users/jfoura/CLionProjects/bitcoin/scripts/")) {
// TODO: Pass tx, opts as parameters (in some form) to the script
bool accept = lua.script_file(entry.path());

if(!accept)
{
out_reason = std::string("TX was rejected by script: ") + entry.path().string();

std::cout << out_reason << std::endl;

return false;
}
}

std::cout << "TX was accepted by all scripts" << std::endl;
}

return true;
}

Expand Down
53 changes: 53 additions & 0 deletions src/sol/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// The MIT License (MIT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to use the system-installed sol if possible (optional at compile time). Or at least a subtree.


// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// This file was generated with a script.
// Generated 2022-06-25 08:14:19.336233 UTC
// This header was generated with sol v3.3.0 (revision eba86625)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_CONFIG_HPP
#define SOL_SINGLE_CONFIG_HPP

// beginning of sol/config.hpp

/* Base, empty configuration file!

To override, place a file in your include paths of the form:

. (your include path here)
| sol (directory, or equivalent)
| config.hpp (your config.hpp file)

So that when sol2 includes the file

#include <sol/config.hpp>

it gives you the configuration values you desire. Configuration values can be
seen in the safety.rst of the doc/src, or at
https://sol2.readthedocs.io/en/latest/safety.html ! You can also pass them through
the build system, or the command line options of your compiler.

*/

// end of sol/config.hpp

#endif // SOL_SINGLE_CONFIG_HPP
Loading