Merge #18610: scripted-diff: test: replace command with msgtype (naming)

9df32e820d scripted-diff: test: replace command with msgtype (Sebastian Falbesoner)

Pull request description:

  This is a follow-up PR to https://github.com/bitcoin/bitcoin/pull/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 9df32e820d . 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
This commit is contained in:
MarcoFalke 2020-04-19 09:16:16 -04:00
commit d2882a012b
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
4 changed files with 49 additions and 49 deletions

View file

@ -19,7 +19,7 @@ from test_framework.test_framework import BitcoinTestFramework
class msg_unrecognized:
"""Nonsensical message. Modeled after similar types in test_framework.messages."""
command = b'badmsg'
msgtype = b'badmsg'
def __init__(self, *, str_data):
self.str_data = str_data.encode() if not isinstance(str_data, bytes) else str_data
@ -28,7 +28,7 @@ class msg_unrecognized:
return messages.ser_string(self.str_data)
def __repr__(self):
return "{}(data={})".format(self.command, self.str_data)
return "{}(data={})".format(self.msgtype, self.str_data)
class InvalidMessagesTest(BitcoinTestFramework):
@ -50,7 +50,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
self.test_magic_bytes()
self.test_checksum()
self.test_size()
self.test_command()
self.test_msgtype()
self.test_large_inv()
node = self.nodes[0]
@ -168,7 +168,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
msg = conn.build_message(msg_unrecognized(str_data="d"))
cut_len = (
4 + # magic
12 + # command
12 + # msgtype
4 #len
)
# modify checksum
@ -183,7 +183,7 @@ class InvalidMessagesTest(BitcoinTestFramework):
msg = conn.build_message(msg_unrecognized(str_data="d"))
cut_len = (
4 + # magic
12 # command
12 # msgtype
)
# modify len to MAX_SIZE + 1
msg = msg[:cut_len] + struct.pack("<I", 0x02000000 + 1) + msg[cut_len + 4:]
@ -191,13 +191,13 @@ class InvalidMessagesTest(BitcoinTestFramework):
conn.wait_for_disconnect(timeout=1)
self.nodes[0].disconnect_p2ps()
def test_command(self):
def test_msgtype(self):
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
with self.nodes[0].assert_debug_log(['PROCESSMESSAGE: ERRORS IN HEADER']):
msg = msg_unrecognized(str_data="d")
msg.command = b'\xff' * 12
msg.msgtype = b'\xff' * 12
msg = conn.build_message(msg)
# Modify command
# Modify msgtype
msg = msg[:7] + b'\x00' + msg[7 + 1:]
self.nodes[0].p2p.send_raw_message(msg)
conn.sync_with_ping(timeout=1)

View file

@ -37,7 +37,7 @@ class CLazyNode(P2PInterface):
def bad_message(self, message):
self.unexpected_msg = True
self.log.info("should not have received message: %s" % message.command)
self.log.info("should not have received message: %s" % message.msgtype)
def on_open(self):
self.ever_connected = True

View file

@ -946,7 +946,7 @@ class CMerkleBlock:
class msg_version:
__slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices",
"nStartingHeight", "nTime", "nVersion", "strSubVer")
command = b"version"
msgtype = b"version"
def __init__(self):
self.nVersion = MY_VERSION
@ -1004,7 +1004,7 @@ class msg_version:
class msg_verack:
__slots__ = ()
command = b"verack"
msgtype = b"verack"
def __init__(self):
pass
@ -1021,7 +1021,7 @@ class msg_verack:
class msg_addr:
__slots__ = ("addrs",)
command = b"addr"
msgtype = b"addr"
def __init__(self):
self.addrs = []
@ -1038,7 +1038,7 @@ class msg_addr:
class msg_inv:
__slots__ = ("inv",)
command = b"inv"
msgtype = b"inv"
def __init__(self, inv=None):
if inv is None:
@ -1058,7 +1058,7 @@ class msg_inv:
class msg_getdata:
__slots__ = ("inv",)
command = b"getdata"
msgtype = b"getdata"
def __init__(self, inv=None):
self.inv = inv if inv is not None else []
@ -1075,7 +1075,7 @@ class msg_getdata:
class msg_getblocks:
__slots__ = ("locator", "hashstop")
command = b"getblocks"
msgtype = b"getblocks"
def __init__(self):
self.locator = CBlockLocator()
@ -1099,7 +1099,7 @@ class msg_getblocks:
class msg_tx:
__slots__ = ("tx",)
command = b"tx"
msgtype = b"tx"
def __init__(self, tx=CTransaction()):
self.tx = tx
@ -1123,7 +1123,7 @@ class msg_no_witness_tx(msg_tx):
class msg_block:
__slots__ = ("block",)
command = b"block"
msgtype = b"block"
def __init__(self, block=None):
if block is None:
@ -1142,12 +1142,12 @@ class msg_block:
# for cases where a user needs tighter control over what is sent over the wire
# note that the user must supply the name of the command, and the data
# note that the user must supply the name of the msgtype, and the data
class msg_generic:
__slots__ = ("command", "data")
__slots__ = ("msgtype", "data")
def __init__(self, command, data=None):
self.command = command
def __init__(self, msgtype, data=None):
self.msgtype = msgtype
self.data = data
def serialize(self):
@ -1165,7 +1165,7 @@ class msg_no_witness_block(msg_block):
class msg_getaddr:
__slots__ = ()
command = b"getaddr"
msgtype = b"getaddr"
def __init__(self):
pass
@ -1182,7 +1182,7 @@ class msg_getaddr:
class msg_ping:
__slots__ = ("nonce",)
command = b"ping"
msgtype = b"ping"
def __init__(self, nonce=0):
self.nonce = nonce
@ -1201,7 +1201,7 @@ class msg_ping:
class msg_pong:
__slots__ = ("nonce",)
command = b"pong"
msgtype = b"pong"
def __init__(self, nonce=0):
self.nonce = nonce
@ -1220,7 +1220,7 @@ class msg_pong:
class msg_mempool:
__slots__ = ()
command = b"mempool"
msgtype = b"mempool"
def __init__(self):
pass
@ -1237,7 +1237,7 @@ class msg_mempool:
class msg_notfound:
__slots__ = ("vec", )
command = b"notfound"
msgtype = b"notfound"
def __init__(self, vec=None):
self.vec = vec or []
@ -1254,7 +1254,7 @@ class msg_notfound:
class msg_sendheaders:
__slots__ = ()
command = b"sendheaders"
msgtype = b"sendheaders"
def __init__(self):
pass
@ -1275,7 +1275,7 @@ class msg_sendheaders:
# hash_stop (hash of last desired block header, 0 to get as many as possible)
class msg_getheaders:
__slots__ = ("hashstop", "locator",)
command = b"getheaders"
msgtype = b"getheaders"
def __init__(self):
self.locator = CBlockLocator()
@ -1301,7 +1301,7 @@ class msg_getheaders:
# <count> <vector of block headers>
class msg_headers:
__slots__ = ("headers",)
command = b"headers"
msgtype = b"headers"
def __init__(self, headers=None):
self.headers = headers if headers is not None else []
@ -1322,7 +1322,7 @@ class msg_headers:
class msg_merkleblock:
__slots__ = ("merkleblock",)
command = b"merkleblock"
msgtype = b"merkleblock"
def __init__(self, merkleblock=None):
if merkleblock is None:
@ -1342,7 +1342,7 @@ class msg_merkleblock:
class msg_filterload:
__slots__ = ("data", "nHashFuncs", "nTweak", "nFlags")
command = b"filterload"
msgtype = b"filterload"
def __init__(self, data=b'00', nHashFuncs=0, nTweak=0, nFlags=0):
self.data = data
@ -1371,7 +1371,7 @@ class msg_filterload:
class msg_filteradd:
__slots__ = ("data")
command = b"filteradd"
msgtype = b"filteradd"
def __init__(self, data):
self.data = data
@ -1390,7 +1390,7 @@ class msg_filteradd:
class msg_filterclear:
__slots__ = ()
command = b"filterclear"
msgtype = b"filterclear"
def __init__(self):
pass
@ -1407,7 +1407,7 @@ class msg_filterclear:
class msg_feefilter:
__slots__ = ("feerate",)
command = b"feefilter"
msgtype = b"feefilter"
def __init__(self, feerate=0):
self.feerate = feerate
@ -1426,7 +1426,7 @@ class msg_feefilter:
class msg_sendcmpct:
__slots__ = ("announce", "version")
command = b"sendcmpct"
msgtype = b"sendcmpct"
def __init__(self):
self.announce = False
@ -1448,7 +1448,7 @@ class msg_sendcmpct:
class msg_cmpctblock:
__slots__ = ("header_and_shortids",)
command = b"cmpctblock"
msgtype = b"cmpctblock"
def __init__(self, header_and_shortids = None):
self.header_and_shortids = header_and_shortids
@ -1468,7 +1468,7 @@ class msg_cmpctblock:
class msg_getblocktxn:
__slots__ = ("block_txn_request",)
command = b"getblocktxn"
msgtype = b"getblocktxn"
def __init__(self):
self.block_txn_request = None
@ -1488,7 +1488,7 @@ class msg_getblocktxn:
class msg_blocktxn:
__slots__ = ("block_transactions",)
command = b"blocktxn"
msgtype = b"blocktxn"
def __init__(self):
self.block_transactions = BlockTransactions()

View file

@ -180,7 +180,7 @@ class P2PConnection(asyncio.Protocol):
raise ValueError("magic bytes mismatch: {} != {}".format(repr(self.magic_bytes), repr(self.recvbuf)))
if len(self.recvbuf) < 4 + 12 + 4 + 4:
return
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msgtype = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
checksum = self.recvbuf[4+12+4:4+12+4+4]
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
@ -191,10 +191,10 @@ class P2PConnection(asyncio.Protocol):
if checksum != h[:4]:
raise ValueError("got bad checksum " + repr(self.recvbuf))
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
if command not in MESSAGEMAP:
raise ValueError("Received unknown command from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, command, repr(msg)))
if msgtype not in MESSAGEMAP:
raise ValueError("Received unknown msgtype from %s:%d: '%s' %s" % (self.dstaddr, self.dstport, msgtype, repr(msg)))
f = BytesIO(msg)
t = MESSAGEMAP[command]()
t = MESSAGEMAP[msgtype]()
t.deserialize(f)
self._log_message("receive", t)
self.on_message(t)
@ -233,11 +233,11 @@ class P2PConnection(asyncio.Protocol):
def build_message(self, message):
"""Build a serialized P2P message"""
command = message.command
msgtype = message.msgtype
data = message.serialize()
tmsg = self.magic_bytes
tmsg += command
tmsg += b"\x00" * (12 - len(command))
tmsg += msgtype
tmsg += b"\x00" * (12 - len(msgtype))
tmsg += struct.pack("<I", len(data))
th = sha256(data)
h = sha256(th)
@ -304,10 +304,10 @@ class P2PInterface(P2PConnection):
and the most recent message of each type."""
with mininode_lock:
try:
command = message.command.decode('ascii')
self.message_count[command] += 1
self.last_message[command] = message
getattr(self, 'on_' + command)(message)
msgtype = message.msgtype.decode('ascii')
self.message_count[msgtype] += 1
self.last_message[msgtype] = message
getattr(self, 'on_' + msgtype)(message)
except:
print("ERROR delivering %s (%s)" % (repr(message), sys.exc_info()[0]))
raise