~ubuntu-branches/debian/stretch/bitcoin/stretch

« back to all changes in this revision

Viewing changes to qa/rpc-tests/test_framework/blocktools.py

  • Committer: Package Import Robot
  • Author(s): Anthony Towns
  • Date: 2016-10-21 17:13:13 UTC
  • mfrom: (1.3.2)
  • Revision ID: package-import@ubuntu.com-20161021171313-7eu2ltpbk0xag3q1
Tags: 0.13.0-0.1
* Non-maintainer upload.
* New upstream release.
* Allow compilation with gcc/g++ 6. (Closes: Bug#835963)
* Additional fixes for openssl 1.1 compatibility. (See Bug#828248)
* Check if -latomic is needed (it is on mips*).
* Remove reproducible build patch, since leveldb build system is
  no longer used in 0.13. (See Bug#791834)
* Update description since the blockchain is much more than "several GB"
  now. (Closes: Bug#835809)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
1
2
# blocktools.py - utilities for manipulating blocks and transactions
2
 
#
3
 
# Distributed under the MIT/X11 software license, see the accompanying
 
3
# Copyright (c) 2015-2016 The Bitcoin Core developers
 
4
# Distributed under the MIT software license, see the accompanying
4
5
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
 
#
6
6
 
7
 
from mininode import *
8
 
from script import CScript, OP_TRUE, OP_CHECKSIG
 
7
from .mininode import *
 
8
from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN
9
9
 
10
10
# Create a block (with regtest difficulty)
11
11
def create_block(hashprev, coinbase, nTime=None):
22
22
    block.calc_sha256()
23
23
    return block
24
24
 
 
25
# From BIP141
 
26
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
 
27
 
 
28
# According to BIP141, blocks with witness rules active must commit to the
 
29
# hash of all in-block transactions including witness.
 
30
def add_witness_commitment(block, nonce=0):
 
31
    # First calculate the merkle root of the block's
 
32
    # transactions, with witnesses.
 
33
    witness_nonce = nonce
 
34
    witness_root = block.calc_witness_merkle_root()
 
35
    witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
 
36
    # witness_nonce should go to coinbase witness.
 
37
    block.vtx[0].wit.vtxinwit = [CTxInWitness()]
 
38
    block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
 
39
 
 
40
    # witness commitment is the last OP_RETURN output in coinbase
 
41
    output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
 
42
    block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
 
43
    block.vtx[0].rehash()
 
44
    block.hashMerkleRoot = block.calc_merkle_root()
 
45
    block.rehash()
 
46
 
 
47
 
25
48
def serialize_script_num(value):
26
49
    r = bytearray(0)
27
50
    if value == 0:
29
52
    neg = value < 0
30
53
    absvalue = -value if neg else value
31
54
    while (absvalue):
32
 
        r.append(chr(absvalue & 0xff))
 
55
        r.append(int(absvalue & 0xff))
33
56
        absvalue >>= 8
34
57
    if r[-1] & 0x80:
35
58
        r.append(0x80 if neg else 0)
45
68
    coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff), 
46
69
                ser_string(serialize_script_num(height)), 0xffffffff))
47
70
    coinbaseoutput = CTxOut()
48
 
    coinbaseoutput.nValue = 50*100000000
 
71
    coinbaseoutput.nValue = 50 * COIN
49
72
    halvings = int(height/150) # regtest
50
73
    coinbaseoutput.nValue >>= halvings
51
74
    if (pubkey != None):
56
79
    coinbase.calc_sha256()
57
80
    return coinbase
58
81
 
59
 
# Create a transaction with an anyone-can-spend output, that spends the
60
 
# nth output of prevtx.
61
 
def create_transaction(prevtx, n, sig, value):
 
82
# Create a transaction.
 
83
# If the scriptPubKey is not specified, make it anyone-can-spend.
 
84
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
62
85
    tx = CTransaction()
63
86
    assert(n < len(prevtx.vout))
64
87
    tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
65
 
    tx.vout.append(CTxOut(value, ""))
 
88
    tx.vout.append(CTxOut(value, scriptPubKey))
66
89
    tx.calc_sha256()
67
90
    return tx
 
91
 
 
92
def get_legacy_sigopcount_block(block, fAccurate=True):
 
93
    count = 0
 
94
    for tx in block.vtx:
 
95
        count += get_legacy_sigopcount_tx(tx, fAccurate)
 
96
    return count
 
97
 
 
98
def get_legacy_sigopcount_tx(tx, fAccurate=True):
 
99
    count = 0
 
100
    for i in tx.vout:
 
101
        count += i.scriptPubKey.GetSigOpCount(fAccurate)
 
102
    for j in tx.vin:
 
103
        # scriptSig might be of type bytes, so convert to CScript for the moment
 
104
        count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
 
105
    return count