Skip to content
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

Fix mempool reorg issue #1034

Merged
merged 7 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
36 changes: 36 additions & 0 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,41 @@ UniValue isappliedcustomtx(const JSONRPCRequest& request) {
return result;
}

static UniValue clearmempool(const JSONRPCRequest& request)
{
auto pwallet = GetWallet(request);

RPCHelpMan("clearmempool",
"\nClears the memory pool and returns a list of the removed transactions.\n",
{},
RPCResult{
"[ (json array of string)\n"
" \"hash\" (string) The transaction hash\n"
" ,...\n"
"]\n"
},
RPCExamples{
HelpExampleCli("clearmempool", "")
+ HelpExampleRpc("clearmempool", "")
}
).Check(request);

std::vector<uint256> vtxid;
mempool.queryHashes(vtxid);

UniValue removed(UniValue::VARR);
for (const uint256& hash : vtxid)
removed.push_back(hash.ToString());

LOCK(cs_main);
mempool.clear();

std::vector<uint256> vHashOut;
pwallet->ZapSelectTx(vtxid, vHashOut);

return removed;
}

static const CRPCCommand commands[] =
{
// category name actor (function) params
Expand All @@ -772,6 +807,7 @@ static const CRPCCommand commands[] =
{"blockchain", "getgov", &getgov, {"name"}},
{"blockchain", "listgovs", &listgovs, {""}},
{"blockchain", "isappliedcustomtx", &isappliedcustomtx, {"txid", "blockHeight"}},
{"blockchain", "clearmempool", &clearmempool, {} },
};

void RegisterMNBlockchainRPCCommands(CRPCTable& tableRPC) {
Expand Down
31 changes: 0 additions & 31 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,36 +573,6 @@ static UniValue getrawmempool(const JSONRPCRequest& request)
return MempoolToJSON(::mempool, fVerbose);
}

static UniValue clearmempool(const JSONRPCRequest& request)
{
RPCHelpMan("clearmempool",
"\nClears the memory pool and returns a list of the removed transactions.\n",
{},
RPCResult{
"[ (json array of string)\n"
" \"hash\" (string) The transaction hash\n"
" ,...\n"
"]\n"
},
RPCExamples{
HelpExampleCli("clearmempool", "")
+ HelpExampleRpc("clearmempool", "")
}
).Check(request);

std::vector<uint256> vtxid;
mempool.queryHashes(vtxid);

UniValue removed(UniValue::VARR);
for (const uint256& hash : vtxid)
removed.push_back(hash.ToString());

LOCK(cs_main);
mempool.clear();

return removed;
}

static UniValue getmempoolancestors(const JSONRPCRequest& request)
{
RPCHelpMan{"getmempoolancestors",
Expand Down Expand Up @@ -2344,7 +2314,6 @@ static const CRPCCommand commands[] =
{ "blockchain", "getmempooldescendants", &getmempooldescendants, {"txid","verbose"} },
{ "blockchain", "getmempoolentry", &getmempoolentry, {"txid"} },
{ "blockchain", "getmempoolinfo", &getmempoolinfo, {} },
{ "blockchain", "clearmempool", &clearmempool, {} },
{ "blockchain", "getrawmempool", &getrawmempool, {"verbose"} },
{ "blockchain", "gettxout", &gettxout, {"txid","n","include_mempool"} },
{ "blockchain", "gettxoutsetinfo", &gettxoutsetinfo, {} },
Expand Down
10 changes: 6 additions & 4 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator)
// of transactions in the pool
nCheckFrequency = 0;
accountsViewDirty = false;
forceRebuildForReorg = false;
}

bool CTxMemPool::isSpent(const COutPoint& outpoint) const
Expand Down Expand Up @@ -591,6 +592,7 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigne
}

if (pcustomcsview) {
accountsViewDirty |= forceRebuildForReorg;
rebuildAccountsView(nBlockHeight, &::ChainstateActive().CoinsTip());
}

Expand All @@ -615,9 +617,7 @@ void CTxMemPool::clear()
{
LOCK(cs);
_clear();
if (pcustomcsview) {
accountsView().Discard();
}
acview.reset();
}

static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& mempoolDuplicate, const CCustomCSView * mnview, const int64_t spendheight, const CChainParams& chainparams)
Expand Down Expand Up @@ -947,7 +947,8 @@ void CTxMemPool::RemoveStaged(const setEntries &stage, bool updateDescendants, M
for (txiter it : stage) {
removeUnchecked(it, reason);
}
accountsViewDirty = accountsViewDirty || !stage.empty();
accountsViewDirty |= !stage.empty();
forceRebuildForReorg |= reason == MemPoolRemovalReason::REORG;
}

int CTxMemPool::Expire(int64_t time) {
Expand Down Expand Up @@ -1126,6 +1127,7 @@ void CTxMemPool::rebuildAccountsView(int height, const CCoinsViewCache& coinsCac

viewDuplicate.Flush();
accountsViewDirty = false;
forceRebuildForReorg = false;
}

uint64_t CTxMemPool::CalculateDescendantMaximum(txiter entry) const {
Expand Down
1 change: 1 addition & 0 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ class CTxMemPool
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);

bool accountsViewDirty;
bool forceRebuildForReorg;
std::unique_ptr<CCustomCSView> acview;
public:
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
Expand Down
60 changes: 60 additions & 0 deletions test/functional/feature_restore_utxo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2019 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test restoration of UTXOs"""

from test_framework.test_framework import DefiTestFramework

from test_framework.util import assert_equal

class TestRestoreUTXOs(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1'],
['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1']]

def run_test(self):
self.nodes[0].generate(101)
self.sync_blocks()

self.nodes[0].createtoken({
"symbol": "BTC",
"name": "Bitcoin",
"isDAT": True,
"collateralAddress": self.nodes[0].get_genesis_keys().ownerAuthAddress
})
self.nodes[0].generate(1)

self.nodes[0].minttokens("1@BTC")
self.nodes[0].generate(1)

source = self.nodes[1].getnewaddress("", "legacy")

# Fund UTXO
self.nodes[0].sendtoaddress(source, 1)

# Fund account
self.nodes[0].accounttoaccount(self.nodes[0].get_genesis_keys().ownerAuthAddress, {source: "1@BTC"})
self.nodes[0].generate(1)

# Check UTXO
assert_equal(len(self.nodes[1].listunspent()), 1)

# Create account Tx
destination = self.nodes[1].getnewaddress("", "legacy")
self.nodes[1].accounttoaccount(source, {destination: "1@BTC"})

# Clear mempool
self.nodes[1].clearmempool()

# Make sure that UTXO has restored
assert_equal(len(self.nodes[1].listunspent()), 1)

# Create another account Tx
self.nodes[1].accounttoaccount(source, {destination: "1@BTC"})

if __name__ == '__main__':
TestRestoreUTXOs().main()
3 changes: 1 addition & 2 deletions test/functional/rpc_mn_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def run_test(self):

assert_equal(len(self.nodes[0].listmasternodes()), 8)
mempool = self.nodes[0].getrawmempool()
assert(idnode0 in mempool and fundingTx in mempool)
assert_equal(len(mempool), 3) # + auto auth
assert_equal(len(mempool), 1) # auto auth

collateral0 = self.nodes[0].getnewaddress("", "legacy")
self.nodes[0].createmasternode(collateral0)
Expand Down
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
'feature_auth_return_change.py',
'feature_setgov.py',
'interface_zmq.py',
'feature_restore_utxo.py',
'interface_defi_cli.py',
'mempool_resurrect.py',
'wallet_txn_doublespend.py --mineblock',
Expand Down