mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
Merge #13047: [trivial] Tidy blocktools.py
4d355bfb2b
[tests] tidy up blocktools.py (John Newbery)cab8be5adf
[tests] Fix flake8 warnings in blocktools.py (John Newbery)b184127db2
[doc][trivial] no retargeting in regtest mode (Jesse Cohen) Pull request description: Tidies up the blocktools.py module: - fixes flake8 warnings - changes function-level comments to docstrings. Takes in @skeees's commitb184127db2
Tree-SHA512: 0f4c59ac8ccc9057492ec1996381e73380d65e85240f2ba9607174c0743d3a1853c4ed35a9e1bc704b2b6d6d823ac77aa7e81bd150cf5033de79293c24b791b0
This commit is contained in:
commit
f644ea487a
@ -10,7 +10,24 @@ from .address import (
|
||||
script_to_p2sh_p2wsh,
|
||||
script_to_p2wsh,
|
||||
)
|
||||
from .mininode import *
|
||||
from .messages import (
|
||||
CBlock,
|
||||
COIN,
|
||||
COutPoint,
|
||||
CTransaction,
|
||||
CTxIn,
|
||||
CTxInWitness,
|
||||
CTxOut,
|
||||
FromHex,
|
||||
ToHex,
|
||||
bytes_to_hex_str,
|
||||
hash256,
|
||||
hex_str_to_bytes,
|
||||
ser_string,
|
||||
ser_uint256,
|
||||
sha256,
|
||||
uint256_from_str,
|
||||
)
|
||||
from .script import (
|
||||
CScript,
|
||||
OP_0,
|
||||
@ -23,34 +40,34 @@ from .script import (
|
||||
)
|
||||
from .util import assert_equal
|
||||
|
||||
# Create a block (with regtest difficulty)
|
||||
def create_block(hashprev, coinbase, nTime=None):
|
||||
# From BIP141
|
||||
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
|
||||
|
||||
def create_block(hashprev, coinbase, ntime=None):
|
||||
"""Create a block (with regtest difficulty)."""
|
||||
block = CBlock()
|
||||
if nTime is None:
|
||||
if ntime is None:
|
||||
import time
|
||||
block.nTime = int(time.time() + 600)
|
||||
else:
|
||||
block.nTime = nTime
|
||||
block.nTime = ntime
|
||||
block.hashPrevBlock = hashprev
|
||||
block.nBits = 0x207fffff # Will break after a difficulty adjustment...
|
||||
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
|
||||
block.vtx.append(coinbase)
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
block.calc_sha256()
|
||||
return block
|
||||
|
||||
# From BIP141
|
||||
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
|
||||
|
||||
|
||||
def get_witness_script(witness_root, witness_nonce):
|
||||
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)))
|
||||
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
|
||||
return CScript([OP_RETURN, output_data])
|
||||
|
||||
|
||||
# According to BIP141, blocks with witness rules active must commit to the
|
||||
# hash of all in-block transactions including witness.
|
||||
def add_witness_commitment(block, nonce=0):
|
||||
"""Add a witness commitment to the block's coinbase transaction.
|
||||
|
||||
According to BIP141, blocks with witness rules active must commit to the
|
||||
hash of all in-block transactions including witness."""
|
||||
# First calculate the merkle root of the block's
|
||||
# transactions, with witnesses.
|
||||
witness_nonce = nonce
|
||||
@ -65,7 +82,6 @@ def add_witness_commitment(block, nonce=0):
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
block.rehash()
|
||||
|
||||
|
||||
def serialize_script_num(value):
|
||||
r = bytearray(0)
|
||||
if value == 0:
|
||||
@ -81,10 +97,11 @@ def serialize_script_num(value):
|
||||
r[-1] |= 0x80
|
||||
return r
|
||||
|
||||
# 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.
|
||||
def create_coinbase(height, pubkey=None):
|
||||
"""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."""
|
||||
coinbase = CTransaction()
|
||||
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
|
||||
ser_string(serialize_script_num(height)), 0xffffffff))
|
||||
@ -92,7 +109,7 @@ def create_coinbase(height, pubkey = None):
|
||||
coinbaseoutput.nValue = 50 * COIN
|
||||
halvings = int(height / 150) # regtest
|
||||
coinbaseoutput.nValue >>= halvings
|
||||
if (pubkey != None):
|
||||
if (pubkey is not None):
|
||||
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
|
||||
else:
|
||||
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
|
||||
@ -100,36 +117,39 @@ def create_coinbase(height, pubkey = None):
|
||||
coinbase.calc_sha256()
|
||||
return coinbase
|
||||
|
||||
# Create a transaction.
|
||||
# If the scriptPubKey is not specified, make it anyone-can-spend.
|
||||
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
|
||||
def create_transaction(prevtx, n, sig, value, script_pub_key=CScript()):
|
||||
"""Create a transaction.
|
||||
|
||||
If the script_pub_key is not specified, make it anyone-can-spend."""
|
||||
tx = CTransaction()
|
||||
assert(n < len(prevtx.vout))
|
||||
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
|
||||
tx.vout.append(CTxOut(value, scriptPubKey))
|
||||
tx.vout.append(CTxOut(value, script_pub_key))
|
||||
tx.calc_sha256()
|
||||
return tx
|
||||
|
||||
def get_legacy_sigopcount_block(block, fAccurate=True):
|
||||
def get_legacy_sigopcount_block(block, accurate=True):
|
||||
count = 0
|
||||
for tx in block.vtx:
|
||||
count += get_legacy_sigopcount_tx(tx, fAccurate)
|
||||
count += get_legacy_sigopcount_tx(tx, accurate)
|
||||
return count
|
||||
|
||||
def get_legacy_sigopcount_tx(tx, fAccurate=True):
|
||||
def get_legacy_sigopcount_tx(tx, accurate=True):
|
||||
count = 0
|
||||
for i in tx.vout:
|
||||
count += i.scriptPubKey.GetSigOpCount(fAccurate)
|
||||
count += i.scriptPubKey.GetSigOpCount(accurate)
|
||||
for j in tx.vin:
|
||||
# scriptSig might be of type bytes, so convert to CScript for the moment
|
||||
count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
|
||||
count += CScript(j.scriptSig).GetSigOpCount(accurate)
|
||||
return count
|
||||
|
||||
# Create a scriptPubKey corresponding to either a P2WPKH output for the
|
||||
# given pubkey, or a P2WSH output of a 1-of-1 multisig for the given
|
||||
# pubkey. Returns the hex encoding of the scriptPubKey.
|
||||
def witness_script(use_p2wsh, pubkey):
|
||||
if (use_p2wsh == False):
|
||||
"""Create a scriptPubKey for a pay-to-wtiness TxOut.
|
||||
|
||||
This is either a P2WPKH output for the given pubkey, or a P2WSH output of a
|
||||
1-of-1 multisig for the given pubkey. Returns the hex encoding of the
|
||||
scriptPubKey."""
|
||||
if not use_p2wsh:
|
||||
# P2WPKH instead
|
||||
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
|
||||
pkscript = CScript([OP_0, pubkeyhash])
|
||||
@ -140,9 +160,10 @@ def witness_script(use_p2wsh, pubkey):
|
||||
pkscript = CScript([OP_0, scripthash])
|
||||
return bytes_to_hex_str(pkscript)
|
||||
|
||||
# Return a transaction (in hex) that spends the given utxo to a segwit output,
|
||||
# optionally wrapping the segwit output using P2SH.
|
||||
def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
|
||||
"""Return a transaction (in hex) that spends the given utxo to a segwit output.
|
||||
|
||||
Optionally wrap the segwit output using P2SH."""
|
||||
if use_p2wsh:
|
||||
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
|
||||
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
|
||||
@ -152,12 +173,13 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
|
||||
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
|
||||
return node.createrawtransaction([utxo], {addr: amount})
|
||||
|
||||
# Create a transaction spending a given utxo to a segwit output corresponding
|
||||
# to the given pubkey: use_p2wsh determines whether to use P2WPKH or P2WSH;
|
||||
# encode_p2sh determines whether to wrap in P2SH.
|
||||
# sign=True will have the given node sign the transaction.
|
||||
# insert_redeem_script will be added to the scriptSig, if given.
|
||||
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
|
||||
"""Create a transaction spending a given utxo to a segwit output.
|
||||
|
||||
The output corresponds to the given pubkey: use_p2wsh determines whether to
|
||||
use P2WPKH or P2WSH; encode_p2sh determines whether to wrap in P2SH.
|
||||
sign=True will have the given node sign the transaction.
|
||||
insert_redeem_script will be added to the scriptSig, if given."""
|
||||
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
|
||||
if (sign):
|
||||
signed = node.signrawtransactionwithwallet(tx_to_witness)
|
||||
|
Loading…
Reference in New Issue
Block a user