Changeset View
Changeset View
Standalone View
Standalone View
test/functional/abc-mempool-accept-txn.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# 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. | ||||
""" | """ | ||||
This test checks acceptance of transactions by the mempool | This test checks acceptance of transactions by the mempool | ||||
It is derived from the much more complex p2p-fullblocktest. | It is derived from the much more complex p2p-fullblocktest. | ||||
""" | """ | ||||
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, | ||||
) | ) | ||||
from test_framework.cdefs import MAX_STANDARD_TX_SIGOPS | from test_framework.cdefs import MAX_STANDARD_TX_SIGOPS | ||||
from test_framework.key import CECKey | from test_framework.key import CECKey | ||||
from test_framework.messages import ( | from test_framework.messages import ( | ||||
COutPoint, | COutPoint, | ||||
CTransaction, | CTransaction, | ||||
CTxIn, | CTxIn, | ||||
CTxOut, | CTxOut, | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | class FullBlockTest(BitcoinTestFramework): | ||||
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) | ||||
block.vtx = [block.vtx[0]] + \ | block.vtx = [block.vtx[0]] + \ | ||||
sorted(block.vtx[1:], key=lambda tx: tx.get_id()) | sorted(block.vtx[1:], key=lambda tx: tx.get_id()) | ||||
# 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])): | ||||
tx = create_transaction(spend_tx, n, b"", value, script) | tx = create_tx_with_script(spend_tx, n, b"", value, script) | ||||
return tx | return tx | ||||
# 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 | # this signs input 0 in tx, which is assumed to be spending output n in | ||||
# spend_tx | # 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 | ||||
Show All 26 Lines | def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE])): | ||||
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[ | coinbase.vout[0].nValue += spend.tx.vout[ | ||||
spend.n].nValue - 1 | 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() | ||||
# Do PoW, which is very inexpensive on regnet | # Do PoW, which is very inexpensive on regnet | ||||
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 128 Lines • Show Last 20 Lines |