qa: Only allow disconnecting all NodeConns
[bitcoinplatinum.git] / test / functional / test_framework / blocktools.py
blob5dcf516dc6e983f0f7992fc56fd1c0d5eadc57fd
1 #!/usr/bin/env python3
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):
12 block = CBlock()
13 if nTime is None:
14 import time
15 block.nTime = int(time.time()+600)
16 else:
17 block.nTime = nTime
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()
22 block.calc_sha256()
23 return block
25 # From BIP141
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.
40 witness_nonce = nonce
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)))
48 block.vtx[0].rehash()
49 block.hashMerkleRoot = block.calc_merkle_root()
50 block.rehash()
53 def serialize_script_num(value):
54 r = bytearray(0)
55 if value == 0:
56 return r
57 neg = value < 0
58 absvalue = -value if neg else value
59 while (absvalue):
60 r.append(int(absvalue & 0xff))
61 absvalue >>= 8
62 if r[-1] & 0x80:
63 r.append(0x80 if neg else 0)
64 elif neg:
65 r[-1] |= 0x80
66 return r
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
79 if (pubkey != None):
80 coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
81 else:
82 coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
83 coinbase.vout = [ coinbaseoutput ]
84 coinbase.calc_sha256()
85 return coinbase
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()):
90 tx = CTransaction()
91 assert(n < len(prevtx.vout))
92 tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
93 tx.vout.append(CTxOut(value, scriptPubKey))
94 tx.calc_sha256()
95 return tx
97 def get_legacy_sigopcount_block(block, fAccurate=True):
98 count = 0
99 for tx in block.vtx:
100 count += get_legacy_sigopcount_tx(tx, fAccurate)
101 return count
103 def get_legacy_sigopcount_tx(tx, fAccurate=True):
104 count = 0
105 for i in tx.vout:
106 count += i.scriptPubKey.GetSigOpCount(fAccurate)
107 for j in tx.vin:
108 # scriptSig might be of type bytes, so convert to CScript for the moment
109 count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
110 return count