mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-12 11:19:08 -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)
|
r = struct.pack("<BQ", 255, l)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
def deser_compact_size(f):
|
def deser_compact_size(f):
|
||||||
nit = struct.unpack("<B", f.read(1))[0]
|
nit = struct.unpack("<B", f.read(1))[0]
|
||||||
if nit == 253:
|
if nit == 253:
|
||||||
|
@ -103,35 +104,26 @@ def deser_compact_size(f):
|
||||||
nit = struct.unpack("<Q", f.read(8))[0]
|
nit = struct.unpack("<Q", f.read(8))[0]
|
||||||
return nit
|
return nit
|
||||||
|
|
||||||
|
|
||||||
def deser_string(f):
|
def deser_string(f):
|
||||||
nit = deser_compact_size(f)
|
nit = deser_compact_size(f)
|
||||||
return f.read(nit)
|
return f.read(nit)
|
||||||
|
|
||||||
|
|
||||||
def ser_string(s):
|
def ser_string(s):
|
||||||
return ser_compact_size(len(s)) + s
|
return ser_compact_size(len(s)) + s
|
||||||
|
|
||||||
|
|
||||||
def deser_uint256(f):
|
def deser_uint256(f):
|
||||||
r = 0
|
return int.from_bytes(f.read(32), 'little')
|
||||||
for i in range(8):
|
|
||||||
t = struct.unpack("<I", f.read(4))[0]
|
|
||||||
r += t << (i * 32)
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def ser_uint256(u):
|
def ser_uint256(u):
|
||||||
rs = b""
|
return u.to_bytes(32, 'little')
|
||||||
for _ in range(8):
|
|
||||||
rs += struct.pack("<I", u & 0xFFFFFFFF)
|
|
||||||
u >>= 32
|
|
||||||
return rs
|
|
||||||
|
|
||||||
|
|
||||||
def uint256_from_str(s):
|
def uint256_from_str(s):
|
||||||
r = 0
|
return int.from_bytes(s[:32], 'little')
|
||||||
t = struct.unpack("<IIIIIIII", s[:32])
|
|
||||||
for i in range(8):
|
|
||||||
r += t[i] << (i * 32)
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def uint256_from_compact(c):
|
def uint256_from_compact(c):
|
||||||
|
|
Loading…
Add table
Reference in a new issue