Merge bitcoin/bitcoin#22257: test: refactor: various (de)serialization helpers cleanups/improvements

bdb8b9a347 test: doc: improve doc for `from_hex` helper (mention `to_hex` alternative) (Sebastian Falbesoner)
1914054208 scripted-diff: test: rename `FromHex` to `from_hex` (Sebastian Falbesoner)
a79396fe5f test: remove `ToHex` helper, use .serialize().hex() instead (Sebastian Falbesoner)
2ce7b47958 test: introduce `tx_from_hex` helper for tx deserialization (Sebastian Falbesoner)

Pull request description:

  There are still many functional tests that perform conversions from a hex-string to a message object (deserialization) manually. This PR identifies all those instances and replaces them with a newly introduced helper `tx_from_hex`.

  Instances were found via
  * `git grep "deserialize.*BytesIO"`

  and some of them manually, when it were not one-liners.

  Further, the helper `ToHex` was removed and simply replaced by `.serialize().hex()`, since now both variants are in use (sometimes even within the same test) and using the helper doesn't really have an advantage in readability. (see discussion https://github.com/bitcoin/bitcoin/pull/22257#discussion_r652404782)

ACKs for top commit:
  MarcoFalke:
    review re-ACK bdb8b9a347 😁

Tree-SHA512: e25d7dc85918de1d6755a5cea65471b07a743204c20ad1c2f71ff07ef48cc1b9ad3fe5f515c1efaba2b2e3d89384e7980380c5d81895f9826e2046808cd3266e
This commit is contained in:
MarcoFalke committed 2021-06-24 12:47:13 +02:00
commit d6a59166a1
33 files changed
+396 -261

No files matched your search

+6 -6
View File
@@ -23,7 +23,7 @@ PATH_BASE_TEST_FUNCTIONAL = os.path.abspath(os.path.join(PATH_BASE_CONTRIB_SIGNE
sys.path.insert(0, PATH_BASE_TEST_FUNCTIONAL) sys.path.insert(0, PATH_BASE_TEST_FUNCTIONAL)
from test_framework.blocktools import WITNESS_COMMITMENT_HEADER, script_BIP34_coinbase_height # noqa: E402 from test_framework.blocktools import WITNESS_COMMITMENT_HEADER, script_BIP34_coinbase_height # noqa: E402
from test_framework.messages import CBlock, CBlockHeader, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, ToHex, deser_string, hash256, ser_compact_size, ser_string, ser_uint256, uint256_from_str # noqa: E402 from test_framework.messages import CBlock, CBlockHeader, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, from_hex, deser_string, hash256, ser_compact_size, ser_string, ser_uint256, tx_from_hex, uint256_from_str # noqa: E402
from test_framework.script import CScriptOp # noqa: E402 from test_framework.script import CScriptOp # noqa: E402
logging.basicConfig( logging.basicConfig(
@@ -37,7 +37,7 @@ RE_MULTIMINER = re.compile("^(\d+)(-(\d+))?/(\d+)$")
# #### some helpers that could go into test_framework # #### some helpers that could go into test_framework
# like FromHex, but without the hex part # like from_hex, but without the hex part
def FromBinary(cls, stream): def FromBinary(cls, stream):
"""deserialize a binary stream (or bytes object) into an object""" """deserialize a binary stream (or bytes object) into an object"""
# handle bytes object by turning it into a stream # handle bytes object by turning it into a stream
@@ -195,7 +195,7 @@ def finish_block(block, signet_solution, grind_cmd):
headhex = CBlockHeader.serialize(block).hex() headhex = CBlockHeader.serialize(block).hex()
cmd = grind_cmd.split(" ") + [headhex] cmd = grind_cmd.split(" ") + [headhex]
newheadhex = subprocess.run(cmd, stdout=subprocess.PIPE, input=b"", check=True).stdout.strip() newheadhex = subprocess.run(cmd, stdout=subprocess.PIPE, input=b"", check=True).stdout.strip()
newhead = FromHex(CBlockHeader(), newheadhex.decode('utf8')) newhead = from_hex(CBlockHeader(), newheadhex.decode('utf8'))
block.nNonce = newhead.nNonce block.nNonce = newhead.nNonce
block.rehash() block.rehash()
return block return block
@@ -216,7 +216,7 @@ def generate_psbt(tmpl, reward_spk, *, blocktime=None):
block.nTime = tmpl["mintime"] block.nTime = tmpl["mintime"]
block.nBits = int(tmpl["bits"], 16) block.nBits = int(tmpl["bits"], 16)
block.nNonce = 0 block.nNonce = 0
block.vtx = [cbtx] + [FromHex(CTransaction(), t["data"]) for t in tmpl["transactions"]] block.vtx = [cbtx] + [tx_from_hex(t["data"]) for t in tmpl["transactions"]]
witnonce = 0 witnonce = 0
witroot = block.calc_witness_merkle_root() witroot = block.calc_witness_merkle_root()
@@ -274,7 +274,7 @@ def do_genpsbt(args):
def do_solvepsbt(args): def do_solvepsbt(args):
block, signet_solution = do_decode_psbt(sys.stdin.read()) block, signet_solution = do_decode_psbt(sys.stdin.read())
block = finish_block(block, signet_solution, args.grind_cmd) block = finish_block(block, signet_solution, args.grind_cmd)
print(ToHex(block)) print(block.serialize().hex())
def nbits_to_target(nbits): def nbits_to_target(nbits):
shift = (nbits >> 24) & 0xff shift = (nbits >> 24) & 0xff
@@ -503,7 +503,7 @@ def do_generate(args):
block = finish_block(block, signet_solution, args.grind_cmd) block = finish_block(block, signet_solution, args.grind_cmd)
# submit block # submit block
r = args.bcli("-stdin", "submitblock", input=ToHex(block).encode('utf8')) r = args.bcli("-stdin", "submitblock", input=block.serialize().hex().encode('utf8'))
# report # report
bstr = "block" if is_mine else "backup block" bstr = "block" if is_mine else "backup block"
+32 -21
View File
@@ -6,8 +6,19 @@
import time import time
from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment from test_framework.blocktools import (
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex NORMAL_GBT_REQUEST_PARAMS,
add_witness_commitment,
create_block,
)
from test_framework.messages import (
COIN,
COutPoint,
CTransaction,
CTxIn,
CTxOut,
tx_from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@@ -89,7 +100,7 @@ class BIP68Test(BitcoinTestFramework):
tx1.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), nSequence=sequence_value)] tx1.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), nSequence=sequence_value)]
tx1.vout = [CTxOut(value, DUMMY_P2WPKH_SCRIPT)] tx1.vout = [CTxOut(value, DUMMY_P2WPKH_SCRIPT)]
tx1_signed = self.nodes[0].signrawtransactionwithwallet(ToHex(tx1))["hex"] tx1_signed = self.nodes[0].signrawtransactionwithwallet(tx1.serialize().hex())["hex"]
tx1_id = self.nodes[0].sendrawtransaction(tx1_signed) tx1_id = self.nodes[0].sendrawtransaction(tx1_signed)
tx1_id = int(tx1_id, 16) tx1_id = int(tx1_id, 16)
@@ -102,13 +113,13 @@ class BIP68Test(BitcoinTestFramework):
tx2.vout = [CTxOut(int(value - self.relayfee * COIN), DUMMY_P2WPKH_SCRIPT)] tx2.vout = [CTxOut(int(value - self.relayfee * COIN), DUMMY_P2WPKH_SCRIPT)]
tx2.rehash() tx2.rehash()
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx2)) assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, tx2.serialize().hex())
# Setting the version back down to 1 should disable the sequence lock, # Setting the version back down to 1 should disable the sequence lock,
# so this should be accepted. # so this should be accepted.
tx2.nVersion = 1 tx2.nVersion = 1
self.nodes[0].sendrawtransaction(ToHex(tx2)) self.nodes[0].sendrawtransaction(tx2.serialize().hex())
# Calculate the median time past of a prior block ("confirmations" before # Calculate the median time past of a prior block ("confirmations" before
# the current tip). # the current tip).
@@ -193,9 +204,9 @@ class BIP68Test(BitcoinTestFramework):
tx.vin.append(CTxIn(COutPoint(int(utxos[j]["txid"], 16), utxos[j]["vout"]), nSequence=sequence_value)) tx.vin.append(CTxIn(COutPoint(int(utxos[j]["txid"], 16), utxos[j]["vout"]), nSequence=sequence_value))
value += utxos[j]["amount"]*COIN value += utxos[j]["amount"]*COIN
# Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output # Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output
tx_size = len(ToHex(tx))//2 + 120*num_inputs + 50 tx_size = len(tx.serialize().hex())//2 + 120*num_inputs + 50
tx.vout.append(CTxOut(int(value-self.relayfee*tx_size*COIN/1000), DUMMY_P2WPKH_SCRIPT)) tx.vout.append(CTxOut(int(value-self.relayfee*tx_size*COIN/1000), DUMMY_P2WPKH_SCRIPT))
rawtx = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))["hex"] rawtx = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())["hex"]
if (using_sequence_locks and not should_pass): if (using_sequence_locks and not should_pass):
# This transaction should be rejected # This transaction should be rejected
@@ -215,7 +226,7 @@ class BIP68Test(BitcoinTestFramework):
# Create a mempool tx. # Create a mempool tx.
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2) txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid)) tx1 = tx_from_hex(self.nodes[0].getrawtransaction(txid))
tx1.rehash() tx1.rehash()
# Anyone-can-spend mempool tx. # Anyone-can-spend mempool tx.
@@ -224,8 +235,8 @@ class BIP68Test(BitcoinTestFramework):
tx2.nVersion = 2 tx2.nVersion = 2
tx2.vin = [CTxIn(COutPoint(tx1.sha256, 0), nSequence=0)] tx2.vin = [CTxIn(COutPoint(tx1.sha256, 0), nSequence=0)]
tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), DUMMY_P2WPKH_SCRIPT)] tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), DUMMY_P2WPKH_SCRIPT)]
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"] tx2_raw = self.nodes[0].signrawtransactionwithwallet(tx2.serialize().hex())["hex"]
tx2 = FromHex(tx2, tx2_raw) tx2 = tx_from_hex(tx2_raw)
tx2.rehash() tx2.rehash()
self.nodes[0].sendrawtransaction(tx2_raw) self.nodes[0].sendrawtransaction(tx2_raw)
@@ -246,10 +257,10 @@ class BIP68Test(BitcoinTestFramework):
if (orig_tx.hash in node.getrawmempool()): if (orig_tx.hash in node.getrawmempool()):
# sendrawtransaction should fail if the tx is in the mempool # sendrawtransaction should fail if the tx is in the mempool
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, node.sendrawtransaction, ToHex(tx)) assert_raises_rpc_error(-26, NOT_FINAL_ERROR, node.sendrawtransaction, tx.serialize().hex())
else: else:
# sendrawtransaction should succeed if the tx is not in the mempool # sendrawtransaction should succeed if the tx is not in the mempool
node.sendrawtransaction(ToHex(tx)) node.sendrawtransaction(tx.serialize().hex())
return tx return tx
@@ -299,7 +310,7 @@ class BIP68Test(BitcoinTestFramework):
utxos = self.nodes[0].listunspent() utxos = self.nodes[0].listunspent()
tx5.vin.append(CTxIn(COutPoint(int(utxos[0]["txid"], 16), utxos[0]["vout"]), nSequence=1)) tx5.vin.append(CTxIn(COutPoint(int(utxos[0]["txid"], 16), utxos[0]["vout"]), nSequence=1))
tx5.vout[0].nValue += int(utxos[0]["amount"]*COIN) tx5.vout[0].nValue += int(utxos[0]["amount"]*COIN)
raw_tx5 = self.nodes[0].signrawtransactionwithwallet(ToHex(tx5))["hex"] raw_tx5 = self.nodes[0].signrawtransactionwithwallet(tx5.serialize().hex())["hex"]
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, raw_tx5) assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, raw_tx5)
@@ -325,7 +336,7 @@ class BIP68Test(BitcoinTestFramework):
block.rehash() block.rehash()
block.solve() block.solve()
tip = block.sha256 tip = block.sha256
assert_equal(None if i == 1 else 'inconclusive', self.nodes[0].submitblock(ToHex(block))) assert_equal(None if i == 1 else 'inconclusive', self.nodes[0].submitblock(block.serialize().hex()))
tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS) tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
tmpl['previousblockhash'] = '%x' % tip tmpl['previousblockhash'] = '%x' % tip
tmpl['transactions'] = [] tmpl['transactions'] = []
@@ -348,7 +359,7 @@ class BIP68Test(BitcoinTestFramework):
assert not softfork_active(self.nodes[0], 'csv') assert not softfork_active(self.nodes[0], 'csv')
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2) txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid)) tx1 = tx_from_hex(self.nodes[0].getrawtransaction(txid))
tx1.rehash() tx1.rehash()
# Make an anyone-can-spend transaction # Make an anyone-can-spend transaction
@@ -358,11 +369,11 @@ class BIP68Test(BitcoinTestFramework):
tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), DUMMY_P2WPKH_SCRIPT)] tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), DUMMY_P2WPKH_SCRIPT)]
# sign tx2 # sign tx2
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"] tx2_raw = self.nodes[0].signrawtransactionwithwallet(tx2.serialize().hex())["hex"]
tx2 = FromHex(tx2, tx2_raw) tx2 = tx_from_hex(tx2_raw)
tx2.rehash() tx2.rehash()
self.nodes[0].sendrawtransaction(ToHex(tx2)) self.nodes[0].sendrawtransaction(tx2.serialize().hex())
# Now make an invalid spend of tx2 according to BIP68 # Now make an invalid spend of tx2 according to BIP68
sequence_value = 100 # 100 block relative locktime sequence_value = 100 # 100 block relative locktime
@@ -373,7 +384,7 @@ class BIP68Test(BitcoinTestFramework):
tx3.vout = [CTxOut(int(tx2.vout[0].nValue - self.relayfee * COIN), DUMMY_P2WPKH_SCRIPT)] tx3.vout = [CTxOut(int(tx2.vout[0].nValue - self.relayfee * COIN), DUMMY_P2WPKH_SCRIPT)]
tx3.rehash() tx3.rehash()
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx3)) assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, tx3.serialize().hex())
# make a block that violates bip68; ensure that the tip updates # make a block that violates bip68; ensure that the tip updates
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)) block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
@@ -404,9 +415,9 @@ class BIP68Test(BitcoinTestFramework):
outputs = { self.nodes[1].getnewaddress() : 1.0 } outputs = { self.nodes[1].getnewaddress() : 1.0 }
rawtx = self.nodes[1].createrawtransaction(inputs, outputs) rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
rawtxfund = self.nodes[1].fundrawtransaction(rawtx)['hex'] rawtxfund = self.nodes[1].fundrawtransaction(rawtx)['hex']
tx = FromHex(CTransaction(), rawtxfund) tx = tx_from_hex(rawtxfund)
tx.nVersion = 2 tx.nVersion = 2
tx_signed = self.nodes[1].signrawtransactionwithwallet(ToHex(tx))["hex"] tx_signed = self.nodes[1].signrawtransactionwithwallet(tx.serialize().hex())["hex"]
self.nodes[1].sendrawtransaction(tx_signed) self.nodes[1].sendrawtransaction(tx_signed)
if __name__ == '__main__': if __name__ == '__main__':
+2 -3
View File
@@ -22,7 +22,6 @@ from test_framework.messages import (
CTransaction, CTransaction,
CTxIn, CTxIn,
CTxOut, CTxOut,
ToHex,
) )
from test_framework.script import ( from test_framework.script import (
CScript, CScript,
@@ -170,7 +169,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
tx2 = CTransaction() tx2 = CTransaction()
tx2.vin.append(CTxIn(COutPoint(int(tx1_txid, 16), n), b'')) tx2.vin.append(CTxIn(COutPoint(int(tx1_txid, 16), n), b''))
tx2.vout.append(CTxOut(int(20.99 * COIN), CScript([OP_RETURN] + [OP_FALSE]*30))) tx2.vout.append(CTxOut(int(20.99 * COIN), CScript([OP_RETURN] + [OP_FALSE]*30)))
tx2_hex = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))['hex'] tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx2.serialize().hex())['hex']
self.nodes[0].sendrawtransaction(tx2_hex) self.nodes[0].sendrawtransaction(tx2_hex)
# Include both txs in a block # Include both txs in a block
@@ -207,7 +206,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
block_time = self.nodes[0].getblock(tip)['time'] + 1 block_time = self.nodes[0].getblock(tip)['time'] + 1
block = create_block(int(tip, 16), cb, block_time) block = create_block(int(tip, 16), cb, block_time)
block.solve() block.solve()
self.nodes[0].submitblock(ToHex(block)) self.nodes[0].submitblock(block.serialize().hex())
self.sync_all() self.sync_all()
self.wait_until(lambda: not try_rpc(-32603, "Unable to read UTXO set", index_node.gettxoutsetinfo, 'muhash')) self.wait_until(lambda: not try_rpc(-32603, "Unable to read UTXO set", index_node.gettxoutsetinfo, 'muhash'))
+1 -2
View File
@@ -36,7 +36,6 @@ from test_framework.messages import (
CTransaction, CTransaction,
CTxIn, CTxIn,
CTxOut, CTxOut,
ToHex,
) )
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
@@ -208,7 +207,7 @@ class ChainstateWriteCrashTest(BitcoinTestFramework):
tx.vout.append(CTxOut(output_amount, hex_str_to_bytes(utxo['scriptPubKey']))) tx.vout.append(CTxOut(output_amount, hex_str_to_bytes(utxo['scriptPubKey'])))
# Sign and send the transaction to get into the mempool # Sign and send the transaction to get into the mempool
tx_signed_hex = node.signrawtransactionwithwallet(ToHex(tx))['hex'] tx_signed_hex = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
node.sendrawtransaction(tx_signed_hex) node.sendrawtransaction(tx_signed_hex)
num_transactions += 1 num_transactions += 1
+21 -6
View File
@@ -6,8 +6,23 @@
from decimal import Decimal from decimal import Decimal
import random import random
from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN from test_framework.messages import (
from test_framework.script import CScript, OP_1, OP_DROP, OP_2, OP_HASH160, OP_EQUAL, hash160, OP_TRUE COIN,
COutPoint,
CTransaction,
CTxIn,
CTxOut,
)
from test_framework.script import (
CScript,
OP_1,
OP_2,
OP_DROP,
OP_EQUAL,
OP_HASH160,
OP_TRUE,
hash160,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@@ -64,11 +79,11 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
# the ScriptSig that will satisfy the ScriptPubKey. # the ScriptSig that will satisfy the ScriptPubKey.
for inp in tx.vin: for inp in tx.vin:
inp.scriptSig = SCRIPT_SIG[inp.prevout.n] inp.scriptSig = SCRIPT_SIG[inp.prevout.n]
txid = from_node.sendrawtransaction(hexstring=ToHex(tx), maxfeerate=0) txid = from_node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
unconflist.append({"txid": txid, "vout": 0, "amount": total_in - amount - fee}) unconflist.append({"txid": txid, "vout": 0, "amount": total_in - amount - fee})
unconflist.append({"txid": txid, "vout": 1, "amount": amount}) unconflist.append({"txid": txid, "vout": 1, "amount": amount})
return (ToHex(tx), fee) return (tx.serialize().hex(), fee)
def split_inputs(from_node, txins, txouts, initial_split=False): def split_inputs(from_node, txins, txouts, initial_split=False):
@@ -91,10 +106,10 @@ def split_inputs(from_node, txins, txouts, initial_split=False):
# If this is the initial split we actually need to sign the transaction # If this is the initial split we actually need to sign the transaction
# Otherwise we just need to insert the proper ScriptSig # Otherwise we just need to insert the proper ScriptSig
if (initial_split): if (initial_split):
completetx = from_node.signrawtransactionwithwallet(ToHex(tx))["hex"] completetx = from_node.signrawtransactionwithwallet(tx.serialize().hex())["hex"]
else: else:
tx.vin[0].scriptSig = SCRIPT_SIG[prevtxout["vout"]] tx.vin[0].scriptSig = SCRIPT_SIG[prevtxout["vout"]]
completetx = ToHex(tx) completetx = tx.serialize().hex()
txid = from_node.sendrawtransaction(hexstring=completetx, maxfeerate=0) txid = from_node.sendrawtransaction(hexstring=completetx, maxfeerate=0)
txouts.append({"txid": txid, "vout": 0, "amount": half_change}) txouts.append({"txid": txid, "vout": 0, "amount": half_change})
txouts.append({"txid": txid, "vout": 1, "amount": rem_change}) txouts.append({"txid": txid, "vout": 1, "amount": rem_change})
+7 -3
View File
@@ -11,8 +11,12 @@ This test takes 30 mins or more (up to 2 hours)
import os import os
from test_framework.blocktools import create_coinbase from test_framework.blocktools import create_coinbase
from test_framework.messages import CBlock, ToHex from test_framework.messages import CBlock
from test_framework.script import CScript, OP_RETURN, OP_NOP from test_framework.script import (
CScript,
OP_NOP,
OP_RETURN,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@@ -62,7 +66,7 @@ def mine_large_blocks(node, n):
block.solve() block.solve()
# Submit to the node # Submit to the node
node.submitblock(ToHex(block)) node.submitblock(block.serialize().hex())
previousblockhash = block.sha256 previousblockhash = block.sha256
height += 1 height += 1
+42 -39
View File
@@ -7,7 +7,14 @@
from decimal import Decimal from decimal import Decimal
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, BIP125_SEQUENCE_NUMBER from test_framework.messages import (
BIP125_SEQUENCE_NUMBER,
COIN,
COutPoint,
CTransaction,
CTxIn,
CTxOut,
)
from test_framework.script import CScript, OP_DROP from test_framework.script import CScript, OP_DROP
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
@@ -17,10 +24,6 @@ from test_framework.wallet import MiniWallet
MAX_REPLACEMENT_LIMIT = 100 MAX_REPLACEMENT_LIMIT = 100
def txToHex(tx):
return tx.serialize().hex()
def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT): def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
"""Create a txout with a given amount and scriptPubKey """Create a txout with a given amount and scriptPubKey
@@ -44,7 +47,7 @@ def make_utxo(node, amount, confirmed=True, scriptPubKey=DUMMY_P2WPKH_SCRIPT):
tx2.vout = [CTxOut(amount, scriptPubKey)] tx2.vout = [CTxOut(amount, scriptPubKey)]
tx2.rehash() tx2.rehash()
signed_tx = node.signrawtransactionwithwallet(txToHex(tx2)) signed_tx = node.signrawtransactionwithwallet(tx2.serialize().hex())
txid = node.sendrawtransaction(signed_tx['hex'], 0) txid = node.sendrawtransaction(signed_tx['hex'], 0)
@@ -133,7 +136,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1a = CTransaction() tx1a = CTransaction()
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1a_hex = txToHex(tx1a) tx1a_hex = tx1a.serialize().hex()
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0) tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0)
self.sync_all() self.sync_all()
@@ -142,7 +145,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1b.vout = [CTxOut(1 * COIN, DUMMY_2_P2WPKH_SCRIPT)] tx1b.vout = [CTxOut(1 * COIN, DUMMY_2_P2WPKH_SCRIPT)]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
# This will raise an exception due to insufficient fee # This will raise an exception due to insufficient fee
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0)
@@ -151,7 +154,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)] tx1b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
# Works when enabled # Works when enabled
tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, 0) tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, 0)
@@ -176,7 +179,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx = CTransaction() tx = CTransaction()
tx.vin = [CTxIn(prevout, nSequence=0)] tx.vin = [CTxIn(prevout, nSequence=0)]
tx.vout = [CTxOut(remaining_value, CScript([1, OP_DROP] * 15 + [1]))] tx.vout = [CTxOut(remaining_value, CScript([1, OP_DROP] * 15 + [1]))]
tx_hex = txToHex(tx) tx_hex = tx.serialize().hex()
txid = self.nodes[0].sendrawtransaction(tx_hex, 0) txid = self.nodes[0].sendrawtransaction(tx_hex, 0)
chain_txids.append(txid) chain_txids.append(txid)
prevout = COutPoint(int(txid, 16), 0) prevout = COutPoint(int(txid, 16), 0)
@@ -186,7 +189,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
dbl_tx = CTransaction() dbl_tx = CTransaction()
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)] dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
dbl_tx.vout = [CTxOut(initial_nValue - 30 * COIN, DUMMY_P2WPKH_SCRIPT)] dbl_tx.vout = [CTxOut(initial_nValue - 30 * COIN, DUMMY_P2WPKH_SCRIPT)]
dbl_tx_hex = txToHex(dbl_tx) dbl_tx_hex = dbl_tx.serialize().hex()
# This will raise an exception due to insufficient fee # This will raise an exception due to insufficient fee
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0)
@@ -195,7 +198,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
dbl_tx = CTransaction() dbl_tx = CTransaction()
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)] dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
dbl_tx.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] dbl_tx.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
dbl_tx_hex = txToHex(dbl_tx) dbl_tx_hex = dbl_tx.serialize().hex()
self.nodes[0].sendrawtransaction(dbl_tx_hex, 0) self.nodes[0].sendrawtransaction(dbl_tx_hex, 0)
mempool = self.nodes[0].getrawmempool() mempool = self.nodes[0].getrawmempool()
@@ -223,7 +226,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx = CTransaction() tx = CTransaction()
tx.vin = [CTxIn(prevout, nSequence=0)] tx.vin = [CTxIn(prevout, nSequence=0)]
tx.vout = vout tx.vout = vout
tx_hex = txToHex(tx) tx_hex = tx.serialize().hex()
assert len(tx.serialize()) < 100000 assert len(tx.serialize()) < 100000
txid = self.nodes[0].sendrawtransaction(tx_hex, 0) txid = self.nodes[0].sendrawtransaction(tx_hex, 0)
@@ -248,7 +251,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
dbl_tx = CTransaction() dbl_tx = CTransaction()
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)] dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
dbl_tx.vout = [CTxOut(initial_nValue - fee * n, DUMMY_P2WPKH_SCRIPT)] dbl_tx.vout = [CTxOut(initial_nValue - fee * n, DUMMY_P2WPKH_SCRIPT)]
dbl_tx_hex = txToHex(dbl_tx) dbl_tx_hex = dbl_tx.serialize().hex()
# This will raise an exception due to insufficient fee # This will raise an exception due to insufficient fee
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0)
@@ -256,7 +259,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
dbl_tx = CTransaction() dbl_tx = CTransaction()
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)] dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
dbl_tx.vout = [CTxOut(initial_nValue - fee * n - 1 * COIN, DUMMY_P2WPKH_SCRIPT)] dbl_tx.vout = [CTxOut(initial_nValue - fee * n - 1 * COIN, DUMMY_P2WPKH_SCRIPT)]
dbl_tx_hex = txToHex(dbl_tx) dbl_tx_hex = dbl_tx.serialize().hex()
self.nodes[0].sendrawtransaction(dbl_tx_hex, 0) self.nodes[0].sendrawtransaction(dbl_tx_hex, 0)
mempool = self.nodes[0].getrawmempool() mempool = self.nodes[0].getrawmempool()
@@ -276,7 +279,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
dbl_tx = CTransaction() dbl_tx = CTransaction()
dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)] dbl_tx.vin = [CTxIn(tx0_outpoint, nSequence=0)]
dbl_tx.vout = [CTxOut(initial_nValue - 2 * fee * n, DUMMY_P2WPKH_SCRIPT)] dbl_tx.vout = [CTxOut(initial_nValue - 2 * fee * n, DUMMY_P2WPKH_SCRIPT)]
dbl_tx_hex = txToHex(dbl_tx) dbl_tx_hex = dbl_tx.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0) assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0)
@@ -291,7 +294,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1a = CTransaction() tx1a = CTransaction()
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1a_hex = txToHex(tx1a) tx1a_hex = tx1a.serialize().hex()
self.nodes[0].sendrawtransaction(tx1a_hex, 0) self.nodes[0].sendrawtransaction(tx1a_hex, 0)
# Higher fee, but the fee per KB is much lower, so the replacement is # Higher fee, but the fee per KB is much lower, so the replacement is
@@ -299,7 +302,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1b.vout = [CTxOut(int(0.001 * COIN), CScript([b'a' * 999000]))] tx1b.vout = [CTxOut(int(0.001 * COIN), CScript([b'a' * 999000]))]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
# This will raise an exception due to insufficient fee # This will raise an exception due to insufficient fee
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0)
@@ -312,7 +315,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1a = CTransaction() tx1a = CTransaction()
tx1a.vin = [CTxIn(utxo1, nSequence=0)] tx1a.vin = [CTxIn(utxo1, nSequence=0)]
tx1a.vout = [CTxOut(int(1.1 * COIN), DUMMY_P2WPKH_SCRIPT)] tx1a.vout = [CTxOut(int(1.1 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx1a_hex = txToHex(tx1a) tx1a_hex = tx1a.serialize().hex()
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0) tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0)
tx1a_txid = int(tx1a_txid, 16) tx1a_txid = int(tx1a_txid, 16)
@@ -322,7 +325,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0)] tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0)]
tx2.vin.append(CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)) tx2.vin.append(CTxIn(COutPoint(tx1a_txid, 0), nSequence=0))
tx2.vout = tx1a.vout tx2.vout = tx1a.vout
tx2_hex = txToHex(tx2) tx2_hex = tx2.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, 0) assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, 0)
@@ -331,7 +334,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)] tx1b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)]
tx1b.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1b.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, 0) tx1b_txid = self.nodes[0].sendrawtransaction(tx1b_hex, 0)
tx1b_txid = int(tx1b_txid, 16) tx1b_txid = int(tx1b_txid, 16)
@@ -339,7 +342,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0), tx2.vin = [CTxIn(utxo1, nSequence=0), CTxIn(utxo2, nSequence=0),
CTxIn(COutPoint(tx1b_txid, 0))] CTxIn(COutPoint(tx1b_txid, 0))]
tx2.vout = tx1a.vout tx2.vout = tx1a.vout
tx2_hex = txToHex(tx2) tx2_hex = tx2.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, 0) assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, 0)
@@ -352,13 +355,13 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1 = CTransaction() tx1 = CTransaction()
tx1.vin = [CTxIn(confirmed_utxo)] tx1.vin = [CTxIn(confirmed_utxo)]
tx1.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1_hex = txToHex(tx1) tx1_hex = tx1.serialize().hex()
self.nodes[0].sendrawtransaction(tx1_hex, 0) self.nodes[0].sendrawtransaction(tx1_hex, 0)
tx2 = CTransaction() tx2 = CTransaction()
tx2.vin = [CTxIn(confirmed_utxo), CTxIn(unconfirmed_utxo)] tx2.vin = [CTxIn(confirmed_utxo), CTxIn(unconfirmed_utxo)]
tx2.vout = tx1.vout tx2.vout = tx1.vout
tx2_hex = txToHex(tx2) tx2_hex = tx2.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "replacement-adds-unconfirmed", self.nodes[0].sendrawtransaction, tx2_hex, 0) assert_raises_rpc_error(-26, "replacement-adds-unconfirmed", self.nodes[0].sendrawtransaction, tx2_hex, 0)
@@ -381,7 +384,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
splitting_tx = CTransaction() splitting_tx = CTransaction()
splitting_tx.vin = [CTxIn(utxo, nSequence=0)] splitting_tx.vin = [CTxIn(utxo, nSequence=0)]
splitting_tx.vout = outputs splitting_tx.vout = outputs
splitting_tx_hex = txToHex(splitting_tx) splitting_tx_hex = splitting_tx.serialize().hex()
txid = self.nodes[0].sendrawtransaction(splitting_tx_hex, 0) txid = self.nodes[0].sendrawtransaction(splitting_tx_hex, 0)
txid = int(txid, 16) txid = int(txid, 16)
@@ -391,7 +394,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx_i = CTransaction() tx_i = CTransaction()
tx_i.vin = [CTxIn(COutPoint(txid, i), nSequence=0)] tx_i.vin = [CTxIn(COutPoint(txid, i), nSequence=0)]
tx_i.vout = [CTxOut(split_value - fee, DUMMY_P2WPKH_SCRIPT)] tx_i.vout = [CTxOut(split_value - fee, DUMMY_P2WPKH_SCRIPT)]
tx_i_hex = txToHex(tx_i) tx_i_hex = tx_i.serialize().hex()
self.nodes[0].sendrawtransaction(tx_i_hex, 0) self.nodes[0].sendrawtransaction(tx_i_hex, 0)
# Now create doublespend of the whole lot; should fail. # Now create doublespend of the whole lot; should fail.
@@ -404,7 +407,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
double_tx = CTransaction() double_tx = CTransaction()
double_tx.vin = inputs double_tx.vin = inputs
double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))] double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))]
double_tx_hex = txToHex(double_tx) double_tx_hex = double_tx.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, double_tx_hex, 0) assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, double_tx_hex, 0)
@@ -413,7 +416,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
double_tx = CTransaction() double_tx = CTransaction()
double_tx.vin = inputs[0:-1] double_tx.vin = inputs[0:-1]
double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))] double_tx.vout = [CTxOut(double_spend_value, CScript([b'a']))]
double_tx_hex = txToHex(double_tx) double_tx_hex = double_tx.serialize().hex()
self.nodes[0].sendrawtransaction(double_tx_hex, 0) self.nodes[0].sendrawtransaction(double_tx_hex, 0)
def test_opt_in(self): def test_opt_in(self):
@@ -424,7 +427,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1a = CTransaction() tx1a = CTransaction()
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0xffffffff)] tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0xffffffff)]
tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1a_hex = txToHex(tx1a) tx1a_hex = tx1a.serialize().hex()
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0) tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0)
# This transaction isn't shown as replaceable # This transaction isn't shown as replaceable
@@ -434,7 +437,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)] tx1b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx1b_hex, 0) assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx1b_hex, 0)
@@ -445,14 +448,14 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx2a = CTransaction() tx2a = CTransaction()
tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0xfffffffe)] tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0xfffffffe)]
tx2a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx2a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx2a_hex = txToHex(tx2a) tx2a_hex = tx2a.serialize().hex()
tx2a_txid = self.nodes[0].sendrawtransaction(tx2a_hex, 0) tx2a_txid = self.nodes[0].sendrawtransaction(tx2a_hex, 0)
# Still shouldn't be able to double-spend # Still shouldn't be able to double-spend
tx2b = CTransaction() tx2b = CTransaction()
tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)] tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)]
tx2b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)] tx2b.vout = [CTxOut(int(0.9 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx2b_hex = txToHex(tx2b) tx2b_hex = tx2b.serialize().hex()
# This will raise an exception # This will raise an exception
assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx2b_hex, 0) assert_raises_rpc_error(-26, "txn-mempool-conflict", self.nodes[0].sendrawtransaction, tx2b_hex, 0)
@@ -468,7 +471,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx3a.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0xffffffff), tx3a.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0xffffffff),
CTxIn(COutPoint(tx2a_txid, 0), nSequence=0xfffffffd)] CTxIn(COutPoint(tx2a_txid, 0), nSequence=0xfffffffd)]
tx3a.vout = [CTxOut(int(0.9 * COIN), CScript([b'c'])), CTxOut(int(0.9 * COIN), CScript([b'd']))] tx3a.vout = [CTxOut(int(0.9 * COIN), CScript([b'c'])), CTxOut(int(0.9 * COIN), CScript([b'd']))]
tx3a_hex = txToHex(tx3a) tx3a_hex = tx3a.serialize().hex()
tx3a_txid = self.nodes[0].sendrawtransaction(tx3a_hex, 0) tx3a_txid = self.nodes[0].sendrawtransaction(tx3a_hex, 0)
@@ -478,12 +481,12 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx3b = CTransaction() tx3b = CTransaction()
tx3b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)] tx3b.vin = [CTxIn(COutPoint(tx1a_txid, 0), nSequence=0)]
tx3b.vout = [CTxOut(int(0.5 * COIN), DUMMY_P2WPKH_SCRIPT)] tx3b.vout = [CTxOut(int(0.5 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx3b_hex = txToHex(tx3b) tx3b_hex = tx3b.serialize().hex()
tx3c = CTransaction() tx3c = CTransaction()
tx3c.vin = [CTxIn(COutPoint(tx2a_txid, 0), nSequence=0)] tx3c.vin = [CTxIn(COutPoint(tx2a_txid, 0), nSequence=0)]
tx3c.vout = [CTxOut(int(0.5 * COIN), DUMMY_P2WPKH_SCRIPT)] tx3c.vout = [CTxOut(int(0.5 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx3c_hex = txToHex(tx3c) tx3c_hex = tx3c.serialize().hex()
self.nodes[0].sendrawtransaction(tx3b_hex, 0) self.nodes[0].sendrawtransaction(tx3b_hex, 0)
# If tx3b was accepted, tx3c won't look like a replacement, # If tx3b was accepted, tx3c won't look like a replacement,
@@ -500,14 +503,14 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx1a = CTransaction() tx1a = CTransaction()
tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1a.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx1a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx1a_hex = txToHex(tx1a) tx1a_hex = tx1a.serialize().hex()
tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0) tx1a_txid = self.nodes[0].sendrawtransaction(tx1a_hex, 0)
# Higher fee, but the actual fee per KB is much lower. # Higher fee, but the actual fee per KB is much lower.
tx1b = CTransaction() tx1b = CTransaction()
tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)] tx1b.vin = [CTxIn(tx0_outpoint, nSequence=0)]
tx1b.vout = [CTxOut(int(0.001 * COIN), CScript([b'a' * 740000]))] tx1b.vout = [CTxOut(int(0.001 * COIN), CScript([b'a' * 740000]))]
tx1b_hex = txToHex(tx1b) tx1b_hex = tx1b.serialize().hex()
# Verify tx1b cannot replace tx1a. # Verify tx1b cannot replace tx1a.
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx1b_hex, 0)
@@ -526,7 +529,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx2a = CTransaction() tx2a = CTransaction()
tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0)] tx2a.vin = [CTxIn(tx1_outpoint, nSequence=0)]
tx2a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)] tx2a.vout = [CTxOut(1 * COIN, DUMMY_P2WPKH_SCRIPT)]
tx2a_hex = txToHex(tx2a) tx2a_hex = tx2a.serialize().hex()
self.nodes[0].sendrawtransaction(tx2a_hex, 0) self.nodes[0].sendrawtransaction(tx2a_hex, 0)
# Lower fee, but we'll prioritise it # Lower fee, but we'll prioritise it
@@ -534,7 +537,7 @@ class ReplaceByFeeTest(BitcoinTestFramework):
tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)] tx2b.vin = [CTxIn(tx1_outpoint, nSequence=0)]
tx2b.vout = [CTxOut(int(1.01 * COIN), DUMMY_P2WPKH_SCRIPT)] tx2b.vout = [CTxOut(int(1.01 * COIN), DUMMY_P2WPKH_SCRIPT)]
tx2b.rehash() tx2b.rehash()
tx2b_hex = txToHex(tx2b) tx2b_hex = tx2b.serialize().hex()
# Verify tx2b cannot replace tx2a. # Verify tx2b cannot replace tx2a.
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx2b_hex, 0) assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx2b_hex, 0)
+35 -13
View File
@@ -5,7 +5,6 @@
"""Test the SegWit changeover logic.""" """Test the SegWit changeover logic."""
from decimal import Decimal from decimal import Decimal
from io import BytesIO
from test_framework.address import ( from test_framework.address import (
key_to_p2pkh, key_to_p2pkh,
@@ -14,9 +13,34 @@ from test_framework.address import (
script_to_p2sh_p2wsh, script_to_p2sh_p2wsh,
script_to_p2wsh, script_to_p2wsh,
) )
from test_framework.blocktools import witness_script, send_to_witness from test_framework.blocktools import (
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, sha256, ToHex send_to_witness,
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP witness_script,
)
from test_framework.messages import (
COIN,
COutPoint,
CTransaction,
CTxIn,
CTxOut,
sha256,
tx_from_hex,
)
from test_framework.script import (
CScript,
OP_0,
OP_1,
OP_2,
OP_CHECKMULTISIG,
OP_CHECKSIG,
OP_DROP,
OP_DUP,
OP_EQUAL,
OP_EQUALVERIFY,
OP_HASH160,
OP_TRUE,
hash160,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@@ -179,7 +203,7 @@ class SegWitTest(BitcoinTestFramework):
assert self.nodes[1].getblock(blockhash, False) == self.nodes[2].getblock(blockhash, False) assert self.nodes[1].getblock(blockhash, False) == self.nodes[2].getblock(blockhash, False)
for tx_id in segwit_tx_list: for tx_id in segwit_tx_list:
tx = FromHex(CTransaction(), self.nodes[2].gettransaction(tx_id)["hex"]) tx = tx_from_hex(self.nodes[2].gettransaction(tx_id)["hex"])
assert self.nodes[2].getrawtransaction(tx_id, False, blockhash) != self.nodes[0].getrawtransaction(tx_id, False, blockhash) assert self.nodes[2].getrawtransaction(tx_id, False, blockhash) != self.nodes[0].getrawtransaction(tx_id, False, blockhash)
assert self.nodes[1].getrawtransaction(tx_id, False, blockhash) == self.nodes[2].getrawtransaction(tx_id, False, blockhash) assert self.nodes[1].getrawtransaction(tx_id, False, blockhash) == self.nodes[2].getrawtransaction(tx_id, False, blockhash)
assert self.nodes[0].getrawtransaction(tx_id, False, blockhash) != self.nodes[2].gettransaction(tx_id)["hex"] assert self.nodes[0].getrawtransaction(tx_id, False, blockhash) != self.nodes[2].gettransaction(tx_id)["hex"]
@@ -225,12 +249,12 @@ class SegWitTest(BitcoinTestFramework):
# tx1 is allowed to appear in the block, but no others. # tx1 is allowed to appear in the block, but no others.
txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996")) txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
hex_tx = self.nodes[0].gettransaction(txid)['hex'] hex_tx = self.nodes[0].gettransaction(txid)['hex']
tx = FromHex(CTransaction(), hex_tx) tx = tx_from_hex(hex_tx)
assert tx.wit.is_null() # This should not be a segwit input assert tx.wit.is_null() # This should not be a segwit input
assert txid1 in self.nodes[0].getrawmempool() assert txid1 in self.nodes[0].getrawmempool()
tx1_hex = self.nodes[0].gettransaction(txid1)['hex'] tx1_hex = self.nodes[0].gettransaction(txid1)['hex']
tx1 = FromHex(CTransaction(), tx1_hex) tx1 = tx_from_hex(tx1_hex)
# Check that wtxid is properly reported in mempool entry (txid1) # Check that wtxid is properly reported in mempool entry (txid1)
assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True)) assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True))
@@ -243,9 +267,9 @@ class SegWitTest(BitcoinTestFramework):
tx = CTransaction() tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b'')) tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
tx2_hex = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))['hex'] tx2_hex = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())['hex']
txid2 = self.nodes[0].sendrawtransaction(tx2_hex) txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
tx = FromHex(CTransaction(), tx2_hex) tx = tx_from_hex(tx2_hex)
assert not tx.wit.is_null() assert not tx.wit.is_null()
# Check that wtxid is properly reported in mempool entry (txid2) # Check that wtxid is properly reported in mempool entry (txid2)
@@ -260,7 +284,7 @@ class SegWitTest(BitcoinTestFramework):
tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b"")) tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) # Huge fee tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) # Huge fee
tx.calc_sha256() tx.calc_sha256()
txid3 = self.nodes[0].sendrawtransaction(hexstring=ToHex(tx), maxfeerate=0) txid3 = self.nodes[0].sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
assert tx.wit.is_null() assert tx.wit.is_null()
assert txid3 in self.nodes[0].getrawmempool() assert txid3 in self.nodes[0].getrawmempool()
@@ -611,10 +635,8 @@ class SegWitTest(BitcoinTestFramework):
def create_and_mine_tx_from_txids(self, txids, success=True): def create_and_mine_tx_from_txids(self, txids, success=True):
tx = CTransaction() tx = CTransaction()
for i in txids: for i in txids:
txtmp = CTransaction()
txraw = self.nodes[0].getrawtransaction(i, 0, txs_mined[i]) txraw = self.nodes[0].getrawtransaction(i, 0, txs_mined[i])
f = BytesIO(hex_str_to_bytes(txraw)) txtmp = tx_from_hex(txraw)
txtmp.deserialize(f)
for j in range(len(txtmp.vout)): for j in range(len(txtmp.vout)):
tx.vin.append(CTxIn(COutPoint(int('0x' + i, 0), j))) tx.vin.append(CTxIn(COutPoint(int('0x' + i, 0), j)))
tx.vout.append(CTxOut(0, CScript())) tx.vout.append(CTxOut(0, CScript()))
+1 -2
View File
@@ -19,7 +19,6 @@ from test_framework.messages import (
CTxIn, CTxIn,
CTxInWitness, CTxInWitness,
CTxOut, CTxOut,
ToHex,
) )
from test_framework.script import ( from test_framework.script import (
ANNEX_TAG, ANNEX_TAG,
@@ -1306,7 +1305,7 @@ class TaprootTest(BitcoinTestFramework):
# Add change # Add change
fund_tx.vout.append(CTxOut(balance - 10000, random.choice(host_spks))) fund_tx.vout.append(CTxOut(balance - 10000, random.choice(host_spks)))
# Ask the wallet to sign # Ask the wallet to sign
ss = BytesIO(bytes.fromhex(node.signrawtransactionwithwallet(ToHex(fund_tx))["hex"])) ss = BytesIO(bytes.fromhex(node.signrawtransactionwithwallet(fund_tx.serialize().hex())["hex"]))
fund_tx.deserialize(ss) fund_tx.deserialize(ss)
# Construct UTXOData entries # Construct UTXOData entries
fund_tx.rehash() fund_tx.rehash()
+3 -3
View File
@@ -9,7 +9,7 @@ import struct
from test_framework.messages import ( from test_framework.messages import (
CBlock, CBlock,
COutPoint, COutPoint,
FromHex, from_hex,
) )
from test_framework.muhash import MuHash3072 from test_framework.muhash import MuHash3072
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@@ -32,13 +32,13 @@ class UTXOSetHashTest(BitcoinTestFramework):
# Generate 100 blocks and remove the first since we plan to spend its # Generate 100 blocks and remove the first since we plan to spend its
# coinbase # coinbase
block_hashes = wallet.generate(1) + node.generate(99) block_hashes = wallet.generate(1) + node.generate(99)
blocks = list(map(lambda block: FromHex(CBlock(), node.getblock(block, False)), block_hashes)) blocks = list(map(lambda block: from_hex(CBlock(), node.getblock(block, False)), block_hashes))
blocks.pop(0) blocks.pop(0)
# Create a spending transaction and mine a block which includes it # Create a spending transaction and mine a block which includes it
txid = wallet.send_self_transfer(from_node=node)['txid'] txid = wallet.send_self_transfer(from_node=node)['txid']
tx_block = node.generateblock(output=wallet.get_address(), transactions=[txid]) tx_block = node.generateblock(output=wallet.get_address(), transactions=[txid])
blocks.append(FromHex(CBlock(), node.getblock(tx_block['hash'], False))) blocks.append(from_hex(CBlock(), node.getblock(tx_block['hash'], False)))
# Serialize the outputs that should be in the UTXO set and add them to # Serialize the outputs that should be in the UTXO set and add them to
# a MuHash object # a MuHash object
+16 -5
View File
@@ -5,10 +5,21 @@
"""Test the ZMQ notification interface.""" """Test the ZMQ notification interface."""
import struct import struct
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE, ADDRESS_BCRT1_P2WSH_OP_TRUE from test_framework.address import (
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment ADDRESS_BCRT1_P2WSH_OP_TRUE,
ADDRESS_BCRT1_UNSPENDABLE,
)
from test_framework.blocktools import (
add_witness_commitment,
create_block,
create_coinbase,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import CTransaction, hash256, FromHex from test_framework.messages import (
CTransaction,
hash256,
tx_from_hex,
)
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
assert_raises_rpc_error, assert_raises_rpc_error,
@@ -393,10 +404,10 @@ class ZMQTest (BitcoinTestFramework):
bump_info = self.nodes[0].bumpfee(orig_txid) bump_info = self.nodes[0].bumpfee(orig_txid)
# Mine the pre-bump tx # Mine the pre-bump tx
block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1)) block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1))
tx = FromHex(CTransaction(), raw_tx) tx = tx_from_hex(raw_tx)
block.vtx.append(tx) block.vtx.append(tx)
for txid in more_tx: for txid in more_tx:
tx = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid)) tx = tx_from_hex(self.nodes[0].getrawtransaction(txid))
block.vtx.append(tx) block.vtx.append(tx)
add_witness_commitment(block) add_witness_commitment(block)
block.solve() block.solve()
+27 -30
View File
@@ -5,7 +5,6 @@
"""Test mempool acceptance of raw transactions.""" """Test mempool acceptance of raw transactions."""
from decimal import Decimal from decimal import Decimal
from io import BytesIO
import math import math
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
@@ -14,10 +13,10 @@ from test_framework.messages import (
BIP125_SEQUENCE_NUMBER, BIP125_SEQUENCE_NUMBER,
COIN, COIN,
COutPoint, COutPoint,
CTransaction,
CTxOut, CTxOut,
MAX_BLOCK_BASE_SIZE, MAX_BLOCK_BASE_SIZE,
MAX_MONEY, MAX_MONEY,
tx_from_hex,
) )
from test_framework.script import ( from test_framework.script import (
hash160, hash160,
@@ -33,7 +32,6 @@ from test_framework.script import (
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
assert_raises_rpc_error, assert_raises_rpc_error,
hex_str_to_bytes,
) )
@@ -91,8 +89,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
inputs=[{"txid": txid_in_block, "vout": 0, "sequence": BIP125_SEQUENCE_NUMBER}], # RBF is used later inputs=[{"txid": txid_in_block, "vout": 0, "sequence": BIP125_SEQUENCE_NUMBER}], # RBF is used later
outputs=[{node.getnewaddress(): Decimal('0.3') - fee}], outputs=[{node.getnewaddress(): Decimal('0.3') - fee}],
))['hex'] ))['hex']
tx = CTransaction() tx = tx_from_hex(raw_tx_0)
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0)))
txid_0 = tx.rehash() txid_0 = tx.rehash()
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee}}], result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee}}],
@@ -107,7 +104,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
outputs=[{node.getnewaddress(): output_amount}], outputs=[{node.getnewaddress(): output_amount}],
locktime=node.getblockcount() + 2000, # Can be anything locktime=node.getblockcount() + 2000, # Can be anything
))['hex'] ))['hex']
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_final))) tx = tx_from_hex(raw_tx_final)
fee_expected = coin['amount'] - output_amount fee_expected = coin['amount'] - output_amount
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee_expected}}], result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee_expected}}],
@@ -126,11 +123,11 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction that replaces a mempool transaction') self.log.info('A transaction that replaces a mempool transaction')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0))) tx = tx_from_hex(raw_tx_0)
tx.vout[0].nValue -= int(fee * COIN) # Double the fee tx.vout[0].nValue -= int(fee * COIN) # Double the fee
tx.vin[0].nSequence = BIP125_SEQUENCE_NUMBER + 1 # Now, opt out of RBF tx.vin[0].nSequence = BIP125_SEQUENCE_NUMBER + 1 # Now, opt out of RBF
raw_tx_0 = node.signrawtransactionwithwallet(tx.serialize().hex())['hex'] raw_tx_0 = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0))) tx = tx_from_hex(raw_tx_0)
txid_0 = tx.rehash() txid_0 = tx.rehash()
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': (2 * fee)}}], result_expected=[{'txid': txid_0, 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': (2 * fee)}}],
@@ -141,7 +138,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
# Send the transaction that replaces the mempool transaction and opts out of replaceability # Send the transaction that replaces the mempool transaction and opts out of replaceability
node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0) node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
# take original raw_tx_0 # take original raw_tx_0
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0))) tx = tx_from_hex(raw_tx_0)
tx.vout[0].nValue -= int(4 * fee * COIN) # Set more fee tx.vout[0].nValue -= int(4 * fee * COIN) # Set more fee
# skip re-signing the tx # skip re-signing the tx
self.check_mempool_result( self.check_mempool_result(
@@ -151,7 +148,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with missing inputs, that never existed') self.log.info('A transaction with missing inputs, that never existed')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0))) tx = tx_from_hex(raw_tx_0)
tx.vin[0].prevout = COutPoint(hash=int('ff' * 32, 16), n=14) tx.vin[0].prevout = COutPoint(hash=int('ff' * 32, 16), n=14)
# skip re-signing the tx # skip re-signing the tx
self.check_mempool_result( self.check_mempool_result(
@@ -160,7 +157,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with missing inputs, that existed once in the past') self.log.info('A transaction with missing inputs, that existed once in the past')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_0))) tx = tx_from_hex(raw_tx_0)
tx.vin[0].prevout.n = 1 # Set vout to 1, to spend the other outpoint (49 coins) of the in-chain-tx we want to double spend tx.vin[0].prevout.n = 1 # Set vout to 1, to spend the other outpoint (49 coins) of the in-chain-tx we want to double spend
raw_tx_1 = node.signrawtransactionwithwallet(tx.serialize().hex())['hex'] raw_tx_1 = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
txid_1 = node.sendrawtransaction(hexstring=raw_tx_1, maxfeerate=0) txid_1 = node.sendrawtransaction(hexstring=raw_tx_1, maxfeerate=0)
@@ -190,7 +187,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
inputs=[{'txid': txid_spend_both, 'vout': 0}], inputs=[{'txid': txid_spend_both, 'vout': 0}],
outputs=[{node.getnewaddress(): 0.05}], outputs=[{node.getnewaddress(): 0.05}],
))['hex'] ))['hex']
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
# Reference tx should be valid on itself # Reference tx should be valid on itself
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal('0.1') - Decimal('0.05')}}], result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': { 'base': Decimal('0.1') - Decimal('0.05')}}],
@@ -199,17 +196,17 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with no outputs') self.log.info('A transaction with no outputs')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout = [] tx.vout = []
# Skip re-signing the transaction for context independent checks from now on # Skip re-signing the transaction for context independent checks from now on
# tx.deserialize(BytesIO(hex_str_to_bytes(node.signrawtransactionwithwallet(tx.serialize().hex())['hex']))) # tx = tx_from_hex(node.signrawtransactionwithwallet(tx.serialize().hex())['hex'])
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-empty'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-empty'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
self.log.info('A really large transaction') self.log.info('A really large transaction')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_BASE_SIZE / len(tx.vin[0].serialize())) tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_BASE_SIZE / len(tx.vin[0].serialize()))
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-oversize'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-oversize'}],
@@ -217,7 +214,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with negative output value') self.log.info('A transaction with negative output value')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout[0].nValue *= -1 tx.vout[0].nValue *= -1
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-negative'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-negative'}],
@@ -226,7 +223,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
# The following two validations prevent overflow of the output amounts (see CVE-2010-5139). # The following two validations prevent overflow of the output amounts (see CVE-2010-5139).
self.log.info('A transaction with too large output value') self.log.info('A transaction with too large output value')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout[0].nValue = MAX_MONEY + 1 tx.vout[0].nValue = MAX_MONEY + 1
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-toolarge'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-vout-toolarge'}],
@@ -234,7 +231,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with too large sum of output values') self.log.info('A transaction with too large sum of output values')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout = [tx.vout[0]] * 2 tx.vout = [tx.vout[0]] * 2
tx.vout[0].nValue = MAX_MONEY tx.vout[0].nValue = MAX_MONEY
self.check_mempool_result( self.check_mempool_result(
@@ -243,7 +240,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction with duplicate inputs') self.log.info('A transaction with duplicate inputs')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin = [tx.vin[0]] * 2 tx.vin = [tx.vin[0]] * 2
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-inputs-duplicate'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-inputs-duplicate'}],
@@ -253,26 +250,26 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
self.log.info('A coinbase transaction') self.log.info('A coinbase transaction')
# Pick the input of the first tx we signed, so it has to be a coinbase tx # Pick the input of the first tx we signed, so it has to be a coinbase tx
raw_tx_coinbase_spent = node.getrawtransaction(txid=node.decoderawtransaction(hexstring=raw_tx_in_block)['vin'][0]['txid']) raw_tx_coinbase_spent = node.getrawtransaction(txid=node.decoderawtransaction(hexstring=raw_tx_in_block)['vin'][0]['txid'])
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_coinbase_spent))) tx = tx_from_hex(raw_tx_coinbase_spent)
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'coinbase'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'coinbase'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
self.log.info('Some nonstandard transactions') self.log.info('Some nonstandard transactions')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.nVersion = 3 # A version currently non-standard tx.nVersion = 3 # A version currently non-standard
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'version'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'version'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout[0].scriptPubKey = CScript([OP_0]) # Some non-standard script tx.vout[0].scriptPubKey = CScript([OP_0]) # Some non-standard script
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptpubkey'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptpubkey'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
key = ECKey() key = ECKey()
key.generate() key.generate()
pubkey = key.get_pubkey().get_bytes() pubkey = key.get_pubkey().get_bytes()
@@ -281,19 +278,19 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bare-multisig'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin[0].scriptSig = CScript([OP_HASH160]) # Some not-pushonly scriptSig tx.vin[0].scriptSig = CScript([OP_HASH160]) # Some not-pushonly scriptSig
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptsig-not-pushonly'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptsig-not-pushonly'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin[0].scriptSig = CScript([b'a' * 1648]) # Some too large scriptSig (>1650 bytes) tx.vin[0].scriptSig = CScript([b'a' * 1648]) # Some too large scriptSig (>1650 bytes)
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptsig-size'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'scriptsig-size'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=CScript([OP_HASH160, hash160(b'burn'), OP_EQUAL])) output_p2sh_burn = CTxOut(nValue=540, scriptPubKey=CScript([OP_HASH160, hash160(b'burn'), OP_EQUAL]))
num_scripts = 100000 // len(output_p2sh_burn.serialize()) # Use enough outputs to make the tx too large for our policy num_scripts = 100000 // len(output_p2sh_burn.serialize()) # Use enough outputs to make the tx too large for our policy
tx.vout = [output_p2sh_burn] * num_scripts tx.vout = [output_p2sh_burn] * num_scripts
@@ -301,14 +298,14 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'tx-size'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'tx-size'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout[0] = output_p2sh_burn tx.vout[0] = output_p2sh_burn
tx.vout[0].nValue -= 1 # Make output smaller, such that it is dust for our policy tx.vout[0].nValue -= 1 # Make output smaller, such that it is dust for our policy
self.check_mempool_result( self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'dust'}], result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'dust'}],
rawtxs=[tx.serialize().hex()], rawtxs=[tx.serialize().hex()],
) )
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vout[0].scriptPubKey = CScript([OP_RETURN, b'\xff']) tx.vout[0].scriptPubKey = CScript([OP_RETURN, b'\xff'])
tx.vout = [tx.vout[0]] * 2 tx.vout = [tx.vout[0]] * 2
self.check_mempool_result( self.check_mempool_result(
@@ -317,7 +314,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A timelocked transaction') self.log.info('A timelocked transaction')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin[0].nSequence -= 1 # Should be non-max, so locktime is not ignored tx.vin[0].nSequence -= 1 # Should be non-max, so locktime is not ignored
tx.nLockTime = node.getblockcount() + 1 tx.nLockTime = node.getblockcount() + 1
self.check_mempool_result( self.check_mempool_result(
@@ -326,7 +323,7 @@ class MempoolAcceptanceTest(BitcoinTestFramework):
) )
self.log.info('A transaction that is locked by BIP68 sequence logic') self.log.info('A transaction that is locked by BIP68 sequence logic')
tx.deserialize(BytesIO(hex_str_to_bytes(raw_tx_reference))) tx = tx_from_hex(raw_tx_reference)
tx.vin[0].nSequence = 2 # We could include it in the second block mined from now, but not the very next one tx.vin[0].nSequence = 2 # We could include it in the second block mined from now, but not the very next one
# Can skip re-signing the tx because of early rejection # Can skip re-signing the tx because of early rejection
self.check_mempool_result( self.check_mempool_result(
+55 -10
View File
@@ -15,11 +15,56 @@ from test_framework.blocktools import (
add_witness_commitment, add_witness_commitment,
create_block, create_block,
) )
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex from test_framework.messages import (
from test_framework.p2p import p2p_lock, P2PInterface BlockTransactions,
from test_framework.script import CScript, OP_TRUE, OP_DROP BlockTransactionsRequest,
CBlock,
CBlockHeader,
CInv,
COutPoint,
CTransaction,
CTxIn,
CTxInWitness,
CTxOut,
from_hex,
HeaderAndShortIDs,
MSG_BLOCK,
MSG_CMPCT_BLOCK,
MSG_WITNESS_FLAG,
NODE_NETWORK,
P2PHeaderAndShortIDs,
PrefilledTransaction,
calculate_shortid,
msg_block,
msg_blocktxn,
msg_cmpctblock,
msg_getblocktxn,
msg_getdata,
msg_getheaders,
msg_headers,
msg_inv,
msg_no_witness_block,
msg_no_witness_blocktxn,
msg_sendcmpct,
msg_sendheaders,
msg_tx,
ser_uint256,
tx_from_hex,
)
from test_framework.p2p import (
P2PInterface,
p2p_lock,
)
from test_framework.script import (
CScript,
OP_DROP,
OP_TRUE,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, softfork_active from test_framework.util import (
assert_equal,
softfork_active,
)
# TestP2PConn: A peer we use to send messages to bitcoind, and store responses. # TestP2PConn: A peer we use to send messages to bitcoind, and store responses.
class TestP2PConn(P2PInterface): class TestP2PConn(P2PInterface):
@@ -257,7 +302,7 @@ class CompactBlocksTest(BitcoinTestFramework):
for _ in range(num_transactions): for _ in range(num_transactions):
txid = node.sendtoaddress(address, 0.1) txid = node.sendtoaddress(address, 0.1)
hex_tx = node.gettransaction(txid)["hex"] hex_tx = node.gettransaction(txid)["hex"]
tx = FromHex(CTransaction(), hex_tx) tx = tx_from_hex(hex_tx)
if not tx.wit.is_null(): if not tx.wit.is_null():
segwit_tx_generated = True segwit_tx_generated = True
@@ -276,7 +321,7 @@ class CompactBlocksTest(BitcoinTestFramework):
block_hash = int(node.generate(1)[0], 16) block_hash = int(node.generate(1)[0], 16)
# Store the raw block in our internal format. # Store the raw block in our internal format.
block = FromHex(CBlock(), node.getblock("%064x" % block_hash, False)) block = from_hex(CBlock(), node.getblock("%064x" % block_hash, False))
for tx in block.vtx: for tx in block.vtx:
tx.calc_sha256() tx.calc_sha256()
block.rehash() block.rehash()
@@ -569,7 +614,7 @@ class CompactBlocksTest(BitcoinTestFramework):
current_height = chain_height current_height = chain_height
while (current_height >= chain_height - MAX_GETBLOCKTXN_DEPTH): while (current_height >= chain_height - MAX_GETBLOCKTXN_DEPTH):
block_hash = node.getblockhash(current_height) block_hash = node.getblockhash(current_height)
block = FromHex(CBlock(), node.getblock(block_hash, False)) block = from_hex(CBlock(), node.getblock(block_hash, False))
msg = msg_getblocktxn() msg = msg_getblocktxn()
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), []) msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [])
@@ -672,9 +717,9 @@ class CompactBlocksTest(BitcoinTestFramework):
[l.clear_block_announcement() for l in listeners] [l.clear_block_announcement() for l in listeners]
# ToHex() won't serialize with witness, but this block has no witnesses # serialize without witness (this block has no witnesses anyway).
# anyway. TODO: repeat this test with witness tx's to a segwit node. # TODO: repeat this test with witness tx's to a segwit node.
node.submitblock(ToHex(block)) node.submitblock(block.serialize().hex())
for l in listeners: for l in listeners:
l.wait_until(lambda: "cmpctblock" in l.last_message, timeout=30) l.wait_until(lambda: "cmpctblock" in l.last_message, timeout=30)
+3 -3
View File
@@ -6,7 +6,7 @@
from test_framework.messages import ( from test_framework.messages import (
CBlockHeader, CBlockHeader,
FromHex, from_hex,
) )
from test_framework.p2p import ( from test_framework.p2p import (
P2PInterface, P2PInterface,
@@ -42,8 +42,8 @@ class RejectLowDifficultyHeadersTest(BitcoinTestFramework):
self.headers = [l for l in h_lines if not l.startswith(FORK_PREFIX)] self.headers = [l for l in h_lines if not l.startswith(FORK_PREFIX)]
self.headers_fork = [l[len(FORK_PREFIX):] for l in h_lines if l.startswith(FORK_PREFIX)] self.headers_fork = [l[len(FORK_PREFIX):] for l in h_lines if l.startswith(FORK_PREFIX)]
self.headers = [FromHex(CBlockHeader(), h) for h in self.headers] self.headers = [from_hex(CBlockHeader(), h) for h in self.headers]
self.headers_fork = [FromHex(CBlockHeader(), h) for h in self.headers_fork] self.headers_fork = [from_hex(CBlockHeader(), h) for h in self.headers_fork]
self.log.info("Feed all non-fork headers, including and up to the first checkpoint") self.log.info("Feed all non-fork headers, including and up to the first checkpoint")
peer_checkpoint = self.nodes[0].add_p2p_connection(P2PInterface()) peer_checkpoint = self.nodes[0].add_p2p_connection(P2PInterface())
+6 -2
View File
@@ -20,7 +20,11 @@ from test_framework.blocktools import (
create_block, create_block,
create_coinbase, create_coinbase,
) )
from test_framework.messages import CTransaction, FromHex, msg_pong, msg_tx from test_framework.messages import (
msg_pong,
msg_tx,
tx_from_hex,
)
from test_framework.p2p import P2PDataStore, P2PInterface from test_framework.p2p import P2PDataStore, P2PInterface
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.util import assert_equal
@@ -89,7 +93,7 @@ class P2PEvict(BitcoinTestFramework):
'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'], 'scriptPubKey': prevtx['vout'][0]['scriptPubKey']['hex'],
}], }],
)['hex'] )['hex']
txpeer.send_message(msg_tx(FromHex(CTransaction(), sigtx))) txpeer.send_message(msg_tx(tx_from_hex(sigtx)))
protected_peers.add(current_peer) protected_peers.add(current_peer)
self.log.info("Create 8 peers and protect them from eviction by having faster pings") self.log.info("Create 8 peers and protect them from eviction by having faster pings")
+2 -4
View File
@@ -9,9 +9,8 @@ Test that permissions are correctly calculated and applied
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
from test_framework.messages import ( from test_framework.messages import (
CTransaction,
CTxInWitness, CTxInWitness,
FromHex, tx_from_hex,
) )
from test_framework.p2p import P2PDataStore from test_framework.p2p import P2PDataStore
from test_framework.script import ( from test_framework.script import (
@@ -105,8 +104,7 @@ class P2PPermissionsTests(BitcoinTestFramework):
p2p_rebroadcast_wallet = self.nodes[1].add_p2p_connection(P2PDataStore()) p2p_rebroadcast_wallet = self.nodes[1].add_p2p_connection(P2PDataStore())
self.log.debug("Send a tx from the wallet initially") self.log.debug("Send a tx from the wallet initially")
tx = FromHex( tx = tx_from_hex(
CTransaction(),
self.nodes[0].createrawtransaction( self.nodes[0].createrawtransaction(
inputs=[{ inputs=[{
'txid': block_op_true['tx'][0], 'txid': block_op_true['tx'][0],
+3 -3
View File
@@ -40,8 +40,8 @@ from test_framework.messages import (
ser_uint256, ser_uint256,
ser_vector, ser_vector,
sha256, sha256,
tx_from_hex,
uint256_from_str, uint256_from_str,
FromHex,
) )
from test_framework.p2p import ( from test_framework.p2p import (
P2PInterface, P2PInterface,
@@ -2122,14 +2122,14 @@ class SegWitTest(BitcoinTestFramework):
unspent = next(u for u in self.nodes[0].listunspent() if u['spendable'] and u['address'].startswith('bcrt')) unspent = next(u for u in self.nodes[0].listunspent() if u['spendable'] and u['address'].startswith('bcrt'))
raw = self.nodes[0].createrawtransaction([{"txid": unspent['txid'], "vout": unspent['vout']}], {self.nodes[0].getnewaddress(): 1}) raw = self.nodes[0].createrawtransaction([{"txid": unspent['txid'], "vout": unspent['vout']}], {self.nodes[0].getnewaddress(): 1})
tx = FromHex(CTransaction(), raw) tx = tx_from_hex(raw)
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True) assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True)
with self.nodes[0].assert_debug_log(['Superfluous witness record']): with self.nodes[0].assert_debug_log(['Superfluous witness record']):
self.test_node.send_and_ping(msg_bogus_tx(tx)) self.test_node.send_and_ping(msg_bogus_tx(tx))
raw = self.nodes[0].signrawtransactionwithwallet(raw) raw = self.nodes[0].signrawtransactionwithwallet(raw)
assert raw['complete'] assert raw['complete']
raw = raw['hex'] raw = raw['hex']
tx = FromHex(CTransaction(), raw) tx = tx_from_hex(raw)
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True) assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, hexstring=serialize_with_bogus_witness(tx).hex(), iswitness=True)
with self.nodes[0].assert_debug_log(['Unknown transaction optional data']): with self.nodes[0].assert_debug_log(['Unknown transaction optional data']):
self.test_node.send_and_ping(msg_bogus_tx(tx)) self.test_node.send_and_ping(msg_bogus_tx(tx))
+2 -3
View File
@@ -8,13 +8,12 @@ Test transaction download behavior
from test_framework.messages import ( from test_framework.messages import (
CInv, CInv,
CTransaction,
FromHex,
MSG_TX, MSG_TX,
MSG_TYPE_MASK, MSG_TYPE_MASK,
MSG_WTX, MSG_WTX,
msg_inv, msg_inv,
msg_notfound, msg_notfound,
tx_from_hex,
) )
from test_framework.p2p import ( from test_framework.p2p import (
P2PInterface, P2PInterface,
@@ -100,7 +99,7 @@ class TxDownloadTest(BitcoinTestFramework):
hexstring=tx, hexstring=tx,
privkeys=[self.nodes[0].get_deterministic_priv_key().key], privkeys=[self.nodes[0].get_deterministic_priv_key().key],
)['hex'] )['hex']
ctx = FromHex(CTransaction(), tx) ctx = tx_from_hex(tx)
txid = int(ctx.rehash(), 16) txid = int(ctx.rehash(), 16)
self.log.info( self.log.info(
+4 -5
View File
@@ -4,9 +4,9 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test deprecation of reqSigs and addresses RPC fields.""" """Test deprecation of reqSigs and addresses RPC fields."""
from io import BytesIO from test_framework.messages import (
tx_from_hex,
from test_framework.messages import CTransaction )
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
@@ -35,8 +35,7 @@ class AddressesDeprecationTest(BitcoinTestFramework):
signed = node.signrawtransactionwithwallet(raw)['hex'] signed = node.signrawtransactionwithwallet(raw)['hex']
# This transaction is derived from test/util/data/txcreatemultisig1.json # This transaction is derived from test/util/data/txcreatemultisig1.json
tx = CTransaction() tx = tx_from_hex(signed)
tx.deserialize(BytesIO(hex_str_to_bytes(signed)))
tx.vout[0].scriptPubKey = hex_str_to_bytes("522102a5613bd857b7048924264d1e70e08fb2a7e6527d32b7ab1bb993ac59964ff39721021ac43c7ff740014c3b33737ede99c967e4764553d1b2b83db77c83b8715fa72d2102df2089105c77f266fa11a9d33f05c735234075f2e8780824c6b709415f9fb48553ae") tx.vout[0].scriptPubKey = hex_str_to_bytes("522102a5613bd857b7048924264d1e70e08fb2a7e6527d32b7ab1bb993ac59964ff39721021ac43c7ff740014c3b33737ede99c967e4764553d1b2b83db77c83b8715fa72d2102df2089105c77f266fa11a9d33f05c735234075f2e8780824c6b709415f9fb48553ae")
tx_signed = node.signrawtransactionwithwallet(tx.serialize().hex())['hex'] tx_signed = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
txid = node.sendrawtransaction(hexstring=tx_signed, maxfeerate=0) txid = node.sendrawtransaction(hexstring=tx_signed, maxfeerate=0)
+2 -2
View File
@@ -31,7 +31,7 @@ from test_framework.blocktools import (
) )
from test_framework.messages import ( from test_framework.messages import (
CBlockHeader, CBlockHeader,
FromHex, from_hex,
msg_block, msg_block,
) )
from test_framework.p2p import P2PInterface from test_framework.p2p import P2PInterface
@@ -314,7 +314,7 @@ class BlockchainTest(BitcoinTestFramework):
header_hex = node.getblockheader(blockhash=besthash, verbose=False) header_hex = node.getblockheader(blockhash=besthash, verbose=False)
assert_is_hex_string(header_hex) assert_is_hex_string(header_hex)
header = FromHex(CBlockHeader(), header_hex) header = from_hex(CBlockHeader(), header_hex)
header.calc_sha256() header.calc_sha256()
assert_equal(header.hash, besthash) assert_equal(header.hash, besthash)
+9 -5
View File
@@ -4,11 +4,16 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test decoding scripts via decodescript RPC command.""" """Test decoding scripts via decodescript RPC command."""
from test_framework.messages import CTransaction, sha256 from test_framework.messages import (
sha256,
tx_from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, hex_str_to_bytes from test_framework.util import (
assert_equal,
hex_str_to_bytes,
)
from io import BytesIO
class DecodeScriptTest(BitcoinTestFramework): class DecodeScriptTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@@ -179,8 +184,7 @@ class DecodeScriptTest(BitcoinTestFramework):
assert_equal('0 3045022100ae3b4e589dfc9d48cb82d41008dc5fa6a86f94d5c54f9935531924602730ab8002202f88cf464414c4ed9fa11b773c5ee944f66e9b05cc1e51d97abc22ce098937ea[ALL] 3045022100b44883be035600e9328a01b66c7d8439b74db64187e76b99a68f7893b701d5380220225bf286493e4c4adcf928c40f785422572eb232f84a0b83b0dea823c3a19c75[ALL] 5221020743d44be989540d27b1b4bbbcfd17721c337cb6bc9af20eb8a32520b393532f2102c0120a1dda9e51a938d39ddd9fe0ebc45ea97e1d27a7cbd671d5431416d3dd87210213820eb3d5f509d7438c9eeecb4157b2f595105e7cd564b3cdbb9ead3da41eed53ae', rpc_result['vin'][0]['scriptSig']['asm']) assert_equal('0 3045022100ae3b4e589dfc9d48cb82d41008dc5fa6a86f94d5c54f9935531924602730ab8002202f88cf464414c4ed9fa11b773c5ee944f66e9b05cc1e51d97abc22ce098937ea[ALL] 3045022100b44883be035600e9328a01b66c7d8439b74db64187e76b99a68f7893b701d5380220225bf286493e4c4adcf928c40f785422572eb232f84a0b83b0dea823c3a19c75[ALL] 5221020743d44be989540d27b1b4bbbcfd17721c337cb6bc9af20eb8a32520b393532f2102c0120a1dda9e51a938d39ddd9fe0ebc45ea97e1d27a7cbd671d5431416d3dd87210213820eb3d5f509d7438c9eeecb4157b2f595105e7cd564b3cdbb9ead3da41eed53ae', rpc_result['vin'][0]['scriptSig']['asm'])
assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm']) assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm']) assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])
txSave = CTransaction() txSave = tx_from_hex(tx)
txSave.deserialize(BytesIO(hex_str_to_bytes(tx)))
# make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type # make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type
tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000' tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000'
+12 -24
View File
@@ -5,7 +5,6 @@
"""RPCs that handle raw transaction packages.""" """RPCs that handle raw transaction packages."""
from decimal import Decimal from decimal import Decimal
from io import BytesIO
import random import random
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
@@ -13,8 +12,8 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import ( from test_framework.messages import (
BIP125_SEQUENCE_NUMBER, BIP125_SEQUENCE_NUMBER,
COIN, COIN,
CTransaction,
CTxInWitness, CTxInWitness,
tx_from_hex,
) )
from test_framework.script import ( from test_framework.script import (
CScript, CScript,
@@ -22,7 +21,6 @@ from test_framework.script import (
) )
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
hex_str_to_bytes,
) )
class RPCPackagesTest(BitcoinTestFramework): class RPCPackagesTest(BitcoinTestFramework):
@@ -97,9 +95,8 @@ class RPCPackagesTest(BitcoinTestFramework):
"amount": parent_value, "amount": parent_value,
}] if parent_locking_script else None }] if parent_locking_script else None
signedtx = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys, prevtxs=prevtxs) signedtx = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys, prevtxs=prevtxs)
tx = CTransaction()
assert signedtx["complete"] assert signedtx["complete"]
tx.deserialize(BytesIO(hex_str_to_bytes(signedtx["hex"]))) tx = tx_from_hex(signedtx["hex"])
return (tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex()) return (tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex())
def test_independent(self): def test_independent(self):
@@ -110,8 +107,7 @@ class RPCPackagesTest(BitcoinTestFramework):
self.log.info("Test an otherwise valid package with an extra garbage tx appended") self.log.info("Test an otherwise valid package with an extra garbage tx appended")
garbage_tx = node.createrawtransaction([{"txid": "00" * 32, "vout": 5}], {self.address: 1}) garbage_tx = node.createrawtransaction([{"txid": "00" * 32, "vout": 5}], {self.address: 1})
tx = CTransaction() tx = tx_from_hex(garbage_tx)
tx.deserialize(BytesIO(hex_str_to_bytes(garbage_tx)))
# Only the txid and wtxids are returned because validation is incomplete for the independent txns. # Only the txid and wtxids are returned because validation is incomplete for the independent txns.
# Package validation is atomic: if the node cannot find a UTXO for any single tx in the package, # Package validation is atomic: if the node cannot find a UTXO for any single tx in the package,
# it terminates immediately to avoid unnecessary, expensive signature verification. # it terminates immediately to avoid unnecessary, expensive signature verification.
@@ -123,8 +119,7 @@ class RPCPackagesTest(BitcoinTestFramework):
coin = self.coins.pop() coin = self.coins.pop()
tx_bad_sig_hex = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}], tx_bad_sig_hex = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}],
{self.address : coin["amount"] - Decimal("0.0001")}) {self.address : coin["amount"] - Decimal("0.0001")})
tx_bad_sig = CTransaction() tx_bad_sig = tx_from_hex(tx_bad_sig_hex)
tx_bad_sig.deserialize(BytesIO(hex_str_to_bytes(tx_bad_sig_hex)))
testres_bad_sig = node.testmempoolaccept(self.independent_txns_hex + [tx_bad_sig_hex]) testres_bad_sig = node.testmempoolaccept(self.independent_txns_hex + [tx_bad_sig_hex])
# By the time the signature for the last transaction is checked, all the other transactions # By the time the signature for the last transaction is checked, all the other transactions
# have been fully validated, which is why the node returns full validation results for all # have been fully validated, which is why the node returns full validation results for all
@@ -141,8 +136,7 @@ class RPCPackagesTest(BitcoinTestFramework):
{self.address : coin["amount"] - Decimal("0.999")}) {self.address : coin["amount"] - Decimal("0.999")})
tx_high_fee_signed = node.signrawtransactionwithkey(hexstring=tx_high_fee_raw, privkeys=self.privkeys) tx_high_fee_signed = node.signrawtransactionwithkey(hexstring=tx_high_fee_raw, privkeys=self.privkeys)
assert tx_high_fee_signed["complete"] assert tx_high_fee_signed["complete"]
tx_high_fee = CTransaction() tx_high_fee = tx_from_hex(tx_high_fee_signed["hex"])
tx_high_fee.deserialize(BytesIO(hex_str_to_bytes(tx_high_fee_signed["hex"])))
testres_high_fee = node.testmempoolaccept([tx_high_fee_signed["hex"]]) testres_high_fee = node.testmempoolaccept([tx_high_fee_signed["hex"]])
assert_equal(testres_high_fee, [ assert_equal(testres_high_fee, [
{"txid": tx_high_fee.rehash(), "wtxid": tx_high_fee.getwtxid(), "allowed": False, "reject-reason": "max-fee-exceeded"} {"txid": tx_high_fee.rehash(), "wtxid": tx_high_fee.getwtxid(), "allowed": False, "reject-reason": "max-fee-exceeded"}
@@ -198,9 +192,8 @@ class RPCPackagesTest(BitcoinTestFramework):
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
parent_signed = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys) parent_signed = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys)
parent_tx = CTransaction()
assert parent_signed["complete"] assert parent_signed["complete"]
parent_tx.deserialize(BytesIO(hex_str_to_bytes(parent_signed["hex"]))) parent_tx = tx_from_hex(parent_signed["hex"])
parent_txid = parent_tx.rehash() parent_txid = parent_tx.rehash()
assert node.testmempoolaccept([parent_signed["hex"]])[0]["allowed"] assert node.testmempoolaccept([parent_signed["hex"]])[0]["allowed"]
@@ -213,8 +206,7 @@ class RPCPackagesTest(BitcoinTestFramework):
# Child B # Child B
rawtx_b = node.createrawtransaction([{"txid": parent_txid, "vout": 1}], {self.address : child_value}) rawtx_b = node.createrawtransaction([{"txid": parent_txid, "vout": 1}], {self.address : child_value})
tx_child_b = CTransaction() tx_child_b = tx_from_hex(rawtx_b)
tx_child_b.deserialize(BytesIO(hex_str_to_bytes(rawtx_b)))
tx_child_b.wit.vtxinwit = [CTxInWitness()] tx_child_b.wit.vtxinwit = [CTxInWitness()]
tx_child_b.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])] tx_child_b.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
tx_child_b_hex = tx_child_b.serialize().hex() tx_child_b_hex = tx_child_b.serialize().hex()
@@ -293,10 +285,8 @@ class RPCPackagesTest(BitcoinTestFramework):
rawtx2 = node.createrawtransaction(inputs, output2) rawtx2 = node.createrawtransaction(inputs, output2)
signedtx1 = node.signrawtransactionwithkey(hexstring=rawtx1, privkeys=self.privkeys) signedtx1 = node.signrawtransactionwithkey(hexstring=rawtx1, privkeys=self.privkeys)
signedtx2 = node.signrawtransactionwithkey(hexstring=rawtx2, privkeys=self.privkeys) signedtx2 = node.signrawtransactionwithkey(hexstring=rawtx2, privkeys=self.privkeys)
tx1 = CTransaction() tx1 = tx_from_hex(signedtx1["hex"])
tx1.deserialize(BytesIO(hex_str_to_bytes(signedtx1["hex"]))) tx2 = tx_from_hex(signedtx2["hex"])
tx2 = CTransaction()
tx2.deserialize(BytesIO(hex_str_to_bytes(signedtx2["hex"])))
assert signedtx1["complete"] assert signedtx1["complete"]
assert signedtx2["complete"] assert signedtx2["complete"]
@@ -327,19 +317,17 @@ class RPCPackagesTest(BitcoinTestFramework):
raw_replaceable_tx = node.createrawtransaction(inputs, output) raw_replaceable_tx = node.createrawtransaction(inputs, output)
signed_replaceable_tx = node.signrawtransactionwithkey(hexstring=raw_replaceable_tx, privkeys=self.privkeys) signed_replaceable_tx = node.signrawtransactionwithkey(hexstring=raw_replaceable_tx, privkeys=self.privkeys)
testres_replaceable = node.testmempoolaccept([signed_replaceable_tx["hex"]]) testres_replaceable = node.testmempoolaccept([signed_replaceable_tx["hex"]])
replaceable_tx = CTransaction() replaceable_tx = tx_from_hex(signed_replaceable_tx["hex"])
replaceable_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replaceable_tx["hex"])))
assert_equal(testres_replaceable, [ assert_equal(testres_replaceable, [
{"txid": replaceable_tx.rehash(), "wtxid": replaceable_tx.getwtxid(), {"txid": replaceable_tx.rehash(), "wtxid": replaceable_tx.getwtxid(),
"allowed": True, "vsize": replaceable_tx.get_vsize(), "fees": { "base": fee }} "allowed": True, "vsize": replaceable_tx.get_vsize(), "fees": { "base": fee }}
]) ])
# Replacement transaction is identical except has double the fee # Replacement transaction is identical except has double the fee
replacement_tx = CTransaction() replacement_tx = tx_from_hex(signed_replaceable_tx["hex"])
replacement_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replaceable_tx["hex"])))
replacement_tx.vout[0].nValue -= int(fee * COIN) # Doubled fee replacement_tx.vout[0].nValue -= int(fee * COIN) # Doubled fee
signed_replacement_tx = node.signrawtransactionwithkey(replacement_tx.serialize().hex(), self.privkeys) signed_replacement_tx = node.signrawtransactionwithkey(replacement_tx.serialize().hex(), self.privkeys)
replacement_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replacement_tx["hex"]))) replacement_tx = tx_from_hex(signed_replacement_tx["hex"])
self.log.info("Test that transactions within a package cannot replace each other") self.log.info("Test that transactions within a package cannot replace each other")
testres_rbf_conflicting = node.testmempoolaccept([signed_replaceable_tx["hex"], signed_replacement_tx["hex"]]) testres_rbf_conflicting = node.testmempoolaccept([signed_replaceable_tx["hex"], signed_replacement_tx["hex"]])
+9 -9
View File
@@ -14,16 +14,17 @@ Test the following RPCs:
from collections import OrderedDict from collections import OrderedDict
from decimal import Decimal from decimal import Decimal
from io import BytesIO
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import CTransaction, ToHex from test_framework.messages import (
CTransaction,
tx_from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
assert_raises_rpc_error, assert_raises_rpc_error,
find_vout_for_address, find_vout_for_address,
hex_str_to_bytes,
) )
@@ -127,23 +128,22 @@ class RawTransactionsTest(BitcoinTestFramework):
assert_raises_rpc_error(-3, "Expected type bool", self.nodes[0].createrawtransaction, [], {}, 0, 'foo') assert_raises_rpc_error(-3, "Expected type bool", self.nodes[0].createrawtransaction, [], {}, 0, 'foo')
self.log.info('Check that createrawtransaction accepts an array and object as outputs') self.log.info('Check that createrawtransaction accepts an array and object as outputs')
tx = CTransaction()
# One output # One output
tx.deserialize(BytesIO(hex_str_to_bytes(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs={address: 99})))) tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs={address: 99}))
assert_equal(len(tx.vout), 1) assert_equal(len(tx.vout), 1)
assert_equal( assert_equal(
tx.serialize().hex(), tx.serialize().hex(),
self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=[{address: 99}]), self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=[{address: 99}]),
) )
# Two outputs # Two outputs
tx.deserialize(BytesIO(hex_str_to_bytes(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=OrderedDict([(address, 99), (address2, 99)]))))) tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=OrderedDict([(address, 99), (address2, 99)])))
assert_equal(len(tx.vout), 2) assert_equal(len(tx.vout), 2)
assert_equal( assert_equal(
tx.serialize().hex(), tx.serialize().hex(),
self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=[{address: 99}, {address2: 99}]), self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=[{address: 99}, {address2: 99}]),
) )
# Multiple mixed outputs # Multiple mixed outputs
tx.deserialize(BytesIO(hex_str_to_bytes(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=multidict([(address, 99), (address2, 99), ('data', '99')]))))) tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': txid, 'vout': 9}], outputs=multidict([(address, 99), (address2, 99), ('data', '99')])))
assert_equal(len(tx.vout), 3) assert_equal(len(tx.vout), 3)
assert_equal( assert_equal(
tx.serialize().hex(), tx.serialize().hex(),
@@ -450,14 +450,14 @@ class RawTransactionsTest(BitcoinTestFramework):
# As transaction version is unsigned, this should convert to its unsigned equivalent. # As transaction version is unsigned, this should convert to its unsigned equivalent.
tx = CTransaction() tx = CTransaction()
tx.nVersion = -0x80000000 tx.nVersion = -0x80000000
rawtx = ToHex(tx) rawtx = tx.serialize().hex()
decrawtx = self.nodes[0].decoderawtransaction(rawtx) decrawtx = self.nodes[0].decoderawtransaction(rawtx)
assert_equal(decrawtx['version'], 0x80000000) assert_equal(decrawtx['version'], 0x80000000)
# Test the maximum transaction version number that fits in a signed 32-bit integer. # Test the maximum transaction version number that fits in a signed 32-bit integer.
tx = CTransaction() tx = CTransaction()
tx.nVersion = 0x7fffffff tx.nVersion = 0x7fffffff
rawtx = ToHex(tx) rawtx = tx.serialize().hex()
decrawtx = self.nodes[0].decoderawtransaction(rawtx) decrawtx = self.nodes[0].decoderawtransaction(rawtx)
assert_equal(decrawtx['version'], 0x7fffffff) assert_equal(decrawtx['version'], 0x7fffffff)
+36 -11
View File
@@ -5,17 +5,44 @@
"""Test transaction signing using the signrawtransaction* RPCs.""" """Test transaction signing using the signrawtransaction* RPCs."""
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.address import check_script, script_to_p2sh, script_to_p2wsh from test_framework.address import (
check_script,
script_to_p2sh,
script_to_p2wsh,
)
from test_framework.key import ECKey from test_framework.key import ECKey
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, find_vout_for_address, hex_str_to_bytes from test_framework.util import (
from test_framework.messages import sha256, CTransaction, CTxInWitness assert_equal,
from test_framework.script import CScript, OP_0, OP_CHECKSIG, OP_CHECKSEQUENCEVERIFY, OP_CHECKLOCKTIMEVERIFY, OP_DROP, OP_TRUE assert_raises_rpc_error,
from test_framework.script_util import key_to_p2pkh_script, script_to_p2sh_p2wsh_script, script_to_p2wsh_script find_vout_for_address,
hex_str_to_bytes,
)
from test_framework.messages import (
CTxInWitness,
sha256,
tx_from_hex,
)
from test_framework.script import (
CScript,
OP_0,
OP_CHECKLOCKTIMEVERIFY,
OP_CHECKSIG,
OP_CHECKSEQUENCEVERIFY,
OP_DROP,
OP_TRUE,
)
from test_framework.script_util import (
key_to_p2pkh_script,
script_to_p2sh_p2wsh_script,
script_to_p2wsh_script,
)
from test_framework.wallet_util import bytes_to_wif from test_framework.wallet_util import bytes_to_wif
from decimal import Decimal, getcontext from decimal import (
from io import BytesIO Decimal,
getcontext,
)
class SignRawTransactionsTest(BitcoinTestFramework): class SignRawTransactionsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
@@ -265,8 +292,7 @@ class SignRawTransactionsTest(BitcoinTestFramework):
) )
# Set the witness script # Set the witness script
ctx = CTransaction() ctx = tx_from_hex(tx)
ctx.deserialize(BytesIO(hex_str_to_bytes(tx)))
ctx.wit.vtxinwit.append(CTxInWitness()) ctx.wit.vtxinwit.append(CTxInWitness())
ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script] ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script]
tx = ctx.serialize_with_witness().hex() tx = ctx.serialize_with_witness().hex()
@@ -301,8 +327,7 @@ class SignRawTransactionsTest(BitcoinTestFramework):
) )
# Set the witness script # Set the witness script
ctx = CTransaction() ctx = tx_from_hex(tx)
ctx.deserialize(BytesIO(hex_str_to_bytes(tx)))
ctx.wit.vtxinwit.append(CTxInWitness()) ctx.wit.vtxinwit.append(CTxInWitness())
ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script] ctx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE]), script]
tx = ctx.serialize_with_witness().hex() tx = ctx.serialize_with_witness().hex()
+11 -5
View File
@@ -5,9 +5,15 @@
"""Test gettxoutproof and verifytxoutproof RPCs.""" """Test gettxoutproof and verifytxoutproof RPCs."""
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.messages import CMerkleBlock, FromHex, ToHex from test_framework.messages import (
CMerkleBlock,
from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
from test_framework.wallet import MiniWallet from test_framework.wallet import MiniWallet
@@ -88,10 +94,10 @@ class MerkleBlockTest(BitcoinTestFramework):
assert txid1 in self.nodes[0].verifytxoutproof(proof) assert txid1 in self.nodes[0].verifytxoutproof(proof)
assert txid2 in self.nodes[1].verifytxoutproof(proof) assert txid2 in self.nodes[1].verifytxoutproof(proof)
tweaked_proof = FromHex(CMerkleBlock(), proof) tweaked_proof = from_hex(CMerkleBlock(), proof)
# Make sure that our serialization/deserialization is working # Make sure that our serialization/deserialization is working
assert txid1 in self.nodes[0].verifytxoutproof(ToHex(tweaked_proof)) assert txid1 in self.nodes[0].verifytxoutproof(tweaked_proof.serialize().hex())
# Check to see if we can go up the merkle tree and pass this off as a # Check to see if we can go up the merkle tree and pass this off as a
# single-transaction block # single-transaction block
@@ -100,7 +106,7 @@ class MerkleBlockTest(BitcoinTestFramework):
tweaked_proof.txn.vBits = [True] + [False]*7 tweaked_proof.txn.vBits = [True] + [False]*7
for n in self.nodes: for n in self.nodes:
assert not n.verifytxoutproof(ToHex(tweaked_proof)) assert not n.verifytxoutproof(tweaked_proof.serialize().hex())
# TODO: try more variants, eg transactions at different depths, and # TODO: try more variants, eg transactions at different depths, and
# verify that the proofs are invalid # verify that the proofs are invalid
+5 -6
View File
@@ -23,12 +23,11 @@ from .messages import (
CTxIn, CTxIn,
CTxInWitness, CTxInWitness,
CTxOut, CTxOut,
FromHex,
ToHex,
hash256, hash256,
hex_str_to_bytes, hex_str_to_bytes,
ser_uint256, ser_uint256,
sha256, sha256,
tx_from_hex,
uint256_from_str, uint256_from_str,
) )
from .script import ( from .script import (
@@ -79,7 +78,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
if txlist: if txlist:
for tx in txlist: for tx in txlist:
if not hasattr(tx, 'calc_sha256'): if not hasattr(tx, 'calc_sha256'):
tx = FromHex(CTransaction(), tx) tx = tx_from_hex(tx)
block.vtx.append(tx) block.vtx.append(tx)
block.hashMerkleRoot = block.calc_merkle_root() block.hashMerkleRoot = block.calc_merkle_root()
block.calc_sha256() block.calc_sha256()
@@ -166,7 +165,7 @@ def create_transaction(node, txid, to_address, *, amount):
sign for the output that is being spent. sign for the output that is being spent.
""" """
raw_tx = create_raw_transaction(node, txid, to_address, amount=amount) raw_tx = create_raw_transaction(node, txid, to_address, amount=amount)
tx = FromHex(CTransaction(), raw_tx) tx = tx_from_hex(raw_tx)
return tx return tx
def create_raw_transaction(node, txid, to_address, *, amount): def create_raw_transaction(node, txid, to_address, *, amount):
@@ -243,9 +242,9 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru
return node.sendrawtransaction(signed["hex"]) return node.sendrawtransaction(signed["hex"])
else: else:
if (insert_redeem_script): if (insert_redeem_script):
tx = FromHex(CTransaction(), tx_to_witness) tx = tx_from_hex(tx_to_witness)
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)]) tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
tx_to_witness = ToHex(tx) tx_to_witness = tx.serialize().hex()
return node.sendrawtransaction(tx_to_witness) return node.sendrawtransaction(tx_to_witness)
+11 -5
View File
@@ -190,14 +190,20 @@ def ser_string_vector(l):
return r return r
# Deserialize from a hex string representation (eg from RPC) def from_hex(obj, hex_string):
def FromHex(obj, hex_string): """Deserialize from a hex string representation (e.g. from RPC)
Note that there is no complementary helper like e.g. `to_hex` for the
inverse operation. To serialize a message object to a hex string, simply
use obj.serialize().hex()"""
obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) obj.deserialize(BytesIO(hex_str_to_bytes(hex_string)))
return obj return obj
# Convert a binary-serializable object to hex (eg for submission via RPC)
def ToHex(obj): def tx_from_hex(hex_string):
return obj.serialize().hex() """Deserialize from hex string to a transaction object"""
return from_hex(CTransaction(), hex_string)
# Objects that map to bitcoind objects, which can be serialized/deserialized # Objects that map to bitcoind objects, which can be serialized/deserialized
+2 -4
View File
@@ -19,7 +19,6 @@ import unittest
from . import coverage from . import coverage
from .authproxy import AuthServiceProxy, JSONRPCException from .authproxy import AuthServiceProxy, JSONRPCException
from io import BytesIO
from typing import Callable, Optional from typing import Callable, Optional
logger = logging.getLogger("TestFramework.utils") logger = logging.getLogger("TestFramework.utils")
@@ -528,7 +527,7 @@ def gen_return_txouts():
def create_lots_of_big_transactions(node, txouts, utxos, num, fee): def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
addr = node.getnewaddress() addr = node.getnewaddress()
txids = [] txids = []
from .messages import CTransaction from .messages import tx_from_hex
for _ in range(num): for _ in range(num):
t = utxos.pop() t = utxos.pop()
inputs = [{"txid": t["txid"], "vout": t["vout"]}] inputs = [{"txid": t["txid"], "vout": t["vout"]}]
@@ -536,8 +535,7 @@ def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
change = t['amount'] - fee change = t['amount'] - fee
outputs[addr] = satoshi_round(change) outputs[addr] = satoshi_round(change)
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
tx = CTransaction() tx = tx_from_hex(rawtx)
tx.deserialize(BytesIO(hex_str_to_bytes(rawtx)))
for txout in txouts: for txout in txouts:
tx.vout.append(txout) tx.vout.append(txout)
newtx = tx.serialize().hex() newtx = tx.serialize().hex()
+12 -8
View File
@@ -14,17 +14,23 @@ added in the future, they should try to follow the same convention and not
make assumptions about execution order. make assumptions about execution order.
""" """
from decimal import Decimal from decimal import Decimal
import io
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import (
from test_framework.blocktools import add_witness_commitment, create_block, create_coinbase, send_to_witness COINBASE_MATURITY,
from test_framework.messages import BIP125_SEQUENCE_NUMBER, CTransaction add_witness_commitment,
create_block,
create_coinbase,
send_to_witness,
)
from test_framework.messages import (
BIP125_SEQUENCE_NUMBER,
tx_from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
assert_greater_than, assert_greater_than,
assert_raises_rpc_error, assert_raises_rpc_error,
hex_str_to_bytes,
) )
WALLET_PASSPHRASE = "test" WALLET_PASSPHRASE = "test"
@@ -576,9 +582,7 @@ def spend_one_input(node, dest_address, change_size=Decimal("0.00049000")):
def submit_block_with_tx(node, tx): def submit_block_with_tx(node, tx):
ctx = CTransaction() ctx = tx_from_hex(tx)
ctx.deserialize(io.BytesIO(hex_str_to_bytes(tx)))
tip = node.getbestblockhash() tip = node.getbestblockhash()
height = node.getblockcount() + 1 height = node.getblockcount() + 1
block_time = node.getblockheader(tip)["mediantime"] + 1 block_time = node.getblockheader(tip)["mediantime"] + 1
+5 -3
View File
@@ -6,7 +6,9 @@
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import CTransaction, FromHex, ToHex from test_framework.messages import (
tx_from_hex,
)
from test_framework.util import ( from test_framework.util import (
assert_approx, assert_approx,
assert_equal, assert_equal,
@@ -154,10 +156,10 @@ class WalletGroupTest(BitcoinTestFramework):
self.log.info("Fill a wallet with 10,000 outputs corresponding to the same scriptPubKey") self.log.info("Fill a wallet with 10,000 outputs corresponding to the same scriptPubKey")
for _ in range(5): for _ in range(5):
raw_tx = self.nodes[0].createrawtransaction([{"txid":"0"*64, "vout":0}], [{addr2[0]: 0.05}]) raw_tx = self.nodes[0].createrawtransaction([{"txid":"0"*64, "vout":0}], [{addr2[0]: 0.05}])
tx = FromHex(CTransaction(), raw_tx) tx = tx_from_hex(raw_tx)
tx.vin = [] tx.vin = []
tx.vout = [tx.vout[0]] * 2000 tx.vout = [tx.vout[0]] * 2000
funded_tx = self.nodes[0].fundrawtransaction(ToHex(tx)) funded_tx = self.nodes[0].fundrawtransaction(tx.serialize().hex())
signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx['hex']) signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx['hex'])
self.nodes[0].sendrawtransaction(signed_tx['hex']) self.nodes[0].sendrawtransaction(signed_tx['hex'])
self.nodes[0].generate(1) self.nodes[0].generate(1)
+4 -9
View File
@@ -4,22 +4,17 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the listtransactions API.""" """Test the listtransactions API."""
from decimal import Decimal from decimal import Decimal
from io import BytesIO
from test_framework.messages import COIN, CTransaction from test_framework.messages import (
COIN,
tx_from_hex,
)
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_array_result, assert_array_result,
assert_equal, assert_equal,
hex_str_to_bytes,
) )
def tx_from_hex(hexstring):
tx = CTransaction()
f = BytesIO(hex_str_to_bytes(hexstring))
tx.deserialize(f)
return tx
class ListTransactionsTest(BitcoinTestFramework): class ListTransactionsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.num_nodes = 2 self.num_nodes = 2
@@ -5,8 +5,10 @@
"""Test that the wallet resends transactions periodically.""" """Test that the wallet resends transactions periodically."""
import time import time
from test_framework.blocktools import create_block, create_coinbase from test_framework.blocktools import (
from test_framework.messages import ToHex create_block,
create_coinbase,
)
from test_framework.p2p import P2PTxInvStore from test_framework.p2p import P2PTxInvStore
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.util import assert_equal
@@ -48,7 +50,7 @@ class ResendWalletTransactionsTest(BitcoinTestFramework):
block = create_block(int(node.getbestblockhash(), 16), create_coinbase(node.getblockcount() + 1), block_time) block = create_block(int(node.getbestblockhash(), 16), create_coinbase(node.getblockcount() + 1), block_time)
block.rehash() block.rehash()
block.solve() block.solve()
node.submitblock(ToHex(block)) node.submitblock(block.serialize().hex())
# Set correct m_best_block_time, which is used in ResendWalletTransactions # Set correct m_best_block_time, which is used in ResendWalletTransactions
node.syncwithvalidationinterfacequeue() node.syncwithvalidationinterfacequeue()
+5 -4
View File
@@ -4,12 +4,14 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the wallet accounts properly when there are cloned transactions with malleated scriptsigs.""" """Test the wallet accounts properly when there are cloned transactions with malleated scriptsigs."""
import io
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
assert_equal, assert_equal,
) )
from test_framework.messages import CTransaction, COIN from test_framework.messages import (
COIN,
tx_from_hex,
)
class TxnMallTest(BitcoinTestFramework): class TxnMallTest(BitcoinTestFramework):
@@ -71,8 +73,7 @@ class TxnMallTest(BitcoinTestFramework):
clone_raw = self.nodes[0].createrawtransaction(clone_inputs, clone_outputs, clone_locktime) clone_raw = self.nodes[0].createrawtransaction(clone_inputs, clone_outputs, clone_locktime)
# createrawtransaction randomizes the order of its outputs, so swap them if necessary. # createrawtransaction randomizes the order of its outputs, so swap them if necessary.
clone_tx = CTransaction() clone_tx = tx_from_hex(clone_raw)
clone_tx.deserialize(io.BytesIO(bytes.fromhex(clone_raw)))
if (rawtx1["vout"][0]["value"] == 40 and clone_tx.vout[0].nValue != 40*COIN or rawtx1["vout"][0]["value"] != 40 and clone_tx.vout[0].nValue == 40*COIN): if (rawtx1["vout"][0]["value"] == 40 and clone_tx.vout[0].nValue != 40*COIN or rawtx1["vout"][0]["value"] != 40 and clone_tx.vout[0].nValue == 40*COIN):
(clone_tx.vout[0], clone_tx.vout[1]) = (clone_tx.vout[1], clone_tx.vout[0]) (clone_tx.vout[0], clone_tx.vout[1]) = (clone_tx.vout[1], clone_tx.vout[0])