2018-07-12 11:04:42 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-12-31 01:00:00 +01:00
|
|
|
# Copyright (c) 2018-2024 The Dash Core developers
|
2018-07-12 11:04:42 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
from test_framework.test_framework import DashTestFramework
|
2022-09-24 14:36:35 +02:00
|
|
|
from test_framework.util import assert_equal, assert_raises_rpc_error
|
2018-07-12 11:04:42 +02:00
|
|
|
|
|
|
|
'''
|
2020-07-17 01:44:20 +02:00
|
|
|
p2p_instantsend.py
|
2019-10-01 16:15:28 +02:00
|
|
|
|
|
|
|
Tests InstantSend functionality (prevent doublespend for unconfirmed transactions)
|
2018-07-12 11:04:42 +02:00
|
|
|
'''
|
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
class InstantSendTest(DashTestFramework):
|
2019-09-24 00:57:30 +02:00
|
|
|
def set_test_params(self):
|
2024-08-27 12:30:39 +02:00
|
|
|
self.set_dash_test_params(8, 4)
|
2018-07-12 11:04:42 +02:00
|
|
|
# set sender, receiver, isolated nodes
|
2019-03-15 09:48:24 +01:00
|
|
|
self.isolated_idx = 1
|
|
|
|
self.receiver_idx = 2
|
|
|
|
self.sender_idx = 3
|
2018-07-12 11:04:42 +02:00
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
def run_test(self):
|
2022-06-18 18:52:45 +02:00
|
|
|
self.nodes[0].sporkupdate("SPORK_17_QUORUM_DKG_ENABLED", 0)
|
2019-03-03 08:31:23 +01:00
|
|
|
self.wait_for_sporks_same()
|
2023-11-20 17:17:04 +01:00
|
|
|
self.activate_v19(expected_activation_height=900)
|
|
|
|
self.log.info("Activated v19 at height:" + str(self.nodes[0].getblockcount()))
|
|
|
|
self.move_to_next_cycle()
|
|
|
|
self.log.info("Cycle H height:" + str(self.nodes[0].getblockcount()))
|
|
|
|
self.move_to_next_cycle()
|
|
|
|
self.log.info("Cycle H+C height:" + str(self.nodes[0].getblockcount()))
|
|
|
|
self.move_to_next_cycle()
|
|
|
|
self.log.info("Cycle H+2C height:" + str(self.nodes[0].getblockcount()))
|
|
|
|
(quorum_info_i_0, quorum_info_i_1) = self.mine_cycle_quorum(llmq_type_name='llmq_test_dip0024', llmq_type=103)
|
2019-03-03 08:31:23 +01:00
|
|
|
|
2019-05-06 15:26:27 +02:00
|
|
|
self.test_mempool_doublespend()
|
|
|
|
self.test_block_doublespend()
|
2019-03-03 08:31:23 +01:00
|
|
|
|
2019-05-06 15:26:27 +02:00
|
|
|
def test_block_doublespend(self):
|
2019-03-18 14:50:44 +01:00
|
|
|
sender = self.nodes[self.sender_idx]
|
|
|
|
receiver = self.nodes[self.receiver_idx]
|
|
|
|
isolated = self.nodes[self.isolated_idx]
|
2019-05-06 15:26:27 +02:00
|
|
|
|
2018-07-12 11:04:42 +02:00
|
|
|
# feed the sender with some balance
|
2019-03-18 14:50:44 +01:00
|
|
|
sender_addr = sender.getnewaddress()
|
2023-06-21 05:49:41 +02:00
|
|
|
is_id = self.nodes[0].sendtoaddress(sender_addr, 1)
|
|
|
|
for node in self.nodes:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
2019-10-01 16:15:28 +02:00
|
|
|
self.bump_mocktime(1)
|
2019-05-06 15:26:27 +02:00
|
|
|
self.nodes[0].generate(2)
|
2018-11-26 13:40:36 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
2019-05-06 15:26:27 +02:00
|
|
|
# create doublespending transaction, but don't relay it
|
2019-03-18 14:50:44 +01:00
|
|
|
dblspnd_tx = self.create_raw_tx(sender, isolated, 0.5, 1, 100)
|
2019-05-06 16:58:38 +02:00
|
|
|
# isolate one node from network
|
2022-09-24 14:36:35 +02:00
|
|
|
self.isolate_node(self.isolated_idx)
|
2018-07-12 11:04:42 +02:00
|
|
|
# instantsend to receiver
|
2019-03-18 14:50:44 +01:00
|
|
|
receiver_addr = receiver.getnewaddress()
|
2019-07-09 16:50:08 +02:00
|
|
|
is_id = sender.sendtoaddress(receiver_addr, 0.9)
|
2019-10-01 16:14:26 +02:00
|
|
|
# wait for the transaction to propagate
|
|
|
|
connected_nodes = self.nodes.copy()
|
|
|
|
del connected_nodes[self.isolated_idx]
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_mempools(connected_nodes)
|
2019-10-01 16:14:26 +02:00
|
|
|
for node in connected_nodes:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
2018-07-12 11:04:42 +02:00
|
|
|
# send doublespend transaction to isolated node
|
2023-06-21 05:49:41 +02:00
|
|
|
dblspnd_txid = isolated.sendrawtransaction(dblspnd_tx['hex'])
|
2018-07-12 11:04:42 +02:00
|
|
|
# generate block on isolated node with doublespend transaction
|
2023-06-21 05:49:41 +02:00
|
|
|
self.bump_mocktime(599)
|
2024-09-26 21:13:22 +02:00
|
|
|
wrong_early_block = self.generate(isolated, 1)[0]
|
2023-06-21 05:49:41 +02:00
|
|
|
assert not "confirmation" in isolated.getrawtransaction(dblspnd_txid, 1)
|
|
|
|
isolated.invalidateblock(wrong_early_block)
|
2019-08-09 01:14:11 +02:00
|
|
|
self.bump_mocktime(1)
|
2024-09-26 21:13:22 +02:00
|
|
|
wrong_block = self.generate(isolated, 1)[0]
|
2023-06-21 05:49:41 +02:00
|
|
|
assert_equal(isolated.getrawtransaction(dblspnd_txid, 1)["confirmations"], 1)
|
2018-07-12 11:04:42 +02:00
|
|
|
# connect isolated block to network
|
2022-09-24 14:36:35 +02:00
|
|
|
self.reconnect_isolated_node(self.isolated_idx, 0)
|
2018-07-12 11:04:42 +02:00
|
|
|
# check doublespend block is rejected by other nodes
|
|
|
|
timeout = 10
|
2022-04-07 12:15:17 +02:00
|
|
|
for idx, node in enumerate(self.nodes):
|
|
|
|
if idx == self.isolated_idx:
|
2019-03-15 09:48:24 +01:00
|
|
|
continue
|
2022-04-07 12:15:17 +02:00
|
|
|
res = node.waitforblock(wrong_block, timeout)
|
2021-08-27 21:03:02 +02:00
|
|
|
assert res['hash'] != wrong_block
|
2018-07-12 11:04:42 +02:00
|
|
|
# wait for long time only for first node
|
|
|
|
timeout = 1
|
2019-08-23 20:02:33 +02:00
|
|
|
# send coins back to the controller node without waiting for confirmations
|
|
|
|
receiver.sendtoaddress(self.nodes[0].getnewaddress(), 0.9, "", "", True)
|
|
|
|
assert_equal(receiver.getwalletinfo()["balance"], 0)
|
2019-03-03 08:31:23 +01:00
|
|
|
# mine more blocks
|
|
|
|
# TODO: mine these blocks on an isolated node
|
2019-08-09 01:14:11 +02:00
|
|
|
self.bump_mocktime(1)
|
2020-04-09 08:15:58 +02:00
|
|
|
# make sure the above TX is on node0
|
|
|
|
self.sync_mempools([n for n in self.nodes if n is not isolated])
|
2019-03-03 08:31:23 +01:00
|
|
|
self.nodes[0].generate(2)
|
|
|
|
self.sync_all()
|
2018-07-12 11:04:42 +02:00
|
|
|
|
2019-05-06 15:26:27 +02:00
|
|
|
def test_mempool_doublespend(self):
|
|
|
|
sender = self.nodes[self.sender_idx]
|
|
|
|
receiver = self.nodes[self.receiver_idx]
|
|
|
|
isolated = self.nodes[self.isolated_idx]
|
2021-11-30 12:14:08 +01:00
|
|
|
connected_nodes = self.nodes.copy()
|
|
|
|
del connected_nodes[self.isolated_idx]
|
2019-05-06 15:26:27 +02:00
|
|
|
|
|
|
|
# feed the sender with some balance
|
|
|
|
sender_addr = sender.getnewaddress()
|
2023-06-21 05:49:41 +02:00
|
|
|
is_id = self.nodes[0].sendtoaddress(sender_addr, 1)
|
|
|
|
for node in self.nodes:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
2019-10-01 16:15:28 +02:00
|
|
|
self.bump_mocktime(1)
|
2019-05-06 15:26:27 +02:00
|
|
|
self.nodes[0].generate(2)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# create doublespending transaction, but don't relay it
|
|
|
|
dblspnd_tx = self.create_raw_tx(sender, isolated, 0.5, 1, 100)
|
|
|
|
# isolate one node from network
|
2022-09-24 14:36:35 +02:00
|
|
|
self.isolate_node(self.isolated_idx)
|
2019-05-06 15:26:27 +02:00
|
|
|
# send doublespend transaction to isolated node
|
2021-11-30 12:14:08 +01:00
|
|
|
dblspnd_txid = isolated.sendrawtransaction(dblspnd_tx['hex'])
|
|
|
|
assert dblspnd_txid in set(isolated.getrawmempool())
|
2019-05-06 15:26:27 +02:00
|
|
|
# let isolated node rejoin the network
|
|
|
|
# The previously isolated node should NOT relay the doublespending TX
|
2022-09-24 14:36:35 +02:00
|
|
|
self.reconnect_isolated_node(self.isolated_idx, 0)
|
2021-11-30 12:14:08 +01:00
|
|
|
for node in connected_nodes:
|
|
|
|
assert_raises_rpc_error(-5, "No such mempool or blockchain transaction", node.getrawtransaction, dblspnd_txid)
|
|
|
|
# Instantsend to receiver. The previously isolated node won't accept the tx but it should
|
|
|
|
# request the correct TX from other nodes once the corresponding lock is received.
|
|
|
|
# And this time the doublespend TX should be pruned once the correct tx is received.
|
2019-05-06 15:26:27 +02:00
|
|
|
receiver_addr = receiver.getnewaddress()
|
2019-07-09 16:50:08 +02:00
|
|
|
is_id = sender.sendtoaddress(receiver_addr, 0.9)
|
2019-10-01 16:14:26 +02:00
|
|
|
# wait for the transaction to propagate
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_mempools()
|
2019-05-06 15:26:27 +02:00
|
|
|
for node in self.nodes:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
2021-11-30 12:14:08 +01:00
|
|
|
assert dblspnd_txid not in set(isolated.getrawmempool())
|
2019-08-23 20:02:33 +02:00
|
|
|
# send coins back to the controller node without waiting for confirmations
|
2021-11-30 12:14:08 +01:00
|
|
|
sentback_id = receiver.sendtoaddress(self.nodes[0].getnewaddress(), 0.9, "", "", True)
|
|
|
|
self.sync_mempools()
|
|
|
|
for node in self.nodes:
|
|
|
|
self.wait_for_instantlock(sentback_id, node)
|
2019-08-23 20:02:33 +02:00
|
|
|
assert_equal(receiver.getwalletinfo()["balance"], 0)
|
2019-10-01 16:15:28 +02:00
|
|
|
# mine more blocks
|
|
|
|
self.bump_mocktime(1)
|
|
|
|
self.nodes[0].generate(2)
|
|
|
|
self.sync_all()
|
2019-05-06 15:26:27 +02:00
|
|
|
|
2018-07-12 11:04:42 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
InstantSendTest().main()
|