Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
191 changes: 191 additions & 0 deletions src/test/evo_deterministicmns_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,179 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup)
}
}

// A pending ProRegTx that references an external collateral can never be mined once a block
// spends that collateral. It must leave the mempool then: nothing else evicts it, and an
// InstantSend-locked transaction does not expire, which would leave its inputs frozen.
void FuncTestMempoolProRegSpentCollateral(TestChainSetup& setup)
{
auto& chainman = *Assert(setup.m_node.chainman.get());
auto tip_index = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()); };
auto tip_height = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Height()); };

const CScript coinbase_pk = GetScriptForRawPubKey(setup.coinbaseKey.GetPubKey());
auto utxos = BuildSimpleUtxoMap(setup.m_coinbase_txns);

CKey ownerKey;
CKey payoutKey;
CKey collateralKey;
CBLSSecretKey operatorKey;
ownerKey.MakeNewKey(true);
payoutKey.MakeNewKey(true);
collateralKey.MakeNewKey(true);
operatorKey.MakeNewKey();

auto scriptPayout = GetScriptForDestination(PKHash(payoutKey.GetPubKey()));
auto scriptCollateral = GetScriptForDestination(PKHash(collateralKey.GetPubKey()));

auto tx_collateral = CreateSpendTx(chainman, utxos, scriptCollateral, dmn_types::Regular.collat_amount,
setup.coinbaseKey);
auto block = std::make_shared<CBlock>(setup.CreateBlock({tx_collateral}, coinbase_pk, chainman.ActiveChainstate()));
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
setup.m_node.dmnman->UpdatedBlockTip(tip_index());

const auto collateralOutpoint = GetCollateralOutpoint(tx_collateral);
auto tx_reg = CreateProRegTxExternalCollateral(chainman, utxos, /*port=*/1, collateralOutpoint, scriptPayout,
ownerKey, operatorKey, collateralKey, setup.coinbaseKey);

CMutableTransaction tx_unrelated;
tx_unrelated.vin.emplace_back(COutPoint(tx_collateral.GetHash(), collateralOutpoint.n + 1));
tx_unrelated.vout.emplace_back(0, CScript() << OP_RETURN);

CMutableTransaction tx_spend;
tx_spend.vin.emplace_back(collateralOutpoint);
tx_spend.vout.emplace_back(0, CScript() << OP_RETURN);

CTxMemPool testPool{MemPoolOptionsForTest(setup.m_node)};
TestMemPoolEntryHelper entry;
LOCK2(cs_main, testPool.cs);

testPool.addUnchecked(entry.FromTx(tx_reg));
BOOST_CHECK_EQUAL(testPool.size(), 1U);

testPool.removeForBlock({MakeTransactionRef(tx_unrelated)}, tip_height() + 1);
BOOST_CHECK(testPool.exists(tx_reg.GetHash()));

testPool.removeForBlock({MakeTransactionRef(tx_spend)}, tip_height() + 1);
BOOST_CHECK(!testPool.exists(tx_reg.GetHash()));
}

// Pending updates of a registered masternode can never be mined once a block spends its
// collateral, since the masternode is gone. They must leave the mempool then.
void FuncTestMempoolProUpdatesSpentCollateral(TestChainSetup& setup)
{
auto& chainman = *Assert(setup.m_node.chainman.get());
auto& dmnman = *Assert(setup.m_node.dmnman);
auto tip_index = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()); };
auto tip_height = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Height()); };

const CScript coinbase_pk = GetScriptForRawPubKey(setup.coinbaseKey.GetPubKey());
auto utxos = BuildSimpleUtxoMap(setup.m_coinbase_txns);

CKey ownerKey;
CKey payoutKey;
CKey collateralKey;
CBLSSecretKey operatorKey;
ownerKey.MakeNewKey(true);
payoutKey.MakeNewKey(true);
collateralKey.MakeNewKey(true);
operatorKey.MakeNewKey();

auto scriptPayout = GetScriptForDestination(PKHash(payoutKey.GetPubKey()));
auto scriptCollateral = GetScriptForDestination(PKHash(collateralKey.GetPubKey()));

auto tx_collateral = CreateSpendTx(chainman, utxos, scriptCollateral, dmn_types::Regular.collat_amount,
setup.coinbaseKey);
auto block = std::make_shared<CBlock>(setup.CreateBlock({tx_collateral}, coinbase_pk, chainman.ActiveChainstate()));
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
dmnman.UpdatedBlockTip(tip_index());

const auto collateralOutpoint = GetCollateralOutpoint(tx_collateral);
auto tx_reg = CreateProRegTxExternalCollateral(chainman, utxos, /*port=*/1, collateralOutpoint, scriptPayout,
ownerKey, operatorKey, collateralKey, setup.coinbaseKey);
const uint256 proTxHash = tx_reg.GetHash();
block = std::make_shared<CBlock>(setup.CreateBlock({tx_reg}, coinbase_pk, chainman.ActiveChainstate()));
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
dmnman.UpdatedBlockTip(tip_index());
BOOST_REQUIRE(dmnman.GetListAtChainTip().HasMN(proTxHash));

auto tx_up_serv = CreateProUpServTx(chainman, utxos, proTxHash, operatorKey, /*port=*/2, CScript(), setup.coinbaseKey);
auto tx_up_reg = CreateProUpRegTx(chainman, utxos, proTxHash, ownerKey, operatorKey.GetPublicKey(),
ownerKey.GetPubKey().GetID(), scriptPayout, setup.coinbaseKey);
auto tx_up_rev = CreateProUpRevTx(chainman, utxos, proTxHash, operatorKey, setup.coinbaseKey);

CMutableTransaction tx_unrelated;
tx_unrelated.vin.emplace_back(COutPoint(tx_collateral.GetHash(), collateralOutpoint.n + 1));
tx_unrelated.vout.emplace_back(0, CScript() << OP_RETURN);

CMutableTransaction tx_spend;
tx_spend.vin.emplace_back(collateralOutpoint);
tx_spend.vout.emplace_back(0, CScript() << OP_RETURN);

CTxMemPool testPool{MemPoolOptionsForTest(setup.m_node)};
TestMemPoolEntryHelper entry;
LOCK2(cs_main, testPool.cs);

testPool.addUnchecked(entry.FromTx(tx_up_serv));
testPool.addUnchecked(entry.FromTx(tx_up_reg));
testPool.addUnchecked(entry.FromTx(tx_up_rev));
BOOST_CHECK_EQUAL(testPool.size(), 3U);

testPool.removeForBlock({MakeTransactionRef(tx_unrelated)}, tip_height() + 1);
BOOST_CHECK(testPool.exists(tx_up_serv.GetHash()));
BOOST_CHECK(testPool.exists(tx_up_reg.GetHash()));
BOOST_CHECK(testPool.exists(tx_up_rev.GetHash()));

testPool.removeForBlock({MakeTransactionRef(tx_spend)}, tip_height() + 1);
BOOST_CHECK(!testPool.exists(tx_up_serv.GetHash()));
BOOST_CHECK(!testPool.exists(tx_up_reg.GetHash()));
BOOST_CHECK(!testPool.exists(tx_up_rev.GetHash()));
}

// A pending ProUpServTx is signed by the operator key and stays valid across a registrar update
// that keeps that key, so only a registrar update that changes the key may evict it.
void FuncTestMempoolProUpServOperatorKeyChange(TestChainSetup& setup)
{
auto& chainman = *Assert(setup.m_node.chainman.get());
auto& dmnman = *Assert(setup.m_node.dmnman);
auto tip_index = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()); };
auto tip_height = [&] { return WITH_LOCK(::cs_main, return chainman.ActiveChain().Height()); };

const CScript coinbase_pk = GetScriptForRawPubKey(setup.coinbaseKey.GetPubKey());
auto utxos = BuildSimpleUtxoMap(setup.m_coinbase_txns);
const CScript scriptPayout = GenerateRandomAddress();

CKey ownerKey;
CBLSSecretKey operatorKey;
auto tx_reg = CreateProRegTx(chainman, utxos, /*port=*/1, scriptPayout, setup.coinbaseKey, ownerKey, operatorKey);
const uint256 proTxHash = tx_reg.GetHash();
auto block = std::make_shared<CBlock>(setup.CreateBlock({tx_reg}, coinbase_pk, chainman.ActiveChainstate()));
BOOST_REQUIRE(chainman.ProcessNewBlock(block, true, nullptr));
dmnman.UpdatedBlockTip(tip_index());
BOOST_REQUIRE(dmnman.GetListAtChainTip().HasMN(proTxHash));

CBLSSecretKey newOperatorKey;
newOperatorKey.MakeNewKey();

auto tx_up_serv = CreateProUpServTx(chainman, utxos, proTxHash, operatorKey, /*port=*/2, CScript(), setup.coinbaseKey);
auto tx_same_key = CreateProUpRegTx(chainman, utxos, proTxHash, ownerKey, operatorKey.GetPublicKey(),
ownerKey.GetPubKey().GetID(), scriptPayout, setup.coinbaseKey);
auto tx_new_key = CreateProUpRegTx(chainman, utxos, proTxHash, ownerKey, newOperatorKey.GetPublicKey(),
ownerKey.GetPubKey().GetID(), scriptPayout, setup.coinbaseKey);

CTxMemPool testPool{MemPoolOptionsForTest(setup.m_node)};
TestMemPoolEntryHelper entry;
LOCK2(cs_main, testPool.cs);

testPool.addUnchecked(entry.FromTx(tx_up_serv));
BOOST_CHECK_EQUAL(testPool.size(), 1U);

testPool.removeForBlock({MakeTransactionRef(tx_same_key)}, tip_height() + 1);
BOOST_CHECK(testPool.exists(tx_up_serv.GetHash()));

testPool.removeForBlock({MakeTransactionRef(tx_new_key)}, tip_height() + 1);
BOOST_CHECK(!testPool.exists(tx_up_serv.GetHash()));
}

void FuncVerifyDB(TestChainSetup& setup)
{
auto& chainman = *Assert(setup.m_node.chainman.get());
Expand Down Expand Up @@ -3489,6 +3662,24 @@ BOOST_AUTO_TEST_CASE(test_mempool_proreg_replacement_update_conflict)
FuncTestMempoolProRegReplacementUpdateConflict(setup);
}

BOOST_AUTO_TEST_CASE(test_mempool_proreg_spent_collateral)
{
TestChainV19Setup setup;
FuncTestMempoolProRegSpentCollateral(setup);
}

BOOST_AUTO_TEST_CASE(test_mempool_proupdates_spent_collateral)
{
TestChainV19Setup setup;
FuncTestMempoolProUpdatesSpentCollateral(setup);
}

BOOST_AUTO_TEST_CASE(test_mempool_proupserv_operator_key_change)
{
TestChainV19Setup setup;
FuncTestMempoolProUpServOperatorKeyChange(setup);
}

//This one can be started only with legacy scheme, since inside undo block will switch it back to legacy resulting into an inconsistency
BOOST_AUTO_TEST_CASE(verify_db_legacy)
{
Expand Down
12 changes: 10 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
// Without a masternode to take the key from, any registrar update evicts it
if (auto dmn = m_dmnman.GetListAtChainTip().GetMN(proTx.proTxHash)) {
newit->validForProTxKey = ::SerializeHash(dmn->pdmnState->pubKeyOperator);
}
for (const auto& entry : proTx.netInfo->GetEntries()) {
mapProTxAddresses.emplace(entry, tx_hash);
}
Expand Down Expand Up @@ -1052,8 +1056,12 @@ void CTxMemPool::removeProTxSpentCollateralConflicts(const CTransaction &tx)
for (const auto& in : tx.vin) {
auto collateralIt = mapProTxCollaterals.find(in.prevout);
if (collateralIt != mapProTxCollaterals.end()) {
// These are not yet mined ProRegTxs
removeProTxReferences(collateralIt->second);
// A not yet mined ProRegTx whose collateral is now spent, and TXs referring to it
const uint256 proRegTxHash{collateralIt->second};
if (auto it = mapTx.find(proRegTxHash); it != mapTx.end()) {
removeRecursive(it->GetTx(), MemPoolRemovalReason::CONFLICT);
}
removeProTxReferences(proRegTxHash);
}
auto dmn = mnList.GetMNByCollateral(in.prevout);
if (dmn) {
Expand Down
35 changes: 35 additions & 0 deletions test/functional/feature_masternode_shares.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,40 @@ def test_voting_payee_conflict_eviction(self, node, protx_hash):
assert_equal(confirmed_address, voting)
assert pending_txid not in node.getrawmempool()

def test_dissolution_evicts_pending_updates(self, node):
self.log.info("A confirmed dissolution evicts pending updates of the dissolved masternode")
shares = [
{"amount": 600 * COIN, "refundAddress": node.getnewaddress(), "ownerAddress": node.getnewaddress()},
{"amount": 400 * COIN, "refundAddress": node.getnewaddress(), "ownerAddress": node.getnewaddress()},
]
protx_hash, _ = self.register_shared(node, shares, port_offset=8)
registrar_fee, share_fee = node.getnewaddress(), node.getnewaddress()
funding_txid = node.sendmany("", {registrar_fee: 1, share_fee: 1})
node.syncwithvalidationinterfacequeue()
self.bump_mocktime(10 * 60 + 1)
funding_block = self.generate(node, 1, sync_fun=self.no_op)[0]
assert funding_txid in node.getblock(funding_block)["tx"]

prepared = node.protx("shared_update_registrar_prepare", protx_hash, "", node.getnewaddress(), registrar_fee)
registrar_sigs = node.protx("shared_sign", prepared["tx"])["signatures"]
registrar = node.protx("shared_combine", prepared["tx"], registrar_sigs)
share_update = node.protx("shared_update_share", protx_hash, 0, node.getnewaddress(), share_fee, False)
dissolve = node.protx("shared_dissolve", protx_hash, 0, DISSOLVE_FEE, False)
self.sync_all()
self.disconnect_nodes(0, 1)
pending_txids = {node.sendrawtransaction(registrar), node.sendrawtransaction(share_update)}
assert pending_txids.issubset(node.getrawmempool())
# Simulate another miner that never saw the pending updates confirming the dissolution.
other = self.nodes[1]
dissolve_txid = other.sendrawtransaction(dissolve)
self.bump_mocktime(10 * 60 + 1)
dissolve_block = self.generate(other, 1, sync_fun=self.no_op)[0]
assert dissolve_txid in other.getblock(dissolve_block)["tx"]
self.connect_nodes(0, 1)
self.sync_blocks()
assert_raises_rpc_error(None, None, node.protx, "info", protx_hash)
assert pending_txids.isdisjoint(node.getrawmempool())

def test_separate_participant_wallets(self):
self.log.info("Eight separate wallets fund and authorize a shared masternode")
node = self.nodes[0]
Expand Down Expand Up @@ -1164,6 +1198,7 @@ def submit_with_sigs(sigs):

self.test_pending_registrar_update(node, protx_hash4)
self.test_voting_payee_conflict_eviction(node, protx_hash4)
self.test_dissolution_evicts_pending_updates(node)

self.log.info("Shared state survives a node restart")
state_before_restart = node.protx("info", protx_hash4)["state"]
Expand Down
Loading