Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_block.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2015-2017 The Bitcoin Core developers | # Copyright (c) 2015-2017 The Bitcoin Core developers | ||||
# Copyright (c) 2017 The Bitcoin developers | # Copyright (c) 2017 The Bitcoin developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test block processing.""" | """Test block processing.""" | ||||
import copy | import copy | ||||
import struct | import struct | ||||
import time | import time | ||||
from test_framework.blocktools import ( | from test_framework.blocktools import ( | ||||
create_block, | create_block, | ||||
create_coinbase, | create_coinbase, | ||||
create_transaction, | create_tx_with_script, | ||||
get_legacy_sigopcount_block, | get_legacy_sigopcount_block, | ||||
make_conform_to_ctor, | make_conform_to_ctor, | ||||
) | ) | ||||
from test_framework.cdefs import LEGACY_MAX_BLOCK_SIZE, MAX_BLOCK_SIGOPS_PER_MB | from test_framework.cdefs import LEGACY_MAX_BLOCK_SIZE, MAX_BLOCK_SIGOPS_PER_MB | ||||
from test_framework.key import CECKey | from test_framework.key import CECKey | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
CBlock, | CBlock, | ||||
COIN, | COIN, | ||||
▲ Show 20 Lines • Show All 1,282 Lines • ▼ Show 20 Lines | |||||
################ | ################ | ||||
def add_transactions_to_block(self, block, tx_list): | def add_transactions_to_block(self, block, tx_list): | ||||
[tx.rehash() for tx in tx_list] | [tx.rehash() for tx in tx_list] | ||||
block.vtx.extend(tx_list) | block.vtx.extend(tx_list) | ||||
# this is a little handier to use than the version in blocktools.py | # this is a little handier to use than the version in blocktools.py | ||||
def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): | def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): | ||||
return create_transaction(spend_tx, n, b"", value, script) | return create_tx_with_script(spend_tx, n, b"", value, script) | ||||
# sign a transaction, using the key we know about | # sign a transaction, using the key we know about | ||||
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx | # this signs input 0 in tx, which is assumed to be spending output n in spend_tx | ||||
def sign_tx(self, tx, spend_tx, n): | def sign_tx(self, tx, spend_tx, n): | ||||
scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) | scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) | ||||
if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend | if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend | ||||
tx.vin[0].scriptSig = CScript() | tx.vin[0].scriptSig = CScript() | ||||
return | return | ||||
Show All 23 Lines | def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE]), solve=True): | ||||
if spend is None: | if spend is None: | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
else: | else: | ||||
# all but one satoshi to fees | # all but one satoshi to fees | ||||
coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 | ||||
coinbase.rehash() | coinbase.rehash() | ||||
block = create_block(base_block_hash, coinbase, block_time) | block = create_block(base_block_hash, coinbase, block_time) | ||||
# spend 1 satoshi | # spend 1 satoshi | ||||
tx = create_transaction(spend.tx, spend.n, b"", 1, script) | tx = create_tx_with_script(spend.tx, spend.n, b"", 1, script) | ||||
self.sign_tx(tx, spend.tx, spend.n) | self.sign_tx(tx, spend.tx, spend.n) | ||||
self.add_transactions_to_block(block, [tx]) | self.add_transactions_to_block(block, [tx]) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
if solve: | if solve: | ||||
block.solve() | block.solve() | ||||
self.tip = block | self.tip = block | ||||
self.block_heights[block.sha256] = height | self.block_heights[block.sha256] = height | ||||
assert number not in self.blocks | assert number not in self.blocks | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |