2018-07-12 11:04:42 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2018 The Dash Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
from test_framework.mininode import *
|
2018-10-20 16:08:02 +02:00
|
|
|
from test_framework.test_framework import DashTestFramework
|
2018-07-12 11:04:42 +02:00
|
|
|
from test_framework.util import *
|
|
|
|
from time import *
|
|
|
|
|
|
|
|
'''
|
|
|
|
InstantSendTest -- test InstantSend functionality (prevent doublespend for unconfirmed transactions)
|
|
|
|
'''
|
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
class InstantSendTest(DashTestFramework):
|
2018-07-12 11:04:42 +02:00
|
|
|
def __init__(self):
|
2019-03-15 09:48:24 +01:00
|
|
|
super().__init__(9, 5, [], fast_dip3_enforcement=True)
|
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):
|
2019-03-03 08:31:23 +01:00
|
|
|
self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)
|
|
|
|
self.wait_for_sporks_same()
|
|
|
|
self.mine_quorum()
|
|
|
|
|
2019-07-09 16:50:08 +02:00
|
|
|
self.nodes[0].spork("SPORK_2_INSTANTSEND_ENABLED", 0)
|
|
|
|
self.nodes[0].spork("SPORK_3_INSTANTSEND_BLOCK_FILTERING", 0)
|
2019-03-03 08:31:23 +01:00
|
|
|
self.wait_for_sporks_same()
|
|
|
|
|
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()
|
2018-07-12 11:04:42 +02:00
|
|
|
self.nodes[0].sendtoaddress(sender_addr, 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
|
|
|
|
isolate_node(isolated)
|
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-03-19 08:38:16 +01:00
|
|
|
for node in self.nodes:
|
|
|
|
if node is not isolated:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
2018-07-12 11:04:42 +02:00
|
|
|
# send doublespend transaction to isolated node
|
2019-03-18 14:50:44 +01:00
|
|
|
isolated.sendrawtransaction(dblspnd_tx['hex'])
|
2018-07-12 11:04:42 +02:00
|
|
|
# generate block on isolated node with doublespend transaction
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
2018-11-01 22:58:57 +01:00
|
|
|
set_node_times(self.nodes, get_mocktime())
|
2019-03-18 14:50:44 +01:00
|
|
|
isolated.generate(1)
|
|
|
|
wrong_block = isolated.getbestblockhash()
|
2018-07-12 11:04:42 +02:00
|
|
|
# connect isolated block to network
|
2019-05-06 16:58:38 +02:00
|
|
|
reconnect_isolated_node(isolated, 0)
|
2018-07-12 11:04:42 +02:00
|
|
|
# check doublespend block is rejected by other nodes
|
|
|
|
timeout = 10
|
2019-03-15 09:48:24 +01:00
|
|
|
for i in range(0, self.num_nodes):
|
|
|
|
if i == self.isolated_idx:
|
|
|
|
continue
|
2018-07-12 11:04:42 +02:00
|
|
|
res = self.nodes[i].waitforblock(wrong_block, timeout)
|
|
|
|
assert (res['hash'] != wrong_block)
|
|
|
|
# wait for long time only for first node
|
|
|
|
timeout = 1
|
2019-03-03 08:31:23 +01:00
|
|
|
# mine more blocks
|
|
|
|
# TODO: mine these blocks on an isolated node
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
|
|
|
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]
|
|
|
|
|
|
|
|
# feed the sender with some balance
|
|
|
|
sender_addr = sender.getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(sender_addr, 1)
|
|
|
|
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)
|
|
|
|
dblspnd_txid = bytes_to_hex_str(hash256(hex_str_to_bytes(dblspnd_tx['hex']))[::-1])
|
|
|
|
# isolate one node from network
|
2019-05-06 16:58:38 +02:00
|
|
|
isolate_node(isolated)
|
2019-05-06 15:26:27 +02:00
|
|
|
# send doublespend transaction to isolated node
|
|
|
|
isolated.sendrawtransaction(dblspnd_tx['hex'])
|
|
|
|
# let isolated node rejoin the network
|
|
|
|
# The previously isolated node should NOT relay the doublespending TX
|
2019-05-06 16:58:38 +02:00
|
|
|
reconnect_isolated_node(isolated, 0)
|
2019-05-06 15:26:27 +02:00
|
|
|
for node in self.nodes:
|
|
|
|
if node is not isolated:
|
|
|
|
assert_raises_jsonrpc(-5, "No such mempool or blockchain transaction", node.getrawtransaction, dblspnd_txid)
|
|
|
|
# instantsend to receiver. The previously isolated node should prune the doublespend TX and request the correct
|
|
|
|
# TX from other nodes.
|
|
|
|
receiver_addr = receiver.getnewaddress()
|
2019-07-09 16:50:08 +02:00
|
|
|
is_id = sender.sendtoaddress(receiver_addr, 0.9)
|
2019-05-06 15:26:27 +02:00
|
|
|
for node in self.nodes:
|
|
|
|
self.wait_for_instantlock(is_id, node)
|
|
|
|
assert_raises_jsonrpc(-5, "No such mempool or blockchain transaction", isolated.getrawtransaction, dblspnd_txid)
|
|
|
|
|
2018-07-12 11:04:42 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
InstantSendTest().main()
|