2018-09-26 16:17:47 +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-09-26 16:17:47 +02:00
|
|
|
from test_framework.util import *
|
|
|
|
from time import *
|
|
|
|
|
|
|
|
'''
|
|
|
|
p2p-autoinstantsend.py
|
|
|
|
|
|
|
|
Test automatic InstantSend locks functionality.
|
|
|
|
|
|
|
|
Checks that simple transactions automatically become InstantSend locked,
|
|
|
|
complex transactions don't become IS-locked and this functionality is
|
|
|
|
activated only if it is BIP9-activated and SPORK_16_INSTANTSEND_AUTOLOCKS is
|
|
|
|
active.
|
|
|
|
|
|
|
|
Also checks that this functionality doesn't influence regular InstantSend
|
|
|
|
transactions with high fee.
|
|
|
|
'''
|
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
class AutoInstantSendTest(DashTestFramework):
|
2018-09-26 16:17:47 +02:00
|
|
|
def __init__(self):
|
2018-10-20 16:08:02 +02:00
|
|
|
super().__init__(14, 10, [])
|
2018-09-26 16:17:47 +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
|
|
|
|
|
|
|
|
def get_autoix_bip9_status(self):
|
|
|
|
info = self.nodes[0].getblockchaininfo()
|
2018-10-26 18:42:52 +02:00
|
|
|
# we reuse the dip3 deployment
|
|
|
|
return info['bip9_softforks']['dip0003']['status']
|
2018-09-26 16:17:47 +02:00
|
|
|
|
|
|
|
def activate_autoix_bip9(self):
|
|
|
|
# sync nodes periodically
|
|
|
|
# if we sync them too often, activation takes too many time
|
|
|
|
# if we sync them too rarely, nodes failed to update its state and
|
|
|
|
# bip9 status is not updated
|
|
|
|
# so, in this code nodes are synced once per 20 blocks
|
|
|
|
counter = 0
|
|
|
|
sync_period = 10
|
|
|
|
|
|
|
|
while self.get_autoix_bip9_status() == 'defined':
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
counter += 1
|
|
|
|
if counter % sync_period == 0:
|
|
|
|
# sync nodes
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
while self.get_autoix_bip9_status() == 'started':
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
counter += 1
|
|
|
|
if counter % sync_period == 0:
|
|
|
|
# sync nodes
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
while self.get_autoix_bip9_status() == 'locked_in':
|
|
|
|
set_mocktime(get_mocktime() + 1)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
counter += 1
|
|
|
|
if counter % sync_period == 0:
|
|
|
|
# sync nodes
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# sync nodes
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
assert(self.get_autoix_bip9_status() == 'active')
|
|
|
|
|
|
|
|
def get_autoix_spork_state(self):
|
|
|
|
info = self.nodes[0].spork('active')
|
|
|
|
return info['SPORK_16_INSTANTSEND_AUTOLOCKS']
|
|
|
|
|
|
|
|
def set_autoix_spork_state(self, state):
|
2018-10-15 13:59:57 +02:00
|
|
|
set_mocktime(get_mocktime() + 1)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
2018-09-26 16:17:47 +02:00
|
|
|
if state:
|
|
|
|
value = 0
|
|
|
|
else:
|
|
|
|
value = 4070908800
|
|
|
|
self.nodes[0].spork('SPORK_16_INSTANTSEND_AUTOLOCKS', value)
|
|
|
|
|
|
|
|
# sends regular IX with high fee and may inputs (not-simple transaction)
|
|
|
|
def send_regular_IX(self):
|
|
|
|
receiver_addr = self.nodes[self.receiver_idx].getnewaddress()
|
|
|
|
txid = self.nodes[0].instantsendtoaddress(receiver_addr, 1.0)
|
2018-10-15 13:59:57 +02:00
|
|
|
MIN_FEE = satoshi_round(-0.0001)
|
|
|
|
fee = self.nodes[0].gettransaction(txid)['fee']
|
|
|
|
expected_fee = MIN_FEE * len(self.nodes[0].getrawtransaction(txid, True)['vin'])
|
|
|
|
assert_equal(fee, expected_fee)
|
2018-10-20 16:08:02 +02:00
|
|
|
return self.wait_for_instantlock(txid, self.nodes[0])
|
2018-09-26 16:17:47 +02:00
|
|
|
|
|
|
|
# sends simple trx, it should become IX if autolocks are allowed
|
|
|
|
def send_simple_tx(self):
|
|
|
|
raw_tx = self.create_raw_trx(self.nodes[0], self.nodes[self.receiver_idx], 1.0, 1, 4)
|
|
|
|
txid = self.nodes[0].sendrawtransaction(raw_tx['hex'])
|
|
|
|
self.sync_all()
|
2018-10-20 16:08:02 +02:00
|
|
|
return self.wait_for_instantlock(txid, self.nodes[0])
|
2018-09-26 16:17:47 +02:00
|
|
|
|
|
|
|
# sends complex trx, it should never become IX
|
|
|
|
def send_complex_tx(self):
|
|
|
|
raw_tx = self.create_raw_trx(self.nodes[0], self.nodes[self.receiver_idx], 1.0, 5, 100)
|
|
|
|
txid = self.nodes[0].sendrawtransaction(raw_tx['hex'])
|
|
|
|
self.sync_all()
|
2018-10-20 16:08:02 +02:00
|
|
|
return self.wait_for_instantlock(txid, self.nodes[0])
|
2018-09-26 16:17:47 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2018-11-05 10:28:04 +01:00
|
|
|
# make sure masternodes are synced
|
|
|
|
sync_masternodes(self.nodes)
|
2018-10-20 16:08:02 +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)
|
|
|
|
set_node_times(self.nodes, get_mocktime())
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
2018-09-26 16:17:47 +02:00
|
|
|
self.enforce_masternode_payments() # required for bip9 activation
|
|
|
|
assert(self.get_autoix_bip9_status() == 'defined')
|
|
|
|
assert(not self.get_autoix_spork_state())
|
|
|
|
|
|
|
|
assert(self.send_regular_IX())
|
|
|
|
assert(not self.send_simple_tx())
|
|
|
|
assert(not self.send_complex_tx())
|
|
|
|
|
|
|
|
self.activate_autoix_bip9()
|
|
|
|
self.set_autoix_spork_state(True)
|
|
|
|
|
|
|
|
assert(self.get_autoix_bip9_status() == 'active')
|
|
|
|
assert(self.get_autoix_spork_state())
|
|
|
|
|
|
|
|
assert(self.send_regular_IX())
|
|
|
|
assert(self.send_simple_tx())
|
|
|
|
assert(not self.send_complex_tx())
|
|
|
|
|
2018-10-15 13:59:57 +02:00
|
|
|
self.set_autoix_spork_state(False)
|
|
|
|
assert(not self.get_autoix_spork_state())
|
|
|
|
|
|
|
|
assert(self.send_regular_IX())
|
|
|
|
assert(not self.send_simple_tx())
|
|
|
|
assert(not self.send_complex_tx())
|
|
|
|
|
2018-10-20 16:08:02 +02:00
|
|
|
|
2018-09-26 16:17:47 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
AutoInstantSendTest().main()
|