Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
def ripemd160(s): | def ripemd160(s): | ||||
return hashlib.new('ripemd160', s).digest() | return hashlib.new('ripemd160', s).digest() | ||||
def hash256(s): | def hash256(s): | ||||
return sha256(sha256(s)) | return sha256(sha256(s)) | ||||
def ser_compact_size(l): | def ser_compact_size(cs): | ||||
jasonbcox: Nit: `cs` doesn't mean anything about the variable or what it contains. `list` or something… | |||||
r = b"" | r = b"" | ||||
if l < 253: | if cs < 253: | ||||
r = struct.pack("B", l) | r = struct.pack("B", cs) | ||||
elif l < 0x10000: | elif cs < 0x10000: | ||||
r = struct.pack("<BH", 253, l) | r = struct.pack("<BH", 253, cs) | ||||
elif l < 0x100000000: | elif cs < 0x100000000: | ||||
r = struct.pack("<BI", 254, l) | r = struct.pack("<BI", 254, cs) | ||||
else: | else: | ||||
r = struct.pack("<BQ", 255, l) | r = struct.pack("<BQ", 255, cs) | ||||
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: | ||||
nit = struct.unpack("<H", f.read(2))[0] | nit = struct.unpack("<H", f.read(2))[0] | ||||
elif nit == 254: | elif nit == 254: | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | for i in range(nit): | ||||
t = c() | t = c() | ||||
t.deserialize(f) | t.deserialize(f) | ||||
r.append(t) | r.append(t) | ||||
return r | return r | ||||
# ser_function_name: Allow for an alternate serialization function on the | # ser_function_name: Allow for an alternate serialization function on the | ||||
# entries in the vector. | # entries in the vector. | ||||
def ser_vector(l, ser_function_name=None): | def ser_vector(v, ser_function_name=None): | ||||
r = ser_compact_size(len(l)) | r = ser_compact_size(len(v)) | ||||
for i in l: | for i in v: | ||||
if ser_function_name: | if ser_function_name: | ||||
r += getattr(i, ser_function_name)() | r += getattr(i, ser_function_name)() | ||||
else: | else: | ||||
r += i.serialize() | r += i.serialize() | ||||
return r | return r | ||||
def deser_uint256_vector(f): | def deser_uint256_vector(f): | ||||
nit = deser_compact_size(f) | nit = deser_compact_size(f) | ||||
r = [] | r = [] | ||||
for i in range(nit): | for i in range(nit): | ||||
t = deser_uint256(f) | t = deser_uint256(f) | ||||
r.append(t) | r.append(t) | ||||
return r | return r | ||||
def ser_uint256_vector(l): | def ser_uint256_vector(v): | ||||
r = ser_compact_size(len(l)) | r = ser_compact_size(len(v)) | ||||
for i in l: | for i in v: | ||||
r += ser_uint256(i) | r += ser_uint256(i) | ||||
return r | return r | ||||
def deser_string_vector(f): | def deser_string_vector(f): | ||||
nit = deser_compact_size(f) | nit = deser_compact_size(f) | ||||
r = [] | r = [] | ||||
for i in range(nit): | for i in range(nit): | ||||
t = deser_string(f) | t = deser_string(f) | ||||
r.append(t) | r.append(t) | ||||
return r | return r | ||||
def ser_string_vector(l): | def ser_string_vector(v): | ||||
r = ser_compact_size(len(l)) | r = ser_compact_size(len(v)) | ||||
for sv in l: | for sv in v: | ||||
r += ser_string(sv) | r += ser_string(sv) | ||||
return r | return r | ||||
# Deserialize from a hex string representation (eg from RPC) | # Deserialize from a hex string representation (eg from RPC) | ||||
def FromHex(obj, hex_string): | def FromHex(obj, hex_string): | ||||
▲ Show 20 Lines • Show All 1,279 Lines • Show Last 20 Lines |
Nit: cs doesn't mean anything about the variable or what it contains. list or something would be better here (assuming l was supposed to be a list in the first place).