2 # Copyright (c) 2015-2016 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Utilities for manipulating blocks and transactions."""
7 from .mininode
import *
8 from .script
import CScript
, OP_TRUE
, OP_CHECKSIG
, OP_RETURN
10 # Create a block (with regtest difficulty)
11 def create_block(hashprev
, coinbase
, nTime
=None):
15 block
.nTime
= int(time
.time()+600)
18 block
.hashPrevBlock
= hashprev
19 block
.nBits
= 0x207fffff # Will break after a difficulty adjustment...
20 block
.vtx
.append(coinbase
)
21 block
.hashMerkleRoot
= block
.calc_merkle_root()
26 WITNESS_COMMITMENT_HEADER
= b
"\xaa\x21\xa9\xed"
29 def get_witness_script(witness_root
, witness_nonce
):
30 witness_commitment
= uint256_from_str(hash256(ser_uint256(witness_root
)+ser_uint256(witness_nonce
)))
31 output_data
= WITNESS_COMMITMENT_HEADER
+ ser_uint256(witness_commitment
)
32 return CScript([OP_RETURN
, output_data
])
35 # According to BIP141, blocks with witness rules active must commit to the
36 # hash of all in-block transactions including witness.
37 def add_witness_commitment(block
, nonce
=0):
38 # First calculate the merkle root of the block's
39 # transactions, with witnesses.
41 witness_root
= block
.calc_witness_merkle_root()
42 # witness_nonce should go to coinbase witness.
43 block
.vtx
[0].wit
.vtxinwit
= [CTxInWitness()]
44 block
.vtx
[0].wit
.vtxinwit
[0].scriptWitness
.stack
= [ser_uint256(witness_nonce
)]
46 # witness commitment is the last OP_RETURN output in coinbase
47 block
.vtx
[0].vout
.append(CTxOut(0, get_witness_script(witness_root
, witness_nonce
)))
49 block
.hashMerkleRoot
= block
.calc_merkle_root()
53 def serialize_script_num(value
):
58 absvalue
= -value
if neg
else value
60 r
.append(int(absvalue
& 0xff))
63 r
.append(0x80 if neg
else 0)
68 # Create a coinbase transaction, assuming no miner fees.
69 # If pubkey is passed in, the coinbase output will be a P2PK output;
70 # otherwise an anyone-can-spend output.
71 def create_coinbase(height
, pubkey
= None):
72 coinbase
= CTransaction()
73 coinbase
.vin
.append(CTxIn(COutPoint(0, 0xffffffff),
74 ser_string(serialize_script_num(height
)), 0xffffffff))
75 coinbaseoutput
= CTxOut()
76 coinbaseoutput
.nValue
= 50 * COIN
77 halvings
= int(height
/150) # regtest
78 coinbaseoutput
.nValue
>>= halvings
80 coinbaseoutput
.scriptPubKey
= CScript([pubkey
, OP_CHECKSIG
])
82 coinbaseoutput
.scriptPubKey
= CScript([OP_TRUE
])
83 coinbase
.vout
= [ coinbaseoutput
]
84 coinbase
.calc_sha256()
87 # Create a transaction.
88 # If the scriptPubKey is not specified, make it anyone-can-spend.
89 def create_transaction(prevtx
, n
, sig
, value
, scriptPubKey
=CScript()):
91 assert(n
< len(prevtx
.vout
))
92 tx
.vin
.append(CTxIn(COutPoint(prevtx
.sha256
, n
), sig
, 0xffffffff))
93 tx
.vout
.append(CTxOut(value
, scriptPubKey
))
97 def get_legacy_sigopcount_block(block
, fAccurate
=True):
100 count
+= get_legacy_sigopcount_tx(tx
, fAccurate
)
103 def get_legacy_sigopcount_tx(tx
, fAccurate
=True):
106 count
+= i
.scriptPubKey
.GetSigOpCount(fAccurate
)
108 # scriptSig might be of type bytes, so convert to CScript for the moment
109 count
+= CScript(j
.scriptSig
).GetSigOpCount(fAccurate
)