Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show All 22 Lines | |||||
import hashlib | import hashlib | ||||
from io import BytesIO | from io import BytesIO | ||||
import random | import random | ||||
import socket | import socket | ||||
import struct | import struct | ||||
import time | import time | ||||
from test_framework.siphash import siphash256 | from test_framework.siphash import siphash256 | ||||
from test_framework.util import bytes_to_hex_str, hex_str_to_bytes | from test_framework.util import hex_str_to_bytes | ||||
MIN_VERSION_SUPPORTED = 60001 | MIN_VERSION_SUPPORTED = 60001 | ||||
# past bip-31 for ping/pong | # past bip-31 for ping/pong | ||||
MY_VERSION = 70014 | MY_VERSION = 70014 | ||||
MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" | MY_SUBVERSION = b"/python-mininode-tester:0.0.3/" | ||||
# from version 70001 onwards, fRelay should be appended to version messages (BIP37) | # from version 70001 onwards, fRelay should be appended to version messages (BIP37) | ||||
MY_RELAY = 1 | MY_RELAY = 1 | ||||
▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | |||||
def FromHex(obj, hex_string): | def FromHex(obj, hex_string): | ||||
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) | # Convert a binary-serializable object to hex (eg for submission via RPC) | ||||
def ToHex(obj): | def ToHex(obj): | ||||
return bytes_to_hex_str(obj.serialize()) | 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): | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.prevout.serialize() | r += self.prevout.serialize() | ||||
r += ser_string(self.scriptSig) | r += ser_string(self.scriptSig) | ||||
r += struct.pack("<I", self.nSequence) | r += struct.pack("<I", self.nSequence) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CTxIn(prevout={} scriptSig={} nSequence={})".format( | return "CTxIn(prevout={} scriptSig={} nSequence={})".format( | ||||
repr(self.prevout), bytes_to_hex_str(self.scriptSig), self.nSequence) | repr(self.prevout), self.scriptSig.hex(), self.nSequence) | ||||
class CTxOut: | class CTxOut: | ||||
__slots__ = ("nValue", "scriptPubKey") | __slots__ = ("nValue", "scriptPubKey") | ||||
def __init__(self, nValue=0, scriptPubKey=b""): | def __init__(self, nValue=0, scriptPubKey=b""): | ||||
self.nValue = nValue | self.nValue = nValue | ||||
self.scriptPubKey = scriptPubKey | self.scriptPubKey = scriptPubKey | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.nValue = struct.unpack("<q", f.read(8))[0] | self.nValue = struct.unpack("<q", f.read(8))[0] | ||||
self.scriptPubKey = deser_string(f) | self.scriptPubKey = deser_string(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<q", self.nValue) | r += struct.pack("<q", self.nValue) | ||||
r += ser_string(self.scriptPubKey) | r += ser_string(self.scriptPubKey) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CTxOut(nValue={}.{:08d} scriptPubKey={})".format( | return "CTxOut(nValue={}.{:08d} scriptPubKey={})".format( | ||||
self.nValue // COIN, self.nValue % COIN, | self.nValue // COIN, self.nValue % COIN, self.scriptPubKey.hex()) | ||||
bytes_to_hex_str(self.scriptPubKey)) | |||||
class CTransaction: | class CTransaction: | ||||
__slots__ = ("hash", "nLockTime", "nVersion", "sha256", "vin", "vout") | __slots__ = ("hash", "nLockTime", "nVersion", "sha256", "vin", "vout") | ||||
def __init__(self, tx=None): | def __init__(self, tx=None): | ||||
if tx is None: | if tx is None: | ||||
self.nVersion = 1 | self.nVersion = 1 | ||||
▲ Show 20 Lines • Show All 963 Lines • Show Last 20 Lines |