Skip to content

Commit fa1d766

Browse files
author
MarcoFalke
committed
tests: Make msg_block a witness block
This diff has been generated with the following script, but is better reviewed without looking at the script. # -BEGIN VERIFY SCRIPT- echo "Use msg_witness_block everywhere, except for tests that require msg_block" # This could be a separate commit, but it is combined with the # following scripts to reduce the overall diff sed -i -e 's/msg_block/msg_witness_block/g' ./test/functional/{feature_assumevalid,feature_cltv,feature_dersig,feature_versionbits_warning,p2p_fingerprint,p2p_sendheaders,p2p_unrequested_blocks,example_test,rpc_blockchain}.py echo "Rename msg_block to msg_no_witness_block" # Rename msg_block to msg_no_witness_block in all tests (not the # framework) sed -i -e 's/msg_block/msg_no_witness_block/g' $(git grep -l msg_block ./test/functional/*.py) # Derive msg_no_witness_block from msg_block # Make msg_block a witness block in messages.py patch -p1 --fuzz 0 << EOF diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 00190e4..e454ed5 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -1133 +1133 @@ class msg_block: - return self.block.serialize(with_witness=False) + return self.block.serialize() @@ -1155 +1155 @@ class msg_generic: -class msg_witness_block(msg_block): +class msg_no_witness_block(msg_block): @@ -1158,2 +1158 @@ class msg_witness_block(msg_block): - r = self.block.serialize() - return r + return self.block.serialize(with_witness=False) @@ -1445 +1444 @@ class msg_blocktxn: - r += self.block_transactions.serialize(with_witness=False) + r += self.block_transactions.serialize() @@ -1452 +1451 @@ class msg_blocktxn: -class msg_witness_blocktxn(msg_blocktxn): +class msg_no_witness_blocktxn(msg_blocktxn): @@ -1456,3 +1455 @@ class msg_witness_blocktxn(msg_blocktxn): - r = b"" - r += self.block_transactions.serialize() - return r + return self.block_transactions.serialize(with_witness=False) EOF # Conclude rename of msg_block to msg_no_witness_block sed -i -e 's/msg_witness_block/msg_block/g' $(git grep -l msg_witness_block) # -END VERIFY SCRIPT-
1 parent fa52eb5 commit fa1d766

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

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: 6 additions & 6 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,
@@ -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

@@ -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)

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()
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()
1458-
return r
1455+
return self.block_transactions.serialize(with_witness=False)

0 commit comments

Comments
 (0)