Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/messages.py
Show First 20 Lines • Show All 234 Lines • ▼ Show 20 Lines | def serialize(self, with_time=True): | ||||
r += struct.pack("<i", self.time) | r += struct.pack("<i", self.time) | ||||
r += struct.pack("<Q", self.nServices) | r += struct.pack("<Q", self.nServices) | ||||
r += self.pchReserved | r += self.pchReserved | ||||
r += socket.inet_aton(self.ip) | r += socket.inet_aton(self.ip) | ||||
r += struct.pack(">H", self.port) | r += struct.pack(">H", self.port) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CAddress(nServices=%i ip=%s port=%i)" % (self.nServices, | return "CAddress(nServices={} ip={} port={})".format( | ||||
self.ip, self.port) | self.nServices, self.ip, self.port) | ||||
class CInv(): | class CInv(): | ||||
typemap = { | typemap = { | ||||
0: "Error", | 0: "Error", | ||||
1: "TX", | 1: "TX", | ||||
2: "Block", | 2: "Block", | ||||
4: "CompactBlock" | 4: "CompactBlock" | ||||
Show All 9 Lines | class CInv(): | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<i", self.type) | r += struct.pack("<i", self.type) | ||||
r += ser_uint256(self.hash) | r += ser_uint256(self.hash) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CInv(type=%s hash=%064x)" \ | return "CInv(type={} hash={:064x})".format( | ||||
% (self.typemap[self.type], self.hash) | self.typemap[self.type], self.hash) | ||||
class CBlockLocator(): | class CBlockLocator(): | ||||
def __init__(self): | def __init__(self): | ||||
self.nVersion = MY_VERSION | self.nVersion = MY_VERSION | ||||
self.vHave = [] | self.vHave = [] | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.nVersion = struct.unpack("<i", f.read(4))[0] | self.nVersion = struct.unpack("<i", f.read(4))[0] | ||||
self.vHave = deser_uint256_vector(f) | self.vHave = deser_uint256_vector(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<i", self.nVersion) | r += struct.pack("<i", self.nVersion) | ||||
r += ser_uint256_vector(self.vHave) | r += ser_uint256_vector(self.vHave) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CBlockLocator(nVersion=%i vHave=%s)" \ | return "CBlockLocator(nVersion={} vHave={})".format( | ||||
% (self.nVersion, repr(self.vHave)) | self.nVersion, repr(self.vHave)) | ||||
class COutPoint(): | class COutPoint(): | ||||
def __init__(self, hash=0, n=0): | def __init__(self, hash=0, n=0): | ||||
self.hash = hash | self.hash = hash | ||||
self.n = n | self.n = n | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.hash = deser_uint256(f) | self.hash = deser_uint256(f) | ||||
self.n = struct.unpack("<I", f.read(4))[0] | self.n = struct.unpack("<I", f.read(4))[0] | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += ser_uint256(self.hash) | r += ser_uint256(self.hash) | ||||
r += struct.pack("<I", self.n) | r += struct.pack("<I", self.n) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "COutPoint(hash=%064x n=%i)" % (self.hash, self.n) | return "COutPoint(hash={:064x} n={})".format(self.hash, self.n) | ||||
class CTxIn(): | class CTxIn(): | ||||
def __init__(self, outpoint=None, scriptSig=b"", nSequence=0): | def __init__(self, outpoint=None, scriptSig=b"", nSequence=0): | ||||
if outpoint is None: | if outpoint is None: | ||||
self.prevout = COutPoint() | self.prevout = COutPoint() | ||||
else: | else: | ||||
self.prevout = outpoint | self.prevout = outpoint | ||||
Show All 9 Lines | class CTxIn(): | ||||
def serialize(self): | 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=%s scriptSig=%s nSequence=%i)" \ | return "CTxIn(prevout={} scriptSig={} nSequence={})".format( | ||||
% (repr(self.prevout), bytes_to_hex_str(self.scriptSig), | repr(self.prevout), bytes_to_hex_str(self.scriptSig), self.nSequence) | ||||
self.nSequence) | |||||
class CTxOut(): | class CTxOut(): | ||||
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=%i.%08i scriptPubKey=%s)" \ | return "CTxOut(nValue={}.{:08d} scriptPubKey={})".format( | ||||
% (self.nValue // COIN, self.nValue % COIN, | self.nValue // COIN, self.nValue % COIN, | ||||
bytes_to_hex_str(self.scriptPubKey)) | bytes_to_hex_str(self.scriptPubKey)) | ||||
class CTransaction(): | class CTransaction(): | ||||
def __init__(self, tx=None): | def __init__(self, tx=None): | ||||
if tx is None: | if tx is None: | ||||
self.nVersion = 1 | self.nVersion = 1 | ||||
self.vin = [] | self.vin = [] | ||||
self.vout = [] | self.vout = [] | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | class CTransaction(): | ||||
def is_valid(self): | def is_valid(self): | ||||
self.calc_sha256() | self.calc_sha256() | ||||
for tout in self.vout: | for tout in self.vout: | ||||
if tout.nValue < 0 or tout.nValue > 21000000 * COIN: | if tout.nValue < 0 or tout.nValue > 21000000 * COIN: | ||||
return False | return False | ||||
return True | return True | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CTransaction(nVersion=%i vin=%s vout=%s nLockTime=%i)" \ | return "CTransaction(nVersion={} vin={} vout={} nLockTime={})".format( | ||||
% (self.nVersion, repr(self.vin), repr(self.vout), self.nLockTime) | self.nVersion, repr(self.vin), repr(self.vout), self.nLockTime) | ||||
class CBlockHeader(): | class CBlockHeader(): | ||||
def __init__(self, header=None): | def __init__(self, header=None): | ||||
if header is None: | if header is None: | ||||
self.set_null() | self.set_null() | ||||
else: | else: | ||||
self.nVersion = header.nVersion | self.nVersion = header.nVersion | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | def calc_sha256(self): | ||||
self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') | self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') | ||||
def rehash(self): | def rehash(self): | ||||
self.sha256 = None | self.sha256 = None | ||||
self.calc_sha256() | self.calc_sha256() | ||||
return self.sha256 | return self.sha256 | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CBlockHeader(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x)" \ | return "CBlockHeader(nVersion={} hashPrevBlock={:064x} hashMerkleRoot={:064x} nTime={} nBits={:08x} nNonce={:08x})".format( | ||||
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, | self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, | ||||
time.ctime(self.nTime), self.nBits, self.nNonce) | time.ctime(self.nTime), self.nBits, self.nNonce) | ||||
class CBlock(CBlockHeader): | class CBlock(CBlockHeader): | ||||
def __init__(self, header=None): | def __init__(self, header=None): | ||||
super(CBlock, self).__init__(header) | super(CBlock, self).__init__(header) | ||||
self.vtx = [] | self.vtx = [] | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
Show All 38 Lines | class CBlock(CBlockHeader): | ||||
def solve(self): | def solve(self): | ||||
self.rehash() | self.rehash() | ||||
target = uint256_from_compact(self.nBits) | target = uint256_from_compact(self.nBits) | ||||
while self.sha256 > target: | while self.sha256 > target: | ||||
self.nNonce += 1 | self.nNonce += 1 | ||||
self.rehash() | self.rehash() | ||||
def __repr__(self): | def __repr__(self): | ||||
return "CBlock(nVersion=%i hashPrevBlock=%064x hashMerkleRoot=%064x nTime=%s nBits=%08x nNonce=%08x vtx=%s)" \ | return "CBlock(nVersion={} hashPrevBlock={:064x} hashMerkleRoot={:064x} nTime={} nBits={:08x} nNonce={:08x} vtx={})".format( | ||||
% (self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, | self.nVersion, self.hashPrevBlock, self.hashMerkleRoot, | ||||
time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx)) | time.ctime(self.nTime), self.nBits, self.nNonce, repr(self.vtx)) | ||||
class PrefilledTransaction(): | class PrefilledTransaction(): | ||||
def __init__(self, index=0, tx=None): | def __init__(self, index=0, tx=None): | ||||
self.index = index | self.index = index | ||||
self.tx = tx | self.tx = tx | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.index = deser_compact_size(f) | self.index = deser_compact_size(f) | ||||
self.tx = CTransaction() | self.tx = CTransaction() | ||||
self.tx.deserialize(f) | self.tx.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += ser_compact_size(self.index) | r += ser_compact_size(self.index) | ||||
r += self.tx.serialize() | r += self.tx.serialize() | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "PrefilledTransaction(index=%d, tx=%s)" % (self.index, repr(self.tx)) | return "PrefilledTransaction(index={}, tx={})".format( | ||||
self.index, repr(self.tx)) | |||||
# This is what we send on the wire, in a cmpctblock message. | # This is what we send on the wire, in a cmpctblock message. | ||||
class P2PHeaderAndShortIDs(): | class P2PHeaderAndShortIDs(): | ||||
def __init__(self): | def __init__(self): | ||||
self.header = CBlockHeader() | self.header = CBlockHeader() | ||||
self.nonce = 0 | self.nonce = 0 | ||||
Show All 21 Lines | def serialize(self): | ||||
r += ser_compact_size(self.shortids_length) | r += ser_compact_size(self.shortids_length) | ||||
for x in self.shortids: | for x in self.shortids: | ||||
# We only want the first 6 bytes | # We only want the first 6 bytes | ||||
r += struct.pack("<Q", x)[0:6] | r += struct.pack("<Q", x)[0:6] | ||||
r += ser_vector(self.prefilled_txn) | r += ser_vector(self.prefilled_txn) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "P2PHeaderAndShortIDs(header=%s, nonce=%d, shortids_length=%d, shortids=%s, prefilled_txn_length=%d, prefilledtxn=%s" % (repr(self.header), self.nonce, self.shortids_length, repr(self.shortids), self.prefilled_txn_length, repr(self.prefilled_txn)) | return "P2PHeaderAndShortIDs(header={}, nonce={}, shortids_length={}, shortids={}, prefilled_txn_length={}, prefilledtxn={}".format( | ||||
repr(self.header), self.nonce, self.shortids_length, | |||||
repr(self.shortids), self.prefilled_txn_length, | |||||
repr(self.prefilled_txn)) | |||||
# Calculate the BIP 152-compact blocks shortid for a given transaction hash | # 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): | ||||
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 | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | def initialize_from_block(self, block, nonce=0, prefill_list=[0]): | ||||
self.shortids = [] | self.shortids = [] | ||||
[k0, k1] = self.get_siphash_keys() | [k0, k1] = self.get_siphash_keys() | ||||
for i in range(len(block.vtx)): | for i in range(len(block.vtx)): | ||||
if i not in prefill_list: | if i not in prefill_list: | ||||
tx_hash = block.vtx[i].sha256 | tx_hash = block.vtx[i].sha256 | ||||
self.shortids.append(calculate_shortid(k0, k1, tx_hash)) | self.shortids.append(calculate_shortid(k0, k1, tx_hash)) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "HeaderAndShortIDs(header=%s, nonce=%d, shortids=%s, prefilledtxn=%s" % (repr(self.header), self.nonce, repr(self.shortids), repr(self.prefilled_txn)) | return "HeaderAndShortIDs(header={}, nonce={}, shortids={}, prefilledtxn={}".format( | ||||
repr(self.header), self.nonce, repr(self.shortids), | |||||
repr(self.prefilled_txn)) | |||||
class BlockTransactionsRequest(): | class BlockTransactionsRequest(): | ||||
def __init__(self, blockhash=0, indexes=None): | def __init__(self, blockhash=0, indexes=None): | ||||
self.blockhash = blockhash | self.blockhash = blockhash | ||||
self.indexes = indexes if indexes != None else [] | self.indexes = indexes if indexes != None else [] | ||||
Show All 23 Lines | def to_absolute(self): | ||||
absolute_indexes = [] | absolute_indexes = [] | ||||
last_index = -1 | last_index = -1 | ||||
for x in self.indexes: | for x in self.indexes: | ||||
absolute_indexes.append(x + last_index + 1) | absolute_indexes.append(x + last_index + 1) | ||||
last_index = absolute_indexes[-1] | last_index = absolute_indexes[-1] | ||||
return absolute_indexes | return absolute_indexes | ||||
def __repr__(self): | def __repr__(self): | ||||
return "BlockTransactionsRequest(hash=%064x indexes=%s)" % (self.blockhash, repr(self.indexes)) | return "BlockTransactionsRequest(hash={:064x} indexes={})".format( | ||||
self.blockhash, repr(self.indexes)) | |||||
class BlockTransactions(): | class BlockTransactions(): | ||||
def __init__(self, blockhash=0, transactions=None): | def __init__(self, blockhash=0, transactions=None): | ||||
self.blockhash = blockhash | self.blockhash = blockhash | ||||
self.transactions = transactions if transactions != None else [] | self.transactions = transactions if transactions != None else [] | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.blockhash = deser_uint256(f) | self.blockhash = deser_uint256(f) | ||||
self.transactions = deser_vector(f, CTransaction) | self.transactions = deser_vector(f, CTransaction) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += ser_uint256(self.blockhash) | r += ser_uint256(self.blockhash) | ||||
r += ser_vector(self.transactions) | r += ser_vector(self.transactions) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "BlockTransactions(hash=%064x transactions=%s)" % (self.blockhash, repr(self.transactions)) | return "BlockTransactions(hash={:064x} transactions={})".format( | ||||
self.blockhash, repr(self.transactions)) | |||||
# Objects that correspond to messages on the wire | # Objects that correspond to messages on the wire | ||||
class msg_version(): | class msg_version(): | ||||
command = b"version" | command = b"version" | ||||
def __init__(self): | def __init__(self): | ||||
self.nVersion = MY_VERSION | self.nVersion = MY_VERSION | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | def serialize(self): | ||||
r += self.addrFrom.serialize(False) | r += self.addrFrom.serialize(False) | ||||
r += struct.pack("<Q", self.nNonce) | r += struct.pack("<Q", self.nNonce) | ||||
r += ser_string(self.strSubVer) | r += ser_string(self.strSubVer) | ||||
r += struct.pack("<i", self.nStartingHeight) | r += struct.pack("<i", self.nStartingHeight) | ||||
r += struct.pack("<b", self.nRelay) | r += struct.pack("<b", self.nRelay) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return 'msg_version(nVersion=%i nServices=%i nTime=%s addrTo=%s addrFrom=%s nNonce=0x%016X strSubVer=%s nStartingHeight=%i nRelay=%i)' \ | return 'msg_version(nVersion={} nServices={} nTime={} addrTo={} addrFrom={} nNonce=0x{:016X} strSubVer={} nStartingHeight={} nRelay={})'.format( | ||||
% (self.nVersion, self.nServices, time.ctime(self.nTime), | self.nVersion, self.nServices, time.ctime(self.nTime), | ||||
repr(self.addrTo), repr(self.addrFrom), self.nNonce, | repr(self.addrTo), repr(self.addrFrom), self.nNonce, | ||||
self.strSubVer, self.nStartingHeight, self.nRelay) | self.strSubVer, self.nStartingHeight, self.nRelay) | ||||
class msg_verack(): | class msg_verack(): | ||||
command = b"verack" | command = b"verack" | ||||
def __init__(self): | def __init__(self): | ||||
pass | pass | ||||
Show All 15 Lines | class msg_addr(): | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.addrs = deser_vector(f, CAddress) | self.addrs = deser_vector(f, CAddress) | ||||
def serialize(self): | def serialize(self): | ||||
return ser_vector(self.addrs) | return ser_vector(self.addrs) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_addr(addrs=%s)" % (repr(self.addrs)) | return "msg_addr(addrs={})".format(repr(self.addrs)) | ||||
class msg_inv(): | class msg_inv(): | ||||
command = b"inv" | command = b"inv" | ||||
def __init__(self, inv=None): | def __init__(self, inv=None): | ||||
if inv is None: | if inv is None: | ||||
self.inv = [] | self.inv = [] | ||||
else: | else: | ||||
self.inv = inv | self.inv = inv | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.inv = deser_vector(f, CInv) | self.inv = deser_vector(f, CInv) | ||||
def serialize(self): | def serialize(self): | ||||
return ser_vector(self.inv) | return ser_vector(self.inv) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_inv(inv=%s)" % (repr(self.inv)) | return "msg_inv(inv={})".format(repr(self.inv)) | ||||
class msg_getdata(): | class msg_getdata(): | ||||
command = b"getdata" | command = b"getdata" | ||||
def __init__(self, inv=None): | def __init__(self, inv=None): | ||||
self.inv = inv if inv != None else [] | self.inv = inv if inv != None else [] | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.inv = deser_vector(f, CInv) | self.inv = deser_vector(f, CInv) | ||||
def serialize(self): | def serialize(self): | ||||
return ser_vector(self.inv) | return ser_vector(self.inv) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_getdata(inv=%s)" % (repr(self.inv)) | return "msg_getdata(inv={})".format(repr(self.inv)) | ||||
class msg_getblocks(): | class msg_getblocks(): | ||||
command = b"getblocks" | command = b"getblocks" | ||||
def __init__(self): | def __init__(self): | ||||
self.locator = CBlockLocator() | self.locator = CBlockLocator() | ||||
self.hashstop = 0 | self.hashstop = 0 | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.locator = CBlockLocator() | self.locator = CBlockLocator() | ||||
self.locator.deserialize(f) | self.locator.deserialize(f) | ||||
self.hashstop = deser_uint256(f) | self.hashstop = deser_uint256(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.locator.serialize() | r += self.locator.serialize() | ||||
r += ser_uint256(self.hashstop) | r += ser_uint256(self.hashstop) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_getblocks(locator=%s hashstop=%064x)" \ | return "msg_getblocks(locator={} hashstop={:064x})".format( | ||||
% (repr(self.locator), self.hashstop) | repr(self.locator), self.hashstop) | ||||
class msg_tx(): | class msg_tx(): | ||||
command = b"tx" | command = b"tx" | ||||
def __init__(self, tx=CTransaction()): | def __init__(self, tx=CTransaction()): | ||||
self.tx = tx | self.tx = tx | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.tx.deserialize(f) | self.tx.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
return self.tx.serialize() | return self.tx.serialize() | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_tx(tx=%s)" % (repr(self.tx)) | return "msg_tx(tx={})".format(repr(self.tx)) | ||||
class msg_block(): | class msg_block(): | ||||
command = b"block" | command = b"block" | ||||
def __init__(self, block=None): | def __init__(self, block=None): | ||||
if block is None: | if block is None: | ||||
self.block = CBlock() | self.block = CBlock() | ||||
else: | else: | ||||
self.block = block | self.block = block | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.block.deserialize(f) | self.block.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
return self.block.serialize() | return self.block.serialize() | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_block(block=%s)" % (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(): | ||||
def __init__(self, command, data=None): | def __init__(self, command, data=None): | ||||
self.command = command | self.command = command | ||||
Show All 32 Lines | def deserialize(self, f): | ||||
self.nonce = struct.unpack("<Q", f.read(8))[0] | self.nonce = struct.unpack("<Q", f.read(8))[0] | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<Q", self.nonce) | r += struct.pack("<Q", self.nonce) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_ping(nonce=%08x)" % self.nonce | return "msg_ping(nonce={:08x})".format(self.nonce) | ||||
class msg_pong(): | class msg_pong(): | ||||
command = b"pong" | command = b"pong" | ||||
def __init__(self, nonce=0): | def __init__(self, nonce=0): | ||||
self.nonce = nonce | self.nonce = nonce | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.nonce = struct.unpack("<Q", f.read(8))[0] | self.nonce = struct.unpack("<Q", f.read(8))[0] | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<Q", self.nonce) | r += struct.pack("<Q", self.nonce) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_pong(nonce=%08x)" % self.nonce | return "msg_pong(nonce={:08x})".format(self.nonce) | ||||
class msg_mempool(): | class msg_mempool(): | ||||
command = b"mempool" | command = b"mempool" | ||||
def __init__(self): | def __init__(self): | ||||
pass | pass | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | class msg_getheaders(): | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.locator.serialize() | r += self.locator.serialize() | ||||
r += ser_uint256(self.hashstop) | r += ser_uint256(self.hashstop) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_getheaders(locator=%s, stop=%064x)" \ | return "msg_getheaders(locator={}, stop={:064x})".format( | ||||
% (repr(self.locator), self.hashstop) | repr(self.locator), self.hashstop) | ||||
# headers message has | # headers message has | ||||
# <count> <vector of block headers> | # <count> <vector of block headers> | ||||
class msg_headers(): | class msg_headers(): | ||||
command = b"headers" | command = b"headers" | ||||
def __init__(self, headers=None): | def __init__(self, headers=None): | ||||
self.headers = headers if headers is not None else [] | self.headers = headers if headers is not None else [] | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
# comment in bitcoind indicates these should be deserialized as blocks | # comment in bitcoind indicates these should be deserialized as blocks | ||||
blocks = deser_vector(f, CBlock) | blocks = deser_vector(f, CBlock) | ||||
for x in blocks: | for x in blocks: | ||||
self.headers.append(CBlockHeader(x)) | self.headers.append(CBlockHeader(x)) | ||||
def serialize(self): | def serialize(self): | ||||
blocks = [CBlock(x) for x in self.headers] | blocks = [CBlock(x) for x in self.headers] | ||||
return ser_vector(blocks) | return ser_vector(blocks) | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_headers(headers=%s)" % repr(self.headers) | return "msg_headers(headers={})".format(repr(self.headers)) | ||||
class msg_reject(): | class msg_reject(): | ||||
command = b"reject" | command = b"reject" | ||||
REJECT_MALFORMED = 1 | REJECT_MALFORMED = 1 | ||||
def __init__(self): | def __init__(self): | ||||
self.message = b"" | self.message = b"" | ||||
Show All 14 Lines | def serialize(self): | ||||
r += struct.pack("<B", self.code) | r += struct.pack("<B", self.code) | ||||
r += ser_string(self.reason) | r += ser_string(self.reason) | ||||
if (self.code != self.REJECT_MALFORMED and | if (self.code != self.REJECT_MALFORMED and | ||||
(self.message == b"block" or self.message == b"tx")): | (self.message == b"block" or self.message == b"tx")): | ||||
r += ser_uint256(self.data) | r += ser_uint256(self.data) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_reject: %s %d %s [%064x]" \ | return "msg_reject: {} {} {} [{:064x}]".format( | ||||
% (self.message, self.code, self.reason, self.data) | self.message, self.code, self.reason, self.data) | ||||
class msg_feefilter(): | class msg_feefilter(): | ||||
command = b"feefilter" | command = b"feefilter" | ||||
def __init__(self, feerate=0): | def __init__(self, feerate=0): | ||||
self.feerate = feerate | self.feerate = feerate | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.feerate = struct.unpack("<Q", f.read(8))[0] | self.feerate = struct.unpack("<Q", f.read(8))[0] | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<Q", self.feerate) | r += struct.pack("<Q", self.feerate) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_feefilter(feerate=%08x)" % self.feerate | return "msg_feefilter(feerate={:08x})".format(self.feerate) | ||||
class msg_sendcmpct(): | class msg_sendcmpct(): | ||||
command = b"sendcmpct" | command = b"sendcmpct" | ||||
def __init__(self): | def __init__(self): | ||||
self.announce = False | self.announce = False | ||||
self.version = 1 | self.version = 1 | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.announce = struct.unpack("<?", f.read(1))[0] | self.announce = struct.unpack("<?", f.read(1))[0] | ||||
self.version = struct.unpack("<Q", f.read(8))[0] | self.version = struct.unpack("<Q", f.read(8))[0] | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += struct.pack("<?", self.announce) | r += struct.pack("<?", self.announce) | ||||
r += struct.pack("<Q", self.version) | r += struct.pack("<Q", self.version) | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_sendcmpct(announce=%s, version=%lu)" % (self.announce, self.version) | return "msg_sendcmpct(announce={}, version={})".format( | ||||
self.announce, self.version) | |||||
class msg_cmpctblock(): | class msg_cmpctblock(): | ||||
command = b"cmpctblock" | command = b"cmpctblock" | ||||
def __init__(self, header_and_shortids=None): | def __init__(self, header_and_shortids=None): | ||||
self.header_and_shortids = header_and_shortids | self.header_and_shortids = header_and_shortids | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.header_and_shortids = P2PHeaderAndShortIDs() | self.header_and_shortids = P2PHeaderAndShortIDs() | ||||
self.header_and_shortids.deserialize(f) | self.header_and_shortids.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.header_and_shortids.serialize() | r += self.header_and_shortids.serialize() | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_cmpctblock(HeaderAndShortIDs=%s)" % repr(self.header_and_shortids) | return "msg_cmpctblock(HeaderAndShortIDs={})".format( | ||||
repr(self.header_and_shortids)) | |||||
class msg_getblocktxn(): | class msg_getblocktxn(): | ||||
command = b"getblocktxn" | command = b"getblocktxn" | ||||
def __init__(self): | def __init__(self): | ||||
self.block_txn_request = None | self.block_txn_request = None | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.block_txn_request = BlockTransactionsRequest() | self.block_txn_request = BlockTransactionsRequest() | ||||
self.block_txn_request.deserialize(f) | self.block_txn_request.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.block_txn_request.serialize() | r += self.block_txn_request.serialize() | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_getblocktxn(block_txn_request=%s)" % (repr(self.block_txn_request)) | return "msg_getblocktxn(block_txn_request={})".format( | ||||
repr(self.block_txn_request)) | |||||
class msg_blocktxn(): | class msg_blocktxn(): | ||||
command = b"blocktxn" | command = b"blocktxn" | ||||
def __init__(self): | def __init__(self): | ||||
self.block_transactions = BlockTransactions() | self.block_transactions = BlockTransactions() | ||||
def deserialize(self, f): | def deserialize(self, f): | ||||
self.block_transactions.deserialize(f) | self.block_transactions.deserialize(f) | ||||
def serialize(self): | def serialize(self): | ||||
r = b"" | r = b"" | ||||
r += self.block_transactions.serialize() | r += self.block_transactions.serialize() | ||||
return r | return r | ||||
def __repr__(self): | def __repr__(self): | ||||
return "msg_blocktxn(block_transactions=%s)" % (repr(self.block_transactions)) | return "msg_blocktxn(block_transactions={})".format( | ||||
repr(self.block_transactions)) |