Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/blocktools.py
Show All 25 Lines | |||||
) | ) | ||||
from .txtools import pad_tx | from .txtools import pad_tx | ||||
from .util import assert_equal, satoshi_round | from .util import assert_equal, satoshi_round | ||||
# Genesis block time (regtest) | # Genesis block time (regtest) | ||||
TIME_GENESIS_BLOCK = 1296688602 | TIME_GENESIS_BLOCK = 1296688602 | ||||
def create_block(hashprev, coinbase, nTime=None): | def create_block(hashprev, coinbase, ntime=None): | ||||
"""Create a block (with regtest difficulty).""" | """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... | # difficulty retargeting is disabled in REGTEST chainparams | ||||
block.nBits = 0x207fffff | |||||
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() | ||||
return block | return block | ||||
def make_conform_to_ctor(block): | def make_conform_to_ctor(block): | ||||
for tx in block.vtx: | for tx in block.vtx: | ||||
Show All 12 Lines | while (absvalue): | ||||
r.append(int(absvalue & 0xff)) | r.append(int(absvalue & 0xff)) | ||||
absvalue >>= 8 | absvalue >>= 8 | ||||
if r[-1] & 0x80: | if r[-1] & 0x80: | ||||
r.append(0x80 if neg else 0) | r.append(0x80 if neg else 0) | ||||
elif neg: | elif neg: | ||||
r[-1] |= 0x80 | r[-1] |= 0x80 | ||||
return r | 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): | 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 = 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 = 50 * COIN | coinbaseoutput.nValue = 50 * COIN | ||||
halvings = int(height / 150) # regtest | halvings = int(height / 150) # regtest | ||||
coinbaseoutput.nValue >>= halvings | coinbaseoutput.nValue >>= halvings | ||||
if (pubkey is not None): | if (pubkey is not None): | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | def create_raw_transaction(node, txid, to_address, amount): | ||||
inputs = [{"txid": txid, "vout": 0}] | inputs = [{"txid": txid, "vout": 0}] | ||||
outputs = {to_address: amount} | outputs = {to_address: amount} | ||||
rawtx = node.createrawtransaction(inputs, outputs) | rawtx = node.createrawtransaction(inputs, outputs) | ||||
signresult = node.signrawtransactionwithwallet(rawtx) | signresult = node.signrawtransactionwithwallet(rawtx) | ||||
assert_equal(signresult["complete"], True) | assert_equal(signresult["complete"], True) | ||||
return signresult['hex'] | return signresult['hex'] | ||||
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 | # scriptSig might be of type bytes, so convert to CScript for the | ||||
# moment | # moment | ||||
count += CScript(j.scriptSig).GetSigOpCount(fAccurate) | count += CScript(j.scriptSig).GetSigOpCount(accurate) | ||||
return count | return count | ||||
def create_confirmed_utxos(node, count, age=101): | def create_confirmed_utxos(node, count, age=101): | ||||
""" | """ | ||||
Helper to create at least "count" utxos | Helper to create at least "count" utxos | ||||
""" | """ | ||||
to_generate = int(0.5 * count) + age | to_generate = int(0.5 * count) + age | ||||
▲ Show 20 Lines • Show All 70 Lines • Show Last 20 Lines |