Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | |||||
NODE_AVALANCHE = (1 << 24) | NODE_AVALANCHE = (1 << 24) | ||||
MSG_TX = 1 | MSG_TX = 1 | ||||
MSG_BLOCK = 2 | MSG_BLOCK = 2 | ||||
MSG_FILTERED_BLOCK = 3 | MSG_FILTERED_BLOCK = 3 | ||||
MSG_CMPCTBLOCK = 4 | MSG_CMPCTBLOCK = 4 | ||||
MSG_TYPE_MASK = 0xffffffff >> 2 | MSG_TYPE_MASK = 0xffffffff >> 2 | ||||
# Serialization/deserialization tools | |||||
# Serialization/deserialization tools | |||||
def sha256(s): | def sha256(s): | ||||
return hashlib.new('sha256', s).digest() | return hashlib.new('sha256', s).digest() | ||||
def hash256(s): | def hash256(s): | ||||
return sha256(sha256(s)) | return sha256(sha256(s)) | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | |||||
def ser_string_vector(v): | def ser_string_vector(v): | ||||
r = ser_compact_size(len(v)) | r = ser_compact_size(len(v)) | ||||
for sv in v: | 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) | |||||
def FromHex(obj, hex_string): | def FromHex(obj, hex_string): | ||||
"""Deserialize from a hex string representation (eg from RPC)""" | |||||
obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) | obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) | ||||
return obj | return obj | ||||
# Convert a binary-serializable object to hex (eg for submission via RPC) | |||||
def ToHex(obj): | def ToHex(obj): | ||||
"""Convert a binary-serializable object to hex | |||||
(eg for submission via RPC)""" | |||||
return obj.serialize().hex() | return obj.serialize().hex() | ||||
# Objects that map to bitcoind objects, which can be serialized/deserialized | |||||
# Objects that map to bitcoind objects, which can be serialized/deserialized | |||||
class CAddress: | class CAddress: | ||||
__slots__ = ("ip", "nServices", "pchReserved", "port", "time") | __slots__ = ("ip", "nServices", "pchReserved", "port", "time") | ||||
def __init__(self): | def __init__(self): | ||||
self.time = 0 | self.time = 0 | ||||
self.nServices = 1 | self.nServices = 1 | ||||
self.pchReserved = b"\x00" * 10 + b"\xff" * 2 | self.pchReserved = b"\x00" * 10 + b"\xff" * 2 | ||||
▲ Show 20 Lines • Show All 412 Lines • ▼ Show 20 Lines | def serialize(self): | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "P2PHeaderAndShortIDs(header={}, nonce={}, shortids_length={}, shortids={}, prefilled_txn_length={}, prefilledtxn={}".format( | return "P2PHeaderAndShortIDs(header={}, nonce={}, shortids_length={}, shortids={}, prefilled_txn_length={}, prefilledtxn={}".format( | ||||
repr(self.header), self.nonce, self.shortids_length, | repr(self.header), self.nonce, self.shortids_length, | ||||
repr(self.shortids), self.prefilled_txn_length, | repr(self.shortids), self.prefilled_txn_length, | ||||
repr(self.prefilled_txn)) | repr(self.prefilled_txn)) | ||||
# Calculate the BIP 152-compact blocks shortid for a given transaction hash | |||||
def calculate_shortid(k0, k1, tx_hash): | def calculate_shortid(k0, k1, tx_hash): | ||||
"""Calculate the BIP 152-compact blocks shortid for a given | |||||
transaction hash""" | |||||
expected_shortid = siphash256(k0, k1, tx_hash) | expected_shortid = siphash256(k0, k1, tx_hash) | ||||
expected_shortid &= 0x0000ffffffffffff | expected_shortid &= 0x0000ffffffffffff | ||||
return expected_shortid | return expected_shortid | ||||
# This version gets rid of the array lengths, and reinterprets the differential | # This version gets rid of the array lengths, and reinterprets the differential | ||||
# encoding into indices that can be used for lookup. | # encoding into indices that can be used for lookup. | ||||
class HeaderAndShortIDs: | class HeaderAndShortIDs: | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Lines | class CMerkleBlock: | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CMerkleBlock(header={}, txn={})".format( | return "CMerkleBlock(header={}, txn={})".format( | ||||
repr(self.header), repr(self.txn)) | repr(self.header), repr(self.txn)) | ||||
# Objects that correspond to messages on the wire | # Objects that correspond to messages on the wire | ||||
class msg_version: | class msg_version: | ||||
__slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices", | __slots__ = ("addrFrom", "addrTo", "nNonce", "nRelay", "nServices", | ||||
"nStartingHeight", "nTime", "nVersion", "strSubVer") | "nStartingHeight", "nTime", "nVersion", "strSubVer") | ||||
command = b"version" | command = b"version" | ||||
def __init__(self): | def __init__(self): | ||||
self.nVersion = MY_VERSION | self.nVersion = MY_VERSION | ||||
self.nServices = 1 | self.nServices = 1 | ||||
▲ Show 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | def serialize(self): | ||||
return self.block.serialize() | return self.block.serialize() | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_block(block={})".format(repr(self.block)) | return "msg_block(block={})".format(repr(self.block)) | ||||
# for cases where a user needs tighter control over what is sent over the wire | # for cases where a user needs tighter control over what is sent over the wire | ||||
# note that the user must supply the name of the command, and the data | # note that the user must supply the name of the command, and the data | ||||
class msg_generic: | class msg_generic: | ||||
__slots__ = ("command", "data") | __slots__ = ("command", "data") | ||||
def __init__(self, command, data=None): | def __init__(self, command, data=None): | ||||
self.command = command | self.command = command | ||||
self.data = data | self.data = data | ||||
def serialize(self): | def serialize(self): | ||||
▲ Show 20 Lines • Show All 407 Lines • Show Last 20 Lines |