Skip to content

Commit

Permalink
Sync before clearing mempool (#1099)
Browse files Browse the repository at this point in the history
* Sync before clearing mempool

* lint: remove unused imports
  • Loading branch information
Bushstar authored Feb 7, 2022
1 parent c262d38 commit dbf9b7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 60 deletions.
13 changes: 7 additions & 6 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,14 @@ static UniValue clearmempool(const JSONRPCRequest& request)
}
).Check(request);

SyncWithValidationInterfaceQueue();

std::vector<uint256> vtxid;
mempool.queryHashes(vtxid);
{
LOCK(mempool.cs);
mempool.queryHashes(vtxid);
mempool.clear();
}

{
auto locked_chain = pwallet->chain().lock();
Expand All @@ -846,11 +852,6 @@ static UniValue clearmempool(const JSONRPCRequest& request)
}
}

{
LOCK(mempool.cs);
mempool.clear();
}

UniValue removed(UniValue::VARR);
for (const uint256& hash : vtxid) {
removed.push_back(hash.ToString());
Expand Down
66 changes: 12 additions & 54 deletions test/functional/feature_restore_utxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

from test_framework.test_framework import DefiTestFramework

from test_framework.authproxy import JSONRPCException
from test_framework.util import assert_equal
import time
from test_framework.util import assert_equal, disconnect_nodes

class TestRestoreUTXOs(DefiTestFramework):
def set_test_params(self):
Expand All @@ -18,46 +16,12 @@ def set_test_params(self):
self.extra_args = [['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1'],
['-txnotokens=0', '-amkheight=1', '-bayfrontheight=1']]

def clearmempool(self, node: int):
try:
self.nodes[node].clearmempool()
except JSONRPCException as e:
return False
return True

def account_to_account(self, node: int, soure, destination):
try:
self.nodes[node].accounttoaccount(soure, {destination: "1@BTC"})
except JSONRPCException as e:
return False
return True

def account_to_account_loop(self, node: int, soure, destination):
count = 0
while not self.account_to_account(node, soure, destination):
if count == 5:
return False
else:
count += 1
time.sleep(1)
return True


def rollback(self, count):
block = self.nodes[0].getblockhash(count)
self.nodes[0].invalidateblock(block)
block = self.nodes[1].getblockhash(count)
self.nodes[1].invalidateblock(block)
assert(len(self.nodes[0].getrawmempool()) > 0)
assert(len(self.nodes[1].getrawmempool()) > 0)
while not self.clearmempool(0):
pass
while not self.clearmempool(1):
pass
assert_equal(len(self.nodes[0].getrawmempool()), 0)
self.nodes[1].clearmempool()
assert_equal(len(self.nodes[1].getrawmempool()), 0)
assert_equal(self.nodes[0].getblockcount(), count - 1)
assert_equal(self.nodes[1].getblockcount(), count - 1)
self.sync_blocks()

def run_test(self):
self.nodes[0].generate(101)
Expand All @@ -82,6 +46,7 @@ def run_test(self):
# Fund account
self.nodes[0].accounttoaccount(self.nodes[0].get_genesis_keys().ownerAuthAddress, {node1_source: "1@BTC"})
self.nodes[0].generate(1)
self.sync_blocks()

# Check UTXO
assert_equal(len(self.nodes[1].listunspent()), 1)
Expand All @@ -103,25 +68,18 @@ def run_test(self):
self.nodes[1].clearmempool()

# Set up for rollback tests
node0_source = self.nodes[0].getnewaddress("", "legacy")
node0_destination = self.nodes[0].getnewaddress("", "legacy")
self.nodes[0].accounttoaccount(self.nodes[0].get_genesis_keys().ownerAuthAddress, {node0_source: "1@BTC"})
self.nodes[0].generate(1)
self.sync_blocks()
block = self.nodes[0].getblockcount() + 1
node0_utxos = len(self.nodes[0].listunspent())
disconnect_nodes(self.nodes[0], 1)
block = self.nodes[1].getblockcount() + 1
node1_utxos = len(self.nodes[1].listunspent())

# Test rollbacks
for x in range(50):
assert(self.account_to_account_loop(0, node0_source, node0_destination))
self.nodes[0].generate(1)
self.sync_blocks()
assert(self.account_to_account_loop(1, node1_source, node1_destination))
self.nodes[1].generate(1)
self.sync_blocks()
for _ in range(2):
for _ in range(5):
self.nodes[1].accounttoaccount(node1_source, {node1_source: "1@BTC"})
self.nodes[1].generate(1)
self.nodes[1].accounttoaccount(node1_source, {node1_source: "1@BTC"})
self.nodes[1].generate(1)
self.rollback(block)
assert_equal(len(self.nodes[0].listunspent()), node0_utxos)
assert_equal(len(self.nodes[1].listunspent()), node1_utxos)

if __name__ == '__main__':
Expand Down

0 comments on commit dbf9b7e

Please sign in to comment.