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 restore UTXO test #1080

Merged
merged 12 commits into from
Feb 3, 2022
25 changes: 18 additions & 7 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,19 +836,30 @@ static UniValue clearmempool(const JSONRPCRequest& request)
std::vector<uint256> vtxid;
mempool.queryHashes(vtxid);

UniValue removed(UniValue::VARR);
for (const uint256& hash : vtxid)
removed.push_back(hash.ToString());
{
auto locked_chain = pwallet->chain().lock();
LOCK2(pwallet->cs_wallet, locked_chain->mutex());

LOCK(cs_main);
mempool.clear();
std::vector<uint256> vHashOut;
if (pwallet->ZapSelectTx(vtxid, vHashOut) != DBErrors::LOAD_OK) {
throw JSONRPCError(RPC_WALLET_ERROR, "Could not delete mempool transactions from wallet");
}
}

std::vector<uint256> vHashOut;
pwallet->ZapSelectTx(vtxid, vHashOut);
{
LOCK(mempool.cs);
mempool.clear();
}

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

return removed;
}


static const CRPCCommand commands[] =
{
// category name actor (function) params
Expand Down
7 changes: 5 additions & 2 deletions src/rpc/rawtransaction_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,11 @@ UniValue SignTransaction(CMutableTransaction& mtx, const UniValue& prevTxsUnival
for (unsigned int i = 0; i < mtx.vin.size(); i++) {
CTxIn& txin = mtx.vin[i];
auto coin = coins.find(txin.prevout);
if (coin == coins.end() || coin->second.IsSpent()) {
TxInErrorToJSON(txin, vErrors, "Input not found or already spent");
if (coin == coins.end()) {
TxInErrorToJSON(txin, vErrors, "Input not found");
continue;
} else if (coin->second.IsSpent()) {
TxInErrorToJSON(txin, vErrors, "Input already spent");
continue;
}
const CScript& prevPubKey = coin->second.out.scriptPubKey;
Expand Down
47 changes: 43 additions & 4 deletions test/functional/feature_restore_utxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from test_framework.test_framework import DefiTestFramework

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

class TestRestoreUTXOs(DefiTestFramework):
def set_test_params(self):
Expand All @@ -16,12 +18,45 @@ 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)
self.nodes[1].invalidateblock(block)
self.nodes[0].clearmempool()
self.nodes[1].clearmempool()
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)
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):
Expand Down Expand Up @@ -74,16 +109,20 @@ def run_test(self):
self.nodes[0].generate(1)
self.sync_blocks()
block = self.nodes[0].getblockcount() + 1
node0_utxos = len(self.nodes[0].listunspent())
node1_utxos = len(self.nodes[1].listunspent())

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

if __name__ == '__main__':
TestRestoreUTXOs().main()