From fa3886b7c69cbbe564478f30bb2c35e9e6b1cffa Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 12 Dec 2023 17:57:00 +0100 Subject: test: Treat msg_version.relay as unsigned The C++ code treats bool as uint8_t, so the python tests should as well. This also allows to simplify the code, because converting an empty byte array to int gives int(0). >>> int.from_bytes(b'') 0 --- test/functional/test_framework/messages.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'test/functional/test_framework') diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index d008cb39aa..1e15113d88 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -1132,10 +1132,7 @@ class msg_version: # Relay field is optional for version 70001 onwards # But, unconditionally check it to match behaviour in bitcoind - try: - self.relay = struct.unpack(" Date: Tue, 12 Dec 2023 19:44:18 +0100 Subject: test: Use int from_bytes and to_bytes over struct packing This is done in prepration for the scripted diff, which can not deal with the 0 literal int. --- test/functional/test_framework/messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/functional/test_framework') diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 1e15113d88..c5050c0e6a 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -399,12 +399,12 @@ class CBlockLocator: self.vHave = [] def deserialize(self, f): - struct.unpack(" Date: Tue, 12 Dec 2023 17:06:38 +0100 Subject: scripted-diff: test: Use int from_bytes and to_bytes over struct packing -BEGIN VERIFY SCRIPT- sed -i --regexp-extended 's!struct.unpack\("(|<|>)B", (.*)\)\[0\]!int.from_bytes(\2, "little")!g' ./test/functional/test_framework/messages.py sed -i --regexp-extended 's!struct.unpack\("<(H|I|Q)", (.*)\)\[0\]!int.from_bytes(\2, "little")!g' ./test/functional/test_framework/messages.py sed -i --regexp-extended 's!struct.unpack\("<(h|i|q)", (.*)\)\[0\]!int.from_bytes(\2, "little", signed=True)!g' ./test/functional/test_framework/messages.py sed -i --regexp-extended 's!struct.unpack\(">(H|I|Q)", (.*)\)\[0\]!int.from_bytes(\2, "big")!g' ./test/functional/test_framework/messages.py sed -i --regexp-extended 's!struct.pack\("H", (.*)\)!\1.to_bytes(2, "big")!g' ./test/functional/test_framework/messages.py -END VERIFY SCRIPT- --- test/functional/test_framework/messages.py | 196 ++++++++++++++--------------- 1 file changed, 98 insertions(+), 98 deletions(-) (limited to 'test/functional/test_framework') diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index c5050c0e6a..4b8bf7035c 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -90,7 +90,7 @@ def hash256(s): def ser_compact_size(l): r = b"" if l < 253: - r = struct.pack("B", l) + r = l.to_bytes(1, "little") elif l < 0x10000: r = struct.pack("H", f.read(2))[0] + self.port = int.from_bytes(f.read(2), "big") def serialize(self, *, with_time=True): """Serialize in addrv1 format (pre-BIP155)""" @@ -286,20 +286,20 @@ class CAddress: r = b"" if with_time: # VERSION messages serialize CAddress objects without time - r += struct.pack("H", self.port) + r += self.port.to_bytes(2, "big") return r def deserialize_v2(self, f): """Deserialize from addrv2 format (BIP155)""" - self.time = struct.unpack("H", f.read(2))[0] + self.port = int.from_bytes(f.read(2), "big") def serialize_v2(self): """Serialize in addrv2 format (BIP155)""" assert self.net in self.ADDRV2_NET_NAME r = b"" - r += struct.pack("H", self.port) + r += self.port.to_bytes(2, "big") return r def __repr__(self): @@ -375,12 +375,12 @@ class CInv: self.hash = h def deserialize(self, f): - self.type = struct.unpack(" Date: Tue, 12 Dec 2023 18:39:37 +0100 Subject: test: Remove struct import from messages.py --- test/functional/test_framework/messages.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'test/functional/test_framework') diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 4b8bf7035c..c8a8286e01 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -25,7 +25,6 @@ from io import BytesIO import math import random import socket -import struct import time import unittest @@ -92,11 +91,11 @@ def ser_compact_size(l): if l < 253: r = l.to_bytes(1, "little") elif l < 0x10000: - r = struct.pack("