Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/blocktools.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2019 The Bitcoin Core developers | # Copyright (c) 2015-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# 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 .script import ( | from .script import ( | ||||
CScript, | CScript, | ||||
CScriptNum, | |||||
CScriptOp, | |||||
OP_1, | |||||
OP_CHECKSIG, | OP_CHECKSIG, | ||||
OP_DUP, | OP_DUP, | ||||
OP_EQUALVERIFY, | OP_EQUALVERIFY, | ||||
OP_HASH160, | OP_HASH160, | ||||
OP_RETURN, | OP_RETURN, | ||||
OP_TRUE, | OP_TRUE, | ||||
) | ) | ||||
from .messages import ( | from .messages import ( | ||||
CBlock, | CBlock, | ||||
COIN, | COIN, | ||||
COutPoint, | COutPoint, | ||||
CTransaction, | CTransaction, | ||||
CTxIn, | CTxIn, | ||||
CTxOut, | CTxOut, | ||||
FromHex, | FromHex, | ||||
ToHex, | ToHex, | ||||
ser_string, | |||||
) | ) | ||||
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 | ||||
Show All 17 Lines | |||||
def make_conform_to_ctor(block): | def make_conform_to_ctor(block): | ||||
for tx in block.vtx: | for tx in block.vtx: | ||||
tx.rehash() | tx.rehash() | ||||
block.vtx = [block.vtx[0]] + \ | block.vtx = [block.vtx[0]] + \ | ||||
sorted(block.vtx[1:], key=lambda tx: tx.get_id()) | sorted(block.vtx[1:], key=lambda tx: tx.get_id()) | ||||
def serialize_script_num(value): | def script_BIP34_coinbase_height(height): | ||||
r = bytearray(0) | if height <= 16: | ||||
if value == 0: | res = CScriptOp.encode_op_n(height) | ||||
return r | # Append dummy to increase scriptSig size above 2 | ||||
neg = value < 0 | # (see bad-cb-length consensus rule) | ||||
absvalue = -value if neg else value | return CScript([res, OP_1]) | ||||
deadalnix: You can use OP_TRUE here. | |||||
while (absvalue): | return CScript([CScriptNum(height)]) | ||||
r.append(int(absvalue & 0xff)) | |||||
absvalue >>= 8 | |||||
if r[-1] & 0x80: | |||||
r.append(0x80 if neg else 0) | |||||
elif neg: | |||||
r[-1] |= 0x80 | |||||
return r | |||||
def create_coinbase(height, pubkey=None): | def create_coinbase(height, pubkey=None): | ||||
"""Create a coinbase transaction, assuming no miner fees. | """Create a coinbase transaction, assuming no miner fees. | ||||
If pubkey is passed in, the coinbase output will be a P2PK output; | If pubkey is passed in, the coinbase output will be a P2PK output; | ||||
otherwise an anyone-can-spend 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)) | script_BIP34_coinbase_height(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): | ||||
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG]) | coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG]) | ||||
else: | else: | ||||
coinbaseoutput.scriptPubKey = CScript([OP_TRUE]) | coinbaseoutput.scriptPubKey = CScript([OP_TRUE]) | ||||
▲ Show 20 Lines • Show All 143 Lines • Show Last 20 Lines |
You can use OP_TRUE here.