diff --git a/qa/rpc-tests/llmq-is-cl-conflicts.py b/qa/rpc-tests/llmq-is-cl-conflicts.py index fc07ca5e78..5817ebff5c 100755 --- a/qa/rpc-tests/llmq-is-cl-conflicts.py +++ b/qa/rpc-tests/llmq-is-cl-conflicts.py @@ -20,6 +20,10 @@ def __init__(self): SingleNodeConnCB.__init__(self) self.clsigs = {} self.islocks = {} + self.tx_requests = {} + self.transactions = {} + self.received_islocks = set() + self.notfound_islocks = set() def send_clsig(self, clsig): hash = uint256_from_str(hash256(clsig.serialize())) @@ -37,15 +41,25 @@ def send_islock(self, islock): def on_getdata(self, conn, message): for inv in message.inv: + if inv.type == 1: + self.tx_requests[inv.hash] = self.tx_requests.get(inv.hash, 0) + 1 + if inv.hash in self.transactions: + self.send_message(msg_tx(self.transactions[inv.hash])) if inv.hash in self.clsigs: self.send_message(self.clsigs[inv.hash]) if inv.hash in self.islocks: self.send_message(self.islocks[inv.hash]) + def on_islock(self, conn, message): + self.received_islocks.add(uint256_from_str(hash256(message.serialize()))) + + def on_notfound(self, conn, message): + self.notfound_islocks.update(inv.hash for inv in message.inv if inv.type == 30) + class LLMQ_IS_CL_Conflicts(EvoZnodeTestFramework): def __init__(self): - super().__init__(6, 5) + super().__init__(6, 5, extra_args=[[], ['-blockprioritysize=0']] + [[]] * 4) #disable_mocktime() def run_test(self): @@ -60,11 +74,232 @@ def run_test(self): # mine single block, wait for chainlock self.nodes[0].generate(800 - self.nodes[0].getblockcount()) self.wait_for_chainlock_tip_all_nodes() - + self.miner_peer = TestNode() + self.miner_peer.add_connection(NodeConn('127.0.0.1', p2p_port(1), self.nodes[1], self.miner_peer)) + self.miner_peer.wait_for_verack() + + self.test_islock_before_tx(False) + self.test_islock_before_tx(True) + self.test_missing_transaction_request_rotation() + self.no_txindex_node_idx = len(self.nodes) + initialize_datadir(self.options.tmpdir, self.no_txindex_node_idx) + self.nodes.append(start_node(self.no_txindex_node_idx, self.options.tmpdir, ['-txindex=0', '-debug=instantsend'])) + fresh_node_pid = bitcoind_processes[self.no_txindex_node_idx].pid + connect_nodes(self.nodes[-1], 0) + self.sync_all() + wait_to_sync(self.nodes[-1], True) + self.test_islock_before_block_without_txindex(True) + self.test_islock_before_block_without_txindex(False) + assert_equal(bitcoind_processes[self.no_txindex_node_idx].pid, fresh_node_pid) + self.log.info("Fresh node completed InstantSend and ChainLock recovery without restarting (PID %d)", fresh_node_pid) + self.test_chainlock_prunes_pending_locks() + self.stop_node(self.no_txindex_node_idx) + self.nodes.pop() self.test_chainlock_overrides_islock(False) self.test_chainlock_overrides_islock(True) self.test_islock_overrides_nonchainlock() + def test_islock_before_tx(self, previously_rejected): + self.log.info("Testing IS lock before transaction (previously rejected: %s)", previously_rejected) + node = self.nodes[0] + rawtx = self.create_raw_tx(node, node, 1, 1, 1)['hex'] + conflicting_rawtx = self.create_raw_tx(node, node, 1, 1, 1)['hex'] + tx = FromHex(CTransaction(), rawtx) + tx.rehash() + txid = "%064x" % tx.sha256 + conflict = FromHex(CTransaction(), conflicting_rawtx) + assert_equal([i.prevout.serialize() for i in tx.vin], [i.prevout.serialize() for i in conflict.vin]) + islock = self.create_islock(rawtx) + + if previously_rejected: + conflicting_txid = node.sendrawtransaction(conflicting_rawtx) + for miner in self.nodes[:2]: + assert wait_until(lambda: conflicting_txid in miner.getrawmempool(), timeout=10) + set_mocktime(get_mocktime() + 60 * 11) + set_node_times(self.nodes, get_mocktime()) + for miner in self.nodes[:2]: + assert conflicting_txid in [entry['txid'] for entry in miner.getblocktemplate()['transactions']] + + # Advertise the TX, but withhold it until the node asks again after receiving its lock. + self.test_node.send_message(msg_inv([CInv(1, tx.sha256)])) + assert wait_until(lambda: self.test_node.tx_requests.get(tx.sha256, 0) == 1, timeout=10) + if previously_rejected: + assert_raises_jsonrpc(-26, "txn-mempool-conflict", node.sendrawtransaction, rawtx) + # P2P rejection also populates recentRejects; this internal reject code has no wire reply. + self.test_node.send_message(msg_tx(tx)) + assert self.test_node.sync_with_ping() + assert txid not in node.getrawmempool() + + self.test_node.send_islock(islock) + # No mocktime advance: recovery must bypass outstanding requests and the two-minute retry delay. + assert wait_until(lambda: self.test_node.tx_requests.get(tx.sha256, 0) >= 2, timeout=10) + assert txid not in node.getrawmempool() + if not previously_rejected: + # An incomplete lock must not reject a competing transaction before its own TX is known. + conflicting_txid = node.sendrawtransaction(conflicting_rawtx) + assert conflicting_txid in node.getrawmempool() + lock_hash = uint256_from_str(hash256(islock.serialize())) + def lock_relayed_to_miner(): + self.miner_peer.send_message(msg_getdata([CInv(30, lock_hash)])) + return lock_hash in self.miner_peer.received_islocks + assert wait_until(lock_relayed_to_miner, timeout=10) + for miner in self.nodes[:2]: + assert wait_until(lambda: conflicting_txid in miner.getrawmempool(), timeout=10) + # Cover priority and fee selection, including refresh of a template cached before the pending lock. + set_mocktime(get_mocktime() + (6 if previously_rejected else 60 * 11)) + set_node_times(self.nodes, get_mocktime()) + node.prioritisetransaction(conflicting_txid, 1e15, 0) + for miner in self.nodes[:2]: + assert wait_until(lambda: conflicting_txid in miner.getrawmempool(), timeout=10) + assert conflicting_txid not in [entry['txid'] for entry in miner.getblocktemplate()['transactions']] + + self.test_node.send_message(msg_tx(tx)) + self.wait_for_instantlock(txid, node, do_assert=True) + assert txid in node.getrawmempool() + assert conflicting_txid not in node.getrawmempool() + + set_mocktime(get_mocktime() + 1) + set_node_times(self.nodes, get_mocktime()) + block = node.generate(1)[0] + assert txid in node.getblock(block)['tx'] + self.wait_for_chainlocked_block_all_nodes(block) + + def test_missing_transaction_request_rotation(self): + self.log.info("Testing missing locked transaction recovery after four silent peers") + node = self.nodes[0] + rawtx = self.create_raw_tx(node, node, 1, 1, 1)['hex'] + tx = FromHex(CTransaction(), rawtx) + tx.rehash() + txid = "%064x" % tx.sha256 + islock = self.create_islock(rawtx) + self.test_node.send_message(msg_inv([CInv(1, tx.sha256)])) + assert wait_until(lambda: self.test_node.tx_requests.get(tx.sha256, 0) == 1, timeout=10) + + providers = [] + for _ in range(4): + peer = TestNode() + peer.add_connection(NodeConn('127.0.0.1', p2p_port(0), node, peer)) + peer.wait_for_verack() + peer.send_message(msg_inv([CInv(1, tx.sha256)])) + assert peer.sync_with_ping() + providers.append(peer) + self.test_node.send_islock(islock) + assert wait_until(lambda: self.test_node.tx_requests.get(tx.sha256, 0) >= 2 and + all(peer.tx_requests.get(tx.sha256, 0) >= 1 for peer in providers[:3]), timeout=10) + assert_equal(providers[-1].tx_requests.get(tx.sha256, 0), 0) + assert txid not in node.getrawmempool() + + # Only the fifth advertiser has the body, so recovery must rotate beyond the initial four requests. + providers[-1].transactions[tx.sha256] = tx + set_mocktime(get_mocktime() + 31) + set_node_times(self.nodes, get_mocktime()) + assert wait_until(lambda: providers[-1].tx_requests.get(tx.sha256, 0) >= 1, timeout=10) + self.wait_for_instantlock(txid, node, do_assert=True) + assert txid in node.getrawmempool() + block = node.generate(1)[0] + assert txid in node.getblock(block)['tx'] + self.wait_for_chainlocked_block_all_nodes(block) + for peer in providers: + peer.connection.disconnect_node() + + def test_islock_before_block_without_txindex(self, lock_first): + self.log.info("Testing IS lock and block-only transaction without txindex (lock first: %s)", lock_first) + node = self.nodes[self.no_txindex_node_idx] + rawtx = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 1)['hex'] + tx = FromHex(CTransaction(), rawtx) + tx.rehash() + txid = "%064x" % tx.sha256 + islock = self.create_islock(rawtx) + + # Keep the block away from quorum members until the incomplete lock has finished. + isolate_node(node) + node.setnetworkactive(True) + peer = TestNode() + peer.add_connection(NodeConn('127.0.0.1', p2p_port(self.no_txindex_node_idx), node, peer)) + peer.wait_for_verack() + peer.send_message(msg_inv([CInv(1, tx.sha256)])) + assert wait_until(lambda: peer.tx_requests.get(tx.sha256, 0) == 1, timeout=10) + if lock_first: + peer.send_islock(islock) + assert wait_until(lambda: peer.tx_requests.get(tx.sha256, 0) >= 2, timeout=10) + assert txid not in node.getrawmempool() + + block = self.create_block(node, [tx]) + blockhash = "%064x" % block.sha256 + assert node.submitblock(ToHex(block)) is None + if not lock_first: + peer.send_islock(islock) + assert wait_until(lambda: any(t['txid'] == txid and t['instantlock'] + for t in node.getblock(blockhash, 2)['tx']), timeout=10) + assert txid not in node.getrawmempool() + connect_nodes(node, 0) + # The lock was relayed while isolated, so explicitly introduce it to the quorum members too. + self.test_node.send_islock(islock) + self.sync_all() + for peer_node in self.nodes: + assert wait_until(lambda: peer_node.getrawtransaction(txid, True)['instantlock'] or + peer_node.getblockheader(blockhash)['chainlock'], timeout=10) + self.wait_for_chainlocked_block_all_nodes(blockhash) + + def test_chainlock_prunes_pending_locks(self): + self.log.info("Testing ChainLock pruning of pending parents and a child IS lock") + node = self.nodes[self.no_txindex_node_idx] + isolate_node(node) + node.setnetworkactive(True) + peer = TestNode() + peer.add_connection(NodeConn('127.0.0.1', p2p_port(self.no_txindex_node_idx), node, peer)) + peer.wait_for_verack() + + rawtx = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 1)['hex'] + txid = self.nodes[0].sendrawtransaction(rawtx) + other_rawtx = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 1)['hex'] + self.nodes[0].sendrawtransaction(other_rawtx) + tx = FromHex(CTransaction(), rawtx) + other_tx = FromHex(CTransaction(), other_rawtx) + assert not ({i.prevout.serialize() for i in tx.vin} & {i.prevout.serialize() for i in other_tx.vin}) + inputs = [{"txid": txid, "vout": n} for n, out in enumerate(tx.vout) if out.nValue == COIN] + child_rawtx = self.nodes[0].createrawtransaction(inputs, {self.nodes[0].getnewaddress(): 0.999}) + child_rawtx = self.nodes[0].signrawtransaction(child_rawtx)['hex'] + self.nodes[0].sendrawtransaction(child_rawtx) + locks = [self.create_islock(rawtx), self.create_islock(child_rawtx), self.create_islock(other_rawtx)] + lock_hashes = {uint256_from_str(hash256(lock.serialize())) for lock in locks} + + # The isolated node receives neither transaction body, including the pending lock's parent. + for lock in reversed(locks): + peer.send_message(msg_inv([CInv(1, lock.txid)])) + assert wait_until(lambda: peer.tx_requests.get(lock.txid, 0) == 1, timeout=10) + peer.send_islock(lock) + assert wait_until(lambda: peer.tx_requests.get(lock.txid, 0) >= 2, timeout=10) + peer.send_message(msg_getdata([CInv(30, h) for h in lock_hashes])) + assert wait_until(lambda: peer.received_islocks.issuperset(lock_hashes), timeout=10) + + # One confirmed transaction must prune every independent conflicting lock, as well as its descendants. + conflict_inputs = [{"txid": "%064x" % txin.prevout.hash, "vout": txin.prevout.n} + for parent in (tx, other_tx) for txin in parent.vin] + input_value = sum(node.gettxout(txin['txid'], txin['vout'], False)['value'] for txin in conflict_inputs) + conflicting_rawtx = self.nodes[0].createrawtransaction( + conflict_inputs, {self.nodes[0].getnewaddress(): input_value - Decimal('0.001')}) + conflicting_rawtx = self.nodes[0].signrawtransaction(conflicting_rawtx)['hex'] + block = self.create_block(node, [FromHex(CTransaction(), conflicting_rawtx)]) + blockhash = "%064x" % block.sha256 + old_tip = node.getbestblockhash() + assert_equal(node.submitblock(ToHex(block)), "conflict-pending-tx-lock") + assert_equal(node.getbestblockhash(), old_tip) + assert_equal(node.getblockheader(blockhash)['confirmations'], -1) + assert_equal(next(tip['status'] for tip in node.getchaintips() if tip['hash'] == blockhash), "valid-headers") + clsig = self.create_chainlock(node.getblockcount() + 1, block.sha256) + peer.send_message(clsig) + assert peer.sync_with_ping() + assert_equal(node.submitblock(ToHex(block)), "duplicate") + self.wait_for_chainlock(node, blockhash) + peer.send_message(msg_getdata([CInv(30, h) for h in lock_hashes])) + assert wait_until(lambda: peer.notfound_islocks.issuperset(lock_hashes), timeout=10) + self.test_node.send_message(clsig) + assert self.test_node.sync_with_ping() + connect_nodes(node, 0) + self.sync_all() + self.wait_for_chainlocked_block_all_nodes(blockhash) + def test_chainlock_overrides_islock(self, test_block_conflict): # create three raw TXs, they will conflict with each other rawtx1 = self.create_raw_tx(self.nodes[0], self.nodes[0], 1, 1, 100)['hex'] @@ -169,7 +404,7 @@ def test_islock_overrides_nonchainlock(self): assert(self.nodes[0].getrawtransaction(rawtx1_txid, True)['confirmations'] > 0) assert_raises_jsonrpc(-25, "Missing inputs", self.nodes[0].sendrawtransaction, rawtx2) - # Send the ISLOCK, which should result in the last 2 blocks to be invalidated, even though the nodes don't know + # Send the ISLOCK, which should result in the last 2 blocks to be disconnected, even though the nodes don't know # the locked transaction yet self.test_node.send_islock(islock) sleep(5) @@ -177,8 +412,24 @@ def test_islock_overrides_nonchainlock(self): assert(self.nodes[0].getbestblockhash() == good_tip) assert(self.nodes[1].getbestblockhash() == good_tip) - # Send the actual transaction and mine it + # Incomplete locks leave the conflicting transaction available until the locked TX arrives. + for node in self.nodes: + assert rawtx1_txid in node.getrawmempool() + + # A new candidate must not reactivate the higher-work conflicting branch while its lock is pending. + block = self.create_block(self.nodes[0]) + blockhash = "%064x" % block.sha256 + # Deliver the lower-work candidate explicitly, since header sync still prefers the old higher-work branch. + for node in self.nodes: + assert node.submitblock(ToHex(block)) in (None, "duplicate") + assert_equal(node.getbestblockhash(), blockhash) + self.nodes[0].sendrawtransaction(rawtx2) + set_mocktime(get_mocktime() + 60) + set_node_times(self.nodes, get_mocktime()) + for node in self.nodes: + self.wait_for_instantlock(rawtx2_txid, node, do_assert=True) + assert_raises_jsonrpc(-26, "tx-txlock-conflict", self.nodes[0].sendrawtransaction, rawtx1) self.nodes[0].generate(2) self.sync_all() diff --git a/qa/rpc-tests/test_framework/mininode.py b/qa/rpc-tests/test_framework/mininode.py index ff174986ad..516aa9e807 100644 --- a/qa/rpc-tests/test_framework/mininode.py +++ b/qa/rpc-tests/test_framework/mininode.py @@ -391,23 +391,23 @@ def serialize(self): def __repr__(self): return "CInv(type=%s hash=%064x)" \ - % (self.typemap[self.type], self.hash) + % (self.typemap.get(self.type, self.type), self.hash) class msg_notfound(): command = b"notfound" - def __init__(self): - pass + def __init__(self, inv=None): + self.inv = inv if inv is not None else [] def deserialize(self, f): - pass + self.inv = deser_vector(f, CInv) def serialize(self): - return b"" + return ser_vector(self.inv) def __repr__(self): - return "msg_notfound()" + return "msg_notfound(inv=%s)" % repr(self.inv) class CBlockLocator(object): @@ -2016,7 +2016,7 @@ def on_reject(self, conn, message): pass def on_open(self, conn): pass def on_close(self, conn): pass def on_mempool(self, conn): pass - def on_notfound(self, message): pass + def on_notfound(self, conn, message): pass def on_pong(self, conn, message): pass def on_feefilter(self, conn, message): pass def on_sendheaders(self, conn, message): pass diff --git a/qa/rpc-tests/test_framework/test_framework.py b/qa/rpc-tests/test_framework/test_framework.py index 2c6d187168..558ebb3718 100644 --- a/qa/rpc-tests/test_framework/test_framework.py +++ b/qa/rpc-tests/test_framework/test_framework.py @@ -470,6 +470,9 @@ def prepare_masternode(self, idx): # send to same address to reserve some funds for fees self.nodes[0].sendtoaddress(address, 0.001) + # Without quorums, funding and registration transactions must age past the ten-minute safe-mining delay. + set_mocktime(get_mocktime() + 601) + set_node_times(self.nodes, get_mocktime()) ownerAddr = self.nodes[0].getnewaddress() votingAddr = self.nodes[0].getnewaddress() @@ -482,6 +485,8 @@ def prepare_masternode(self, idx): else: self.nodes[0].generate(1) proTxHash = self.nodes[0].protx('register', txid, collateral_vout, '127.0.0.1:%d' % port, ownerAddr, bls['public'], votingAddr, 0, rewardsAddr, address) + set_mocktime(get_mocktime() + 601) + set_node_times(self.nodes, get_mocktime()) self.nodes[0].generate(1) self.mninfo.append(ZnodeInfo(proTxHash, ownerAddr, votingAddr, bls['public'], bls['secret'], address, txid, collateral_vout)) diff --git a/src/llmq/quorums_chainlocks.h b/src/llmq/quorums_chainlocks.h index 78171146bb..874883ea81 100644 --- a/src/llmq/quorums_chainlocks.h +++ b/src/llmq/quorums_chainlocks.h @@ -47,6 +47,7 @@ class CChainLockSig class CChainLocksHandler : public CRecoveredSigsListener { friend struct CChainLocksHandlerTestAccess; + friend struct CInstantSendManagerTestAccess; static const int64_t CLEANUP_INTERVAL = 1000 * 30; static const int64_t CLEANUP_SEEN_TIMEOUT = 24 * 60 * 60 * 1000; diff --git a/src/llmq/quorums_instantsend.cpp b/src/llmq/quorums_instantsend.cpp index f3b7d3696e..c9cffcf90e 100644 --- a/src/llmq/quorums_instantsend.cpp +++ b/src/llmq/quorums_instantsend.cpp @@ -21,6 +21,7 @@ #include "primitives/mint_spend.h" #include #include +#include #include namespace llmq @@ -357,8 +358,7 @@ void CInstantSendManager::Start() { LOCK(cs_main); - if (chainActive.Tip()) - isNewInstantSendEnabled = CSporkManager::GetSporkManager()->IsFeatureEnabled(CSporkAction::featureInstantSend, chainActive.Tip()); + UpdateActiveState(chainActive.Tip()); } workThread = std::thread(&TraceThread >, "instantsend", std::function(std::bind(&CInstantSendManager::WorkThreadMain, this))); @@ -728,7 +728,7 @@ void CInstantSendManager::ProcessMessageInstantSendLock(CNode* pfrom, const llmq if (db.GetInstantSendLockByHash(hash) != nullptr) { return; } - if (pendingInstantSendLocks.count(hash)) { + if (pendingInstantSendLocks.count(hash) || pendingNoTxInstantSendLocks.count(hash)) { return; } @@ -760,19 +760,36 @@ bool CInstantSendManager::PreVerifyInstantSendLock(NodeId nodeId, const llmq::CI bool CInstantSendManager::ProcessPendingInstantSendLocks() { + if (!IsNewInstantSendEnabled()) { + return false; + } + + std::vector> ready; + std::vector retryTxs; decltype(pendingInstantSendLocks) pend; { LOCK(cs); + for (const auto& p : pendingNoTxInstantSendLocks) { + if (p.second.tx) { + ready.emplace_back(p); + } else if (p.second.nextRequestTime <= GetTime()) { + retryTxs.emplace_back(p.second.islock.txid); + } + } pend = std::move(pendingInstantSendLocks); } - if (pend.empty()) { - return false; + for (const auto& p : ready) { + ProcessInstantSendLock(p.second.from, p.first, p.second.islock); + } + for (const auto& txid : retryTxs) { + AskNodesForLockedTx(txid); } - if (!IsNewInstantSendEnabled()) - return false; + if (pend.empty()) { + return !ready.empty() || !retryTxs.empty(); + } int tipHeight; { @@ -912,65 +929,116 @@ std::unordered_set CInstantSendManager::ProcessPendingInstantSendLocks( void CInstantSendManager::ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLock& islock) { - { - LOCK(cs_main); - g_connman->RemoveAskFor(hash); - } - CTransactionRef tx; uint256 hashBlock; const CBlockIndex* pindexMined = nullptr; - // we ignore failure here as we must be able to propagate the lock even if we don't have the TX locally - if (GetTransaction(islock.txid, tx, Params().GetConsensus(), hashBlock)) { - if (!hashBlock.IsNull()) { + bool newPendingLock = false; + { + // Register the pending lock before a concurrent mempool acceptance can notify SyncTransaction. + LOCK(cs_main); + g_connman->RemoveAskFor(hash); + { + LOCK(cs); + auto it = pendingNoTxInstantSendLocks.find(hash); + if (it != pendingNoTxInstantSendLocks.end()) { + tx = it->second.tx; + pindexMined = it->second.pindexMined; + if (pindexMined && !chainActive.Contains(pindexMined)) { + pindexMined = nullptr; + } + } + } + // we ignore failure here as we must be able to propagate the lock even if we don't have the TX locally + if (!tx && GetTransaction(islock.txid, tx, Params().GetConsensus(), hashBlock, !fTxIndex) && !hashBlock.IsNull()) { + pindexMined = mapBlockIndex.at(hashBlock); + } + if (!tx) { + // Fully spent transactions need their tracked block when txindex is disabled. + const CBlockIndex* trackedBlock = nullptr; { - LOCK(cs_main); - pindexMined = mapBlockIndex.at(hashBlock); + LOCK(cs); + auto it = nonLockedTxs.find(islock.txid); + if (it != nonLockedTxs.end()) { + trackedBlock = it->second.pindexMined; + } + } + if (trackedBlock && chainActive.Contains(trackedBlock)) { + CBlock block; + if (ReadBlockFromDisk(block, trackedBlock, Params().GetConsensus())) { + for (const auto& candidate : block.vtx) { + if (candidate->GetHash() == islock.txid) { + tx = candidate; + pindexMined = trackedBlock; + break; + } + } + } } + } - // Let's see if the TX that was locked by this islock is already mined in a ChainLocked block. If yes, - // we can simply ignore the islock, as the ChainLock implies locking of all TXs in that chain + if (pindexMined) { + // A ChainLock already implies locking of all transactions in its chain. if (llmq::chainLocksHandler->HasChainLock(pindexMined->nHeight, pindexMined->GetBlockHash())) { LogPrint("instantsend", "CInstantSendManager::%s -- txlock=%s, islock=%s: dropping islock as it already got a ChainLock in block %s, peer=%d\n", __func__, - islock.txid.ToString(), hash.ToString(), hashBlock.ToString(), from); + islock.txid.ToString(), hash.ToString(), pindexMined->GetBlockHash().ToString(), from); + LOCK(cs); + ErasePendingInstantSendLock(hash); return; } } - } - { - LOCK(cs); + { + LOCK(cs); - LogPrint("instantsend", "CInstantSendManager::%s -- txid=%s, islock=%s: processsing islock, peer=%d\n", __func__, - islock.txid.ToString(), hash.ToString(), from); + LogPrint("instantsend", "CInstantSendManager::%s -- txid=%s, islock=%s: processsing islock, peer=%d\n", __func__, + islock.txid.ToString(), hash.ToString(), from); - creatingInstantSendLocks.erase(islock.GetRequestId()); - txToCreatingInstantSendLocks.erase(islock.txid); + creatingInstantSendLocks.erase(islock.GetRequestId()); + txToCreatingInstantSendLocks.erase(islock.txid); - CInstantSendLockPtr otherIsLock; - if (db.GetInstantSendLockByHash(hash)) { - return; - } - otherIsLock = db.GetInstantSendLockByTxid(islock.txid); - if (otherIsLock != nullptr) { - LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: duplicate islock, other islock=%s, peer=%d\n", __func__, - islock.txid.ToString(), hash.ToString(), ::SerializeHash(*otherIsLock).ToString(), from); - } - for (auto& in : islock.inputs) { - otherIsLock = db.GetInstantSendLockByInput(in); + CInstantSendLockPtr otherIsLock; + if (db.HasArchivedInstantSendLock(hash)) { + ErasePendingInstantSendLock(hash); + return; + } + if (db.GetInstantSendLockByHash(hash)) { + ErasePendingInstantSendLock(hash); + return; + } + otherIsLock = db.GetInstantSendLockByTxid(islock.txid); if (otherIsLock != nullptr) { - LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: conflicting input in islock. input=%s, other islock=%s, peer=%d\n", __func__, - islock.txid.ToString(), hash.ToString(), in.ToStringShort(), ::SerializeHash(*otherIsLock).ToString(), from); + LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: duplicate islock, other islock=%s, peer=%d\n", __func__, + islock.txid.ToString(), hash.ToString(), ::SerializeHash(*otherIsLock).ToString(), from); + } + for (auto& in : islock.inputs) { + otherIsLock = db.GetInstantSendLockByInput(in); + if (otherIsLock != nullptr) { + LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: conflicting input in islock. input=%s, other islock=%s, peer=%d\n", __func__, + islock.txid.ToString(), hash.ToString(), in.ToStringShort(), ::SerializeHash(*otherIsLock).ToString(), from); + } } - } - db.WriteNewInstantSendLock(hash, islock); - if (pindexMined) { - db.WriteInstantSendLockMined(hash, pindexMined->nHeight); - } + if (tx == nullptr) { + auto indexed = pendingInstantSendLocksByTxid.emplace(islock.txid, hash); + if (!indexed.second && indexed.first->second != hash) { + return; + } + newPendingLock = pendingNoTxInstantSendLocks.emplace(hash, PendingInstantSendLock{from, islock, nullptr, nullptr, {}}).second; + } else { + db.WriteNewInstantSendLock(hash, islock); + ErasePendingInstantSendLock(hash); + if (pindexMined) { + db.WriteInstantSendLockMined(hash, pindexMined->nHeight); + } + } - // This will also add children TXs to pendingRetryTxs - RemoveNonLockedTx(islock.txid, true); + // This will also add children TXs to pendingRetryTxs + RemoveNonLockedTx(islock.txid, true); + } + if (newPendingLock) { + // Pending locks change which retained mempool transactions miners may include. + mempool.AddTransactionsUpdated(1); + } } CInv inv(MSG_ISLOCK, hash); @@ -982,19 +1050,19 @@ void CInstantSendManager::ProcessInstantSendLock(NodeId from, const uint256& has g_connman->RelayInvFiltered(inv, islock.txid, LLMQS_PROTO_VERSION); } - RemoveMempoolConflictsForLock(hash, islock); ResolveBlockConflicts(hash, islock); - UpdateWalletTransaction(islock.txid, tx); + if (tx != nullptr) { + RemoveMempoolConflictsForLock(hash, islock); + UpdateWalletTransaction(islock.txid, tx); + } else { + AskNodesForLockedTx(islock.txid); + } } void CInstantSendManager::UpdateWalletTransaction(const uint256& txid, const CTransactionRef& tx) { #ifdef ENABLE_WALLET - if (!pwalletMain) { - return; - } - - if (pwalletMain->UpdatedTransaction(txid)) { + if (pwalletMain && pwalletMain->UpdatedTransaction(txid)) { // notify an external script once threshold is reached std::string strCmd = GetArg("-instantsendnotify", ""); if (!strCmd.empty()) { @@ -1006,7 +1074,7 @@ void CInstantSendManager::UpdateWalletTransaction(const uint256& txid, const CTr if (tx) { GetMainSignals().NotifyTransactionLock(*tx); - // bump mempool counter to make sure newly mined txes are picked up by getblocktemplate + // bump mempool counter to make sure newly locked txes are picked up by getblocktemplate mempool.AddTransactionsUpdated(1); } } @@ -1030,7 +1098,7 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI // Are we called from validation.cpp/MemPoolConflictRemovalTracker? // TODO refactor this when we backport the BlockConnected signal from Bitcoin, as it gives better info about // conflicted TXs - bool isConflictRemoved = isDisconnect && !inMempool; + bool isConflictRemoved = posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK && !inMempool; if (isConflictRemoved) { LOCK(cs); @@ -1043,9 +1111,19 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI bool allowReSigning = !inMempool && !isDisconnect; uint256 islockHash; + bool pendingIslock = false; { LOCK(cs); islockHash = db.GetInstantSendLockHashByTxid(tx.GetHash()); + if (inMempool || (pindex && !isDisconnect)) { + auto it = pendingInstantSendLocksByTxid.find(tx.GetHash()); + if (it != pendingInstantSendLocksByTxid.end()) { + auto& pending = pendingNoTxInstantSendLocks.at(it->second); + pending.tx = MakeTransactionRef(tx_); + pending.pindexMined = pindex; + pendingIslock = true; + } + } // update DB about when an IS lock was mined if (!islockHash.IsNull() && pindex) { @@ -1063,7 +1141,7 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI } bool chainlocked = pindex && chainLocksHandler->HasChainLock(pindex->nHeight, pindex->GetBlockHash()); - if (islockHash.IsNull() && !chainlocked) { + if (islockHash.IsNull() && !chainlocked && !pendingIslock) { ProcessTx(tx, allowReSigning, Params().GetConsensus()); } @@ -1154,7 +1232,7 @@ void CInstantSendManager::RemoveConflictedTx(const CTransaction& tx) RemoveNonLockedTx(tx.GetHash(), false); for (const auto& in : tx.vin) { - auto inputRequestId = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in)); + auto inputRequestId = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in.prevout)); inputRequestIds.erase(inputRequestId); } } @@ -1164,6 +1242,12 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock) HandleFullyConfirmedBlock(pindexChainLock); } +void CInstantSendManager::UpdateActiveState(const CBlockIndex* pindex) +{ + AssertLockHeld(cs_main); + isNewInstantSendEnabled = pindex && CSporkManager::GetSporkManager()->IsFeatureEnabled(CSporkAction::featureInstantSend, pindex); +} + void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew) { bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev->nHeight >= Params().GetConsensus().DIP0008Height; @@ -1261,7 +1345,6 @@ void CInstantSendManager::RemoveMempoolConflictsForLock(const uint256& hash, con RemoveConflictedTx(*p.second); } } - AskNodesForLockedTx(islock.txid); } } @@ -1315,6 +1398,12 @@ void CInstantSendManager::ResolveBlockConflicts(const uint256& islockHash, const return; } + bool isLockedTxKnown; + { + LOCK(cs); + isLockedTxKnown = pendingNoTxInstantSendLocks.count(islockHash) == 0; + } + bool activateBestChain = false; for (const auto& p : conflicts) { auto pindex = p.first; @@ -1332,12 +1421,20 @@ void CInstantSendManager::ResolveBlockConflicts(const uint256& islockHash, const CValidationState state; // need non-const pointer auto pindex2 = mapBlockIndex.at(pindex->GetBlockHash()); + if (!isLockedTxKnown) { + AddPendingBlockConflict(islockHash, pindex2); + } if (!InvalidateBlock(state, Params(), pindex2)) { LogPrintf("CInstantSendManager::%s -- InvalidateBlock failed: %s\n", __func__, FormatStateMessage(state)); // This should not have happened and we are in a state were it's not safe to continue anymore assert(false); } - activateBestChain = true; + if (isLockedTxKnown) { + activateBestChain = true; + } else { + // Disconnect to make the inputs available, but do not permanently invalidate without the TX. + ResetBlockFailureFlags(pindex2); + } } if (activateBestChain) { @@ -1361,10 +1458,40 @@ void CInstantSendManager::RemoveChainLockConflictingLock(const uint256& islockHa } LOCK(cs); - auto removedIslocks = db.RemoveChainedInstantSendLocks(islockHash, islock.txid, tipHeight); - for (auto& h : removedIslocks) { + std::vector> toRemove{{islockHash, islock.txid}}; + std::unordered_set added{islockHash}; + for (size_t i = 0; i < toRemove.size(); ++i) { + const auto parent = toRemove[i].second; + for (const auto& childHash : db.GetInstantSendLocksByParent(parent)) { + auto child = db.GetInstantSendLockByHash(childHash); + if (child && added.emplace(childHash).second) { + toRemove.emplace_back(childHash, child->txid); + } + } + for (const auto& p : pendingNoTxInstantSendLocks) { + for (const auto& in : p.second.islock.inputs) { + if (in.hash == parent && added.emplace(p.first).second) { + toRemove.emplace_back(p.first, p.second.islock.txid); + } + } + } + } + for (const auto& p : toRemove) { + ErasePendingInstantSendLock(p.first); + pendingInstantSendLocks.erase(p.first); + db.RemoveChainedInstantSendLocks(p.first, p.second, tipHeight); LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: removed (child) ISLOCK %s\n", __func__, - islock.txid.ToString(), islockHash.ToString(), h.ToString()); + islock.txid.ToString(), islockHash.ToString(), p.first.ToString()); + } +} + +void CInstantSendManager::ErasePendingInstantSendLock(const uint256& hash) +{ + AssertLockHeld(cs); + auto it = pendingNoTxInstantSendLocks.find(hash); + if (it != pendingNoTxInstantSendLocks.end()) { + pendingInstantSendLocksByTxid.erase(it->second.islock.txid); + pendingNoTxInstantSendLocks.erase(it); } } @@ -1395,25 +1522,46 @@ bool CInstantSendManager::RemoveISLockByTxId(const uint256& txid) void CInstantSendManager::AskNodesForLockedTx(const uint256& txid) { - std::vector nodesToAskFor; + std::vector availableNodes; g_connman->ForEachNode([&](CNode* pnode) { LOCK(pnode->cs_filter); if (pnode->filterInventoryKnown.contains(txid)) { pnode->AddRef(); - nodesToAskFor.emplace_back(pnode); + availableNodes.emplace_back(pnode); } }); + std::sort(availableNodes.begin(), availableNodes.end(), [](const CNode* a, const CNode* b) { return a->id < b->id; }); + std::vector nodesToAskFor; { + LOCK(cs); + auto it = pendingInstantSendLocksByTxid.find(txid); + if (it != pendingInstantSendLocksByTxid.end()) { + auto& pending = pendingNoTxInstantSendLocks.at(it->second); + // Retry in bounded batches, rotating past peers that may no longer have the body. + pending.nextRequestTime = GetTime() + 30; + const auto start = std::upper_bound(availableNodes.begin(), availableNodes.end(), pending.lastRequestedPeer, + [](NodeId id, const CNode* node) { return id < node->id; }) - availableNodes.begin(); + for (size_t i = 0; i < std::min(4, availableNodes.size()); ++i) { + nodesToAskFor.emplace_back(availableNodes[(start + i) % availableNodes.size()]); + } + if (!nodesToAskFor.empty()) { + pending.lastRequestedPeer = nodesToAskFor.back()->id; + } + } + } + if (!nodesToAskFor.empty()) { LOCK(cs_main); + // A failed earlier download must not suppress or delay recovery of a locked TX. + g_connman->RemoveAskFor(txid); for (CNode* pnode : nodesToAskFor) { LogPrintf("CInstantSendManager::%s -- txid=%s: asking other peer %d for correct TX\n", __func__, txid.ToString(), pnode->id); CInv inv(MSG_TX, txid); - pnode->AskFor(inv); + pnode->AskFor(inv, 0); } } - for (CNode* pnode : nodesToAskFor) { + for (CNode* pnode : availableNodes) { pnode->Release(); } } @@ -1492,7 +1640,8 @@ bool CInstantSendManager::AlreadyHave(const CInv& inv) } LOCK(cs); - return db.GetInstantSendLockByHash(inv.hash) != nullptr || pendingInstantSendLocks.count(inv.hash) != 0 || db.HasArchivedInstantSendLock(inv.hash); + return db.GetInstantSendLockByHash(inv.hash) != nullptr || pendingInstantSendLocks.count(inv.hash) != 0 || + pendingNoTxInstantSendLocks.count(inv.hash) != 0 || db.HasArchivedInstantSendLock(inv.hash); } bool CInstantSendManager::GetInstantSendLockByHash(const uint256& hash, llmq::CInstantSendLock& ret) @@ -1503,11 +1652,16 @@ bool CInstantSendManager::GetInstantSendLockByHash(const uint256& hash, llmq::CI LOCK(cs); auto islock = db.GetInstantSendLockByHash(hash); - if (!islock) { - return false; + if (islock) { + ret = *islock; + return true; } - ret = *islock; - return true; + auto it = pendingNoTxInstantSendLocks.find(hash); + if (it != pendingNoTxInstantSendLocks.end()) { + ret = it->second.islock; + return true; + } + return false; } bool CInstantSendManager::IsLocked(const uint256& txHash) @@ -1520,12 +1674,22 @@ bool CInstantSendManager::IsLocked(const uint256& txHash) return db.GetInstantSendLockByTxid(txHash) != nullptr; } +bool CInstantSendManager::IsWaitingForTx(const uint256& txHash) +{ + if (!IsNewInstantSendEnabled()) { + return false; + } + + LOCK(cs); + return pendingInstantSendLocksByTxid.count(txHash) != 0; +} + bool CInstantSendManager::IsConflicted(const CTransaction& tx) { return GetConflictingLock(tx) != nullptr; } -CInstantSendLockPtr CInstantSendManager::GetConflictingLock(const CTransaction& tx_) +CInstantSendLockPtr CInstantSendManager::GetConflictingLock(const CTransaction& tx_, bool includePending) { if (!IsNewInstantSendEnabled()) { return nullptr; @@ -1544,9 +1708,49 @@ CInstantSendLockPtr CInstantSendManager::GetConflictingLock(const CTransaction& return otherIsLock; } } + if (includePending) { + for (const auto& p : pendingNoTxInstantSendLocks) { + if (p.second.islock.txid == tx.GetHash()) { + continue; + } + for (const auto& in : tx.vin) { + if (std::find(p.second.islock.inputs.begin(), p.second.islock.inputs.end(), in.prevout) != p.second.islock.inputs.end()) { + return std::make_shared(p.second.islock); + } + } + } + } return nullptr; } +bool CInstantSendManager::AddPendingBlockConflict(const uint256& islockHash, const CBlockIndex* pindex) +{ + LOCK(cs); + auto it = pendingNoTxInstantSendLocks.find(islockHash); + if (it == pendingNoTxInstantSendLocks.end()) { + return false; + } + it->second.conflictingBlocks.emplace(pindex); + return true; +} + +bool CInstantSendManager::IsBlockExcluded(const CBlockIndex* pindex) +{ + if (!IsNewInstantSendEnabled()) { + return false; + } + LOCK(cs); + for (const auto& p : pendingNoTxInstantSendLocks) { + for (const auto* conflict : p.second.conflictingBlocks) { + if (pindex->GetAncestor(conflict->nHeight) == conflict && + !chainLocksHandler->HasChainLock(conflict->nHeight, conflict->GetBlockHash())) { + return true; + } + } + } + return false; +} + size_t CInstantSendManager::GetInstantSendLockCount() { LOCK(cs); diff --git a/src/llmq/quorums_instantsend.h b/src/llmq/quorums_instantsend.h index 2cc0722c7b..991f50ec27 100644 --- a/src/llmq/quorums_instantsend.h +++ b/src/llmq/quorums_instantsend.h @@ -13,6 +13,7 @@ #include #include +#include namespace llmq { @@ -74,6 +75,9 @@ class CInstantSendDb class CInstantSendManager : public CRecoveredSigsListener { + friend struct CInstantSendManagerTestAccess; + friend struct CInstantSendManagerSparkTestAccess; + private: CCriticalSection cs; CInstantSendDb db; @@ -99,6 +103,19 @@ class CInstantSendManager : public CRecoveredSigsListener // Incoming and not verified yet std::unordered_map, StaticSaltedHasher> pendingInstantSendLocks; + struct PendingInstantSendLock { + NodeId from; + CInstantSendLock islock; + CTransactionRef tx; + const CBlockIndex* pindexMined{nullptr}; + std::set conflictingBlocks; + int64_t nextRequestTime{0}; + NodeId lastRequestedPeer{-1}; + }; + // Keep verified locks pending until the worker can complete them with a transaction body. + std::unordered_map pendingNoTxInstantSendLocks; + std::unordered_map pendingInstantSendLocksByTxid; + // TXs which are neither IS locked nor ChainLocked. We use this to determine for which TXs we need to retry IS locking // of child TXs struct NonLockedTxInfo { @@ -123,13 +140,17 @@ class CInstantSendManager : public CRecoveredSigsListener void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock); bool IsLocked(const uint256& txHash); + bool IsWaitingForTx(const uint256& txHash); bool GetInstantSendLockByHash(const uint256& hash, CInstantSendLock& ret); - CInstantSendLockPtr GetConflictingLock(const CTransaction& tx); + CInstantSendLockPtr GetConflictingLock(const CTransaction& tx, bool includePending = false); + bool AddPendingBlockConflict(const uint256& islockHash, const CBlockIndex* pindex); + bool IsBlockExcluded(const CBlockIndex* pindex); void RemoveChainLockConflictingLock(const uint256& islockHash, const CInstantSendLock& islock); bool RemoveISLockByTxId(const uint256& txid); void UpdatedBlockTip(const CBlockIndex* pindexNew); + void UpdateActiveState(const CBlockIndex* pindex); void NotifyChainLock(const CBlockIndex* pindexChainLock); void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman); @@ -155,6 +176,7 @@ class CInstantSendManager : public CRecoveredSigsListener bool ProcessPendingInstantSendLocks(); std::unordered_set ProcessPendingInstantSendLocks(int signHeight, const std::unordered_map, StaticSaltedHasher>& pend, bool ban); void ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLock& islock); + void ErasePendingInstantSendLock(const uint256& hash); void UpdateWalletTransaction(const uint256& txid, const CTransactionRef& tx); void AddNonLockedTx(const CTransactionRef& tx); diff --git a/src/miner.cpp b/src/miner.cpp index 943be836d3..26b5f1c1b3 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -51,6 +51,7 @@ #include "llmq/quorums_blockprocessor.h" #include "llmq/quorums_chainlocks.h" +#include "llmq/quorums_instantsend.h" #include @@ -366,6 +367,10 @@ bool BlockAssembler::TestPackageTransactions(const CTxMemPool::setEntries& packa } if (!IsFinalTx(it->GetTx(), nHeight, nLockTimeCutoff)) return false; + if (nHeight >= chainparams.GetConsensus().nInstantSendBlockFilteringStartHeight && + llmq::quorumInstantSendManager->GetConflictingLock(it->GetTx(), true)) { + return false; + } if (!llmq::chainLocksHandler->IsTxSafeForMining(it->GetTx().GetHash())) { return false; } @@ -387,6 +392,10 @@ bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter) if (!spark::IsSparkSpendFormatAllowed(iter->GetTx(), nHeight)) { return false; } + if (nHeight >= chainparams.GetConsensus().nInstantSendBlockFilteringStartHeight && + llmq::quorumInstantSendManager->GetConflictingLock(iter->GetTx(), true)) { + return false; + } if (nBlockWeight + iter->GetTxWeight() >= nBlockMaxWeight) { // If the block is so close to full that no more txs will fit diff --git a/src/net_processing.cpp b/src/net_processing.cpp index e5867aefe7..8427717ece 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -943,7 +943,9 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main) recentRejects->reset(); } - return (recentRejects->contains(inv.hash) && !llmq::quorumInstantSendManager->IsLocked(inv.hash)) || + return (recentRejects->contains(inv.hash) && + !llmq::quorumInstantSendManager->IsLocked(inv.hash) && + !llmq::quorumInstantSendManager->IsWaitingForTx(inv.hash)) || mempool.exists(inv.hash) || mapOrphanTransactions.count(inv.hash) || pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 0)) || // Best effort: only try output 0 and 1 diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 2f2df8e9bf..fbdc1b1c3e 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -56,6 +56,7 @@ add_executable(test_firo ${CMAKE_CURRENT_SOURCE_DIR}/llmq_chainlock_cache_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/logging_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/llmq_dkgsessionhandler_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/llmq_instantsend_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/llmq_signing_shares_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/main_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mbstring_tests.cpp diff --git a/src/test/llmq_instantsend_tests.cpp b/src/test/llmq_instantsend_tests.cpp new file mode 100644 index 0000000000..a3c0256c53 --- /dev/null +++ b/src/test/llmq_instantsend_tests.cpp @@ -0,0 +1,363 @@ +// Copyright (c) 2026 The Firo developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "chainparams.h" +#include "evo/spork.h" +#include "llmq/quorums_chainlocks.h" +#include "llmq/quorums_instantsend.h" +#include "test/test_bitcoin.h" +#include "txmempool.h" +#include "validation.h" +#include "validationinterface.h" + +#include +#include + +namespace llmq +{ +struct CInstantSendManagerTestAccess +{ + static void Enable(CInstantSendManager& manager) + { + manager.isNewInstantSendEnabled = true; + } + + static uint256 AddPending(CInstantSendManager& manager, const CInstantSendLock& islock) + { + LOCK(manager.cs); + const uint256 hash = ::SerializeHash(islock); + manager.pendingNoTxInstantSendLocks.emplace(hash, + CInstantSendManager::PendingInstantSendLock{-1, islock, nullptr, nullptr, {}}); + manager.pendingInstantSendLocksByTxid.emplace(islock.txid, hash); + return hash; + } + + static bool HasCallbackTransaction(CInstantSendManager& manager, const uint256& hash, + const CTransactionRef& tx, const CBlockIndex* pindex) + { + LOCK(manager.cs); + const auto& pending = manager.pendingNoTxInstantSendLocks.at(hash); + return pending.tx && pending.tx->GetHash() == tx->GetHash() && pending.pindexMined == pindex; + } + + static bool ProcessPending(CInstantSendManager& manager) + { + return manager.ProcessPendingInstantSendLocks(); + } + + static void ProcessLock(CInstantSendManager& manager, const CInstantSendLock& islock) + { + manager.ProcessInstantSendLock(-1, ::SerializeHash(islock), islock); + } + + static void WriteLock(CInstantSendManager& manager, const CInstantSendLock& islock) + { + LOCK(manager.cs); + manager.db.WriteNewInstantSendLock(::SerializeHash(islock), islock); + } + + static bool RemoveConfirmed(CInstantSendManager& manager, int height, const uint256& hash) + { + LOCK(manager.cs); + return manager.db.RemoveConfirmedInstantSendLocks(height).count(hash) != 0; + } + + static void AddUnverified(CInstantSendManager& manager, const CInstantSendLock& islock) + { + LOCK(manager.cs); + manager.pendingInstantSendLocks.emplace(::SerializeHash(islock), std::make_pair(-1, islock)); + } + + static void SetChainLock(CChainLocksHandler& handler, const CBlockIndex* pindex) + { + LOCK(handler.cs); + handler.isEnforced = pindex != nullptr; + handler.bestChainLockBlockIndex = pindex; + } + + static void TrackConflict(CInstantSendManager& manager, const CTransactionRef& tx) + { + LOCK(manager.cs); + manager.AddNonLockedTx(tx); + for (const auto& in : tx->vin) { + manager.inputRequestIds.emplace(::SerializeHash(std::make_pair(std::string("inlock"), in.prevout))); + } + } + + static bool TrackingEmpty(CInstantSendManager& manager) + { + LOCK(manager.cs); + return manager.nonLockedTxs.empty() && manager.nonLockedTxsByInputs.empty() && manager.inputRequestIds.empty(); + } +}; +} // namespace llmq + +namespace +{ +CTransactionRef MakeTransaction(const uint256& parent, uint32_t output = 0) +{ + CMutableTransaction tx; + tx.vin.emplace_back(COutPoint(parent, output)); + tx.vout.emplace_back(1, CScript() << OP_TRUE); + return MakeTransactionRef(tx); +} + +llmq::CInstantSendLock MakeLock(const CTransactionRef& tx) +{ + llmq::CInstantSendLock islock; + islock.txid = tx->GetHash(); + for (const auto& in : tx->vin) { + islock.inputs.emplace_back(in.prevout); + } + return islock; +} +} // namespace + +BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_tests, TestingSetup) + +BOOST_AUTO_TEST_CASE(active_state_tracks_sporks_without_clearing_pending_locks) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + const auto islock = MakeLock(MakeTransaction(uint256S("1"))); + llmq::CInstantSendManagerTestAccess::AddPending(manager, islock); + CBlockIndex enabled; + CBlockIndex disabled; + disabled.SetActiveDisablingSporks({{CSporkAction::featureInstantSend, {0, 0}}}); + + LOCK(cs_main); + manager.UpdateActiveState(nullptr); + BOOST_CHECK(!manager.IsNewInstantSendEnabled()); + manager.UpdateActiveState(&enabled); + BOOST_CHECK(manager.IsNewInstantSendEnabled()); + BOOST_CHECK(manager.IsWaitingForTx(islock.txid)); + manager.UpdateActiveState(&disabled); + BOOST_CHECK(!manager.IsNewInstantSendEnabled()); + manager.UpdateActiveState(&enabled); + BOOST_CHECK(manager.IsNewInstantSendEnabled()); + BOOST_CHECK(manager.IsWaitingForTx(islock.txid)); + manager.UpdateActiveState(&disabled); + BOOST_CHECK(!manager.IsNewInstantSendEnabled()); + manager.UpdateActiveState(&enabled); + BOOST_CHECK(manager.IsNewInstantSendEnabled()); + manager.UpdateActiveState(nullptr); + BOOST_CHECK(!manager.IsNewInstantSendEnabled()); + manager.UpdateActiveState(&enabled); + BOOST_CHECK(manager.IsWaitingForTx(islock.txid)); +} + +BOOST_AUTO_TEST_CASE(block_callback_completes_pending_lock_without_txindex) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto tx = MakeTransaction(uint256S("1")); + const auto islock = MakeLock(tx); + const uint256 hash = llmq::CInstantSendManagerTestAccess::AddPending(manager, islock); + const auto* pindex = chainActive.Tip(); + const auto updates = mempool.GetTransactionsUpdated(); + + const bool previousTxIndex = fTxIndex; + BOOST_SCOPE_EXIT(previousTxIndex) { + fTxIndex = previousTxIndex; + } BOOST_SCOPE_EXIT_END + fTxIndex = false; + manager.SyncTransaction(*tx, pindex, 1); + const bool hasCallback = llmq::CInstantSendManagerTestAccess::HasCallbackTransaction(manager, hash, tx, pindex); + const bool processed = llmq::CInstantSendManagerTestAccess::ProcessPending(manager); + + BOOST_CHECK(hasCallback); + BOOST_CHECK(processed); + BOOST_CHECK(manager.IsLocked(tx->GetHash())); + BOOST_CHECK(!manager.IsWaitingForTx(tx->GetHash())); + BOOST_CHECK_EQUAL(mempool.GetTransactionsUpdated(), updates + 1); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::RemoveConfirmed(manager, pindex->nHeight, hash)); +} + +BOOST_AUTO_TEST_CASE(new_pending_lock_invalidates_cached_mining_template_once) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto islock = MakeLock(MakeTransaction(uint256S("1"))); + const bool previousTxIndex = fTxIndex; + BOOST_SCOPE_EXIT(previousTxIndex) { + fTxIndex = previousTxIndex; + } BOOST_SCOPE_EXIT_END + fTxIndex = true; + const auto updates = mempool.GetTransactionsUpdated(); + + llmq::CInstantSendManagerTestAccess::ProcessLock(manager, islock); + BOOST_CHECK(manager.IsWaitingForTx(islock.txid)); + BOOST_CHECK_EQUAL(mempool.GetTransactionsUpdated(), updates + 1); + llmq::CInstantSendManagerTestAccess::ProcessLock(manager, islock); + BOOST_CHECK_EQUAL(mempool.GetTransactionsUpdated(), updates + 1); +} + +BOOST_AUTO_TEST_CASE(chainlock_removes_descendants_across_pending_and_database) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto parent = MakeLock(MakeTransaction(uint256S("1"))); + const auto child = MakeLock(MakeTransaction(parent.txid)); + const auto grandchild = MakeLock(MakeTransaction(child.txid)); + const auto unrelated = MakeLock(MakeTransaction(uint256S("2"))); + const auto parentHash = llmq::CInstantSendManagerTestAccess::AddPending(manager, parent); + llmq::CInstantSendManagerTestAccess::WriteLock(manager, child); + const auto grandchildHash = llmq::CInstantSendManagerTestAccess::AddPending(manager, grandchild); + llmq::CInstantSendManagerTestAccess::AddPending(manager, unrelated); + BOOST_REQUIRE(manager.AddPendingBlockConflict(parentHash, chainActive.Tip())); + BOOST_REQUIRE(manager.AddPendingBlockConflict(grandchildHash, chainActive.Tip())); + BOOST_CHECK(manager.IsBlockExcluded(chainActive.Tip())); + + manager.RemoveChainLockConflictingLock(parentHash, parent); + + llmq::CInstantSendLock result; + for (const auto& islock : {parent, child, grandchild}) { + BOOST_CHECK(!manager.GetInstantSendLockByHash(::SerializeHash(islock), result)); + BOOST_CHECK(!manager.IsWaitingForTx(islock.txid)); + BOOST_CHECK(!manager.IsLocked(islock.txid)); + } + BOOST_CHECK(manager.IsWaitingForTx(unrelated.txid)); + BOOST_CHECK(!manager.IsBlockExcluded(chainActive.Tip())); +} + +BOOST_AUTO_TEST_CASE(chainlocked_descendant_overrides_pending_block_exclusion) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto islock = MakeLock(MakeTransaction(uint256S("1"))); + const auto hash = llmq::CInstantSendManagerTestAccess::AddPending(manager, islock); + const uint256 rootHash = uint256S("10"); + const uint256 descendantHash = uint256S("11"); + CBlockIndex root; + root.phashBlock = &rootHash; + root.pprev = chainActive.Tip(); + root.nHeight = root.pprev->nHeight + 1; + CBlockIndex descendant; + descendant.phashBlock = &descendantHash; + descendant.pprev = &root; + descendant.nHeight = root.nHeight + 1; + BOOST_REQUIRE(manager.AddPendingBlockConflict(hash, &root)); + BOOST_CHECK(manager.IsBlockExcluded(&descendant)); + BOOST_CHECK(!manager.IsBlockExcluded(chainActive.Tip())); + + llmq::CInstantSendManagerTestAccess::SetChainLock(*llmq::chainLocksHandler, &descendant); + BOOST_CHECK(!manager.IsBlockExcluded(&root)); + BOOST_CHECK(!manager.IsBlockExcluded(&descendant)); + BOOST_CHECK(manager.IsWaitingForTx(islock.txid)); + llmq::CInstantSendManagerTestAccess::SetChainLock(*llmq::chainLocksHandler, nullptr); +} + +BOOST_AUTO_TEST_CASE(unverified_locks_are_not_served) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto islock = MakeLock(MakeTransaction(uint256S("1"))); + llmq::CInstantSendManagerTestAccess::AddUnverified(manager, islock); + llmq::CInstantSendLock result; + BOOST_CHECK(!manager.GetInstantSendLockByHash(::SerializeHash(islock), result)); +} + +BOOST_AUTO_TEST_CASE(mempool_conflict_callback_clears_instantsend_tracking) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + const auto tx = MakeTransaction(uint256S("1")); + llmq::CInstantSendManagerTestAccess::TrackConflict(manager, tx); + BOOST_REQUIRE(!llmq::CInstantSendManagerTestAccess::TrackingEmpty(manager)); + + manager.SyncTransaction(*tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::TrackingEmpty(manager)); +} + +BOOST_AUTO_TEST_CASE(pending_lock_body_cannot_be_replaced_before_completion) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + llmq::CInstantSendManagerTestAccess::Enable(manager); + auto* previousManager = llmq::quorumInstantSendManager; + const bool previousReplacement = fEnableReplacement; + BOOST_SCOPE_EXIT(previousManager, previousReplacement) { + llmq::quorumInstantSendManager = previousManager; + fEnableReplacement = previousReplacement; + mempool.clear(); + } BOOST_SCOPE_EXIT_END + llmq::quorumInstantSendManager = &manager; + fEnableReplacement = true; + + CMutableTransaction victim; + victim.vin.emplace_back(COutPoint(uint256S("1"), 0), CScript(), 0); + victim.vout.emplace_back(20000, CScript() << OP_DUP << OP_HASH160 << + std::vector(20, 0) << OP_EQUALVERIFY << OP_CHECKSIG); + const auto tx = MakeTransactionRef(victim); + CMutableTransaction replacement(victim); + replacement.vout[0].nValue -= 1000; + + LOCK(cs_main); + mempool.clear(); + TestMemPoolEntryHelper entry; + mempool.addUnchecked(tx->GetHash(), entry.FromTx(*tx)); + const auto hash = llmq::CInstantSendManagerTestAccess::AddPending(manager, MakeLock(tx)); + manager.SyncTransaction(*tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::HasCallbackTransaction(manager, hash, tx, nullptr)); + BOOST_REQUIRE(!manager.IsLocked(tx->GetHash())); + + CValidationState state; + bool missingInputs = false; + BOOST_CHECK(!AcceptToMemoryPool(mempool, state, MakeTransactionRef(replacement), false, &missingInputs)); + BOOST_CHECK_EQUAL(state.GetRejectReason(), "tx-txlock-conflict"); + BOOST_CHECK(!missingInputs); + BOOST_CHECK(mempool.exists(tx->GetHash())); + BOOST_CHECK(!mempool.exists(replacement.GetHash())); +} + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_activation_tests, TestChain100Setup) + +BOOST_AUTO_TEST_CASE(connect_and_pure_disconnect_refresh_feature_state) +{ + CDBWrapper database(pathTemp / "instantsend", 1 << 20, true); + llmq::CInstantSendManager manager(database); + auto* previousManager = llmq::quorumInstantSendManager; + auto* parent = chainActive.Tip(); + const ActiveSporkMap parentSporks = parent->privacyData().activeDisablingSporks; + BOOST_SCOPE_EXIT(previousManager, parent, parentSporks) { + LOCK(cs_main); + parent->SetActiveDisablingSporks(parentSporks); + llmq::quorumInstantSendManager = previousManager; + } BOOST_SCOPE_EXIT_END + llmq::quorumInstantSendManager = &manager; + BOOST_REQUIRE(!manager.IsNewInstantSendEnabled()); + + // This fixture has no tip-notification listener, so activation must happen in validation. + const CBlock block = CreateAndProcessBlock({}, coinbaseKey); + BOOST_REQUIRE(chainActive.Tip()->GetBlockHash() == block.GetHash()); + BOOST_REQUIRE(chainActive.Tip()->pprev == parent); + BOOST_CHECK(manager.IsNewInstantSendEnabled()); + { + LOCK(cs_main); + parent->SetActiveDisablingSporks({{CSporkAction::featureInstantSend, {0, 0}}}); + } + + BOOST_REQUIRE(DisconnectBlocks(1)); + BOOST_CHECK(chainActive.Tip() == parent); + BOOST_CHECK(!manager.IsNewInstantSendEnabled()); + { + LOCK(cs_main); + parent->SetActiveDisablingSporks(parentSporks); + } + CValidationState state; + BOOST_REQUIRE(ActivateBestChain(state, Params())); + BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.GetHash()); + BOOST_CHECK(manager.IsNewInstantSendEnabled()); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/spark_tests.cpp b/src/test/spark_tests.cpp index e8191ae898..05bbbf7a36 100644 --- a/src/test/spark_tests.cpp +++ b/src/test/spark_tests.cpp @@ -15,6 +15,8 @@ #include "../hash.h" #include "../logging.h" #include "../ui_interface.h" +#include "../llmq/quorums_instantsend.h" +#include "../sparkname.h" #include "test_bitcoin.h" #include "fixtures.h" @@ -23,6 +25,70 @@ #include #include +namespace llmq +{ +struct CInstantSendManagerSparkTestAccess +{ + CInstantSendManager& manager; + bool wasEnabled; + uint256 hash; + boost::signals2::scoped_connection syncConnection; + + CInstantSendManagerSparkTestAccess(CInstantSendManager& managerIn, const CTransaction& tx) + : manager(managerIn), wasEnabled(manager.isNewInstantSendEnabled.exchange(true)) + { + CInstantSendLock islock; + islock.txid = tx.GetHash(); + for (const auto& tag : spark::GetSparkUsedTags(tx)) + islock.inputs.emplace_back(primitives::GetLTagHash(tag), 0); + hash = ::SerializeHash(islock); + LOCK(manager.cs); + manager.pendingNoTxInstantSendLocks.emplace(hash, + CInstantSendManager::PendingInstantSendLock{-1, islock, nullptr, nullptr, {}}); + manager.pendingInstantSendLocksByTxid.emplace(islock.txid, hash); + syncConnection = GetMainSignals().SyncTransaction.connect( + [this](const CTransaction& syncedTx, const CBlockIndex* pindex, int position) { + manager.SyncTransaction(syncedTx, pindex, position); + }); + } + + ~CInstantSendManagerSparkTestAccess() + { + syncConnection.disconnect(); + LOCK(manager.cs); + manager.ErasePendingInstantSendLock(hash); + manager.isNewInstantSendEnabled = wasEnabled; + } + + void TrackConflict(const CTransaction& tx) + { + LOCK(manager.cs); + manager.AddNonLockedTx(MakeTransactionRef(tx)); + for (const auto& tag : spark::GetSparkUsedTags(tx)) { + const COutPoint input(primitives::GetLTagHash(tag), 0); + manager.inputRequestIds.emplace(::SerializeHash(std::make_pair(std::string("inlock"), input))); + } + } + + bool HasTrackedConflict(const CTransaction& tx) + { + LOCK(manager.cs); + if (manager.nonLockedTxs.count(tx.GetHash())) + return true; + for (const auto& entry : manager.nonLockedTxsByInputs) { + if (entry.second.second == tx.GetHash()) + return true; + } + for (const auto& tag : spark::GetSparkUsedTags(tx)) { + const COutPoint input(primitives::GetLTagHash(tag), 0); + if (manager.inputRequestIds.count(::SerializeHash(std::make_pair(std::string("inlock"), input)))) + return true; + } + return false; + } +}; +} + namespace spark { // Generate a random char vector from a random scalar @@ -2587,6 +2653,119 @@ BOOST_AUTO_TEST_CASE(spark_stem_admission_records_linking_tags) txpools.clear(); } +BOOST_AUTO_TEST_CASE(spark_pending_islock_replaces_linking_tag_conflict) +{ + GenerateBlocks(500); + std::vector mintTransactions; + const auto createdMints = GenerateMints({5 * COIN, 1 * COIN}, mintTransactions); + txpools.clear(); + BOOST_REQUIRE(GenerateBlock(mintTransactions)); + GenerateBlocks(10); + + std::vector selected; + for (const auto& mint : createdMints) { + if (mint.v == 5 * COIN) + selected.push_back(pwalletMain->sparkWallet->getMintMeta(mint.k)); + } + BOOST_REQUIRE_EQUAL(selected.size(), 1U); + const CTransaction locked = GenerateCustomSparkSpend(selected, 4 * COIN); + const CTransaction conflicting = GenerateCustomSparkSpend(selected, 3 * COIN); + const auto lockedTags = ParseSparkSpend(locked).getUsedLTags(); + BOOST_REQUIRE(lockedTags == ParseSparkSpend(conflicting).getUsedLTags()); + + LOCK(cs_main); + CValidationState conflictState; + BOOST_REQUIRE(AcceptToMemoryPool(mempool, conflictState, MakeTransactionRef(conflicting), false, nullptr)); + CValidationState rejectedState; + BOOST_CHECK(!AcceptToMemoryPool(mempool, rejectedState, MakeTransactionRef(locked), false, nullptr)); + BOOST_CHECK_EQUAL(rejectedState.GetRejectReason(), "txn-mempool-conflict"); + + // A pending lock must not let an invalid proof evict a valid mempool transaction. + CMutableTransaction invalid(locked); + invalid.vout[0].nValue -= 1; + { + llmq::CInstantSendManagerSparkTestAccess pending(*llmq::quorumInstantSendManager, CTransaction(invalid)); + CValidationState invalidState; + BOOST_CHECK(!AcceptToMemoryPool(mempool, invalidState, MakeTransactionRef(invalid), false, nullptr)); + BOOST_CHECK(mempool.exists(conflicting.GetHash())); + } + + llmq::CInstantSendManagerSparkTestAccess pending(*llmq::quorumInstantSendManager, locked); + pending.TrackConflict(conflicting); + BOOST_REQUIRE(pending.HasTrackedConflict(conflicting)); + CValidationState acceptedState; + BOOST_REQUIRE_MESSAGE(AcceptToMemoryPool(mempool, acceptedState, MakeTransactionRef(locked), false, nullptr), + acceptedState.GetRejectReason()); + BOOST_CHECK(mempool.exists(locked.GetHash())); + BOOST_CHECK(!mempool.exists(conflicting.GetHash())); + BOOST_CHECK(!pending.HasTrackedConflict(conflicting)); + for (const auto& tag : lockedTags) + BOOST_CHECK(mempool.sparkState.GetMempoolConflictingTxHash(tag) == locked.GetHash()); + txpools.clear(); +} + +BOOST_AUTO_TEST_CASE(spark_pending_islock_replaces_name_conflicts) +{ + GenerateBlocks(1900); + std::vector mintTransactions; + GenerateMints({5 * COIN, 5 * COIN}, mintTransactions); + txpools.clear(); + BOOST_REQUIRE(GenerateBlock(mintTransactions)); + GenerateBlocks(10); + BOOST_REQUIRE_EQUAL(mintTransactions.size(), 2U); + + CCoinControl firstCoin; + CCoinControl secondCoin; + for (size_t i = 0; i < mintTransactions.size(); ++i) { + const auto& mintTx = mintTransactions[i]; + const auto mintOutput = std::find_if(mintTx.vout.begin(), mintTx.vout.end(), + [](const CTxOut& output) { return output.scriptPubKey.IsSparkMint(); }); + BOOST_REQUIRE(mintOutput != mintTx.vout.end()); + (i == 0 ? firstCoin : secondCoin).Select(COutPoint( + mintTx.GetHash(), std::distance(mintTx.vout.begin(), mintOutput))); + } + + LOCK(cs_main); + LOCK(pwalletMain->cs_wallet); + const std::string firstAddress = pwalletMain->sparkWallet->getDefaultAddress().encode(GetNetworkType()); + const std::string secondAddress = pwalletMain->sparkWallet->generateNewAddress().encode(GetNetworkType()); + const auto createNameTx = [&](const std::string& name, const std::string& address, const CCoinControl& coin) { + CSparkNameTxData data; + data.nVersion = 1; + data.name = name; + data.sparkAddress = address; + data.sparkNameValidityBlocks = 10; + CAmount fee; + return *pwalletMain->sparkWallet->CreateSparkNameTransaction(data, + consensus.nSparkNamesFee[name.size()] * COIN, fee, &coin).tx; + }; + + const CTransaction locked = createNameTx("pending-lock-name", firstAddress, firstCoin); + const CTransaction sameName = createNameTx("pending-lock-name", secondAddress, secondCoin); + const CTransaction sameAddress = createNameTx("pending-other-name", firstAddress, secondCoin); + BOOST_REQUIRE(ParseSparkSpend(locked).getUsedLTags() != ParseSparkSpend(sameName).getUsedLTags()); + + for (const CTransaction* conflicting : {&sameName, &sameAddress}) { + txpools.clear(); + CValidationState conflictState; + BOOST_REQUIRE_MESSAGE(AcceptToMemoryPool(mempool, conflictState, MakeTransactionRef(*conflicting), false, nullptr), + conflictState.GetRejectReason()); + CValidationState rejectedState; + BOOST_CHECK(!AcceptToMemoryPool(mempool, rejectedState, MakeTransactionRef(locked), false, nullptr)); + BOOST_CHECK_EQUAL(rejectedState.GetRejectReason(), "txn-mempool-conflict"); + + llmq::CInstantSendManagerSparkTestAccess pending(*llmq::quorumInstantSendManager, locked); + CValidationState acceptedState; + BOOST_REQUIRE_MESSAGE(AcceptToMemoryPool(mempool, acceptedState, MakeTransactionRef(locked), false, nullptr), + acceptedState.GetRejectReason()); + BOOST_CHECK(mempool.exists(locked.GetHash())); + BOOST_CHECK(!mempool.exists(conflicting->GetHash())); + BOOST_REQUIRE_EQUAL(mempool.sparkNames.size(), 1U); + BOOST_CHECK(mempool.sparkNames.begin()->second.second == locked.GetHash()); + } + txpools.clear(); +} + BOOST_AUTO_TEST_CASE(spark_block_evicts_all_conflicting_spends_from_mempool_and_stem) { RestoreSparkActivationHeights restoreActivationHeights; diff --git a/src/validation.cpp b/src/validation.cpp index a1b78689d9..1fdf19ad1a 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -225,8 +225,8 @@ int GetNHeight(const CBlockHeader &block) { /* Use this class to start tracking transactions that are removed from the * mempool and pass all those transactions through SyncTransaction when the - * object goes out of scope. This is currently only used to call SyncTransaction - * on conflicts removed from the mempool during block connection. Applied in + * object goes out of scope. Used for conflicts removed by InstantSend locks + * and during block connection. Applied in * ActivateBestChain around ActivateBestStep which in turn calls: * ConnectTip->removeForBlock->removeConflicts */ @@ -238,6 +238,7 @@ class MemPoolConflictRemovalTracker public: MemPoolConflictRemovalTracker(CTxMemPool &_pool) : pool(_pool) { + LOCK(pool.cs); pool.NotifyEntryRemoved.connect(boost::bind(&MemPoolConflictRemovalTracker::NotifyEntryRemoved, this, _1, _2)); } @@ -248,7 +249,10 @@ class MemPoolConflictRemovalTracker } ~MemPoolConflictRemovalTracker() { - pool.NotifyEntryRemoved.disconnect(boost::bind(&MemPoolConflictRemovalTracker::NotifyEntryRemoved, this, _1, _2)); + { + LOCK(pool.cs); + pool.NotifyEntryRemoved.disconnect(boost::bind(&MemPoolConflictRemovalTracker::NotifyEntryRemoved, this, _1, _2)); + } for (const auto& tx : conflictedTxs) { GetMainSignals().SyncTransaction(*tx, NULL, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); } @@ -925,6 +929,8 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C std::vector sparkMintCoins; std::unordered_set txSparkMints; std::vector sparkUsedLTags; + const bool waitingForTx = llmq::quorumInstantSendManager->IsWaitingForTx(hash); + std::set sparkConflicts; CSparkNameTxData sparkNameData; @@ -947,11 +953,13 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C } for (const auto& lTag : sparkUsedLTags) { - if (sparkState->IsUsedLTag(lTag) || pool.sparkState.HasLTag(lTag)) { + if (sparkState->IsUsedLTag(lTag) || (!waitingForTx && pool.sparkState.HasLTag(lTag))) { LogPrintf("AcceptToMemoryPool(): spark linking tag %s has been used\n", lTag.tostring()); return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict"); } + if (waitingForTx && pool.sparkState.HasLTag(lTag)) + sparkConflicts.insert(pool.sparkState.GetMempoolConflictingTxHash(lTag)); } CSparkNameManager *sparkNameManager = CSparkNameManager::GetInstance(); @@ -963,11 +971,15 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C /* nContextualFailureDoS */ 0)) return false; - if (!sparkNameData.name.empty() && - CSparkNameManager::IsInConflict(sparkNameData, pool.sparkNames, [=](decltype(pool.sparkNames)::const_iterator it)->std::string { - return it->second.first; - })) { - return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict"); + if (!sparkNameData.name.empty()) { + const std::string upperName = CSparkNameManager::ToUpper(sparkNameData.name); + for (const auto& entry : pool.sparkNames) { + if (entry.first == upperName || entry.second.first == sparkNameData.sparkAddress) { + if (!waitingForTx) + return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict"); + sparkConflicts.insert(entry.second.second); + } + } } } @@ -1048,6 +1060,18 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C REJECT_INVALID, "tx-txlock-conflict"); } + if (waitingForTx) { + MemPoolConflictRemovalTracker tracker(pool); + LOCK(pool.cs); + pool.removeConflicts(tx); + pool.removeProTxConflicts(tx); + for (const auto& conflictHash : sparkConflicts) { + const auto conflict = pool.get(conflictHash); + if (conflict) + pool.removeRecursive(*conflict, MemPoolRemovalReason::CONFLICT); + } + } + // Check for conflicts with in-memory transactions std::set setConflicts; { @@ -1061,6 +1085,9 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C const CTransaction *ptxConflicting = itConflicting->second; if (!setConflicts.count(ptxConflicting->GetHash())) { + if (llmq::quorumInstantSendManager->IsWaitingForTx(ptxConflicting->GetHash())) + return state.Invalid(false, REJECT_CONFLICT, "tx-txlock-conflict"); + // Allow opt-out of transaction replacement by setting // nSequence >= maxint-1 on all inputs. // @@ -3038,21 +3065,26 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin for (const auto& tx : block.vtx) { // skip txes that have no inputs if (tx->vin.empty()) continue; - llmq::CInstantSendLockPtr conflictLock = llmq::quorumInstantSendManager->GetConflictingLock(*tx); + llmq::CInstantSendLockPtr conflictLock = llmq::quorumInstantSendManager->GetConflictingLock(*tx, true); + while (conflictLock && llmq::chainLocksHandler->HasChainLock(pindex->nHeight, pindex->GetBlockHash())) { + llmq::quorumInstantSendManager->RemoveChainLockConflictingLock(::SerializeHash(*conflictLock), *conflictLock); + conflictLock = llmq::quorumInstantSendManager->GetConflictingLock(*tx, true); + } if (!conflictLock) { continue; } - if (llmq::chainLocksHandler->HasChainLock(pindex->nHeight, pindex->GetBlockHash())) { - llmq::quorumInstantSendManager->RemoveChainLockConflictingLock(::SerializeHash(*conflictLock), *conflictLock); - assert(llmq::quorumInstantSendManager->GetConflictingLock(*tx) == nullptr); - } else { - // The node which relayed this should switch to correct chain. - // TODO: relay instantsend data/proof. - LOCK(cs_main); - mapRejectedBlocks.insert(std::make_pair(block.GetHash(), GetTime())); - return state.DoS(10, error("ConnectBlock(DASH): transaction %s conflicts with transaction lock %s", tx->GetHash().ToString(), conflictLock->txid.ToString()), - REJECT_INVALID, "conflict-tx-lock"); + if (llmq::quorumInstantSendManager->IsWaitingForTx(conflictLock->txid)) { + if (!fJustCheck) { + llmq::quorumInstantSendManager->AddPendingBlockConflict(::SerializeHash(*conflictLock), pindex); + } + // The transaction body is still missing, so keep this branch eligible for reconsideration. + return state.DoS(0, false, REJECT_INVALID, "conflict-pending-tx-lock", true); } + // The node which relayed this should switch to correct chain. + // TODO: relay instantsend data/proof. + mapRejectedBlocks.insert(std::make_pair(block.GetHash(), GetTime())); + return state.DoS(10, error("ConnectBlock(DASH): transaction %s conflicts with transaction lock %s", tx->GetHash().ToString(), conflictLock->txid.ToString()), + REJECT_INVALID, "conflict-tx-lock"); } } @@ -3376,6 +3408,8 @@ void PruneAndFlush() { void static UpdateTip(CBlockIndex *pindexNew, const CChainParams &chainParams) { LogPrint("validation", "UpdateTip() pindexNew.nHeight=%d\n", pindexNew->nHeight); chainActive.SetTip(pindexNew); + // Refresh before transaction callbacks, including initial sync and disconnects. + llmq::quorumInstantSendManager->UpdateActiveState(pindexNew); // New best block txpools.AddTransactionsUpdated(1); @@ -3774,6 +3808,9 @@ static CBlockIndex* FindMostWorkChain() { // Find the best candidate header. { std::set::reverse_iterator it = setBlockIndexCandidates.rbegin(); + while (it != setBlockIndexCandidates.rend() && llmq::quorumInstantSendManager->IsBlockExcluded(*it)) { + ++it; + } if (it == setBlockIndexCandidates.rend()) return NULL; pindexNew = *it;