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 commit b184127db2

Tree-SHA512: 0f4c59ac8ccc9057492ec1996381e73380d65e85240f2ba9607174c0743d3a1853c4ed35a9e1bc704b2b6d6d823ac77aa7e81bd150cf5033de79293c24b791b0
This commit is contained in:
MarcoFalke 2021-04-09 02:26:58 +03:00 committed by UdjinM6
parent add77dd7b1
commit 716d0cd696
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9
4 changed files with 39 additions and 27 deletions

View File

@ -7,6 +7,7 @@
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
from test_framework.blocktools import * from test_framework.blocktools import *
from test_framework.messages import FromHex, ToHex
SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31) SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)
SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height) SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)

View File

@ -304,7 +304,7 @@ class LLMQ_IS_CL_Conflicts(DashTestFramework):
coinbase.calc_sha256() coinbase.calc_sha256()
block = create_block(int(tip_hash, 16), coinbase, nTime=bt['curtime']) block = create_block(int(tip_hash, 16), coinbase, ntime=bt['curtime'])
block.vtx += vtx block.vtx += vtx
# Add quorum commitments from template # Add quorum commitments from template

View File

@ -12,8 +12,8 @@ re-requested.
""" """
import copy import copy
from test_framework.blocktools import create_block, create_coinbase, create_transaction, network_thread_start from test_framework.blocktools import create_block, create_coinbase, create_transaction
from test_framework.mininode import P2PDataStore, COIN from test_framework.mininode import P2PDataStore, COIN, network_thread_start
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.util import assert_equal

View File

@ -4,19 +4,28 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Utilities for manipulating blocks and transactions.""" """Utilities for manipulating blocks and transactions."""
from .mininode import * from .messages import (
CBlock,
CCbTx,
COIN,
COutPoint,
CTransaction,
CTxIn,
CTxOut,
ser_string,
)
from .script import CScript, OP_TRUE, OP_CHECKSIG from .script import CScript, OP_TRUE, OP_CHECKSIG
# Create a block (with regtest difficulty) def create_block(hashprev, coinbase, ntime=None):
def create_block(hashprev, coinbase, nTime=None): """Create a block (with regtest difficulty)."""
block = CBlock() block = CBlock()
if nTime is None: if ntime is None:
import time import time
block.nTime = int(time.time()+600) block.nTime = int(time.time() + 600)
else: else:
block.nTime = nTime block.nTime = ntime
block.hashPrevBlock = hashprev 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.vtx.append(coinbase)
block.hashMerkleRoot = block.calc_merkle_root() block.hashMerkleRoot = block.calc_merkle_root()
block.calc_sha256() block.calc_sha256()
@ -37,22 +46,23 @@ def serialize_script_num(value):
r[-1] |= 0x80 r[-1] |= 0x80
return r return r
# Create a coinbase transaction, assuming no miner fees. def create_coinbase(height, pubkey=None, dip4_activated=False):
# If pubkey is passed in, the coinbase output will be a P2PK output; """Create a coinbase transaction, assuming no miner fees.
# otherwise an anyone-can-spend output.
def create_coinbase(height, pubkey = None, dip4_activated=False): If pubkey is passed in, the coinbase output will be a P2PK output;
otherwise an anyone-can-spend output."""
coinbase = CTransaction() coinbase = CTransaction()
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
ser_string(serialize_script_num(height)), 0xffffffff)) ser_string(serialize_script_num(height)), 0xffffffff))
coinbaseoutput = CTxOut() coinbaseoutput = CTxOut()
coinbaseoutput.nValue = 500 * COIN coinbaseoutput.nValue = 500 * COIN
halvings = int(height/150) # regtest halvings = int(height / 150) # regtest
coinbaseoutput.nValue >>= halvings coinbaseoutput.nValue >>= halvings
if (pubkey != None): if (pubkey is not None):
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG]) coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
else: else:
coinbaseoutput.scriptPubKey = CScript([OP_TRUE]) coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
coinbase.vout = [ coinbaseoutput ] coinbase.vout = [coinbaseoutput]
if dip4_activated: if dip4_activated:
coinbase.nVersion = 3 coinbase.nVersion = 3
coinbase.nType = 5 coinbase.nType = 5
@ -61,29 +71,30 @@ def create_coinbase(height, pubkey = None, dip4_activated=False):
coinbase.calc_sha256() coinbase.calc_sha256()
return coinbase return coinbase
# Create a transaction. def create_transaction(prevtx, n, sig, value, script_pub_key=CScript()):
# If the scriptPubKey is not specified, make it anyone-can-spend. """Create a transaction.
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
If the script_pub_key is not specified, make it anyone-can-spend."""
tx = CTransaction() tx = CTransaction()
assert(n < len(prevtx.vout)) assert(n < len(prevtx.vout))
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff)) 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() tx.calc_sha256()
return tx return tx
def get_legacy_sigopcount_block(block, fAccurate=True): def get_legacy_sigopcount_block(block, accurate=True):
count = 0 count = 0
for tx in block.vtx: for tx in block.vtx:
count += get_legacy_sigopcount_tx(tx, fAccurate) count += get_legacy_sigopcount_tx(tx, accurate)
return count return count
def get_legacy_sigopcount_tx(tx, fAccurate=True): def get_legacy_sigopcount_tx(tx, accurate=True):
count = 0 count = 0
for i in tx.vout: for i in tx.vout:
count += i.scriptPubKey.GetSigOpCount(fAccurate) count += i.scriptPubKey.GetSigOpCount(accurate)
for j in tx.vin: for j in tx.vin:
# scriptSig might be of type bytes, so convert to CScript for the moment # 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 return count
# Identical to GetMasternodePayment in C++ code # Identical to GetMasternodePayment in C++ code