Skip to content

Commit d2882a0

Browse files
author
MarcoFalke
committed
Merge #18610: scripted-diff: test: replace command with msgtype (naming)
9df32e8 scripted-diff: test: replace command with msgtype (Sebastian Falbesoner) Pull request description: This is a follow-up PR to bitcoin/bitcoin#18533, which changed the naming of `strCommand` to `msg_type` in the network processing code. The same approach is done here for the function test framework, to get rid of the wrong "command" terminology for network mesage types. (Commands are usually used in the CLI or RPC context, so using the same name in the network message context would only be confusing.) The commit was created through the following steps: 1. search for all occurences of the string "command" within the folder `test/functional` ```git grep -i command test/functional > command_finds``` 2. manually sort out all false-positives, i.e. occurences of "command" which describe commands in the correct sense (mostly CLI or RPC related, also some with Socks5) 3. put the remaining occurences into a scripted-diff (a quite simple one, actually) that renames "command" to "msgtype" in the concerned files. The name `msgtype` was intentionally chosen without the underscore `_` as classes beginning with `msg_` define concrete types of messages. ACKs for top commit: MarcoFalke: ACK 9df32e8 . Makes sense that tests use the same naming as Bitcoin Core. See `NetMsgType` here: https://doxygen.bitcoincore.org/namespace_net_msg_type.html Tree-SHA512: cd0ee08a382910b7f10ce583acdaf4f8a39f9ba4a22434a914415727eedd98bac538de9bf6633574d5eb86f62558bc8dcb638a3289d99b04f8481f34e7a9a0c7
2 parents b470c75 + 9df32e8 commit d2882a0

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class msg_unrecognized:
2020
"""Nonsensical message. Modeled after similar types in test_framework.messages."""
2121

22-
command = b'badmsg'
22+
msgtype = b'badmsg'
2323

2424
def __init__(self, *, str_data):
2525
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
@@ -28,7 +28,7 @@ def serialize(self):
2828
return messages.ser_string(self.str_data)
2929

3030
def __repr__(self):
31-
return "{}(data={})".format(self.command, self.str_data)
31+
return "{}(data={})".format(self.msgtype, self.str_data)
3232

3333

3434
class InvalidMessagesTest(BitcoinTestFramework):
@@ -50,7 +50,7 @@ def run_test(self):
5050
self.test_magic_bytes()
5151
self.test_checksum()
5252
self.test_size()
53-
self.test_command()
53+
self.test_msgtype()
5454
self.test_large_inv()
5555

5656
node = self.nodes[0]
@@ -168,7 +168,7 @@ def test_checksum(self):
168168
msg = conn.build_message(msg_unrecognized(str_data="d"))
169169
cut_len = (
170170
4 + # magic
171-
12 + # command
171+
12 + # msgtype
172172
4 #len
173173
)
174174
# modify checksum
@@ -183,21 +183,21 @@ def test_size(self):
183183
msg = conn.build_message(msg_unrecognized(str_data="d"))
184184
cut_len = (
185185
4 + # magic
186-
12 # command
186+
12 # msgtype
187187
)
188188
# modify len to MAX_SIZE + 1
189189
msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:]
190190
self.nodes[0].p2p.send_raw_message(msg)
191191
conn.wait_for_disconnect(timeout=1)
192192
self.nodes[0].disconnect_p2ps()
193193

194-
def test_command(self):
194+
def test_msgtype(self):
195195
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
196196
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']):
197197
msg = msg_unrecognized(str_data="d")
198-
msg.command = b'\xff' * 12
198+
msg.msgtype = b'\xff' * 12
199199
msg = conn.build_message(msg)
200-
# Modify command
200+
# Modify msgtype
201201
msg = msg[:7] + b'\x00' + msg[7 + 1:]
202202
self.nodes[0].p2p.send_raw_message(msg)
203203
conn.sync_with_ping(timeout=1)

test/functional/p2p_leak.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self):
3737

3838
def bad_message(self, message):
3939
self.unexpected_msg = True
40-
self.log.info("should not have received message: %s" % message.command)
40+
self.log.info("should not have received message: %s" % message.msgtype)
4141

4242
def on_open(self):
4343
self.ever_connected = True

test/functional/test_framework/messages.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ def __repr__(self):
946946
class msg_version:
947947
__slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices",
948948
"nStartingHeight", "nTime", "nVersion", "strSubVer")
949-
command = b"version"
949+
msgtype = b"version"
950950

951951
def __init__(self):
952952
self.nVersion = MY_VERSION
@@ -1004,7 +1004,7 @@ def __repr__(self):
10041004

10051005
class msg_verack:
10061006
__slots__ = ()
1007-
command = b"verack"
1007+
msgtype = b"verack"
10081008

10091009
def __init__(self):
10101010
pass
@@ -1021,7 +1021,7 @@ def __repr__(self):
10211021

10221022
class msg_addr:
10231023
__slots__ = ("addrs",)
1024-
command = b"addr"
1024+
msgtype = b"addr"
10251025

10261026
def __init__(self):
10271027
self.addrs = []
@@ -1038,7 +1038,7 @@ def __repr__(self):
10381038

10391039
class msg_inv:
10401040
__slots__ = ("inv",)
1041-
command = b"inv"
1041+
msgtype = b"inv"
10421042

10431043
def __init__(self, inv=None):
10441044
if inv is None:
@@ -1058,7 +1058,7 @@ def __repr__(self):
10581058

10591059
class msg_getdata:
10601060
__slots__ = ("inv",)
1061-
command = b"getdata"
1061+
msgtype = b"getdata"
10621062

10631063
def __init__(self, inv=None):
10641064
self.inv = inv if inv is not None else []
@@ -1075,7 +1075,7 @@ def __repr__(self):
10751075

10761076
class msg_getblocks:
10771077
__slots__ = ("locator", "hashstop")
1078-
command = b"getblocks"
1078+
msgtype = b"getblocks"
10791079

10801080
def __init__(self):
10811081
self.locator = CBlockLocator()
@@ -1099,7 +1099,7 @@ def __repr__(self):
10991099

11001100
class msg_tx:
11011101
__slots__ = ("tx",)
1102-
command = b"tx"
1102+
msgtype = b"tx"
11031103

11041104
def __init__(self, tx=CTransaction()):
11051105
self.tx = tx
@@ -1123,7 +1123,7 @@ def serialize(self):
11231123

11241124
class msg_block:
11251125
__slots__ = ("block",)
1126-
command = b"block"
1126+
msgtype = b"block"
11271127

11281128
def __init__(self, block=None):
11291129
if block is None:
@@ -1142,12 +1142,12 @@ def __repr__(self):
11421142

11431143

11441144
# for cases where a user needs tighter control over what is sent over the wire
1145-
# note that the user must supply the name of the command, and the data
1145+
# note that the user must supply the name of the msgtype, and the data
11461146
class msg_generic:
1147-
__slots__ = ("command", "data")
1147+
__slots__ = ("msgtype", "data")
11481148

1149-
def __init__(self, command, data=None):
1150-
self.command = command
1149+
def __init__(self, msgtype, data=None):
1150+
self.msgtype = msgtype
11511151
self.data = data
11521152

11531153
def serialize(self):
@@ -1165,7 +1165,7 @@ def serialize(self):
11651165

11661166
class msg_getaddr:
11671167
__slots__ = ()
1168-
command = b"getaddr"
1168+
msgtype = b"getaddr"
11691169

11701170
def __init__(self):
11711171
pass
@@ -1182,7 +1182,7 @@ def __repr__(self):
11821182

11831183
class msg_ping:
11841184
__slots__ = ("nonce",)
1185-
command = b"ping"
1185+
msgtype = b"ping"
11861186

11871187
def __init__(self, nonce=0):
11881188
self.nonce = nonce
@@ -1201,7 +1201,7 @@ def __repr__(self):
12011201

12021202
class msg_pong:
12031203
__slots__ = ("nonce",)
1204-
command = b"pong"
1204+
msgtype = b"pong"
12051205

12061206
def __init__(self, nonce=0):
12071207
self.nonce = nonce
@@ -1220,7 +1220,7 @@ def __repr__(self):
12201220

12211221
class msg_mempool:
12221222
__slots__ = ()
1223-
command = b"mempool"
1223+
msgtype = b"mempool"
12241224

12251225
def __init__(self):
12261226
pass
@@ -1237,7 +1237,7 @@ def __repr__(self):
12371237

12381238
class msg_notfound:
12391239
__slots__ = ("vec", )
1240-
command = b"notfound"
1240+
msgtype = b"notfound"
12411241

12421242
def __init__(self, vec=None):
12431243
self.vec = vec or []
@@ -1254,7 +1254,7 @@ def __repr__(self):
12541254

12551255
class msg_sendheaders:
12561256
__slots__ = ()
1257-
command = b"sendheaders"
1257+
msgtype = b"sendheaders"
12581258

12591259
def __init__(self):
12601260
pass
@@ -1275,7 +1275,7 @@ def __repr__(self):
12751275
# hash_stop (hash of last desired block header, 0 to get as many as possible)
12761276
class msg_getheaders:
12771277
__slots__ = ("hashstop", "locator",)
1278-
command = b"getheaders"
1278+
msgtype = b"getheaders"
12791279

12801280
def __init__(self):
12811281
self.locator = CBlockLocator()
@@ -1301,7 +1301,7 @@ def __repr__(self):
13011301
# <count> <vector of block headers>
13021302
class msg_headers:
13031303
__slots__ = ("headers",)
1304-
command = b"headers"
1304+
msgtype = b"headers"
13051305

13061306
def __init__(self, headers=None):
13071307
self.headers = headers if headers is not None else []
@@ -1322,7 +1322,7 @@ def __repr__(self):
13221322

13231323
class msg_merkleblock:
13241324
__slots__ = ("merkleblock",)
1325-
command = b"merkleblock"
1325+
msgtype = b"merkleblock"
13261326

13271327
def __init__(self, merkleblock=None):
13281328
if merkleblock is None:
@@ -1342,7 +1342,7 @@ def __repr__(self):
13421342

13431343
class msg_filterload:
13441344
__slots__ = ("data", "nHashFuncs", "nTweak", "nFlags")
1345-
command = b"filterload"
1345+
msgtype = b"filterload"
13461346

13471347
def __init__(self, data=b'00', nHashFuncs=0, nTweak=0, nFlags=0):
13481348
self.data = data
@@ -1371,7 +1371,7 @@ def __repr__(self):
13711371

13721372
class msg_filteradd:
13731373
__slots__ = ("data")
1374-
command = b"filteradd"
1374+
msgtype = b"filteradd"
13751375

13761376
def __init__(self, data):
13771377
self.data = data
@@ -1390,7 +1390,7 @@ def __repr__(self):
13901390

13911391
class msg_filterclear:
13921392
__slots__ = ()
1393-
command = b"filterclear"
1393+
msgtype = b"filterclear"
13941394

13951395
def __init__(self):
13961396
pass
@@ -1407,7 +1407,7 @@ def __repr__(self):
14071407

14081408
class msg_feefilter:
14091409
__slots__ = ("feerate",)
1410-
command = b"feefilter"
1410+
msgtype = b"feefilter"
14111411

14121412
def __init__(self, feerate=0):
14131413
self.feerate = feerate
@@ -1426,7 +1426,7 @@ def __repr__(self):
14261426

14271427
class msg_sendcmpct:
14281428
__slots__ = ("announce", "version")
1429-
command = b"sendcmpct"
1429+
msgtype = b"sendcmpct"
14301430

14311431
def __init__(self):
14321432
self.announce = False
@@ -1448,7 +1448,7 @@ def __repr__(self):
14481448

14491449
class msg_cmpctblock:
14501450
__slots__ = ("header_and_shortids",)
1451-
command = b"cmpctblock"
1451+
msgtype = b"cmpctblock"
14521452

14531453
def __init__(self, header_and_shortids = None):
14541454
self.header_and_shortids = header_and_shortids
@@ -1468,7 +1468,7 @@ def __repr__(self):
14681468

14691469
class msg_getblocktxn:
14701470
__slots__ = ("block_txn_request",)
1471-
command = b"getblocktxn"
1471+
msgtype = b"getblocktxn"
14721472

14731473
def __init__(self):
14741474
self.block_txn_request = None
@@ -1488,7 +1488,7 @@ def __repr__(self):
14881488

14891489
class msg_blocktxn:
14901490
__slots__ = ("block_transactions",)
1491-
command = b"blocktxn"
1491+
msgtype = b"blocktxn"
14921492

14931493
def __init__(self):
14941494
self.block_transactions = BlockTransactions()

test/functional/test_framework/mininode.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _on_data(self):
180180
raise ValueError("magic bytes mismatch: {} != {}".format(repr(self.magic_bytes), repr(self.recvbuf)))
181181
if len(self.recvbuf) < 4 + 12 + 4 + 4:
182182
return
183-
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
183+
msgtype = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
184184
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
185185
checksum = self.recvbuf[4+12+4:4+12+4+4]
186186
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
@@ -191,10 +191,10 @@ def _on_data(self):
191191
if checksum != h[:4]:
192192
raise ValueError("got bad checksum " + repr(self.recvbuf))
193193
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
194-
if command not in MESSAGEMAP:
195-
raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
194+
if msgtype not in MESSAGEMAP:
195+
raise ValueError("Received unknown msgtype from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, msgtype, repr(msg)))
196196
f = BytesIO(msg)
197-
t = MESSAGEMAP[command]()
197+
t = MESSAGEMAP[msgtype]()
198198
t.deserialize(f)
199199
self._log_message("receive", t)
200200
self.on_message(t)
@@ -233,11 +233,11 @@ def maybe_write():
233233

234234
def build_message(self, message):
235235
"""Build a serialized P2P message"""
236-
command = message.command
236+
msgtype = message.msgtype
237237
data = message.serialize()
238238
tmsg = self.magic_bytes
239-
tmsg += command
240-
tmsg += b"\x00" * (12 - len(command))
239+
tmsg += msgtype
240+
tmsg += b"\x00" * (12 - len(msgtype))
241241
tmsg += struct.pack("<I", len(data))
242242
th = sha256(data)
243243
h = sha256(th)
@@ -304,10 +304,10 @@ def on_message(self, message):
304304
and the most recent message of each type."""
305305
with mininode_lock:
306306
try:
307-
command = message.command.decode('ascii')
308-
self.message_count[command] += 1
309-
self.last_message[command] = message
310-
getattr(self, 'on_' + command)(message)
307+
msgtype = message.msgtype.decode('ascii')
308+
self.message_count[msgtype] += 1
309+
self.last_message[msgtype] = message
310+
getattr(self, 'on_' + msgtype)(message)
311311
except:
312312
print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0]))
313313
raise

0 commit comments

Comments
 (0)