9
9
10
10
from decimal import Decimal
11
11
12
- from test_framework .blocktools import create_block , create_coinbase , get_masternode_payment
13
- from test_framework .messages import CCbTx , COIN , CTransaction , FromHex , ToHex , uint256_to_string
12
+ from test_framework .blocktools import create_block_with_mnpayments
13
+ from test_framework .messages import CTransaction , FromHex , ToHex
14
14
from test_framework .test_framework import BitcoinTestFramework
15
15
from test_framework .util import assert_equal , force_finish_mnsync , p2p_port
16
16
@@ -131,15 +131,15 @@ def run_test(self):
131
131
self .assert_mnlist (self .nodes [0 ], mns_tmp )
132
132
133
133
self .log .info ("cause a reorg with a double spend and check that mnlists are still correct on all nodes" )
134
- self .mine_double_spend (self .nodes [0 ], dummy_txins , self .nodes [0 ].getnewaddress (), use_mnmerkleroot_from_tip = True )
134
+ self .mine_double_spend (mns , self .nodes [0 ], dummy_txins , self .nodes [0 ].getnewaddress ())
135
135
self .nodes [0 ].generate (spend_mns_count )
136
136
self .sync_all ()
137
137
self .assert_mnlists (mns_tmp )
138
138
139
139
self .log .info ("test mn payment enforcement with deterministic MNs" )
140
140
for i in range (20 ):
141
141
node = self .nodes [i % len (self .nodes )]
142
- self .test_invalid_mn_payment (node )
142
+ self .test_invalid_mn_payment (mns , node )
143
143
self .nodes [0 ].generate (1 )
144
144
self .sync_all ()
145
145
@@ -218,6 +218,7 @@ def prepare_mn(self, node, idx, alias):
218
218
mn .idx = idx
219
219
mn .alias = alias
220
220
mn .p2p_port = p2p_port (mn .idx )
221
+ mn .operator_reward = (mn .idx % self .num_initial_mn )
221
222
222
223
blsKey = node .bls ('generate' )
223
224
mn .fundsAddr = node .getnewaddress ()
@@ -247,7 +248,7 @@ def register_fund_mn(self, node, mn):
247
248
mn .collateral_address = node .getnewaddress ()
248
249
mn .rewards_address = node .getnewaddress ()
249
250
250
- 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 )
251
+ mn .protx_hash = node .protx ('register_fund' , mn .collateral_address , '127.0.0.1:%d' % mn .p2p_port , mn .ownerAddr , mn .operatorAddr , mn .votingAddr , mn . operator_reward , mn .rewards_address , mn .fundsAddr )
251
252
mn .collateral_txid = mn .protx_hash
252
253
mn .collateral_vout = None
253
254
@@ -263,7 +264,7 @@ def register_mn(self, node, mn):
263
264
node .sendtoaddress (mn .fundsAddr , 0.001 )
264
265
mn .rewards_address = node .getnewaddress ()
265
266
266
- 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 )
267
+ 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 , mn . operator_reward , mn .rewards_address , mn .fundsAddr )
267
268
node .generate (1 )
268
269
269
270
def start_mn (self , mn ):
@@ -353,93 +354,15 @@ def spend_input(self, txid, vout, amount, with_dummy_input_output=False):
353
354
354
355
return dummy_txin
355
356
356
- def mine_block (self , node , vtx = None , miner_address = None , mn_payee = None , mn_amount = None , use_mnmerkleroot_from_tip = False , expected_error = None ):
357
- if vtx is None :
358
- vtx = []
359
- bt = node .getblocktemplate ()
360
- height = bt ['height' ]
361
- tip_hash = bt ['previousblockhash' ]
362
-
363
- tip_block = node .getblock (tip_hash )
364
-
365
- coinbasevalue = bt ['coinbasevalue' ]
366
- if miner_address is None :
367
- miner_address = self .nodes [0 ].getnewaddress ()
368
- if mn_payee is None :
369
- if isinstance (bt ['masternode' ], list ):
370
- mn_payee = bt ['masternode' ][0 ]['payee' ]
371
- else :
372
- mn_payee = bt ['masternode' ]['payee' ]
373
- # we can't take the masternode payee amount from the template here as we might have additional fees in vtx
374
-
375
- # calculate fees that the block template included (we'll have to remove it from the coinbase as we won't
376
- # include the template's transactions
377
- bt_fees = 0
378
- for tx in bt ['transactions' ]:
379
- bt_fees += tx ['fee' ]
380
-
381
- new_fees = 0
382
- for tx in vtx :
383
- in_value = 0
384
- out_value = 0
385
- for txin in tx .vin :
386
- txout = node .gettxout (uint256_to_string (txin .prevout .hash ), txin .prevout .n , False )
387
- in_value += int (txout ['value' ] * COIN )
388
- for txout in tx .vout :
389
- out_value += txout .nValue
390
- new_fees += in_value - out_value
391
-
392
- # fix fees
393
- coinbasevalue -= bt_fees
394
- coinbasevalue += new_fees
395
-
396
- if mn_amount is None :
397
- realloc_info = node .getblockchaininfo ()['softforks' ]['realloc' ]
398
- realloc_height = 99999999
399
- if realloc_info ['active' ]:
400
- realloc_height = realloc_info ['height' ]
401
- mn_amount = get_masternode_payment (height , coinbasevalue , realloc_height )
402
- miner_amount = coinbasevalue - mn_amount
403
-
404
- outputs = {miner_address : str (Decimal (miner_amount ) / COIN )}
405
- if mn_amount > 0 :
406
- outputs [mn_payee ] = str (Decimal (mn_amount ) / COIN )
407
-
408
- coinbase = FromHex (CTransaction (), node .createrawtransaction ([], outputs ))
409
- coinbase .vin = create_coinbase (height ).vin
410
-
411
- # We can't really use this one as it would result in invalid merkle roots for masternode lists
412
- if len (bt ['coinbase_payload' ]) != 0 :
413
- cbtx = FromHex (CCbTx (version = 1 ), bt ['coinbase_payload' ])
414
- if use_mnmerkleroot_from_tip :
415
- if 'cbTx' in tip_block :
416
- cbtx .merkleRootMNList = int (tip_block ['cbTx' ]['merkleRootMNList' ], 16 )
417
- else :
418
- cbtx .merkleRootMNList = 0
419
- coinbase .nVersion = 3
420
- coinbase .nType = 5 # CbTx
421
- coinbase .vExtraPayload = cbtx .serialize ()
422
-
423
- coinbase .calc_sha256 ()
424
-
425
- block = create_block (int (tip_hash , 16 ), coinbase )
426
- block .vtx += vtx
427
-
428
- # Add quorum commitments from template
429
- for tx in bt ['transactions' ]:
430
- tx2 = FromHex (CTransaction (), tx ['data' ])
431
- if tx2 .nType == 6 :
432
- block .vtx .append (tx2 )
433
-
434
- block .hashMerkleRoot = block .calc_merkle_root ()
435
- block .solve ()
357
+ def mine_block (self , mns , node , vtx = None , mn_payee = None , mn_amount = None , expected_error = None ):
358
+ block = create_block_with_mnpayments (mns , node , vtx , mn_payee , mn_amount )
436
359
result = node .submitblock (ToHex (block ))
437
360
if expected_error is not None and result != expected_error :
438
361
raise AssertionError ('mining the block should have failed with error %s, but submitblock returned %s' % (expected_error , result ))
439
362
elif expected_error is None and result is not None :
440
363
raise AssertionError ('submitblock returned %s' % (result ))
441
364
442
- def mine_double_spend (self , node , txins , target_address , use_mnmerkleroot_from_tip = False ):
365
+ def mine_double_spend (self , mns , node , txins , target_address ):
443
366
amount = Decimal (0 )
444
367
for txin in txins :
445
368
txout = node .gettxout (txin ['txid' ], txin ['vout' ], False )
@@ -450,12 +373,12 @@ def mine_double_spend(self, node, txins, target_address, use_mnmerkleroot_from_t
450
373
rawtx = node .signrawtransactionwithwallet (rawtx )['hex' ]
451
374
tx = FromHex (CTransaction (), rawtx )
452
375
453
- self .mine_block (node , [tx ], use_mnmerkleroot_from_tip = use_mnmerkleroot_from_tip )
376
+ self .mine_block (mns , node , [tx ])
454
377
455
- def test_invalid_mn_payment (self , node ):
378
+ def test_invalid_mn_payment (self , mns , node ):
456
379
mn_payee = self .nodes [0 ].getnewaddress ()
457
- self .mine_block (node , mn_payee = mn_payee , expected_error = 'bad-cb-payee' )
458
- self .mine_block (node , mn_amount = 1 , expected_error = 'bad-cb-payee' )
380
+ self .mine_block (mns , node , mn_payee = mn_payee , expected_error = 'bad-cb-payee' )
381
+ self .mine_block (mns , node , mn_amount = 1 , expected_error = 'bad-cb-payee' )
459
382
460
383
if __name__ == '__main__' :
461
384
DIP3Test ().main ()
0 commit comments