Skip to content

Commit d9bafca

Browse files
author
MarcoFalke
committed
Merge bitcoin#15982: tests: Make msg_block a witness block
fa1d766 tests: Make msg_block a witness block (MarcoFalke) fa52eb5 test: Remove True argument to CBlock::serialize (MarcoFalke) Pull request description: Unnamed arguments are confusing as to what they mean without looking up the function signature. Since segwit is active by default in regtest, and all blocks are serialized with witness (bitcoin#15664), remove the argument `with_witness=True` from all calls to `CBlock::serialize` and `BlockTransactions::serialize`. ACKs for commit fa1d76: laanwj: code-review ACK fa1d766 Tree-SHA512: 2c550646f99c9ca86a223ca988c61a730f5e6646807adeaa7174fb2424a32cea3fef8bcd3e0b12e162e7ff192877d0c02fd0654df6ee1a9b821b065707c2dcbc
2 parents fce4123 + fa1d766 commit d9bafca

File tree

6 files changed

+39
-42
lines changed

6 files changed

+39
-42
lines changed

test/functional/feature_bip68_sequence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def test_bip68_not_consensus(self):
378378
add_witness_commitment(block)
379379
block.solve()
380380

381-
self.nodes[0].submitblock(block.serialize(True).hex())
381+
self.nodes[0].submitblock(block.serialize().hex())
382382
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
383383

384384
def activateCSV(self):

test/functional/feature_nulldummy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def block_submit(self, node, txs, witness=False, accept=False):
108108
witness and add_witness_commitment(block)
109109
block.rehash()
110110
block.solve()
111-
node.submitblock(block.serialize(True).hex())
111+
node.submitblock(block.serialize().hex())
112112
if (accept):
113113
assert_equal(node.getbestblockhash(), block.hash)
114114
self.tip = block.sha256

test/functional/p2p_compactblocks.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import random
1111

1212
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
13-
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_block, msg_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_witness_block, msg_witness_blocktxn, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
13+
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_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
1414
from test_framework.mininode import mininode_lock, P2PInterface
1515
from test_framework.script import CScript, OP_TRUE, OP_DROP
1616
from test_framework.test_framework import BitcoinTestFramework
@@ -114,7 +114,7 @@ def build_block_on_tip(self, node, segwit=False):
114114
# Create 10 more anyone-can-spend utxo's for testing.
115115
def make_utxos(self):
116116
block = self.build_block_on_tip(self.nodes[0])
117-
self.segwit_node.send_and_ping(msg_block(block))
117+
self.segwit_node.send_and_ping(msg_no_witness_block(block))
118118
assert int(self.nodes[0].getbestblockhash(), 16) == block.sha256
119119
self.nodes[0].generatetoaddress(100, self.nodes[0].getnewaddress(address_type="bech32"))
120120

@@ -130,7 +130,7 @@ def make_utxos(self):
130130
block2.vtx.append(tx)
131131
block2.hashMerkleRoot = block2.calc_merkle_root()
132132
block2.solve()
133-
self.segwit_node.send_and_ping(msg_block(block2))
133+
self.segwit_node.send_and_ping(msg_no_witness_block(block2))
134134
assert_equal(int(self.nodes[0].getbestblockhash(), 16), block2.sha256)
135135
self.utxos.extend([[tx.sha256, i, out_value] for i in range(10)])
136136

@@ -408,9 +408,9 @@ def test_compactblock_requests(self, test_node, segwit=True):
408408

409409
# Send the coinbase, and verify that the tip advances.
410410
if version == 2:
411-
msg = msg_witness_blocktxn()
412-
else:
413411
msg = msg_blocktxn()
412+
else:
413+
msg = msg_no_witness_blocktxn()
414414
msg.block_transactions.blockhash = block.sha256
415415
msg.block_transactions.transactions = [block.vtx[0]]
416416
test_node.send_and_ping(msg)
@@ -463,9 +463,9 @@ def test_tip_after_message(node, peer, msg, tip):
463463

464464
test_getblocktxn_response(comp_block, test_node, [1, 2, 3, 4, 5])
465465

466-
msg_bt = msg_blocktxn()
466+
msg_bt = msg_no_witness_blocktxn()
467467
if with_witness:
468-
msg_bt = msg_witness_blocktxn() # serialize with witnesses
468+
msg_bt = msg_blocktxn() # serialize with witnesses
469469
msg_bt.block_transactions = BlockTransactions(block.sha256, block.vtx[1:])
470470
test_tip_after_message(node, test_node, msg_bt, block.sha256)
471471

@@ -554,9 +554,9 @@ def test_incorrect_blocktxn_response(self, test_node):
554554
# different peer provide the block further down, so that we're still
555555
# verifying that the block isn't marked bad permanently. This is good
556556
# enough for now.
557-
msg = msg_blocktxn()
557+
msg = msg_no_witness_blocktxn()
558558
if version == 2:
559-
msg = msg_witness_blocktxn()
559+
msg = msg_blocktxn()
560560
msg.block_transactions = BlockTransactions(block.sha256, [block.vtx[5]] + block.vtx[7:])
561561
test_node.send_and_ping(msg)
562562

@@ -571,9 +571,9 @@ def test_incorrect_blocktxn_response(self, test_node):
571571

572572
# Deliver the block
573573
if version == 2:
574-
test_node.send_and_ping(msg_witness_block(block))
575-
else:
576574
test_node.send_and_ping(msg_block(block))
575+
else:
576+
test_node.send_and_ping(msg_no_witness_block(block))
577577
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
578578

579579
def test_getblocktxn_handler(self, test_node):
@@ -785,7 +785,7 @@ def announce_cmpct_block(node, peer):
785785
delivery_peer.send_and_ping(msg_cmpctblock(cmpct_block.to_p2p()))
786786
assert int(node.getbestblockhash(), 16) != block.sha256
787787

788-
msg = msg_blocktxn()
788+
msg = msg_no_witness_blocktxn()
789789
msg.block_transactions.blockhash = block.sha256
790790
msg.block_transactions.transactions = block.vtx[1:]
791791
stalling_peer.send_and_ping(msg)

test/functional/p2p_segwit.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
MSG_WITNESS_FLAG,
2626
NODE_NETWORK,
2727
NODE_WITNESS,
28-
msg_block,
28+
msg_no_witness_block,
2929
msg_getdata,
3030
msg_headers,
3131
msg_inv,
3232
msg_tx,
33-
msg_witness_block,
33+
msg_block,
3434
msg_witness_tx,
3535
ser_uint256,
3636
ser_vector,
@@ -111,7 +111,7 @@ def get_virtual_size(witness_block):
111111
112112
Virtual size is base + witness/4."""
113113
base_size = len(witness_block.serialize(with_witness=False))
114-
total_size = len(witness_block.serialize(with_witness=True))
114+
total_size = len(witness_block.serialize())
115115
# the "+3" is so we round up
116116
vsize = int((3 * base_size + total_size + 3) / 4)
117117
return vsize
@@ -134,7 +134,7 @@ def test_witness_block(node, p2p, block, accepted, with_witness=True, reason=Non
134134
- use the getbestblockhash rpc to check for acceptance."""
135135
reason = [reason] if reason else []
136136
with node.assert_debug_log(expected_msgs=reason):
137-
p2p.send_message(msg_witness_block(block) if with_witness else msg_block(block))
137+
p2p.send_message(msg_block(block) if with_witness else msg_no_witness_block(block))
138138
p2p.sync_with_ping()
139139
assert_equal(node.getbestblockhash() == block.hash, accepted)
140140

@@ -298,7 +298,7 @@ def test_non_witness_transaction(self):
298298

299299
block = self.build_next_block(version=1)
300300
block.solve()
301-
self.test_node.send_message(msg_block(block))
301+
self.test_node.send_message(msg_no_witness_block(block))
302302
self.test_node.sync_with_ping() # make sure the block was processed
303303
txid = block.vtx[0].sha256
304304

@@ -345,7 +345,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
345345

346346
# But it should not be permanently marked bad...
347347
# Resend without witness information.
348-
self.test_node.send_message(msg_block(block))
348+
self.test_node.send_message(msg_no_witness_block(block))
349349
self.test_node.sync_with_ping()
350350
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
351351

@@ -403,7 +403,7 @@ def test_block_relay(self):
403403
block_hash = int(block_hash, 16)
404404
block = self.test_node.request_block(block_hash, 2)
405405
wit_block = self.test_node.request_block(block_hash, 2 | MSG_WITNESS_FLAG)
406-
assert_equal(block.serialize(True), wit_block.serialize(True))
406+
assert_equal(block.serialize(), wit_block.serialize())
407407
assert_equal(block.serialize(), hex_str_to_bytes(rpc_block))
408408
else:
409409
# After activation, witness blocks and non-witness blocks should
@@ -419,15 +419,15 @@ def test_block_relay(self):
419419
rpc_block = self.nodes[0].getblock(block.hash, False)
420420
non_wit_block = self.test_node.request_block(block.sha256, 2)
421421
wit_block = self.test_node.request_block(block.sha256, 2 | MSG_WITNESS_FLAG)
422-
assert_equal(wit_block.serialize(True), hex_str_to_bytes(rpc_block))
422+
assert_equal(wit_block.serialize(), hex_str_to_bytes(rpc_block))
423423
assert_equal(wit_block.serialize(False), non_wit_block.serialize())
424-
assert_equal(wit_block.serialize(True), block.serialize(True))
424+
assert_equal(wit_block.serialize(), block.serialize())
425425

426426
# Test size, vsize, weight
427427
rpc_details = self.nodes[0].getblock(block.hash, True)
428-
assert_equal(rpc_details["size"], len(block.serialize(True)))
428+
assert_equal(rpc_details["size"], len(block.serialize()))
429429
assert_equal(rpc_details["strippedsize"], len(block.serialize(False)))
430-
weight = 3 * len(block.serialize(False)) + len(block.serialize(True))
430+
weight = 3 * len(block.serialize(False)) + len(block.serialize())
431431
assert_equal(rpc_details["weight"], weight)
432432

433433
# Upgraded node should not ask for blocks from unupgraded
@@ -791,7 +791,7 @@ def test_witness_commitments(self):
791791
block.solve()
792792

793793
# Test the test -- witness serialization should be different
794-
assert msg_witness_block(block).serialize() != msg_block(block).serialize()
794+
assert msg_block(block).serialize() != msg_no_witness_block(block).serialize()
795795

796796
# This empty block should be valid.
797797
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
@@ -884,13 +884,13 @@ def test_block_malleability(self):
884884

885885
# We can't send over the p2p network, because this is too big to relay
886886
# TODO: repeat this test with a block that can be relayed
887-
self.nodes[0].submitblock(block.serialize(True).hex())
887+
self.nodes[0].submitblock(block.serialize().hex())
888888

889889
assert self.nodes[0].getbestblockhash() != block.hash
890890

891891
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.pop()
892892
assert get_virtual_size(block) < MAX_BLOCK_BASE_SIZE
893-
self.nodes[0].submitblock(block.serialize(True).hex())
893+
self.nodes[0].submitblock(block.serialize().hex())
894894

895895
assert self.nodes[0].getbestblockhash() == block.hash
896896

@@ -969,7 +969,7 @@ def test_witness_block_size(self):
969969
assert_equal(vsize, MAX_BLOCK_BASE_SIZE + 1)
970970
# Make sure that our test case would exceed the old max-network-message
971971
# limit
972-
assert len(block.serialize(True)) > 2 * 1024 * 1024
972+
assert len(block.serialize()) > 2 * 1024 * 1024
973973

974974
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
975975

@@ -997,14 +997,14 @@ def test_submit_block(self):
997997
add_witness_commitment(block, nonce=1)
998998
block.vtx[0].wit = CTxWitness() # drop the nonce
999999
block.solve()
1000-
self.nodes[0].submitblock(block.serialize(True).hex())
1000+
self.nodes[0].submitblock(block.serialize().hex())
10011001
assert self.nodes[0].getbestblockhash() != block.hash
10021002

10031003
# Now redo commitment with the standard nonce, but let bitcoind fill it in.
10041004
add_witness_commitment(block, nonce=0)
10051005
block.vtx[0].wit = CTxWitness()
10061006
block.solve()
1007-
self.nodes[0].submitblock(block.serialize(True).hex())
1007+
self.nodes[0].submitblock(block.serialize().hex())
10081008
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
10091009

10101010
# This time, add a tx with non-empty witness, but don't supply
@@ -1019,7 +1019,7 @@ def test_submit_block(self):
10191019
block_2.vtx[0].vout.pop()
10201020
block_2.vtx[0].wit = CTxWitness()
10211021

1022-
self.nodes[0].submitblock(block_2.serialize(True).hex())
1022+
self.nodes[0].submitblock(block_2.serialize().hex())
10231023
# Tip should not advance!
10241024
assert self.nodes[0].getbestblockhash() != block_2.hash
10251025

test/functional/test_framework/messages.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ def deserialize(self, f):
11301130
self.block.deserialize(f)
11311131

11321132
def serialize(self):
1133-
return self.block.serialize(with_witness=False)
1133+
return self.block.serialize()
11341134

11351135
def __repr__(self):
11361136
return "msg_block(block=%s)" % (repr(self.block))
@@ -1152,11 +1152,10 @@ def __repr__(self):
11521152
return "msg_generic()"
11531153

11541154

1155-
class msg_witness_block(msg_block):
1155+
class msg_no_witness_block(msg_block):
11561156
__slots__ = ()
11571157
def serialize(self):
1158-
r = self.block.serialize(with_witness=True)
1159-
return r
1158+
return self.block.serialize(with_witness=False)
11601159

11611160

11621161
class msg_getaddr:
@@ -1442,17 +1441,15 @@ def deserialize(self, f):
14421441

14431442
def serialize(self):
14441443
r = b""
1445-
r += self.block_transactions.serialize(with_witness=False)
1444+
r += self.block_transactions.serialize()
14461445
return r
14471446

14481447
def __repr__(self):
14491448
return "msg_blocktxn(block_transactions=%s)" % (repr(self.block_transactions))
14501449

14511450

1452-
class msg_witness_blocktxn(msg_blocktxn):
1451+
class msg_no_witness_blocktxn(msg_blocktxn):
14531452
__slots__ = ()
14541453

14551454
def serialize(self):
1456-
r = b""
1457-
r += self.block_transactions.serialize(with_witness=True)
1458-
return r
1455+
return self.block_transactions.serialize(with_witness=False)

test/functional/wallet_bumpfee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def submit_block_with_tx(node, tx):
360360
block.hashMerkleRoot = block.calc_merkle_root()
361361
add_witness_commitment(block)
362362
block.solve()
363-
node.submitblock(block.serialize(True).hex())
363+
node.submitblock(block.serialize().hex())
364364
return block
365365

366366
def test_no_more_inputs_fails(rbf_node, dest_address):

0 commit comments

Comments
 (0)