InstantSend-related tests refactoring (#2333)

* create base class for dash-specific tests

* use common base class in p2p-autoinstantsend.py

* use common base class in p2p-instantsend.py

* rename check_IX_lock to wait_for_instantlock
This commit is contained in:
gladcow 2018-10-20 17:08:02 +03:00 committed by UdjinM6
parent 81d60bc283
commit 11a0cbf84d
4 changed files with 202 additions and 462 deletions

View File

@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_framework import DashTestFramework
from test_framework.util import *
from time import *
@ -16,116 +16,17 @@ is full (more than 0.1 part from max value).
'''
MASTERNODE_COLLATERAL = 1000
MAX_MEMPOOL_SIZE = 5 # max node mempool in MBs
MB_SIZE = 1000000 # C++ code use this coefficient to calc MB in mempool
AUTO_IX_MEM_THRESHOLD = 0.1
class MasternodeInfo:
def __init__(self, key, collateral_id, collateral_out):
self.key = key
self.collateral_id = collateral_id
self.collateral_out = collateral_out
class AutoIXMempoolTest(BitcoinTestFramework):
class AutoIXMempoolTest(DashTestFramework):
def __init__(self):
super().__init__()
self.mn_count = 10
self.num_nodes = self.mn_count + 3
self.mninfo = []
self.setup_clean_chain = True
self.is_network_split = False
# set sender, receiver, isolated nodes
super().__init__(13, 10, ["-maxmempool=%d" % MAX_MEMPOOL_SIZE])
# set sender, receiver
self.receiver_idx = self.num_nodes - 2
self.sender_idx = self.num_nodes - 3
# additional args
self.extra_args = ["-maxmempool=%d" % MAX_MEMPOOL_SIZE]
def create_simple_node(self):
idx = len(self.nodes)
args = ["-debug"] + self.extra_args
self.nodes.append(start_node(idx, self.options.tmpdir,
args))
for i in range(0, idx):
connect_nodes(self.nodes[i], idx)
def get_mnconf_file(self):
return os.path.join(self.options.tmpdir, "node0/regtest/masternode.conf")
def prepare_masternodes(self):
for idx in range(0, self.mn_count):
key = self.nodes[0].masternode("genkey")
address = self.nodes[0].getnewaddress()
txid = self.nodes[0].sendtoaddress(address, MASTERNODE_COLLATERAL)
txrow = self.nodes[0].getrawtransaction(txid, True)
collateral_vout = 0
for vout_idx in range(0, len(txrow["vout"])):
vout = txrow["vout"][vout_idx]
if vout["value"] == MASTERNODE_COLLATERAL:
collateral_vout = vout_idx
self.nodes[0].lockunspent(False,
[{"txid": txid, "vout": collateral_vout}])
self.mninfo.append(MasternodeInfo(key, txid, collateral_vout))
def write_mn_config(self):
conf = self.get_mnconf_file()
f = open(conf, 'a')
for idx in range(0, self.mn_count):
f.write("mn%d 127.0.0.1:%d %s %s %d\n" % (idx + 1, p2p_port(idx + 1),
self.mninfo[idx].key,
self.mninfo[idx].collateral_id,
self.mninfo[idx].collateral_out))
f.close()
def create_masternodes(self):
for idx in range(0, self.mn_count):
args = ['-debug=masternode', '-externalip=127.0.0.1', '-masternode=1',
'-masternodeprivkey=%s' % self.mninfo[idx].key] + self.extra_args
self.nodes.append(start_node(idx + 1, self.options.tmpdir, args))
for i in range(0, idx + 1):
connect_nodes(self.nodes[i], idx + 1)
def setup_network(self):
self.nodes = []
# create faucet node for collateral and transactions
args = ["-debug"] + self.extra_args
self.nodes.append(start_node(0, self.options.tmpdir, args))
required_balance = MASTERNODE_COLLATERAL * self.mn_count + 1
while self.nodes[0].getbalance() < required_balance:
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.nodes[0].generate(1)
# create masternodes
self.prepare_masternodes()
self.write_mn_config()
stop_node(self.nodes[0], 0)
args = ["-debug",
"-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"] + \
self.extra_args
self.nodes[0] = start_node(0, self.options.tmpdir,
args)
self.create_masternodes()
# create connected simple nodes
for i in range(0, self.num_nodes - self.mn_count - 1):
self.create_simple_node()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.nodes[0].generate(1)
# sync nodes
self.sync_all()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
sync_masternodes(self.nodes)
for i in range(1, self.mn_count + 1):
res = self.nodes[0].masternode("start-alias", "mn%d" % i)
assert(res["result"] == 'successful')
sync_masternodes(self.nodes)
mn_info = self.nodes[0].masternodelist("status")
assert(len(mn_info) == self.mn_count)
for status in mn_info.values():
assert(status == 'ENABLED')
def get_autoix_bip9_status(self):
info = self.nodes[0].getblockchaininfo()
@ -187,78 +88,18 @@ class AutoIXMempoolTest(BitcoinTestFramework):
value = 4070908800
self.nodes[0].spork('SPORK_16_INSTANTSEND_AUTOLOCKS', value)
def enforce_masternode_payments(self):
self.nodes[0].spork('SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT', 0)
def create_raw_trx(self, node_from, node_to, amount, min_inputs, max_inputs):
assert(min_inputs <= max_inputs)
# fill inputs
inputs=[]
balances = node_from.listunspent()
in_amount = 0.0
last_amount = 0.0
for tx in balances:
if len(inputs) < min_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
elif in_amount > amount:
break
elif len(inputs) < max_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
else:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount -= last_amount
in_amount += float(tx['amount'])
inputs[-1] = input
last_amount = float(tx['amount'])
assert(len(inputs) > 0)
assert(in_amount > amount)
# fill outputs
receiver_address = node_to.getnewaddress()
change_address = node_from.getnewaddress()
fee = 0.001
outputs={}
outputs[receiver_address] = satoshi_round(amount)
outputs[change_address] = satoshi_round(in_amount - amount - fee)
rawtx = node_from.createrawtransaction(inputs, outputs)
return node_from.signrawtransaction(rawtx)
def check_IX_lock(self, txid, node):
# wait for instantsend locks
start = time()
locked = False
while True:
is_trx = node.gettransaction(txid)
if is_trx['instantlock']:
locked = True
break
if time() > start + 10:
break
sleep(0.1)
return locked
# sends regular IX with high fee and may inputs (not-simple transaction)
def send_regular_IX(self, sender, receiver):
receiver_addr = receiver.getnewaddress()
txid = sender.instantsendtoaddress(receiver_addr, 1.0)
return self.check_IX_lock(txid, sender)
return self.wait_for_instantlock(txid, sender)
# sends simple trx, it should become IX if autolocks are allowed
def send_simple_tx(self, sender, receiver):
raw_tx = self.create_raw_trx(sender, receiver, 1.0, 1, 4)
txid = self.nodes[0].sendrawtransaction(raw_tx['hex'])
self.sync_all()
return self.check_IX_lock(txid, sender)
return self.wait_for_instantlock(txid, sender)
def get_mempool_size(self, node):
info = node.getmempoolinfo()

View File

@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_framework import DashTestFramework
from test_framework.util import *
from time import *
@ -22,120 +22,14 @@ Also checks that this functionality doesn't influence regular InstantSend
transactions with high fee.
'''
MASTERNODE_COLLATERAL = 1000
class MasternodeInfo:
def __init__(self, key, collateral_id, collateral_out):
self.key = key
self.collateral_id = collateral_id
self.collateral_out = collateral_out
class AutoInstantSendTest(BitcoinTestFramework):
class AutoInstantSendTest(DashTestFramework):
def __init__(self):
super().__init__()
self.mn_count = 10
self.num_nodes = self.mn_count + 4
self.mninfo = []
self.setup_clean_chain = True
self.is_network_split = False
super().__init__(14, 10, [])
# 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 create_simple_node(self):
idx = len(self.nodes)
self.nodes.append(start_node(idx, self.options.tmpdir,
["-debug"]))
for i in range(0, idx):
connect_nodes(self.nodes[i], idx)
def get_mnconf_file(self):
return os.path.join(self.options.tmpdir, "node0/regtest/masternode.conf")
def prepare_masternodes(self):
for idx in range(0, self.mn_count):
key = self.nodes[0].masternode("genkey")
address = self.nodes[0].getnewaddress()
txid = self.nodes[0].sendtoaddress(address, MASTERNODE_COLLATERAL)
txrow = self.nodes[0].getrawtransaction(txid, True)
collateral_vout = 0
for vout_idx in range(0, len(txrow["vout"])):
vout = txrow["vout"][vout_idx]
if vout["value"] == MASTERNODE_COLLATERAL:
collateral_vout = vout_idx
self.nodes[0].lockunspent(False,
[{"txid": txid, "vout": collateral_vout}])
self.mninfo.append(MasternodeInfo(key, txid, collateral_vout))
def write_mn_config(self):
conf = self.get_mnconf_file()
f = open(conf, 'a')
for idx in range(0, self.mn_count):
f.write("mn%d 127.0.0.1:%d %s %s %d\n" % (idx + 1, p2p_port(idx + 1),
self.mninfo[idx].key,
self.mninfo[idx].collateral_id,
self.mninfo[idx].collateral_out))
f.close()
def create_masternodes(self):
for idx in range(0, self.mn_count):
self.nodes.append(start_node(idx + 1, self.options.tmpdir,
['-debug=masternode', '-externalip=127.0.0.1',
'-masternode=1',
'-masternodeprivkey=%s' % self.mninfo[idx].key
]))
for i in range(0, idx + 1):
connect_nodes(self.nodes[i], idx + 1)
def sentinel(self):
for i in range(1, self.mn_count + 1):
self.nodes[i].sentinelping("1.1.0")
def setup_network(self):
self.nodes = []
# create faucet node for collateral and transactions
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
required_balance = MASTERNODE_COLLATERAL * self.mn_count + 1
while self.nodes[0].getbalance() < required_balance:
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.nodes[0].generate(1)
# create masternodes
self.prepare_masternodes()
self.write_mn_config()
stop_node(self.nodes[0], 0)
self.nodes[0] = start_node(0, self.options.tmpdir,
["-debug", "-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"])
self.create_masternodes()
# create connected simple nodes
for i in range(0, self.num_nodes - self.mn_count - 1):
self.create_simple_node()
# 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)
# sync nodes
self.sync_all()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
sync_masternodes(self.nodes)
for i in range(1, self.mn_count + 1):
res = self.nodes[0].masternode("start-alias", "mn%d" % i)
assert(res["result"] == 'successful')
sync_masternodes(self.nodes)
#self.sentinel()
mn_info = self.nodes[0].masternodelist("status")
assert(len(mn_info) == self.mn_count)
for status in mn_info.values():
assert(status == 'ENABLED')
def get_autoix_bip9_status(self):
info = self.nodes[0].getblockchaininfo()
return info['bip9_softforks']['autoix']['status']
@ -198,66 +92,6 @@ class AutoInstantSendTest(BitcoinTestFramework):
value = 4070908800
self.nodes[0].spork('SPORK_16_INSTANTSEND_AUTOLOCKS', value)
def enforce_masternode_payments(self):
self.nodes[0].spork('SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT', 0)
def create_raw_trx(self, node_from, node_to, amount, min_inputs, max_inputs):
assert(min_inputs <= max_inputs)
# fill inputs
inputs=[]
balances = node_from.listunspent()
in_amount = 0.0
last_amount = 0.0
for tx in balances:
if len(inputs) < min_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
elif in_amount > amount:
break
elif len(inputs) < max_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
else:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount -= last_amount
in_amount += float(tx['amount'])
inputs[-1] = input
last_amount = float(tx['amount'])
assert(len(inputs) > 0)
assert(in_amount > amount)
# fill outputs
receiver_address = node_to.getnewaddress()
change_address = node_from.getnewaddress()
fee = 0.001
outputs={}
outputs[receiver_address] = satoshi_round(amount)
outputs[change_address] = satoshi_round(in_amount - amount - fee)
rawtx = node_from.createrawtransaction(inputs, outputs)
return node_from.signrawtransaction(rawtx)
def check_IX_lock(self, txid):
# wait for instantsend locks
start = time()
locked = False
while True:
is_trx = self.nodes[0].gettransaction(txid)
if is_trx['instantlock']:
locked = True
break
if time() > start + 10:
break
sleep(0.1)
return locked
# 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()
@ -266,24 +100,32 @@ class AutoInstantSendTest(BitcoinTestFramework):
fee = self.nodes[0].gettransaction(txid)['fee']
expected_fee = MIN_FEE * len(self.nodes[0].getrawtransaction(txid, True)['vin'])
assert_equal(fee, expected_fee)
return self.check_IX_lock(txid)
return self.wait_for_instantlock(txid, self.nodes[0])
# 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()
return self.check_IX_lock(txid)
return self.wait_for_instantlock(txid, self.nodes[0])
# 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()
return self.check_IX_lock(txid)
return self.wait_for_instantlock(txid, self.nodes[0])
def run_test(self):
# 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)
self.enforce_masternode_payments() # required for bip9 activation
assert(self.get_autoix_bip9_status() == 'defined')
assert(not self.get_autoix_spork_state())
@ -309,5 +151,6 @@ class AutoInstantSendTest(BitcoinTestFramework):
assert(not self.send_simple_tx())
assert(not self.send_complex_tx())
if __name__ == '__main__':
AutoInstantSendTest().main()

View File

@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_framework import DashTestFramework
from test_framework.util import *
from time import *
@ -12,95 +12,15 @@ from time import *
InstantSendTest -- test InstantSend functionality (prevent doublespend for unconfirmed transactions)
'''
MASTERNODE_COLLATERAL = 1000
class MasternodeInfo:
def __init__(self, key, collateral_id, collateral_out):
self.key = key
self.collateral_id = collateral_id
self.collateral_out = collateral_out
class InstantSendTest(BitcoinTestFramework):
class InstantSendTest(DashTestFramework):
def __init__(self):
super().__init__()
self.mn_count = 10
self.num_nodes = self.mn_count + 4
self.mninfo = []
self.setup_clean_chain = True
self.is_network_split = False
super().__init__(14, 10, [])
# 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 create_simple_node(self):
idx = len(self.nodes)
self.nodes.append(start_node(idx, self.options.tmpdir,
["-debug"]))
for i in range(0, idx):
connect_nodes(self.nodes[i], idx)
def get_mnconf_file(self):
return os.path.join(self.options.tmpdir, "node0/regtest/masternode.conf")
def prepare_masternodes(self):
for idx in range(0, self.mn_count):
key = self.nodes[0].masternode("genkey")
address = self.nodes[0].getnewaddress()
txid = self.nodes[0].sendtoaddress(address, MASTERNODE_COLLATERAL)
txrow = self.nodes[0].getrawtransaction(txid, True)
collateral_vout = 0
for vout_idx in range(0, len(txrow["vout"])):
vout = txrow["vout"][vout_idx]
if vout["value"] == MASTERNODE_COLLATERAL:
collateral_vout = vout_idx
self.nodes[0].lockunspent(False,
[{"txid": txid, "vout": collateral_vout}])
self.mninfo.append(MasternodeInfo(key, txid, collateral_vout))
def write_mn_config(self):
conf = self.get_mnconf_file()
f = open(conf, 'a')
for idx in range(0, self.mn_count):
f.write("mn%d 127.0.0.1:%d %s %s %d\n" % (idx + 1, p2p_port(idx + 1),
self.mninfo[idx].key,
self.mninfo[idx].collateral_id,
self.mninfo[idx].collateral_out))
f.close()
def create_masternodes(self):
for idx in range(0, self.mn_count):
self.nodes.append(start_node(idx + 1, self.options.tmpdir,
['-debug=masternode', '-externalip=127.0.0.1',
'-masternode=1',
'-masternodeprivkey=%s' % self.mninfo[idx].key
]))
for i in range(0, idx + 1):
connect_nodes(self.nodes[i], idx + 1)
def sentinel(self):
for i in range(1, self.mn_count + 1):
self.nodes[i].sentinelping("1.1.0")
def setup_network(self):
self.nodes = []
# create faucet node for collateral and transactions
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"]))
required_balance = MASTERNODE_COLLATERAL * self.mn_count + 1
while self.nodes[0].getbalance() < required_balance:
set_mocktime(get_mocktime() + 1)
self.nodes[0].generate(1)
# create masternodes
self.prepare_masternodes()
self.write_mn_config()
stop_node(self.nodes[0], 0)
self.nodes[0] = start_node(0, self.options.tmpdir, ["-debug"])
self.create_masternodes()
# create connected simple nodes
for i in range(0, self.num_nodes - self.mn_count - 1):
self.create_simple_node()
def run_test(self):
# feed the sender with some balance
sender_addr = self.nodes[self.sender_idx].getnewaddress()
self.nodes[0].sendtoaddress(sender_addr, 1)
@ -108,48 +28,10 @@ class InstantSendTest(BitcoinTestFramework):
for i in range(0, 2):
set_mocktime(get_mocktime() + 1)
self.nodes[0].generate(1)
# sync nodes
self.sync_all()
set_mocktime(get_mocktime() + 1)
sync_masternodes(self.nodes)
for i in range(1, self.mn_count + 1):
res = self.nodes[0].masternode("start-alias", "mn%d" % i)
assert(res["result"] == 'successful')
sync_masternodes(self.nodes)
#self.sentinel()
mn_info = self.nodes[0].masternodelist("status")
assert(len(mn_info) == self.mn_count)
for status in mn_info.values():
assert(status == 'ENABLED')
def create_raw_trx(self, node_from, node_to, amount):
# fill inputs
inputs=[]
balances = node_from.listunspent()
for tx in balances:
if tx['amount'] > amount:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount = float(tx['amount'])
inputs.append(input)
break
assert(len(inputs) > 0)
# fill outputs
receiver_address = node_to.getnewaddress()
change_address = node_from.getnewaddress()
fee = 0.001
outputs={}
outputs[receiver_address] = satoshi_round(amount)
outputs[change_address] = satoshi_round(in_amount - amount - fee)
rawtx = node_from.createrawtransaction(inputs, outputs)
return node_from.signrawtransaction(rawtx)
def run_test(self):
# create doublepending transaction, but don't relay it
dblspnd_tx = self.create_raw_trx(self.nodes[self.sender_idx],
self.nodes[self.isolated_idx],
0.5)
0.5, 1, 100)
# stop one node to isolate it from network
stop_node(self.nodes[self.isolated_idx], self.isolated_idx)
# instantsend to receiver

View File

@ -12,13 +12,17 @@ import sys
import shutil
import tempfile
import traceback
from time import time, sleep
from .util import (
initialize_chain,
start_node,
start_nodes,
connect_nodes_bi,
connect_nodes,
sync_blocks,
sync_mempools,
sync_masternodes,
stop_nodes,
stop_node,
enable_coverage,
@ -27,7 +31,11 @@ from .util import (
PortSeed,
set_cache_mocktime,
set_genesis_mocktime,
get_mocktime
get_mocktime,
set_mocktime,
set_node_times,
p2p_port,
satoshi_round
)
from .authproxy import JSONRPCException
@ -195,6 +203,172 @@ class BitcoinTestFramework(object):
sys.exit(1)
MASTERNODE_COLLATERAL = 1000
class MasternodeInfo:
def __init__(self, key, collateral_id, collateral_out):
self.key = key
self.collateral_id = collateral_id
self.collateral_out = collateral_out
class DashTestFramework(BitcoinTestFramework):
def __init__(self, num_nodes, masterodes_count, extra_args):
super().__init__()
self.mn_count = masterodes_count
self.num_nodes = num_nodes
self.mninfo = []
self.setup_clean_chain = True
self.is_network_split = False
# additional args
self.extra_args = extra_args
def create_simple_node(self):
idx = len(self.nodes)
args = ["-debug"] + self.extra_args
self.nodes.append(start_node(idx, self.options.tmpdir,
args))
for i in range(0, idx):
connect_nodes(self.nodes[i], idx)
def get_mnconf_file(self):
return os.path.join(self.options.tmpdir, "node0/regtest/masternode.conf")
def prepare_masternodes(self):
for idx in range(0, self.mn_count):
key = self.nodes[0].masternode("genkey")
address = self.nodes[0].getnewaddress()
txid = self.nodes[0].sendtoaddress(address, MASTERNODE_COLLATERAL)
txrow = self.nodes[0].getrawtransaction(txid, True)
collateral_vout = 0
for vout_idx in range(0, len(txrow["vout"])):
vout = txrow["vout"][vout_idx]
if vout["value"] == MASTERNODE_COLLATERAL:
collateral_vout = vout_idx
self.nodes[0].lockunspent(False,
[{"txid": txid, "vout": collateral_vout}])
self.mninfo.append(MasternodeInfo(key, txid, collateral_vout))
def write_mn_config(self):
conf = self.get_mnconf_file()
f = open(conf, 'a')
for idx in range(0, self.mn_count):
f.write("mn%d 127.0.0.1:%d %s %s %d\n" % (idx + 1, p2p_port(idx + 1),
self.mninfo[idx].key,
self.mninfo[idx].collateral_id,
self.mninfo[idx].collateral_out))
f.close()
def create_masternodes(self):
for idx in range(0, self.mn_count):
args = ['-debug=masternode', '-externalip=127.0.0.1', '-masternode=1',
'-masternodeprivkey=%s' % self.mninfo[idx].key] + self.extra_args
self.nodes.append(start_node(idx + 1, self.options.tmpdir, args))
for i in range(0, idx + 1):
connect_nodes(self.nodes[i], idx + 1)
def setup_network(self):
self.nodes = []
# create faucet node for collateral and transactions
args = ["-debug"] + self.extra_args
self.nodes.append(start_node(0, self.options.tmpdir, args))
required_balance = MASTERNODE_COLLATERAL * self.mn_count + 1
while self.nodes[0].getbalance() < required_balance:
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.nodes[0].generate(1)
# create masternodes
self.prepare_masternodes()
self.write_mn_config()
stop_node(self.nodes[0], 0)
args = ["-debug",
"-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"] + \
self.extra_args
self.nodes[0] = start_node(0, self.options.tmpdir,
args)
self.create_masternodes()
# create connected simple nodes
for i in range(0, self.num_nodes - self.mn_count - 1):
self.create_simple_node()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.nodes[0].generate(1)
# sync nodes
self.sync_all()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
sync_masternodes(self.nodes)
for i in range(1, self.mn_count + 1):
res = self.nodes[0].masternode("start-alias", "mn%d" % i)
assert (res["result"] == 'successful')
sync_masternodes(self.nodes)
mn_info = self.nodes[0].masternodelist("status")
assert (len(mn_info) == self.mn_count)
for status in mn_info.values():
assert (status == 'ENABLED')
def enforce_masternode_payments(self):
self.nodes[0].spork('SPORK_8_MASTERNODE_PAYMENT_ENFORCEMENT', 0)
def create_raw_trx(self, node_from, node_to, amount, min_inputs, max_inputs):
assert (min_inputs <= max_inputs)
# fill inputs
inputs = []
balances = node_from.listunspent()
in_amount = 0.0
last_amount = 0.0
for tx in balances:
if len(inputs) < min_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
elif in_amount > amount:
break
elif len(inputs) < max_inputs:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount += float(tx['amount'])
inputs.append(input)
else:
input = {}
input["txid"] = tx['txid']
input['vout'] = tx['vout']
in_amount -= last_amount
in_amount += float(tx['amount'])
inputs[-1] = input
last_amount = float(tx['amount'])
assert (len(inputs) > 0)
assert (in_amount > amount)
# fill outputs
receiver_address = node_to.getnewaddress()
change_address = node_from.getnewaddress()
fee = 0.001
outputs = {}
outputs[receiver_address] = satoshi_round(amount)
outputs[change_address] = satoshi_round(in_amount - amount - fee)
rawtx = node_from.createrawtransaction(inputs, outputs)
return node_from.signrawtransaction(rawtx)
def wait_for_instantlock(self, txid, node):
# wait for instantsend locks
start = time()
locked = False
while True:
is_trx = node.gettransaction(txid)
if is_trx['instantlock']:
locked = True
break
if time() > start + 10:
break
sleep(0.1)
return locked
# Test framework for doing p2p comparison testing, which sets up some bitcoind
# binaries:
# 1 binary: test binary