Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TEST_UTIL_H = \
test/util/index.h \
test/util/llmq_tests.h \
test/util/logging.h \
test/util/masternode.h \
test/util/mining.h \
test/util/net.h \
test/util/poolresourcetester.h \
Expand All @@ -36,6 +37,7 @@ libtest_util_a_SOURCES = \
test/util/coins.cpp \
test/util/json.cpp \
test/util/logging.cpp \
test/util/masternode.cpp \
test/util/mining.cpp \
test/util/net.cpp \
test/util/script.cpp \
Expand Down
116 changes: 10 additions & 106 deletions src/test/block_reward_reallocation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,24 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <test/util/masternode.h>
#include <test/util/setup_common.h>

#include <bls/bls.h>
#include <evo/deterministicmns.h>
#include <evo/providertx.h>
#include <evo/specialtx.h>
#include <masternode/payments.h>
#include <util/helpers.h>
#include <util/std23.h>

#include <chainparams.h>
#include <deploymentstatus.h>
#include <node/miner.h>
#include <script/interpreter.h>
#include <script/sign.h>
#include <script/signingprovider.h>
#include <script/standard.h>
#include <validation.h>

#include <boost/test/unit_test.hpp>

#include <map>
#include <vector>

using node::BlockAssembler;

using SimpleUTXOMap = std::map<COutPoint, Coin>;

struct TestChainBRRBeforeActivationSetup : public TestChainSetup
{
// Force fast DIP3 activation
Expand All @@ -40,106 +30,20 @@ struct TestChainBRRBeforeActivationSetup : public TestChainSetup
}
};

static SimpleUTXOMap BuildSimpleUtxoMap(const std::vector<CTransactionRef>& txs)
{
SimpleUTXOMap utxos;
for (auto [i, tx] : std23::views::enumerate(txs)) {
for (auto [j, output] : std23::views::enumerate(tx->vout)) {
if (output.scriptPubKey.IsUnspendable()) continue;
utxos.try_emplace(COutPoint(tx->GetHash(), j), Coin(output, static_cast<int>(i) + 1, /*fCoinBaseIn=*/false));
}
}
return utxos;
}

static SimpleUTXOMap SelectUTXOs(const CChain& active_chain, SimpleUTXOMap& utoxs, CAmount amount, CAmount& changeRet)
{
changeRet = 0;

SimpleUTXOMap selectedUtxos;
CAmount selectedAmount = 0;
while (!utoxs.empty()) {
bool found = false;
for (auto it = utoxs.begin(); it != utoxs.end(); ++it) {
if (active_chain.Height() - it->second.nHeight < 101) {
continue;
}

found = true;
selectedAmount += it->second.out.nValue;
selectedUtxos.emplace(it->first, it->second);
utoxs.erase(it);
break;
}
BOOST_REQUIRE(found);
if (selectedAmount >= amount) {
changeRet = selectedAmount - amount;
break;
}
}

return selectedUtxos;
}

// Returns the coins being spent so the caller can sign without a chain/mempool lookup.
static SimpleUTXOMap FundTransaction(const ChainstateManager& chainman, CMutableTransaction& tx, SimpleUTXOMap& utoxs, const CScript& scriptPayout, CAmount amount)
{
CAmount change;
auto inputs = WITH_LOCK(::cs_main, return SelectUTXOs(chainman.ActiveChain(), utoxs, amount, change));
for (const auto& input : inputs) {
tx.vin.emplace_back(input.first);
}
tx.vout.emplace_back(amount, scriptPayout);
if (change != 0) {
tx.vout.emplace_back(change, scriptPayout);
}
return inputs;
}

static void SignTransaction(CMutableTransaction& tx, const SimpleUTXOMap& coins, const CKey& coinbaseKey)
{
FillableSigningProvider tempKeystore;
tempKeystore.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());

std::map<int, bilingual_str> input_errors;
BOOST_REQUIRE(::SignTransaction(tx, &tempKeystore, coins, SIGHASH_ALL, input_errors));
}
BOOST_AUTO_TEST_SUITE(block_reward_reallocation_tests)

static CMutableTransaction CreateProRegTx(const ChainstateManager& chainman, SimpleUTXOMap& utxos, int port, const CScript& scriptPayout, const CKey& coinbaseKey, CKey& ownerKeyRet, CBLSSecretKey& operatorKeyRet)
BOOST_AUTO_TEST_CASE(simple_utxo_map_skips_unspendable_outputs)
{
ownerKeyRet.MakeNewKey(true);
operatorKeyRet.MakeNewKey();

CProRegTx proTx;
proTx.nVersion = ProTxVersion::GetMax(!bls::bls_legacy_scheme, /*is_extended_addr=*/false);
proTx.netInfo = NetInfoInterface::MakeNetInfo(proTx.nVersion);
proTx.collateralOutpoint.n = 0;
BOOST_CHECK_EQUAL(proTx.netInfo->AddEntry(NetInfoPurpose::CORE_P2P, strprintf("1.1.1.1:%d", port)),
NetInfoStatus::Success);
proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID();
proTx.pubKeyOperator.Set(operatorKeyRet.GetPublicKey(), bls::bls_legacy_scheme.load());
proTx.keyIDVoting = ownerKeyRet.GetPubKey().GetID();
proTx.scriptPayout = scriptPayout;

CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_PROVIDER_REGISTER;
const auto spent = FundTransaction(chainman, tx, utxos, scriptPayout, dmn_types::Regular.collat_amount);
proTx.inputsHash = CalcTxInputsHash(CTransaction(tx));
SetTxPayload(tx, proTx);
SignTransaction(tx, spent, coinbaseKey);

return tx;
}
tx.vout.emplace_back(/*nValueIn=*/1, CScript() << OP_TRUE);
tx.vout.emplace_back(/*nValueIn=*/0, CScript() << OP_RETURN);
const auto tx_ref = MakeTransactionRef(tx);

static CScript GenerateRandomAddress()
{
CKey key;
key.MakeNewKey(false);
return GetScriptForDestination(PKHash(key.GetPubKey()));
}
const auto utxos = BuildSimpleUtxoMap({tx_ref});

BOOST_AUTO_TEST_SUITE(block_reward_reallocation_tests)
BOOST_REQUIRE_EQUAL(utxos.size(), 1);
BOOST_CHECK(utxos.contains(COutPoint{tx_ref->GetHash(), 0}));
}

BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationSetup)
{
Expand Down
104 changes: 2 additions & 102 deletions src/test/evo_deterministicmns_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <test/util/masternode.h>
#include <test/util/setup_common.h>

#include <chainparams.h>
Expand Down Expand Up @@ -33,77 +34,10 @@

#include <boost/test/unit_test.hpp>

#include <map>
#include <optional>
#include <vector>

using SimpleUTXOMap = std::map<COutPoint, Coin>;

static SimpleUTXOMap BuildSimpleUtxoMap(const std::vector<CTransactionRef>& txs)
{
SimpleUTXOMap utxos;
for (size_t i = 0; i < txs.size(); i++) {
auto& tx = txs[i];
for (size_t j = 0; j < tx->vout.size(); j++) {
if (tx->vout[j].scriptPubKey.IsUnspendable()) continue;
utxos.emplace(COutPoint(tx->GetHash(), j), Coin(tx->vout[j], static_cast<int>(i) + 1, /*fCoinBaseIn=*/false));
}
}
return utxos;
}

static SimpleUTXOMap SelectUTXOs(const CChain& active_chain, SimpleUTXOMap& utoxs, CAmount amount, CAmount& changeRet)
{
changeRet = 0;

SimpleUTXOMap selectedUtxos;
CAmount selectedAmount = 0;
while (!utoxs.empty()) {
bool found = false;
for (auto it = utoxs.begin(); it != utoxs.end(); ++it) {
if (active_chain.Height() - it->second.nHeight < 101) {
continue;
}

found = true;
selectedAmount += it->second.out.nValue;
selectedUtxos.emplace(it->first, it->second);
utoxs.erase(it);
break;
}
BOOST_REQUIRE(found);
if (selectedAmount >= amount) {
changeRet = selectedAmount - amount;
break;
}
}

return selectedUtxos;
}

// Returns the coins being spent so the caller can sign without a chain/mempool lookup.
static SimpleUTXOMap FundTransaction(const ChainstateManager& chainman, CMutableTransaction& tx, SimpleUTXOMap& utoxs, const CScript& scriptPayout, CAmount amount)
{
CAmount change;
auto inputs = WITH_LOCK(::cs_main, return SelectUTXOs(chainman.ActiveChain(), utoxs, amount, change));
for (const auto& input : inputs) {
tx.vin.emplace_back(CTxIn(input.first));
}
tx.vout.emplace_back(CTxOut(amount, scriptPayout));
if (change != 0) {
tx.vout.emplace_back(CTxOut(change, scriptPayout));
}
return inputs;
}

static void SignTransaction(CMutableTransaction& tx, const SimpleUTXOMap& coins, const CKey& coinbaseKey)
{
FillableSigningProvider tempKeystore;
tempKeystore.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());

std::map<int, bilingual_str> input_errors;
BOOST_REQUIRE(::SignTransaction(tx, &tempKeystore, coins, SIGHASH_ALL, input_errors));
}

static CMutableTransaction CreateSpendTx(const ChainstateManager& chainman, SimpleUTXOMap& utxos, const CScript& scriptPayout, CAmount amount, const CKey& coinbaseKey)
{
CMutableTransaction tx;
Expand All @@ -112,33 +46,6 @@ static CMutableTransaction CreateSpendTx(const ChainstateManager& chainman, Simp
return tx;
}

static CMutableTransaction CreateProRegTx(const ChainstateManager& chainman, SimpleUTXOMap& utxos, int port, const CScript& scriptPayout, const CKey& coinbaseKey, CKey& ownerKeyRet, CBLSSecretKey& operatorKeyRet)
{
ownerKeyRet.MakeNewKey(true);
operatorKeyRet.MakeNewKey();

CProRegTx proTx;
proTx.nVersion = ProTxVersion::GetMax(!bls::bls_legacy_scheme, /*is_extended_addr=*/false);
proTx.netInfo = NetInfoInterface::MakeNetInfo(proTx.nVersion);
proTx.collateralOutpoint.n = 0;
BOOST_CHECK_EQUAL(proTx.netInfo->AddEntry(NetInfoPurpose::CORE_P2P, strprintf("1.1.1.1:%d", port)),
NetInfoStatus::Success);
proTx.keyIDOwner = ownerKeyRet.GetPubKey().GetID();
proTx.pubKeyOperator.Set(operatorKeyRet.GetPublicKey(), bls::bls_legacy_scheme.load());
proTx.keyIDVoting = ownerKeyRet.GetPubKey().GetID();
proTx.scriptPayout = scriptPayout;

CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_PROVIDER_REGISTER;
const auto spent = FundTransaction(chainman, tx, utxos, scriptPayout, dmn_types::Regular.collat_amount);
proTx.inputsHash = CalcTxInputsHash(CTransaction(tx));
SetTxPayload(tx, proTx);
SignTransaction(tx, spent, coinbaseKey);

return tx;
}

static COutPoint GetCollateralOutpoint(const CMutableTransaction& tx)
{
for (size_t i = 0; i < tx.vout.size(); ++i) {
Expand Down Expand Up @@ -254,13 +161,6 @@ static CMutableTransaction MalleateProTxPayout(const CMutableTransaction& tx)
return tx2;
}

static CScript GenerateRandomAddress()
{
CKey key;
key.MakeNewKey(false);
return GetScriptForDestination(PKHash(key.GetPubKey()));
}

static CDeterministicMNCPtr FindPayoutDmn(CDeterministicMNManager& dmnman, const CBlock& block)
{
auto dmnList = dmnman.GetListAtChainTip();
Expand Down
Loading
Loading