2018-09-11 16:32:45 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-06-08 01:36:46 +02:00
|
|
|
# Copyright (c) 2015-2022 The Dash Core developers
|
2018-09-11 16:32:45 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test deterministic masternodes
|
|
|
|
#
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
from test_framework.blocktools import create_block, create_coinbase, get_masternode_payment
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.messages import CCbTx, COIN, CTransaction, FromHex, ToHex, uint256_to_string
|
2018-09-11 16:32:45 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2022-09-24 14:36:35 +02:00
|
|
|
from test_framework.util import assert_equal, force_finish_mnsync, get_bip9_status, p2p_port
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
class Masternode(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class DIP3Test(BitcoinTestFramework):
|
2019-09-23 23:15:43 +02:00
|
|
|
def set_test_params(self):
|
2018-09-11 16:32:45 +02:00
|
|
|
self.num_initial_mn = 11 # Should be >= 11 to make sure quorums are not always the same MNs
|
|
|
|
self.num_nodes = 1 + self.num_initial_mn + 2 # +1 for controller, +1 for mn-qt, +1 for mn created after dip3 activation
|
|
|
|
self.setup_clean_chain = True
|
2019-12-09 19:52:38 +01:00
|
|
|
self.supports_cli = False
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-01-23 17:36:51 +01:00
|
|
|
self.extra_args = ["-budgetparams=10:10:10"]
|
2018-09-11 16:32:45 +02:00
|
|
|
self.extra_args += ["-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"]
|
2019-04-25 17:39:04 +02:00
|
|
|
self.extra_args += ["-dip3params=135:150"]
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-06-20 18:37:09 +02:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2019-06-20 18:37:09 +02:00
|
|
|
def setup_network(self):
|
2019-08-09 01:14:11 +02:00
|
|
|
self.disable_mocktime()
|
2019-09-23 23:15:43 +02:00
|
|
|
self.add_nodes(1)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.start_controller_node()
|
2018-11-02 16:41:29 +01:00
|
|
|
self.import_deterministic_coinbase_privkeys()
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-09-23 23:15:43 +02:00
|
|
|
def start_controller_node(self):
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("starting controller node")
|
2019-09-23 23:15:43 +02:00
|
|
|
self.start_node(0, extra_args=self.extra_args)
|
2022-04-07 12:15:17 +02:00
|
|
|
for node in self.nodes[1:]:
|
|
|
|
if node is not None and node.process is not None:
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(node.index, 0)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("funding controller node")
|
2018-09-11 16:32:45 +02:00
|
|
|
while self.nodes[0].getbalance() < (self.num_initial_mn + 3) * 1000:
|
2020-01-04 12:21:16 +01:00
|
|
|
self.nodes[0].generate(10) # generate enough for collaterals
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("controller node has {} dash".format(self.nodes[0].getbalance()))
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-01-23 17:36:51 +01:00
|
|
|
# Make sure we're below block 135 (which activates dip3)
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("testing rejection of ProTx before dip3 activation")
|
2021-08-27 21:03:02 +02:00
|
|
|
assert self.nodes[0].getblockchaininfo()['blocks'] < 135
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
mns = []
|
|
|
|
|
2018-11-04 12:55:50 +01:00
|
|
|
# prepare mn which should still be accepted later when dip3 activates
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("creating collateral for mn-before-dip3")
|
2018-12-28 17:13:44 +01:00
|
|
|
before_dip3_mn = self.prepare_mn(self.nodes[0], 1, 'mn-before-dip3')
|
|
|
|
self.create_mn_collateral(self.nodes[0], before_dip3_mn)
|
|
|
|
mns.append(before_dip3_mn)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-04-25 17:39:04 +02:00
|
|
|
# block 150 starts enforcing DIP3 MN payments
|
2020-01-04 12:21:16 +01:00
|
|
|
self.nodes[0].generate(150 - self.nodes[0].getblockcount())
|
2021-08-27 21:03:02 +02:00
|
|
|
assert self.nodes[0].getblockcount() == 150
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("mining final block for DIP3 activation")
|
2018-12-28 17:13:44 +01:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
# We have hundreds of blocks to sync here, give it more time
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("syncing blocks for all nodes")
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks(self.nodes, timeout=120)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-04-25 17:39:04 +02:00
|
|
|
# DIP3 is fully enforced here
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
self.register_mn(self.nodes[0], before_dip3_mn)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.start_mn(before_dip3_mn)
|
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("registering MNs")
|
2022-04-07 12:15:17 +02:00
|
|
|
for i in range(self.num_initial_mn):
|
2018-12-28 17:13:44 +01:00
|
|
|
mn = self.prepare_mn(self.nodes[0], i + 2, "mn-%d" % i)
|
|
|
|
mns.append(mn)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
# start a few MNs before they are registered and a few after they are registered
|
|
|
|
start = (i % 3) == 0
|
|
|
|
if start:
|
|
|
|
self.start_mn(mn)
|
2018-10-25 16:29:50 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
# let a few of the protx MNs refer to the existing collaterals
|
|
|
|
fund = (i % 2) == 0
|
2018-10-25 16:29:50 +02:00
|
|
|
if fund:
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("register_fund %s" % mn.alias)
|
2018-12-28 17:13:44 +01:00
|
|
|
self.register_fund_mn(self.nodes[0], mn)
|
2018-10-25 16:29:50 +02:00
|
|
|
else:
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("create_collateral %s" % mn.alias)
|
2018-12-28 17:13:44 +01:00
|
|
|
self.create_mn_collateral(self.nodes[0], mn)
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("register %s" % mn.alias)
|
2018-12-28 17:13:44 +01:00
|
|
|
self.register_mn(self.nodes[0], mn)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
if not start:
|
|
|
|
self.start_mn(mn)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
self.sync_all()
|
|
|
|
self.assert_mnlists(mns)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("test that MNs disappear from the list when the ProTx collateral is spent")
|
2018-09-11 16:32:45 +02:00
|
|
|
spend_mns_count = 3
|
2018-12-28 17:13:44 +01:00
|
|
|
mns_tmp = [] + mns
|
2018-09-11 16:32:45 +02:00
|
|
|
dummy_txins = []
|
|
|
|
for i in range(spend_mns_count):
|
2018-12-28 17:13:44 +01:00
|
|
|
dummy_txin = self.spend_mn_collateral(mns[i], with_dummy_input_output=True)
|
2018-09-11 16:32:45 +02:00
|
|
|
dummy_txins.append(dummy_txin)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
2018-12-28 17:13:44 +01:00
|
|
|
mns_tmp.remove(mns[i])
|
2018-10-25 16:29:50 +02:00
|
|
|
self.assert_mnlists(mns_tmp)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("test that reverting the blockchain on a single node results in the mnlist to be reverted as well")
|
2018-09-11 16:32:45 +02:00
|
|
|
for i in range(spend_mns_count):
|
|
|
|
self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
|
2018-12-28 17:13:44 +01:00
|
|
|
mns_tmp.append(mns[spend_mns_count - 1 - i])
|
2018-10-25 16:29:50 +02:00
|
|
|
self.assert_mnlist(self.nodes[0], mns_tmp)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("cause a reorg with a double spend and check that mnlists are still correct on all nodes")
|
2018-09-11 16:32:45 +02:00
|
|
|
self.mine_double_spend(self.nodes[0], dummy_txins, self.nodes[0].getnewaddress(), use_mnmerkleroot_from_tip=True)
|
|
|
|
self.nodes[0].generate(spend_mns_count)
|
|
|
|
self.sync_all()
|
2018-10-25 16:29:50 +02:00
|
|
|
self.assert_mnlists(mns_tmp)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("test mn payment enforcement with deterministic MNs")
|
2018-09-11 16:32:45 +02:00
|
|
|
for i in range(20):
|
|
|
|
node = self.nodes[i % len(self.nodes)]
|
|
|
|
self.test_invalid_mn_payment(node)
|
2019-09-15 22:08:21 +02:00
|
|
|
self.nodes[0].generate(1)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.sync_all()
|
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("testing ProUpServTx")
|
2018-12-28 17:13:44 +01:00
|
|
|
for mn in mns:
|
2018-09-11 16:32:45 +02:00
|
|
|
self.test_protx_update_service(mn)
|
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("testing P2SH/multisig for payee addresses")
|
2020-05-15 11:34:41 +02:00
|
|
|
|
|
|
|
# Create 1 of 2 multisig
|
|
|
|
addr1 = self.nodes[0].getnewaddress()
|
|
|
|
addr2 = self.nodes[0].getnewaddress()
|
|
|
|
|
2020-12-17 13:46:20 +01:00
|
|
|
addr1Obj = self.nodes[0].getaddressinfo(addr1)
|
|
|
|
addr2Obj = self.nodes[0].getaddressinfo(addr2)
|
2020-05-15 11:34:41 +02:00
|
|
|
|
|
|
|
multisig = self.nodes[0].createmultisig(1, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address']
|
2018-12-28 17:13:44 +01:00
|
|
|
self.update_mn_payee(mns[0], multisig)
|
2018-11-15 08:06:21 +01:00
|
|
|
found_multisig_payee = False
|
2022-04-07 12:15:17 +02:00
|
|
|
for _ in range(len(mns)):
|
2018-11-15 08:06:21 +01:00
|
|
|
bt = self.nodes[0].getblocktemplate()
|
|
|
|
expected_payee = bt['masternode'][0]['payee']
|
|
|
|
expected_amount = bt['masternode'][0]['amount']
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
if expected_payee == multisig:
|
|
|
|
block = self.nodes[0].getblock(self.nodes[0].getbestblockhash())
|
|
|
|
cbtx = self.nodes[0].getrawtransaction(block['tx'][0], 1)
|
|
|
|
for out in cbtx['vout']:
|
|
|
|
if 'addresses' in out['scriptPubKey']:
|
|
|
|
if expected_payee in out['scriptPubKey']['addresses'] and out['valueSat'] == expected_amount:
|
|
|
|
found_multisig_payee = True
|
2021-08-27 21:03:02 +02:00
|
|
|
assert found_multisig_payee
|
2018-11-15 08:06:21 +01:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("testing reusing of collaterals for replaced MNs")
|
2022-04-07 12:15:17 +02:00
|
|
|
for i in range(5):
|
2018-12-28 17:13:44 +01:00
|
|
|
mn = mns[i]
|
2018-11-10 10:54:16 +01:00
|
|
|
# a few of these will actually refer to old ProRegTx internal collaterals,
|
|
|
|
# which should work the same as external collaterals
|
2018-12-28 17:13:44 +01:00
|
|
|
new_mn = self.prepare_mn(self.nodes[0], mn.idx, mn.alias)
|
|
|
|
new_mn.collateral_address = mn.collateral_address
|
|
|
|
new_mn.collateral_txid = mn.collateral_txid
|
|
|
|
new_mn.collateral_vout = mn.collateral_vout
|
|
|
|
|
|
|
|
self.register_mn(self.nodes[0], new_mn)
|
|
|
|
mns[i] = new_mn
|
2018-11-10 10:54:16 +01:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
2018-12-28 17:13:44 +01:00
|
|
|
self.assert_mnlists(mns)
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("restarting MN %s" % new_mn.alias)
|
2018-12-28 17:13:44 +01:00
|
|
|
self.stop_node(new_mn.idx)
|
|
|
|
self.start_mn(new_mn)
|
2018-11-10 10:54:16 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
2019-10-30 20:31:13 +01:00
|
|
|
self.log.info("testing masternode status updates")
|
|
|
|
# change voting address and see if changes are reflected in `masternode status` rpc output
|
|
|
|
mn = mns[0]
|
|
|
|
node = self.nodes[0]
|
|
|
|
old_dmnState = mn.node.masternode("status")["dmnState"]
|
|
|
|
old_voting_address = old_dmnState["votingAddress"]
|
|
|
|
new_voting_address = node.getnewaddress()
|
2021-08-27 21:03:02 +02:00
|
|
|
assert old_voting_address != new_voting_address
|
2019-10-30 20:31:13 +01:00
|
|
|
# also check if funds from payout address are used when no fee source address is specified
|
|
|
|
node.sendtoaddress(mn.rewards_address, 0.001)
|
2019-10-31 18:31:08 +01:00
|
|
|
node.protx('update_registrar', mn.protx_hash, "", new_voting_address, "")
|
2019-10-30 20:31:13 +01:00
|
|
|
node.generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
new_dmnState = mn.node.masternode("status")["dmnState"]
|
|
|
|
new_voting_address_from_rpc = new_dmnState["votingAddress"]
|
2021-08-27 21:03:02 +02:00
|
|
|
assert new_voting_address_from_rpc == new_voting_address
|
2019-10-31 18:31:08 +01:00
|
|
|
# make sure payoutAddress is the same as before
|
2021-08-27 21:03:02 +02:00
|
|
|
assert old_dmnState["payoutAddress"] == new_dmnState["payoutAddress"]
|
2019-10-30 20:31:13 +01:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
def prepare_mn(self, node, idx, alias):
|
2018-09-11 16:32:45 +02:00
|
|
|
mn = Masternode()
|
|
|
|
mn.idx = idx
|
|
|
|
mn.alias = alias
|
|
|
|
mn.p2p_port = p2p_port(mn.idx)
|
|
|
|
|
2018-10-21 21:45:16 +02:00
|
|
|
blsKey = node.bls('generate')
|
2018-12-28 17:13:44 +01:00
|
|
|
mn.fundsAddr = node.getnewaddress()
|
|
|
|
mn.ownerAddr = node.getnewaddress()
|
|
|
|
mn.operatorAddr = blsKey['public']
|
|
|
|
mn.votingAddr = mn.ownerAddr
|
2018-10-21 21:45:16 +02:00
|
|
|
mn.blsMnkey = blsKey['secret']
|
2018-12-28 17:13:44 +01:00
|
|
|
|
|
|
|
return mn
|
|
|
|
|
|
|
|
def create_mn_collateral(self, node, mn):
|
2018-09-11 16:32:45 +02:00
|
|
|
mn.collateral_address = node.getnewaddress()
|
|
|
|
mn.collateral_txid = node.sendtoaddress(mn.collateral_address, 1000)
|
2022-04-07 12:15:17 +02:00
|
|
|
mn.collateral_vout = None
|
2018-12-28 17:13:44 +01:00
|
|
|
node.generate(1)
|
|
|
|
|
|
|
|
rawtx = node.getrawtransaction(mn.collateral_txid, 1)
|
2018-09-11 16:32:45 +02:00
|
|
|
for txout in rawtx['vout']:
|
|
|
|
if txout['value'] == Decimal(1000):
|
|
|
|
mn.collateral_vout = txout['n']
|
|
|
|
break
|
2022-04-07 12:15:17 +02:00
|
|
|
assert mn.collateral_vout is not None
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
# register a protx MN and also fund it (using collateral inside ProRegTx)
|
|
|
|
def register_fund_mn(self, node, mn):
|
2018-12-28 10:24:48 +01:00
|
|
|
node.sendtoaddress(mn.fundsAddr, 1000.001)
|
2018-10-25 16:29:50 +02:00
|
|
|
mn.collateral_address = node.getnewaddress()
|
2018-12-28 17:13:44 +01:00
|
|
|
mn.rewards_address = node.getnewaddress()
|
2018-10-25 16:29:50 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
mn.protx_hash = node.protx('register_fund', mn.collateral_address, '127.0.0.1:%d' % mn.p2p_port, mn.ownerAddr, mn.operatorAddr, mn.votingAddr, 0, mn.rewards_address, mn.fundsAddr)
|
2018-10-25 16:29:50 +02:00
|
|
|
mn.collateral_txid = mn.protx_hash
|
2022-04-07 12:15:17 +02:00
|
|
|
mn.collateral_vout = None
|
2018-10-25 16:29:50 +02:00
|
|
|
|
|
|
|
rawtx = node.getrawtransaction(mn.collateral_txid, 1)
|
2018-09-11 16:32:45 +02:00
|
|
|
for txout in rawtx['vout']:
|
|
|
|
if txout['value'] == Decimal(1000):
|
|
|
|
mn.collateral_vout = txout['n']
|
|
|
|
break
|
2022-04-07 12:15:17 +02:00
|
|
|
assert mn.collateral_vout is not None
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-10-25 16:29:50 +02:00
|
|
|
# create a protx MN which refers to an existing collateral
|
2018-12-28 17:13:44 +01:00
|
|
|
def register_mn(self, node, mn):
|
2018-12-28 10:24:48 +01:00
|
|
|
node.sendtoaddress(mn.fundsAddr, 0.001)
|
2018-10-25 16:29:50 +02:00
|
|
|
mn.rewards_address = node.getnewaddress()
|
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
mn.protx_hash = node.protx('register', mn.collateral_txid, mn.collateral_vout, '127.0.0.1:%d' % mn.p2p_port, mn.ownerAddr, mn.operatorAddr, mn.votingAddr, 0, mn.rewards_address, mn.fundsAddr)
|
|
|
|
node.generate(1)
|
2018-10-25 16:29:50 +02:00
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
def start_mn(self, mn):
|
2022-04-07 12:15:17 +02:00
|
|
|
if len(self.nodes) <= mn.idx:
|
|
|
|
self.add_nodes(mn.idx - len(self.nodes) + 1)
|
|
|
|
assert len(self.nodes) == mn.idx + 1
|
|
|
|
self.start_node(mn.idx, extra_args = self.extra_args + ['-masternodeblsprivkey=%s' % mn.blsMnkey])
|
2019-10-09 18:48:12 +02:00
|
|
|
force_finish_mnsync(self.nodes[mn.idx])
|
2018-09-11 16:32:45 +02:00
|
|
|
mn.node = self.nodes[mn.idx]
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(mn.idx, 0)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def spend_mn_collateral(self, mn, with_dummy_input_output=False):
|
|
|
|
return self.spend_input(mn.collateral_txid, mn.collateral_vout, 1000, with_dummy_input_output)
|
|
|
|
|
2018-11-15 08:06:21 +01:00
|
|
|
def update_mn_payee(self, mn, payee):
|
2018-12-28 10:24:48 +01:00
|
|
|
self.nodes[0].sendtoaddress(mn.fundsAddr, 0.001)
|
|
|
|
self.nodes[0].protx('update_registrar', mn.protx_hash, '', '', payee, mn.fundsAddr)
|
2018-11-15 08:06:21 +01:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
info = self.nodes[0].protx('info', mn.protx_hash)
|
2021-08-27 21:03:02 +02:00
|
|
|
assert info['state']['payoutAddress'] == payee
|
2018-11-15 08:06:21 +01:00
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
def test_protx_update_service(self, mn):
|
2018-12-28 10:24:48 +01:00
|
|
|
self.nodes[0].sendtoaddress(mn.fundsAddr, 0.001)
|
|
|
|
self.nodes[0].protx('update_service', mn.protx_hash, '127.0.0.2:%d' % mn.p2p_port, mn.blsMnkey, "", mn.fundsAddr)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
for node in self.nodes:
|
2018-10-25 16:29:50 +02:00
|
|
|
protx_info = node.protx('info', mn.protx_hash)
|
2018-09-11 16:32:45 +02:00
|
|
|
mn_list = node.masternode('list')
|
2019-01-11 11:05:58 +01:00
|
|
|
assert_equal(protx_info['state']['service'], '127.0.0.2:%d' % mn.p2p_port)
|
2018-09-11 16:32:45 +02:00
|
|
|
assert_equal(mn_list['%s-%d' % (mn.collateral_txid, mn.collateral_vout)]['address'], '127.0.0.2:%d' % mn.p2p_port)
|
|
|
|
|
|
|
|
# undo
|
2018-12-28 10:24:48 +01:00
|
|
|
self.nodes[0].protx('update_service', mn.protx_hash, '127.0.0.1:%d' % mn.p2p_port, mn.blsMnkey, "", mn.fundsAddr)
|
2018-09-11 16:32:45 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
2018-10-25 16:29:50 +02:00
|
|
|
def assert_mnlists(self, mns):
|
2018-09-11 16:32:45 +02:00
|
|
|
for node in self.nodes:
|
2018-10-25 16:29:50 +02:00
|
|
|
self.assert_mnlist(node, mns)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-10-25 16:29:50 +02:00
|
|
|
def assert_mnlist(self, node, mns):
|
|
|
|
if not self.compare_mnlist(node, mns):
|
2018-09-11 16:32:45 +02:00
|
|
|
expected = []
|
|
|
|
for mn in mns:
|
2018-10-25 16:29:50 +02:00
|
|
|
expected.append('%s-%d' % (mn.collateral_txid, mn.collateral_vout))
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.error('mnlist: ' + str(node.masternode('list', 'status')))
|
|
|
|
self.log.error('expected: ' + str(expected))
|
2018-09-11 16:32:45 +02:00
|
|
|
raise AssertionError("mnlists does not match provided mns")
|
|
|
|
|
2018-10-25 16:29:50 +02:00
|
|
|
def compare_mnlist(self, node, mns):
|
2018-09-11 16:32:45 +02:00
|
|
|
mnlist = node.masternode('list', 'status')
|
|
|
|
for mn in mns:
|
|
|
|
s = '%s-%d' % (mn.collateral_txid, mn.collateral_vout)
|
2022-04-07 12:15:17 +02:00
|
|
|
if s not in mnlist:
|
2018-10-25 16:29:50 +02:00
|
|
|
return False
|
2018-09-11 16:32:45 +02:00
|
|
|
mnlist.pop(s, None)
|
|
|
|
if len(mnlist) != 0:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def spend_input(self, txid, vout, amount, with_dummy_input_output=False):
|
|
|
|
# with_dummy_input_output is useful if you want to test reorgs with double spends of the TX without touching the actual txid/vout
|
|
|
|
address = self.nodes[0].getnewaddress()
|
2018-12-28 17:13:44 +01:00
|
|
|
|
|
|
|
txins = [
|
|
|
|
{'txid': txid, 'vout': vout}
|
|
|
|
]
|
|
|
|
targets = {address: amount}
|
|
|
|
|
|
|
|
dummy_txin = None
|
2018-09-11 16:32:45 +02:00
|
|
|
if with_dummy_input_output:
|
|
|
|
dummyaddress = self.nodes[0].getnewaddress()
|
2019-01-21 06:08:31 +01:00
|
|
|
unspent = self.nodes[0].listunspent(110)
|
2018-12-28 17:13:44 +01:00
|
|
|
for u in unspent:
|
|
|
|
if u['amount'] > Decimal(1):
|
|
|
|
dummy_txin = {'txid': u['txid'], 'vout': u['vout']}
|
|
|
|
txins.append(dummy_txin)
|
|
|
|
targets[dummyaddress] = float(u['amount'] - Decimal(0.0001))
|
|
|
|
break
|
|
|
|
|
|
|
|
rawtx = self.nodes[0].createrawtransaction(txins, targets)
|
2018-09-11 16:32:45 +02:00
|
|
|
rawtx = self.nodes[0].fundrawtransaction(rawtx)['hex']
|
2020-12-11 03:25:55 +01:00
|
|
|
rawtx = self.nodes[0].signrawtransactionwithwallet(rawtx)['hex']
|
2020-09-27 07:43:00 +02:00
|
|
|
self.nodes[0].sendrawtransaction(rawtx)
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2018-12-28 17:13:44 +01:00
|
|
|
return dummy_txin
|
2018-09-11 16:32:45 +02:00
|
|
|
|
Merge #16726: tests: Avoid common Python default parameter gotcha when mutable dict/list:s are used as default parameter values
e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1 lint: Catch use of [] or {} as default parameter values in Python functions (practicalswift)
25dd86715039586d92176eee16e9c6644d2547f0 Avoid using mutable default parameter values (practicalswift)
Pull request description:
Avoid common Python default parameter gotcha when mutable `dict`/`list`:s are used as default parameter values.
Examples of this gotcha caught during review:
* https://github.com/bitcoin/bitcoin/pull/16673#discussion_r317415261
* https://github.com/bitcoin/bitcoin/pull/14565#discussion_r241942304
Perhaps surprisingly this is how mutable list and dictionary default parameter values behave in Python:
```
>>> def f(i, j=[], k={}):
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1, 1], {1: True})
>>> f(2)
([1, 1, 2], {1: True, 2: True})
```
In contrast to:
```
>>> def f(i, j=None, k=None):
... if j is None:
... j = []
... if k is None:
... k = {}
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1], {1: True})
>>> f(2)
([2], {2: True})
```
The latter is typically the intended behaviour.
This PR fixes two instances of this and adds a check guarding against this gotcha going forward :-)
ACKs for top commit:
Sjors:
Oh Python... ACK e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1. Testing tip: swap the two commits.
Tree-SHA512: 56e14d24fc866211a20185c9fdb274ed046c3aed2dc0e07699e58b6f9fa3b79f6d0c880fb02d72b7fe5cc5eb7c0ff6da0ead33123344e1a872209370c2e49e3f
2019-08-28 19:34:22 +02:00
|
|
|
def mine_block(self, node, vtx=None, miner_address=None, mn_payee=None, mn_amount=None, use_mnmerkleroot_from_tip=False, expected_error=None):
|
|
|
|
if vtx is None:
|
|
|
|
vtx = []
|
2018-09-11 16:32:45 +02:00
|
|
|
bt = node.getblocktemplate()
|
|
|
|
height = bt['height']
|
|
|
|
tip_hash = bt['previousblockhash']
|
|
|
|
|
|
|
|
tip_block = node.getblock(tip_hash)
|
|
|
|
|
|
|
|
coinbasevalue = bt['coinbasevalue']
|
|
|
|
if miner_address is None:
|
2019-09-15 22:08:21 +02:00
|
|
|
miner_address = self.nodes[0].getnewaddress()
|
2018-09-11 16:32:45 +02:00
|
|
|
if mn_payee is None:
|
|
|
|
if isinstance(bt['masternode'], list):
|
|
|
|
mn_payee = bt['masternode'][0]['payee']
|
|
|
|
else:
|
|
|
|
mn_payee = bt['masternode']['payee']
|
|
|
|
# we can't take the masternode payee amount from the template here as we might have additional fees in vtx
|
|
|
|
|
|
|
|
# calculate fees that the block template included (we'll have to remove it from the coinbase as we won't
|
|
|
|
# include the template's transactions
|
|
|
|
bt_fees = 0
|
|
|
|
for tx in bt['transactions']:
|
|
|
|
bt_fees += tx['fee']
|
|
|
|
|
|
|
|
new_fees = 0
|
|
|
|
for tx in vtx:
|
|
|
|
in_value = 0
|
|
|
|
out_value = 0
|
|
|
|
for txin in tx.vin:
|
2020-11-17 20:40:15 +01:00
|
|
|
txout = node.gettxout(uint256_to_string(txin.prevout.hash), txin.prevout.n, False)
|
2018-09-11 16:32:45 +02:00
|
|
|
in_value += int(txout['value'] * COIN)
|
|
|
|
for txout in tx.vout:
|
|
|
|
out_value += txout.nValue
|
|
|
|
new_fees += in_value - out_value
|
|
|
|
|
|
|
|
# fix fees
|
|
|
|
coinbasevalue -= bt_fees
|
|
|
|
coinbasevalue += new_fees
|
|
|
|
|
|
|
|
if mn_amount is None:
|
2020-09-10 18:23:11 +02:00
|
|
|
realloc_info = get_bip9_status(self.nodes[0], 'realloc')
|
|
|
|
realloc_height = 99999999
|
|
|
|
if realloc_info['status'] == 'active':
|
|
|
|
realloc_height = realloc_info['since']
|
|
|
|
mn_amount = get_masternode_payment(height, coinbasevalue, realloc_height)
|
2018-09-11 16:32:45 +02:00
|
|
|
miner_amount = coinbasevalue - mn_amount
|
|
|
|
|
|
|
|
outputs = {miner_address: str(Decimal(miner_amount) / COIN)}
|
|
|
|
if mn_amount > 0:
|
|
|
|
outputs[mn_payee] = str(Decimal(mn_amount) / COIN)
|
|
|
|
|
|
|
|
coinbase = FromHex(CTransaction(), node.createrawtransaction([], outputs))
|
|
|
|
coinbase.vin = create_coinbase(height).vin
|
|
|
|
|
|
|
|
# We can't really use this one as it would result in invalid merkle roots for masternode lists
|
|
|
|
if len(bt['coinbase_payload']) != 0:
|
2018-11-22 06:05:32 +01:00
|
|
|
cbtx = FromHex(CCbTx(version=1), bt['coinbase_payload'])
|
2018-09-11 16:32:45 +02:00
|
|
|
if use_mnmerkleroot_from_tip:
|
|
|
|
if 'cbTx' in tip_block:
|
|
|
|
cbtx.merkleRootMNList = int(tip_block['cbTx']['merkleRootMNList'], 16)
|
|
|
|
else:
|
|
|
|
cbtx.merkleRootMNList = 0
|
|
|
|
coinbase.nVersion = 3
|
|
|
|
coinbase.nType = 5 # CbTx
|
|
|
|
coinbase.vExtraPayload = cbtx.serialize()
|
|
|
|
|
|
|
|
coinbase.calc_sha256()
|
|
|
|
|
|
|
|
block = create_block(int(tip_hash, 16), coinbase)
|
|
|
|
block.vtx += vtx
|
2018-11-23 15:42:09 +01:00
|
|
|
|
|
|
|
# Add quorum commitments from template
|
|
|
|
for tx in bt['transactions']:
|
|
|
|
tx2 = FromHex(CTransaction(), tx['data'])
|
|
|
|
if tx2.nType == 6:
|
|
|
|
block.vtx.append(tx2)
|
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.solve()
|
|
|
|
result = node.submitblock(ToHex(block))
|
|
|
|
if expected_error is not None and result != expected_error:
|
|
|
|
raise AssertionError('mining the block should have failed with error %s, but submitblock returned %s' % (expected_error, result))
|
|
|
|
elif expected_error is None and result is not None:
|
|
|
|
raise AssertionError('submitblock returned %s' % (result))
|
|
|
|
|
|
|
|
def mine_double_spend(self, node, txins, target_address, use_mnmerkleroot_from_tip=False):
|
|
|
|
amount = Decimal(0)
|
|
|
|
for txin in txins:
|
|
|
|
txout = node.gettxout(txin['txid'], txin['vout'], False)
|
|
|
|
amount += txout['value']
|
|
|
|
amount -= Decimal("0.001") # fee
|
|
|
|
|
|
|
|
rawtx = node.createrawtransaction(txins, {target_address: amount})
|
2020-12-11 03:25:55 +01:00
|
|
|
rawtx = node.signrawtransactionwithwallet(rawtx)['hex']
|
2018-09-11 16:32:45 +02:00
|
|
|
tx = FromHex(CTransaction(), rawtx)
|
|
|
|
|
|
|
|
self.mine_block(node, [tx], use_mnmerkleroot_from_tip=use_mnmerkleroot_from_tip)
|
|
|
|
|
|
|
|
def test_invalid_mn_payment(self, node):
|
|
|
|
mn_payee = self.nodes[0].getnewaddress()
|
|
|
|
self.mine_block(node, mn_payee=mn_payee, expected_error='bad-cb-payee')
|
|
|
|
self.mine_block(node, mn_amount=1, expected_error='bad-cb-payee')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
DIP3Test().main()
|