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-01-29 15:54:38 +01:00
|
|
|
super().__init__(14, 10, [], fast_dip3_enforcement=True)
|
2018-07-12 11:04:42 +02:00
|
|
|
# set sender, receiver, isolated nodes
|
|
|
|
self.isolated_idx = self.num_nodes - 1
|
|
|
|
self.receiver_idx = self.num_nodes - 2
|
|
|
|
self.sender_idx = self.num_nodes - 3
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
print("Test old InstantSend")
|
|
|
|
self.test_doublespend()
|
|
|
|
|
2019-03-04 13:33:34 +01:00
|
|
|
self.nodes[0].spork("SPORK_20_INSTANTSEND_LLMQ_BASED", 0)
|
2019-03-03 08:31:23 +01:00
|
|
|
self.wait_for_sporks_same()
|
|
|
|
|
|
|
|
print("Test new InstantSend")
|
|
|
|
self.test_doublespend()
|
|
|
|
|
|
|
|
def test_doublespend(self):
|
2018-07-12 11:04:42 +02:00
|
|
|
# feed the sender with some balance
|
|
|
|
sender_addr = self.nodes[self.sender_idx].getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(sender_addr, 1)
|
|
|
|
# make sender funds mature for InstantSend
|
|
|
|
for i in range(0, 2):
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
2018-11-01 22:58:57 +01:00
|
|
|
set_node_times(self.nodes, get_mocktime())
|
2018-07-12 11:04:42 +02:00
|
|
|
self.nodes[0].generate(1)
|
2018-11-26 13:40:36 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
2018-07-12 11:04:42 +02:00
|
|
|
# create doublepending transaction, but don't relay it
|
|
|
|
dblspnd_tx = self.create_raw_trx(self.nodes[self.sender_idx],
|
|
|
|
self.nodes[self.isolated_idx],
|
2018-10-20 16:08:02 +02:00
|
|
|
0.5, 1, 100)
|
2018-07-12 11:04:42 +02:00
|
|
|
# stop one node to isolate it from network
|
|
|
|
stop_node(self.nodes[self.isolated_idx], self.isolated_idx)
|
|
|
|
# instantsend to receiver
|
|
|
|
receiver_addr = self.nodes[self.receiver_idx].getnewaddress()
|
|
|
|
is_id = self.nodes[self.sender_idx].instantsendtoaddress(receiver_addr, 0.9)
|
|
|
|
# wait for instantsend locks
|
|
|
|
start = time()
|
|
|
|
locked = False
|
|
|
|
while True:
|
|
|
|
is_trx = self.nodes[self.sender_idx].gettransaction(is_id)
|
|
|
|
if is_trx['instantlock']:
|
|
|
|
locked = True
|
|
|
|
break
|
|
|
|
if time() > start + 10:
|
|
|
|
break
|
2018-11-01 22:58:57 +01:00
|
|
|
sleep(0.1)
|
2018-07-12 11:04:42 +02:00
|
|
|
assert(locked)
|
|
|
|
# start last node
|
|
|
|
self.nodes[self.isolated_idx] = start_node(self.isolated_idx,
|
|
|
|
self.options.tmpdir,
|
2019-03-08 09:05:00 +01:00
|
|
|
self.extra_args)
|
2018-07-12 11:04:42 +02:00
|
|
|
# send doublespend transaction to isolated node
|
|
|
|
self.nodes[self.isolated_idx].sendrawtransaction(dblspnd_tx['hex'])
|
|
|
|
# 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())
|
2018-07-12 11:04:42 +02:00
|
|
|
self.nodes[self.isolated_idx].generate(1)
|
|
|
|
wrong_block = self.nodes[self.isolated_idx].getbestblockhash()
|
|
|
|
# connect isolated block to network
|
|
|
|
for i in range(0, self.isolated_idx):
|
|
|
|
connect_nodes(self.nodes[i], self.isolated_idx)
|
|
|
|
# check doublespend block is rejected by other nodes
|
|
|
|
timeout = 10
|
|
|
|
for i in range(0, self.isolated_idx):
|
|
|
|
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
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
InstantSendTest().main()
|