2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-02-11 17:59:34 +01:00
|
|
|
# Copyright (c) 2015-2019 The Bitcoin Core developers
|
2016-03-19 20:58:06 +01: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.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Utilities for manipulating blocks and transactions."""
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2017-09-14 17:41:30 +02:00
|
|
|
from .address import (
|
|
|
|
key_to_p2sh_p2wpkh,
|
|
|
|
key_to_p2wpkh,
|
|
|
|
script_to_p2sh_p2wsh,
|
|
|
|
script_to_p2wsh,
|
|
|
|
)
|
2018-04-20 20:10:12 +02:00
|
|
|
from .messages import (
|
|
|
|
CBlock,
|
|
|
|
COIN,
|
|
|
|
COutPoint,
|
|
|
|
CTransaction,
|
|
|
|
CTxIn,
|
|
|
|
CTxInWitness,
|
|
|
|
CTxOut,
|
|
|
|
FromHex,
|
|
|
|
ToHex,
|
|
|
|
hash256,
|
|
|
|
hex_str_to_bytes,
|
|
|
|
ser_uint256,
|
|
|
|
sha256,
|
|
|
|
uint256_from_str,
|
|
|
|
)
|
2017-09-14 17:41:30 +02:00
|
|
|
from .script import (
|
|
|
|
CScript,
|
2019-07-09 17:58:43 +02:00
|
|
|
CScriptNum,
|
|
|
|
CScriptOp,
|
2017-09-14 17:41:30 +02:00
|
|
|
OP_0,
|
|
|
|
OP_1,
|
|
|
|
OP_CHECKMULTISIG,
|
|
|
|
OP_CHECKSIG,
|
|
|
|
OP_RETURN,
|
|
|
|
OP_TRUE,
|
|
|
|
hash160,
|
|
|
|
)
|
|
|
|
from .util import assert_equal
|
2018-07-30 10:16:40 +02:00
|
|
|
from io import BytesIO
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2018-10-08 10:56:39 +02:00
|
|
|
MAX_BLOCK_SIGOPS = 20000
|
|
|
|
|
2019-02-11 17:59:34 +01:00
|
|
|
# Genesis block time (regtest)
|
2020-02-13 16:41:28 +01:00
|
|
|
TIME_GENESIS_BLOCK = 1446058291
|
2019-02-11 17:59:34 +01:00
|
|
|
|
2018-04-20 20:16:57 +02:00
|
|
|
# From BIP141
|
|
|
|
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
|
|
|
|
|
2019-02-11 17:59:34 +01:00
|
|
|
|
2018-12-29 18:19:10 +01:00
|
|
|
def create_block(hashprev, coinbase, ntime=None, *, version=1):
|
2018-04-20 20:16:57 +02:00
|
|
|
"""Create a block (with regtest difficulty)."""
|
2015-04-28 18:39:47 +02:00
|
|
|
block = CBlock()
|
2018-12-29 18:19:10 +01:00
|
|
|
block.nVersion = version
|
2018-04-20 20:10:12 +02:00
|
|
|
if ntime is None:
|
2015-04-28 18:39:47 +02:00
|
|
|
import time
|
2018-04-20 20:10:12 +02:00
|
|
|
block.nTime = int(time.time() + 600)
|
2015-04-28 18:39:47 +02:00
|
|
|
else:
|
2018-04-20 20:10:12 +02:00
|
|
|
block.nTime = ntime
|
2015-04-28 18:39:47 +02:00
|
|
|
block.hashPrevBlock = hashprev
|
2018-04-20 20:10:12 +02:00
|
|
|
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
|
2015-04-28 18:39:47 +02:00
|
|
|
block.vtx.append(coinbase)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.calc_sha256()
|
|
|
|
return block
|
|
|
|
|
2017-04-17 18:23:44 +02:00
|
|
|
def get_witness_script(witness_root, witness_nonce):
|
2018-04-20 20:10:12 +02:00
|
|
|
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce)))
|
2017-04-17 18:23:44 +02:00
|
|
|
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
|
|
|
|
return CScript([OP_RETURN, output_data])
|
|
|
|
|
2016-04-09 03:02:24 +02:00
|
|
|
def add_witness_commitment(block, nonce=0):
|
2018-04-20 20:16:57 +02:00
|
|
|
"""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."""
|
2016-04-09 03:02:24 +02:00
|
|
|
# First calculate the merkle root of the block's
|
|
|
|
# transactions, with witnesses.
|
|
|
|
witness_nonce = nonce
|
|
|
|
witness_root = block.calc_witness_merkle_root()
|
|
|
|
# witness_nonce should go to coinbase witness.
|
2016-07-07 01:46:46 +02:00
|
|
|
block.vtx[0].wit.vtxinwit = [CTxInWitness()]
|
2016-04-09 03:02:24 +02:00
|
|
|
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
|
|
|
|
|
|
|
|
# witness commitment is the last OP_RETURN output in coinbase
|
2017-04-17 18:23:44 +02:00
|
|
|
block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce)))
|
2016-04-09 03:02:24 +02:00
|
|
|
block.vtx[0].rehash()
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.rehash()
|
|
|
|
|
2019-07-09 17:58:43 +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_1])
|
|
|
|
return CScript([CScriptNum(height)])
|
|
|
|
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2019-09-17 19:19:49 +02:00
|
|
|
def GetBlockSubsidy(height):
|
|
|
|
if height == 0:
|
|
|
|
return 400000000 * COIN
|
|
|
|
else:
|
|
|
|
return 1 * COIN
|
|
|
|
|
2018-04-20 20:10:12 +02:00
|
|
|
def create_coinbase(height, pubkey=None):
|
2018-04-20 20:16:57 +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-07-09 17:58:43 +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()
|
2019-09-17 19:19:49 +02:00
|
|
|
coinbaseoutput.nValue = GetBlockSubsidy(height)
|
2018-04-20 20:10:12 +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])
|
2018-04-20 20:10:12 +02:00
|
|
|
coinbase.vout = [coinbaseoutput]
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase.calc_sha256()
|
|
|
|
return coinbase
|
|
|
|
|
2018-08-09 19:27:45 +02:00
|
|
|
def create_tx_with_script(prevtx, n, script_sig=b"", *, amount, script_pub_key=CScript()):
|
2018-07-30 10:16:40 +02:00
|
|
|
"""Return one-input, one-output transaction object
|
|
|
|
spending the prevtx's n-th output with the given amount.
|
2018-04-20 20:16:57 +02:00
|
|
|
|
2018-08-14 09:27:30 +02:00
|
|
|
Can optionally pass scriptPubKey and scriptSig, default is anyone-can-spend output.
|
2018-07-30 10:16:40 +02:00
|
|
|
"""
|
2015-04-28 18:39:47 +02:00
|
|
|
tx = CTransaction()
|
2019-02-19 23:43:44 +01:00
|
|
|
assert n < len(prevtx.vout)
|
2018-07-30 10:16:40 +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-02 20:42:09 +02:00
|
|
|
|
2018-08-09 19:27:45 +02:00
|
|
|
def create_transaction(node, txid, to_address, *, amount):
|
2018-07-30 10:16:40 +02:00
|
|
|
""" Return signed transaction spending the first output of the
|
|
|
|
input txid. Note that the node must be able to sign for the
|
|
|
|
output that is being spent, and the node must not be running
|
|
|
|
multiple wallets.
|
|
|
|
"""
|
2018-08-09 19:27:45 +02:00
|
|
|
raw_tx = create_raw_transaction(node, txid, to_address, amount=amount)
|
2018-07-30 10:16:40 +02:00
|
|
|
tx = CTransaction()
|
|
|
|
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx)))
|
|
|
|
return tx
|
|
|
|
|
2018-08-09 19:27:45 +02:00
|
|
|
def create_raw_transaction(node, txid, to_address, *, amount):
|
2018-07-30 10:03:20 +02:00
|
|
|
""" Return raw signed transaction spending the first output of the
|
|
|
|
input txid. Note that the node must be able to sign for the
|
|
|
|
output that is being spent, and the node must not be running
|
|
|
|
multiple wallets.
|
|
|
|
"""
|
2018-08-09 19:27:45 +02:00
|
|
|
rawtx = node.createrawtransaction(inputs=[{"txid": txid, "vout": 0}], outputs={to_address: amount})
|
2018-07-30 10:03:20 +02:00
|
|
|
signresult = node.signrawtransactionwithwallet(rawtx)
|
|
|
|
assert_equal(signresult["complete"], True)
|
|
|
|
return signresult['hex']
|
|
|
|
|
2018-04-20 20:10:12 +02:00
|
|
|
def get_legacy_sigopcount_block(block, accurate=True):
|
2016-06-02 20:42:09 +02:00
|
|
|
count = 0
|
|
|
|
for tx in block.vtx:
|
2018-04-20 20:10:12 +02:00
|
|
|
count += get_legacy_sigopcount_tx(tx, accurate)
|
2016-06-02 20:42:09 +02:00
|
|
|
return count
|
|
|
|
|
2018-04-20 20:10:12 +02:00
|
|
|
def get_legacy_sigopcount_tx(tx, accurate=True):
|
2016-06-02 20:42:09 +02:00
|
|
|
count = 0
|
|
|
|
for i in tx.vout:
|
2018-04-20 20:10:12 +02:00
|
|
|
count += i.scriptPubKey.GetSigOpCount(accurate)
|
2016-06-02 20:42:09 +02:00
|
|
|
for j in tx.vin:
|
|
|
|
# scriptSig might be of type bytes, so convert to CScript for the moment
|
2018-04-20 20:10:12 +02:00
|
|
|
count += CScript(j.scriptSig).GetSigOpCount(accurate)
|
2016-06-02 20:42:09 +02:00
|
|
|
return count
|
2017-09-14 17:41:30 +02:00
|
|
|
|
|
|
|
def witness_script(use_p2wsh, pubkey):
|
2018-10-09 15:18:34 +02:00
|
|
|
"""Create a scriptPubKey for a pay-to-witness TxOut.
|
2018-04-20 20:16:57 +02:00
|
|
|
|
|
|
|
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."""
|
2018-04-20 20:10:12 +02:00
|
|
|
if not use_p2wsh:
|
2017-09-14 17:41:30 +02:00
|
|
|
# P2WPKH instead
|
|
|
|
pubkeyhash = hash160(hex_str_to_bytes(pubkey))
|
|
|
|
pkscript = CScript([OP_0, pubkeyhash])
|
|
|
|
else:
|
|
|
|
# 1-of-1 multisig
|
|
|
|
witness_program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
|
|
|
|
scripthash = sha256(witness_program)
|
|
|
|
pkscript = CScript([OP_0, scripthash])
|
2019-02-18 16:35:48 +01:00
|
|
|
return pkscript.hex()
|
2017-09-14 17:41:30 +02:00
|
|
|
|
|
|
|
def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
|
2018-04-20 20:16:57 +02:00
|
|
|
"""Return a transaction (in hex) that spends the given utxo to a segwit output.
|
|
|
|
|
|
|
|
Optionally wrap the segwit output using P2SH."""
|
2017-09-14 17:41:30 +02:00
|
|
|
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)
|
|
|
|
else:
|
|
|
|
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
|
|
|
|
if not encode_p2sh:
|
2018-02-09 17:12:27 +01:00
|
|
|
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
|
2017-09-14 17:41:30 +02:00
|
|
|
return node.createrawtransaction([utxo], {addr: amount})
|
|
|
|
|
|
|
|
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
|
2018-04-20 20:16:57 +02:00
|
|
|
"""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."""
|
2017-09-14 17:41:30 +02:00
|
|
|
tx_to_witness = create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount)
|
|
|
|
if (sign):
|
2017-09-06 01:49:18 +02:00
|
|
|
signed = node.signrawtransactionwithwallet(tx_to_witness)
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "errors" not in signed or len(["errors"]) == 0
|
2017-09-14 17:41:30 +02:00
|
|
|
return node.sendrawtransaction(signed["hex"])
|
|
|
|
else:
|
|
|
|
if (insert_redeem_script):
|
|
|
|
tx = FromHex(CTransaction(), tx_to_witness)
|
|
|
|
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
|
|
|
|
tx_to_witness = ToHex(tx)
|
|
|
|
|
|
|
|
return node.sendrawtransaction(tx_to_witness)
|