mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
test: simplify uint256 (de)serialization routines
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).
This commit is contained in:
parent
49d07ea9a1
commit
96bf0bca4a
1 changed files with 7 additions and 15 deletions
|
@ -93,6 +93,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:
|
||||
|
@ -103,35 +104,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_from_compact(c):
|
||||
|
|
Loading…
Add table
Reference in a new issue