Skip to content

Commit

Permalink
Merge pull request #10 from VeriBlock/feature/fix-num-pop-tx
Browse files Browse the repository at this point in the history
Make a limit on number of pop tx in block
  • Loading branch information
Warchant authored Jan 22, 2020
2 parents d382da3 + c7751f5 commit 81a9717
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 53 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ on:
- master
- develop
- feature/*
pull_request:
branches:
- master
- develop

jobs:
build:
Expand Down
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,13 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
fi

# fakeit does not work with GCC's devirtualization, which is enabled with -O2 or higher
FAKEIT_CXXFLAGS=
THIRD_PARTY_INCLUDE="-isystem ../third_party/include"

FAKEIT_CXXFLAGS=$THIRD_PARTY_INCLUDE
AX_CHECK_COMPILE_FLAG([-fno-devirtualize],[FAKEIT_CXXFLAGS="$FAKEIT_CXXFLAGS -fno-devirtualize"])
AX_CHECK_COMPILE_FLAG([-Wno-ignored-optimization-argument],[FAKEIT_CXXFLAGS="$FAKEIT_CXXFLAGS -Wno-ignored-optimization-argument"])
AC_SUBST(FAKEIT_CXXFLAGS)
CXXFLAGS="$CXXFLAGS $FAKEIT_CXXFLAGS"

enable_hwcrc32=no
enable_sse41=no
Expand Down
4 changes: 1 addition & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ AM_CPPFLAGS = $(DEBUG_CPPFLAGS) $(HARDENED_CPPFLAGS)
AM_LIBTOOLFLAGS = --preserve-dup-deps
EXTRA_LIBRARIES =

FAKEIT_DIR = $(top_srcdir)/third_party/include
FAKEIT_HEADER = $(FAKEIT_DIR)/fakeit.hpp
THIRD_PARTY_INCLUDE = -isystem $(FAKEIT_DIR)


if EMBEDDED_UNIVALUE
LIBUNIVALUE = univalue/libunivalue.la
Expand Down
4 changes: 1 addition & 3 deletions src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

THIRD_PARTY_INCLUDE = -isystem $(top_srcdir)/third_party/include

bin_PROGRAMS += bench/bench_bitcoin
BENCH_SRCDIR = bench
BENCH_BINARY = bench/bench_bitcoin$(EXEEXT)
Expand Down Expand Up @@ -46,7 +44,7 @@ bench_bench_bitcoin_SOURCES = \
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)

bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/
bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) $(THIRD_PARTY_INCLUDE)
bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) $(FAKEIT_CXXFLAGS)
bench_bench_bitcoin_LDADD = \
$(LIBBITCOIN_SERVER) \
$(LIBBITCOIN_WALLET) \
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ qt_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
endif
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBLEVELDB_SSE42) $(LIBMEMENV) \
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(BDB_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(GRPC_LIBS)
qt_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
qt_bitcoin_qt_LIBTOOLFLAGS = $(AM_LIBTOOLFLAGS) --tag CXX

Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.qttest.include
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) $(LIBBITCOIN_CONSENSUS) $(LIBBITCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) \
$(LIBLEVELDB_SSE42) $(LIBMEMENV) $(BOOST_LIBS) $(QT_DBUS_LIBS) $(QT_TEST_LIBS) $(QT_LIBS) \
$(QR_LIBS) $(BDB_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(GRPC_LIBS)
qt_test_test_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
qt_test_test_bitcoin_qt_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)

Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ BITCOIN_TEST_SUITE += \
endif

test_test_bitcoin_SOURCES = $(BITCOIN_TEST_SUITE) $(BITCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS) $(THIRD_PARTY_INCLUDE)
test_test_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS) $(FAKEIT_CXXFLAGS)
test_test_bitcoin_LDADD = $(LIBTEST_UTIL)
if ENABLE_WALLET
test_test_bitcoin_LDADD += $(LIBBITCOIN_WALLET)
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST_UTIL_H = \
test/util/transaction_utils.h \
test/util/wallet.h

libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS) $(THIRD_PARTY_INCLUDE)
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS) $(FAKEIT_CXXFLAGS)
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) $(FAKEIT_CXXFLAGS)
libtest_util_a_SOURCES = \
test/util/blockfilter.cpp \
Expand Down
4 changes: 1 addition & 3 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static bool AppInit(int argc, char* argv[])
VeriBlock::InitPopService(
gArgs.GetArg("-althost", "127.0.0.1"),
gArgs.GetArg("-altport", "19012"),
gArgs.GetBoolArg("-altautoconfig", true));
gArgs.GetBoolArg("-altautoconfig", false));

if (!AppInitBasicSetup())
{
Expand Down Expand Up @@ -155,8 +155,6 @@ static bool AppInit(int argc, char* argv[])
return false;
}
fRet = AppInitMain(node);
// connman is initialized in AppInitMain
VeriBlock::InitRpcService(node.connman.get());
}
catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInit()");
Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,8 @@ bool AppInitMain(NodeContext& node)
assert(!node.connman);
node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));

VeriBlock::InitRpcService(node.connman.get());

node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), scheduler));
RegisterValidationInterface(node.peer_logic.get());

Expand Down
10 changes: 10 additions & 0 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <config/bitcoin-config.h>
#endif

#include <vbk/init.hpp>
#include <node/context.h>

#include <qt/bitcoin.h>
#include <qt/bitcoingui.h>

Expand Down Expand Up @@ -406,6 +409,8 @@ int GuiMain(int argc, char* argv[])
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
VeriBlock::InitUtilService();
VeriBlock::InitConfig();
SetupEnvironment();
util::ThreadSetInternalName("main");

Expand Down Expand Up @@ -521,6 +526,11 @@ int GuiMain(int argc, char* argv[])
PaymentServer::ipcParseCommandLine(*node, argc, argv);
#endif

VeriBlock::InitPopService(
gArgs.GetArg("-althost", "127.0.0.1"),
gArgs.GetArg("-altport", "19012"),
gArgs.GetBoolArg("-altautoconfig", false));

QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(Params().NetworkIDString()));
assert(!networkStyle.isNull());
// Allow for separate UI settings for testnets
Expand Down
12 changes: 10 additions & 2 deletions src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ TestChain100Setup::TestChain100Setup()

// Create a new block with just given transactions, coinbase paying to
// scriptPubKey, and try to add it to the current chain.
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) {
return this->CreateAndProcessBlock(txns, scriptPubKey, nullptr);
}

CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool* isBlockValid)
{
const CChainParams& chainparams = Params();
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
Expand All @@ -196,7 +200,11 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;

std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
ProcessNewBlock(chainparams, shared_pblock, true, nullptr);

bool isValid = ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
if(isBlockValid != nullptr) {
*isBlockValid = isValid;
}

CBlock result = block;
return result;
Expand Down
3 changes: 3 additions & 0 deletions src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ struct TestChain100Setup : public RegTestingSetup {
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);

CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey, bool* isBlockValid);

~TestChain100Setup() override;

std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
Expand Down
13 changes: 12 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3328,9 +3328,20 @@ bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensu
// First transaction must be coinbase, the rest must not be
if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-missing", "first tx is not coinbase");
for (unsigned int i = 1; i < block.vtx.size(); i++)

size_t totalPopTxes = 0;
for (unsigned int i = 1; i < block.vtx.size(); i++) {
if(VeriBlock::isPopTx(*block.vtx[i]))
++totalPopTxes;

if (block.vtx[i]->IsCoinBase())
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-multiple", "more than one coinbase");
}

auto& config = VeriBlock::getService<VeriBlock::Config>();
if(totalPopTxes > config.max_pop_tx_amount) {
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "too many pop txes", "number of pop vtx in a block is too high");
}

// Check transactions
// Must check for duplicate inputs (see CVE-2018-17144)
Expand Down
2 changes: 0 additions & 2 deletions src/vbk/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ struct Config {

/// The maximum allowed number of PoP transaction in a block
uint32_t max_pop_tx_amount = 50;
/// The maximum allowed number of "UpdateContext" transactions in a block
uint32_t max_update_context_tx_amount = 1;


/////// Pop Rewards section start
Expand Down
21 changes: 13 additions & 8 deletions src/vbk/pop_service/pop_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <script/sigcache.h>
#include <shutdown.h>
#include <streams.h>
#include <validation.h>
#include <util/strencodings.h>
#include <validation.h>

#include <vbk/merkle.hpp>
#include <vbk/service_locator.hpp>
Expand Down Expand Up @@ -71,8 +71,7 @@ PopServiceImpl::PopServiceImpl(bool altautoconfig)
LogPrintf("Alt-service is not working, please run the alt-service before you start the daemon \n");
LogPrintf("-------------------------------------------------------------------------------------------------\n");
StartShutdown();
}
else if (altautoconfig){
} else if (altautoconfig) {
setConfig();
}
}
Expand Down Expand Up @@ -143,7 +142,7 @@ void PopServiceImpl::savePopTxToDatabase(const CBlock& block, const int& nHeight
assert(parsePopTx(tx, &publications, nullptr, &type) && "scriptSig of pop tx is invalid in savePopTxToDatabase");

// skip all non-publications txes
if(type != PopTxType::PUBLICATIONS) {
if (type != PopTxType::PUBLICATIONS) {
continue;
}

Expand Down Expand Up @@ -426,14 +425,14 @@ void PopServiceImpl::setConfig()
calculatorConfig->set_basicreward(std::to_string(COIN));
calculatorConfig->set_payoutrounds(config.payoutRounds);
calculatorConfig->set_keystoneround(config.keystoneRound);

RoundRatioConfig* roundRatioConfig = new RoundRatioConfig();
for (size_t i = 0; i < config.roundRatios.size(); ++i) {
std::string* round = roundRatioConfig->add_roundratio();
*round = config.roundRatios[i];
}
calculatorConfig->set_allocated_roundratios(roundRatioConfig);

RewardCurveConfig* rewardCurveConfig = new RewardCurveConfig();
rewardCurveConfig->set_startofdecreasingline(config.startOfDecreasingLine);
rewardCurveConfig->set_widthofdecreasinglinenormal(config.widthOfDecreasingLineNormal);
Expand All @@ -459,7 +458,7 @@ void PopServiceImpl::setConfig()

calculatorConfig->set_popdifficultyaveraginginterval(config.POP_DIFFICULTY_AVERAGING_INTERVAL);
calculatorConfig->set_poprewardsettlementinterval(config.POP_REWARD_SETTLEMENT_INTERVAL);

//ForkresolutionConfig
ForkresolutionConfigRequest* forkresolutionConfig = new ForkresolutionConfigRequest();
forkresolutionConfig->set_keystonefinalitydelay(config.keystone_finality_delay);
Expand Down Expand Up @@ -499,7 +498,7 @@ bool blockPopValidationImpl(PopServiceImpl& pop, const CBlock& block, const CBlo
{
bool isValid = true;
const auto& config = getService<Config>();

size_t numOfPopTxes = 0;
std::string error_message;

LOCK(mempool.cs);
Expand All @@ -510,6 +509,7 @@ bool blockPopValidationImpl(PopServiceImpl& pop, const CBlock& block, const CBlo
continue;
}

++numOfPopTxes;
Publications publications;
Context context;
PopTxType type = PopTxType::UNKNOWN;
Expand Down Expand Up @@ -604,6 +604,11 @@ bool blockPopValidationImpl(PopServiceImpl& pop, const CBlock& block, const CBlo
}
}

if (numOfPopTxes > config.max_pop_tx_amount) {
error_message += strprintf("too many pop transactions: actual %d > %d expected \n", numOfPopTxes, config.max_pop_tx_amount);
isValid = false;
}

if (!isValid) {
pop.removePayloads(block, pindexPrev.nHeight + 1);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, strprintf("blockPopValidation(): pop check is failed"), "bad pop data \n" + error_message);
Expand Down
2 changes: 1 addition & 1 deletion src/vbk/test/integration/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ VBK_INTEGRATION_TESTS = \


vbk_test_integration_test_int_vbk_SOURCES = $(VBK_INTEGRATION_TEST_SUITE) $(VBK_INTEGRATION_TESTS)
vbk_test_integration_test_int_vbk_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS) $(THIRD_PARTY_INCLUDE)
vbk_test_integration_test_int_vbk_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(TESTDEFS) $(EVENT_CFLAGS) $(FAKEIT_CXXFLAGS)
vbk_test_integration_test_int_vbk_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) $(FAKEIT_CXXFLAGS)

vbk_test_integration_test_int_vbk_LDADD = $(LIBBITCOIN_SERVER) $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL) \
Expand Down
Loading

0 comments on commit 81a9717

Please sign in to comment.