You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.7 KiB
106 lines
3.7 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
||
|
# Distributed under the MIT software license, see the accompanying
|
||
10 years ago
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Utilities for manipulating blocks and transactions."""
|
||
10 years ago
|
|
||
9 years ago
|
from .mininode import *
|
||
9 years ago
|
from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN
|
||
10 years ago
|
|
||
|
# Create a block (with regtest difficulty)
|
||
|
def create_block(hashprev, coinbase, nTime=None):
|
||
|
block = CBlock()
|
||
|
if nTime is None:
|
||
|
import time
|
||
|
block.nTime = int(time.time()+600)
|
||
|
else:
|
||
|
block.nTime = nTime
|
||
|
block.hashPrevBlock = hashprev
|
||
|
block.nBits = 0x207fffff # Will break after a difficulty adjustment...
|
||
|
block.vtx.append(coinbase)
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.calc_sha256()
|
||
|
return block
|
||
|
|
||
9 years ago
|
# From BIP141
|
||
|
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
|
||
|
|
||
|
# 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):
|
||
|
# First calculate the merkle root of the block's
|
||
|
# transactions, with witnesses.
|
||
|
witness_nonce = nonce
|
||
|
witness_root = block.calc_witness_merkle_root()
|
||
|
witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
|
||
|
# witness_nonce should go to coinbase witness.
|
||
9 years ago
|
block.vtx[0].wit.vtxinwit = [CTxInWitness()]
|
||
9 years ago
|
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
|
||
|
|
||
|
# witness commitment is the last OP_RETURN output in coinbase
|
||
|
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
|
||
|
block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
|
||
|
block.vtx[0].rehash()
|
||
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||
|
block.rehash()
|
||
|
|
||
|
|
||
10 years ago
|
def serialize_script_num(value):
|
||
|
r = bytearray(0)
|
||
|
if value == 0:
|
||
|
return r
|
||
|
neg = value < 0
|
||
|
absvalue = -value if neg else value
|
||
|
while (absvalue):
|
||
9 years ago
|
r.append(int(absvalue & 0xff))
|
||
10 years ago
|
absvalue >>= 8
|
||
|
if r[-1] & 0x80:
|
||
|
r.append(0x80 if neg else 0)
|
||
|
elif neg:
|
||
|
r[-1] |= 0x80
|
||
|
return r
|
||
|
|
||
10 years ago
|
# 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):
|
||
10 years ago
|
coinbase = CTransaction()
|
||
|
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
|
||
10 years ago
|
ser_string(serialize_script_num(height)), 0xffffffff))
|
||
10 years ago
|
coinbaseoutput = CTxOut()
|
||
9 years ago
|
coinbaseoutput.nValue = 50 * COIN
|
||
10 years ago
|
halvings = int(height/150) # regtest
|
||
10 years ago
|
coinbaseoutput.nValue >>= halvings
|
||
10 years ago
|
if (pubkey != None):
|
||
|
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
|
||
|
else:
|
||
|
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
|
||
10 years ago
|
coinbase.vout = [ coinbaseoutput ]
|
||
|
coinbase.calc_sha256()
|
||
|
return coinbase
|
||
|
|
||
9 years ago
|
# Create a transaction.
|
||
|
# If the scriptPubKey is not specified, make it anyone-can-spend.
|
||
|
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
|
||
10 years ago
|
tx = CTransaction()
|
||
|
assert(n < len(prevtx.vout))
|
||
|
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
|
||
9 years ago
|
tx.vout.append(CTxOut(value, scriptPubKey))
|
||
10 years ago
|
tx.calc_sha256()
|
||
|
return tx
|
||
9 years ago
|
|
||
|
def get_legacy_sigopcount_block(block, fAccurate=True):
|
||
|
count = 0
|
||
|
for tx in block.vtx:
|
||
|
count += get_legacy_sigopcount_tx(tx, fAccurate)
|
||
|
return count
|
||
|
|
||
|
def get_legacy_sigopcount_tx(tx, fAccurate=True):
|
||
|
count = 0
|
||
|
for i in tx.vout:
|
||
|
count += i.scriptPubKey.GetSigOpCount(fAccurate)
|
||
|
for j in tx.vin:
|
||
|
# scriptSig might be of type bytes, so convert to CScript for the moment
|
||
|
count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
|
||
|
return count
|