Merge bitcoin/bitcoin#27516: test: simplify uint256 (de)serialization routines

96bf0bca4a0e3aa0b7c07d8c225861e72f970fa9 test: simplify uint256 (de)serialization routines (Sebastian Falbesoner)

Pull request description:

  These routines look fancy, but do nothing more than converting between byte objects of length 32 to/from integers in little endian byte order and can be replaced by simple one-liners, using the `int.{from,to}_bytes` methods (available since Python 3.2).

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 96bf0bca4a0e3aa0b7c07d8c225861e72f970fa9
  brunoerg:
    crACK 96bf0bca4a0e3aa0b7c07d8c225861e72f970fa9

Tree-SHA512: f3031502d61a936147867ad8a0efa841a9bbdd2acf8781653036889a38524f4f1a5c86b1e07157bf2d9663097e7b84be6846678d0883d2a334beafd87e9510f0
This commit is contained in:
fanquake 2023-04-25 11:30:50 +01:00 committed by pasta
parent 90d65f25e1
commit 712dcaf86b
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38

View File

@ -86,6 +86,7 @@ def ser_compact_size(l):
r = struct.pack("<BQ", 255, l)
return r
def deser_compact_size(f):
nit = struct.unpack("<B", f.read(1))[0]
if nit == 253:
@ -96,35 +97,26 @@ def deser_compact_size(f):
nit = struct.unpack("<Q", f.read(8))[0]
return nit
def deser_string(f):
nit = deser_compact_size(f)
return f.read(nit)
def ser_string(s):
return ser_compact_size(len(s)) + s
def deser_uint256(f):
r = 0
for i in range(8):
t = struct.unpack("<I", f.read(4))[0]
r += t << (i * 32)
return r
return int.from_bytes(f.read(32), 'little')
def ser_uint256(u):
rs = b""
for _ in range(8):
rs += struct.pack("<I", u & 0xFFFFFFFF)
u >>= 32
return rs
return u.to_bytes(32, 'little')
def uint256_from_str(s):
r = 0
t = struct.unpack("<IIIIIIII", s[:32])
for i in range(8):
r += t[i] << (i * 32)
return r
return int.from_bytes(s[:32], 'little')
def uint256_to_string(uint256):