2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-12-31 18:50:11 +01:00
|
|
|
# Copyright (c) 2015-2020 The Bitcoin Core developers
|
2016-05-06 11:23:48 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2015-04-28 18:39:47 +02:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Utilities for manipulating blocks and transactions."""
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2024-01-25 14:30:00 +01:00
|
|
|
from binascii import a2b_hex
|
2023-11-13 17:02:52 +01:00
|
|
|
from decimal import Decimal
|
2024-01-25 14:30:00 +01:00
|
|
|
import io
|
|
|
|
import struct
|
|
|
|
import time
|
2020-06-04 17:26:47 +02:00
|
|
|
import unittest
|
|
|
|
|
2021-04-09 01:26:58 +02:00
|
|
|
from .messages import (
|
|
|
|
CBlock,
|
|
|
|
CCbTx,
|
|
|
|
COIN,
|
|
|
|
COutPoint,
|
|
|
|
CTransaction,
|
|
|
|
CTxIn,
|
|
|
|
CTxOut,
|
2021-06-24 12:47:04 +02:00
|
|
|
tx_from_hex,
|
|
|
|
from_hex,
|
2023-11-13 17:02:52 +01:00
|
|
|
uint256_to_string,
|
2021-04-09 01:26:58 +02:00
|
|
|
)
|
2019-08-05 14:12:17 +02:00
|
|
|
from .script import CScript, CScriptNum, CScriptOp, OP_TRUE, OP_CHECKSIG
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
from .util import assert_equal, hex_str_to_bytes
|
|
|
|
from io import BytesIO
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2024-08-13 11:23:45 +02:00
|
|
|
MAX_BLOCK_SIGOPS = 40000
|
2019-01-02 14:42:21 +01:00
|
|
|
|
2019-02-15 14:56:43 +01:00
|
|
|
# Genesis block time (regtest)
|
|
|
|
TIME_GENESIS_BLOCK = 1417713337
|
|
|
|
|
2021-05-31 11:26:16 +02:00
|
|
|
# Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
|
|
|
|
COINBASE_MATURITY = 100
|
|
|
|
|
2024-01-25 14:30:00 +01:00
|
|
|
NORMAL_GBT_REQUEST_PARAMS = {"rules": []} # type: ignore[var-annotated]
|
|
|
|
|
2021-08-03 10:09:42 +02:00
|
|
|
VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4
|
|
|
|
|
2024-01-25 14:30:00 +01:00
|
|
|
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None, dip4_activated=False, v20_activated=False):
|
2021-04-09 01:26:58 +02:00
|
|
|
"""Create a block (with regtest difficulty)."""
|
2015-04-28 18:39:47 +02:00
|
|
|
block = CBlock()
|
2024-01-25 14:30:00 +01:00
|
|
|
if tmpl is None:
|
|
|
|
tmpl = {}
|
2021-08-03 10:09:42 +02:00
|
|
|
block.nVersion = version or tmpl.get('version') or VERSIONBITS_LAST_OLD_BLOCK_VERSION
|
2024-01-25 14:30:00 +01:00
|
|
|
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
|
|
|
|
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
|
|
|
|
if tmpl and not tmpl.get('bits') is None:
|
|
|
|
block.nBits = struct.unpack('>I', a2b_hex(tmpl['bits']))[0]
|
2015-04-28 18:39:47 +02:00
|
|
|
else:
|
2024-01-25 14:30:00 +01:00
|
|
|
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
|
|
|
|
if coinbase is None:
|
|
|
|
coinbase = create_coinbase(height=tmpl['height'], dip4_activated=dip4_activated, v20_activated=v20_activated)
|
2015-04-28 18:39:47 +02:00
|
|
|
block.vtx.append(coinbase)
|
2024-01-25 14:30:00 +01:00
|
|
|
if txlist:
|
|
|
|
for tx in txlist:
|
|
|
|
if not hasattr(tx, 'calc_sha256'):
|
|
|
|
txo = CTransaction()
|
|
|
|
txo.deserialize(io.BytesIO(tx))
|
|
|
|
tx = txo
|
|
|
|
block.vtx.append(tx)
|
2015-04-28 18:39:47 +02:00
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.calc_sha256()
|
|
|
|
return block
|
|
|
|
|
2023-11-13 17:02:52 +01:00
|
|
|
def create_block_with_mnpayments(mninfo, node, vtx=None, mn_payee=None, mn_amount=None):
|
|
|
|
if vtx is None:
|
|
|
|
vtx = []
|
|
|
|
bt = node.getblocktemplate()
|
|
|
|
height = bt['height']
|
|
|
|
tip_hash = bt['previousblockhash']
|
|
|
|
coinbasevalue = bt['coinbasevalue']
|
|
|
|
|
|
|
|
assert len(bt['masternode']) <= 2
|
|
|
|
if mn_payee is None:
|
|
|
|
mn_payee = bt['masternode'][0]['payee']
|
|
|
|
|
|
|
|
mn_operator_payee = None
|
|
|
|
if len(bt['masternode']) == 2:
|
|
|
|
mn_operator_payee = bt['masternode'][1]['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:
|
|
|
|
txout = node.gettxout(uint256_to_string(txin.prevout.hash), txin.prevout.n, False)
|
|
|
|
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
|
|
|
|
|
|
|
|
operator_reward = 0
|
|
|
|
if mn_operator_payee is not None:
|
|
|
|
for mn in mninfo:
|
|
|
|
if mn.rewards_address == mn_payee:
|
|
|
|
operator_reward = mn.operator_reward
|
|
|
|
break
|
|
|
|
assert operator_reward > 0
|
|
|
|
|
|
|
|
mn_operator_amount = 0
|
|
|
|
if mn_amount is None:
|
|
|
|
v20_info = node.getblockchaininfo()['softforks']['v20']
|
|
|
|
mn_amount_total = get_masternode_payment(height, coinbasevalue, v20_info['active'])
|
|
|
|
mn_operator_amount = mn_amount_total * operator_reward // 100
|
|
|
|
mn_amount = mn_amount_total - mn_operator_amount
|
|
|
|
miner_amount = coinbasevalue - mn_amount - mn_operator_amount
|
|
|
|
|
|
|
|
miner_address = node.get_deterministic_priv_key().address
|
|
|
|
outputs = {miner_address: str(Decimal(miner_amount) / COIN)}
|
|
|
|
if mn_amount > 0:
|
|
|
|
outputs[mn_payee] = str(Decimal(mn_amount) / COIN)
|
|
|
|
if mn_operator_amount > 0:
|
|
|
|
outputs[mn_operator_payee] = str(Decimal(mn_operator_amount) / COIN)
|
|
|
|
|
2021-06-24 12:47:04 +02:00
|
|
|
coinbase = tx_from_hex(node.createrawtransaction([], outputs))
|
2023-11-13 17:02:52 +01:00
|
|
|
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:
|
|
|
|
tip_block = node.getblock(tip_hash)
|
2021-06-24 12:47:04 +02:00
|
|
|
cbtx = from_hex(CCbTx(version=1), bt['coinbase_payload'])
|
2023-11-13 17:02:52 +01:00
|
|
|
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, ntime=bt['curtime'], version=bt['version'])
|
|
|
|
block.vtx += vtx
|
|
|
|
|
|
|
|
# Add quorum commitments from template
|
|
|
|
for tx in bt['transactions']:
|
2021-06-24 12:47:04 +02:00
|
|
|
tx2 = tx_from_hex(tx['data'])
|
2023-11-13 17:02:52 +01:00
|
|
|
if tx2.nType == 6:
|
|
|
|
block.vtx.append(tx2)
|
|
|
|
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.solve()
|
|
|
|
return block
|
|
|
|
|
2019-08-05 14:12:17 +02:00
|
|
|
def script_BIP34_coinbase_height(height):
|
|
|
|
if height <= 16:
|
|
|
|
res = CScriptOp.encode_op_n(height)
|
|
|
|
# Append dummy to increase scriptSig size above 2 (see bad-cb-length consensus rule)
|
|
|
|
return CScript([res, OP_TRUE])
|
|
|
|
return CScript([CScriptNum(height)])
|
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2023-07-30 13:49:32 +02:00
|
|
|
def create_coinbase(height, pubkey=None, dip4_activated=False, v20_activated=False, nValue=500):
|
2021-04-09 01:26:58 +02:00
|
|
|
"""Create a coinbase transaction, assuming no miner fees.
|
|
|
|
|
|
|
|
If pubkey is passed in, the coinbase output will be a P2PK output;
|
|
|
|
otherwise an anyone-can-spend output."""
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase = CTransaction()
|
2019-08-05 14:12:17 +02:00
|
|
|
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), script_BIP34_coinbase_height(height), 0xffffffff))
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbaseoutput = CTxOut()
|
2023-07-30 13:49:32 +02:00
|
|
|
coinbaseoutput.nValue = nValue * COIN
|
|
|
|
if nValue == 500:
|
|
|
|
halvings = int(height / 150) # regtest
|
|
|
|
coinbaseoutput.nValue >>= halvings
|
2021-04-09 01:26:58 +02:00
|
|
|
if (pubkey is not None):
|
2015-08-05 23:47:34 +02:00
|
|
|
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
|
|
|
|
else:
|
|
|
|
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
|
2021-04-09 01:26:58 +02:00
|
|
|
coinbase.vout = [coinbaseoutput]
|
2018-05-29 14:13:50 +02:00
|
|
|
if dip4_activated:
|
|
|
|
coinbase.nVersion = 3
|
|
|
|
coinbase.nType = 5
|
2023-07-24 18:39:38 +02:00
|
|
|
cbtx_version = 3 if v20_activated else 2
|
|
|
|
cbtx_payload = CCbTx(cbtx_version, height, 0, 0, 0)
|
2018-05-29 14:13:50 +02:00
|
|
|
coinbase.vExtraPayload = cbtx_payload.serialize()
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase.calc_sha256()
|
|
|
|
return coinbase
|
|
|
|
|
2018-08-13 13:29:32 +02:00
|
|
|
def create_tx_with_script(prevtx, n, script_sig=b"", *, amount, script_pub_key=CScript()):
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
"""Return one-input, one-output transaction object
|
|
|
|
spending the prevtx's n-th output with the given amount.
|
2021-04-09 01:26:58 +02:00
|
|
|
|
2018-09-06 00:12:39 +02:00
|
|
|
Can optionally pass scriptPubKey and scriptSig, default is anyone-can-spend output.
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
"""
|
2015-04-28 18:39:47 +02:00
|
|
|
tx = CTransaction()
|
2021-08-27 21:03:02 +02:00
|
|
|
assert n < len(prevtx.vout)
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), script_sig, 0xffffffff))
|
|
|
|
tx.vout.append(CTxOut(amount, script_pub_key))
|
2015-04-28 18:39:47 +02:00
|
|
|
tx.calc_sha256()
|
|
|
|
return tx
|
2016-06-13 11:36:48 +02:00
|
|
|
|
2018-08-13 13:29:32 +02:00
|
|
|
def create_transaction(node, txid, to_address, *, amount):
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
""" Return signed transaction spending the first output of the
|
2020-11-02 16:54:06 +01:00
|
|
|
input txid. Note that the node must have a wallet that can
|
|
|
|
sign for the output that is being spent.
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
"""
|
2018-08-13 13:29:32 +02:00
|
|
|
raw_tx = create_raw_transaction(node, txid, to_address, amount=amount)
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
tx = CTransaction()
|
|
|
|
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx)))
|
|
|
|
return tx
|
|
|
|
|
2018-08-13 13:29:32 +02:00
|
|
|
def create_raw_transaction(node, txid, to_address, *, amount):
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
""" Return raw signed transaction spending the first output of the
|
2020-11-02 16:54:06 +01:00
|
|
|
input txid. Note that the node must have a wallet that can sign
|
|
|
|
for the output that is being spent.
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
"""
|
2020-11-02 16:54:06 +01:00
|
|
|
psbt = node.createpsbt(inputs=[{"txid": txid, "vout": 0}], outputs={to_address: amount})
|
|
|
|
for _ in range(2):
|
|
|
|
for w in node.listwallets():
|
|
|
|
wrpc = node.get_wallet_rpc(w)
|
|
|
|
signed_psbt = wrpc.walletprocesspsbt(psbt)
|
|
|
|
psbt = signed_psbt['psbt']
|
|
|
|
final_psbt = node.finalizepsbt(psbt)
|
|
|
|
assert_equal(final_psbt["complete"], True)
|
|
|
|
return final_psbt['hex']
|
Merge #13669: Tests: Cleanup create_transaction implementations
44bbceeef1 [Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object (Conor Scott)
736f941424 [Tests] Cleanup extra instances of create_transaction (Conor Scott)
157651855f [Tests] Rename create_tx and move to blocktools.py (Conor Scott)
Pull request description:
There currently exist seven ([1](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_cltv.py#L52-L60), [2](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_csv_activation.py#L88-L95) [3](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_dersig.py#L40-L48), [4](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_nulldummy.py#L100-L108), [5](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/util.py#L529-L535), [6](https://github.com/bitcoin/bitcoin/blob/master/test/functional/test_framework/blocktools.py#L120-L129), [7](https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_block.py#L1218-L1220)) implementations of a function called something similar to `create_transaction` in the functional tests, some of which are exact copies of each other.
This PR aims to clean this up into [three different cases implemented in blocktools.py](https://github.com/conscott/bitcoin/blob/create_tx_cleanup/test/functional/test_framework/blocktools.py#L121-L149)
1. `create_tx_with_script`: Return transaction object spending generic tx output optionally specifying scriptSig and scriptPubKey
2. `create_transaction`: Return transaction object spending coinbase tx
2. `create_raw_transaction`: Return raw transaction (hex string) spending coinbase tx
I am not committed to any of these function names, so I'll gladly take suggestions on there.
Additionally there are some related cleanups to feature_block.py tests, specifically removing the [PreviousSpendableOutput](https://github.com/conscott/bitcoin/blob/master/test/functional/feature_block.py#L51-L54) object, which seems like an unnecessary layer given that every instance spends the 0 output.
Tree-SHA512: 63c6233b6f0942c81ba1ca67ea6770809b8c9409314c6d4cf8e5a3991cb9ee92b22bebe88c0dde45cd71e754eb351230c4c404b70ff118f5f43c034452ada65c
2018-08-09 18:09:27 +02:00
|
|
|
|
2021-04-09 01:26:58 +02:00
|
|
|
def get_legacy_sigopcount_block(block, accurate=True):
|
2016-06-13 11:36:48 +02:00
|
|
|
count = 0
|
|
|
|
for tx in block.vtx:
|
2021-04-09 01:26:58 +02:00
|
|
|
count += get_legacy_sigopcount_tx(tx, accurate)
|
2016-06-13 11:36:48 +02:00
|
|
|
return count
|
|
|
|
|
2021-04-09 01:26:58 +02:00
|
|
|
def get_legacy_sigopcount_tx(tx, accurate=True):
|
2016-06-13 11:36:48 +02:00
|
|
|
count = 0
|
|
|
|
for i in tx.vout:
|
2021-04-09 01:26:58 +02:00
|
|
|
count += i.scriptPubKey.GetSigOpCount(accurate)
|
2016-06-13 11:36:48 +02:00
|
|
|
for j in tx.vin:
|
|
|
|
# scriptSig might be of type bytes, so convert to CScript for the moment
|
2021-04-09 01:26:58 +02:00
|
|
|
count += CScript(j.scriptSig).GetSigOpCount(accurate)
|
2016-06-13 11:36:48 +02:00
|
|
|
return count
|
2018-09-11 16:32:45 +02:00
|
|
|
|
2019-09-12 12:28:37 +02:00
|
|
|
"""
|
|
|
|
Dash chaintips rpc returns extra info in each tip (difficulty, chainwork, and
|
|
|
|
forkpoint). Filter down to relevant ones checked in this test.
|
|
|
|
"""
|
|
|
|
def filter_tip_keys(chaintips):
|
|
|
|
check_keys = ["hash", "height", "branchlen", "status"]
|
|
|
|
filtered_tips = []
|
|
|
|
for tip in chaintips:
|
|
|
|
filtered_tips.append({k: tip[k] for k in check_keys})
|
|
|
|
return filtered_tips
|
|
|
|
|
2018-09-11 16:32:45 +02:00
|
|
|
# Identical to GetMasternodePayment in C++ code
|
2024-08-09 07:52:08 +02:00
|
|
|
# TODO: remove it or make **proper** tests for various height
|
2023-11-13 17:02:52 +01:00
|
|
|
def get_masternode_payment(nHeight, blockValue, fV20Active):
|
2018-09-11 16:32:45 +02:00
|
|
|
ret = int(blockValue / 5)
|
|
|
|
|
|
|
|
nMNPIBlock = 350
|
|
|
|
nMNPIPeriod = 10
|
2024-08-09 07:52:08 +02:00
|
|
|
nReallocActivationHeight = 1
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
if nHeight > nMNPIBlock:
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 1):
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 2):
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 3):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 4):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 5):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 6):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 7):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 9):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
|
2020-09-10 18:23:11 +02:00
|
|
|
if nHeight < nReallocActivationHeight:
|
|
|
|
# Block Reward Realocation is not activated yet, nothing to do
|
|
|
|
return ret
|
|
|
|
|
2024-08-09 07:52:08 +02:00
|
|
|
nSuperblockCycle = 20
|
2020-09-10 18:23:11 +02:00
|
|
|
# Actual realocation starts in the cycle next to one activation happens in
|
|
|
|
nReallocStart = nReallocActivationHeight - nReallocActivationHeight % nSuperblockCycle + nSuperblockCycle
|
|
|
|
|
|
|
|
if nHeight < nReallocStart:
|
|
|
|
# Activated but we have to wait for the next cycle to start realocation, nothing to do
|
|
|
|
return ret
|
|
|
|
|
2023-11-13 17:02:52 +01:00
|
|
|
if fV20Active:
|
|
|
|
# Once MNRewardReallocated activates, block reward is 80% of block subsidy (+ tx fees) since treasury is 20%
|
|
|
|
# Since the MN reward needs to be equal to 60% of the block subsidy (according to the proposal), MN reward is set to 75% of the block reward.
|
|
|
|
# Previous reallocation periods are dropped.
|
|
|
|
return blockValue * 3 // 4
|
|
|
|
|
2020-09-10 18:23:11 +02:00
|
|
|
# Periods used to reallocate the masternode reward from 50% to 60%
|
|
|
|
vecPeriods = [
|
|
|
|
513, # Period 1: 51.3%
|
|
|
|
526, # Period 2: 52.6%
|
|
|
|
533, # Period 3: 53.3%
|
|
|
|
540, # Period 4: 54%
|
|
|
|
546, # Period 5: 54.6%
|
|
|
|
552, # Period 6: 55.2%
|
|
|
|
557, # Period 7: 55.7%
|
|
|
|
562, # Period 8: 56.2%
|
|
|
|
567, # Period 9: 56.7%
|
|
|
|
572, # Period 10: 57.2%
|
|
|
|
577, # Period 11: 57.7%
|
|
|
|
582, # Period 12: 58.2%
|
|
|
|
585, # Period 13: 58.5%
|
|
|
|
588, # Period 14: 58.8%
|
|
|
|
591, # Period 15: 59.1%
|
|
|
|
594, # Period 16: 59.4%
|
|
|
|
597, # Period 17: 59.7%
|
|
|
|
599, # Period 18: 59.9%
|
|
|
|
600 # Period 19: 60%
|
|
|
|
]
|
|
|
|
|
|
|
|
nReallocCycle = nSuperblockCycle * 3
|
|
|
|
nCurrentPeriod = min(int((nHeight - nReallocStart) / nReallocCycle), len(vecPeriods) - 1)
|
|
|
|
|
|
|
|
return int(blockValue * vecPeriods[nCurrentPeriod] / 1000)
|
2020-06-04 17:26:47 +02:00
|
|
|
|
|
|
|
class TestFrameworkBlockTools(unittest.TestCase):
|
|
|
|
def test_create_coinbase(self):
|
|
|
|
height = 20
|
|
|
|
coinbase_tx = create_coinbase(height=height)
|
|
|
|
assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), height)
|