def __init__(self, magic=None, addrs=None, buf=None): if addrs is None: addrs = [] if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 9 + len(addrs) * (4 + 18)) self.buf = buf off = BTC_HDR_COMMON_OFF off += pack_int_to_btc_varint(len(addrs), buf, off) for triplet in addrs: # pack the timestamp struct.pack_into('<L', buf, off, triplet[0]) off += 4 # pack the host ip and port pair buf[off:off + 18] = ipaddrport_to_btcbytearray( triplet[1], triplet[2]) off += 18 BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None
def __init__(self, magic=None, message=None, ccode=None, reason=None, b_data=None, buf=None): if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 9 + len(message) + 1 + 9 + len(reason) + len(b_data)) self.buf = buf off = BTC_HDR_COMMON_OFF struct.pack_into('<%dpB' % (len(message) + 1, ), buf, off, message, ccode) off += len(message) + 1 + 1 struct.pack_into('<%dp' % (len(reason) + 1, ), buf, off, reason) off += len(reason) + 1 buf[off:off + len(b_data)] = b_data off += len(b_data) BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._message = self._ccode = self._reason = self._data = self._obj_hash = None
def __init__(self, magic: int = None, on_flag: bool = None, version: int = None, buf: memoryview = None): if buf is None: # pyre-fixme[9]: buf has type `memoryview`; used as `bytearray`. buf = bytearray(BTC_HDR_COMMON_OFF + self.ON_FLAG_LEN + self.VERSION_LEN) off = BTC_HDR_COMMON_OFF struct.pack_into("<B", buf, off, on_flag) off += self.ON_FLAG_LEN struct.pack_into("<Q", buf, off, version) off += self.VERSION_LEN BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._memoryview = memoryview(buf) self._on_flag = self._version = None
def __init__(self, magic=None, version=None, hashes=None, hash_stop=None, command=None, buf=None): if hashes is None: hashes = [] if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 9 + (len(hashes) + 1) * 32) self.buf = buf off = BTC_HDR_COMMON_OFF struct.pack_into("<I", buf, off, version) off += 4 off += pack_int_to_btc_varint(len(hashes), buf, off) for hash_val in hashes: buf[off:off + 32] = hash_val.get_big_endian() off += 32 buf[off:off + 32] = hash_stop.get_big_endian() off += 32 BtcMessage.__init__(self, magic, command, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._version = self._hash_count = self._hashes = self._hash_stop = None
def __init__(self, magic=None, buf=None): if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF) self.buf = buf BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, 0, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None
def __init__(self, magic=None, version=None, dst_ip=None, dst_port=None, src_ip=None, src_port=None, nonce=None, start_height=None, user_agent=None, services=BTC_NODE_SERVICES, buf=None): if buf is None: buf = bytearray(109 + len(user_agent)) self.buf = buf off = BTC_HDR_COMMON_OFF struct.pack_into('<IQQQ', buf, off, version, services, int(time.time()), 1) off += 28 buf[off:off + BTC_IP_ADDR_PORT_SIZE] = ipaddrport_to_btcbytearray( dst_ip, dst_port) off += BTC_IP_ADDR_PORT_SIZE struct.pack_into('<Q', buf, off, 1) off += 8 buf[off:off + BTC_IP_ADDR_PORT_SIZE] = ipaddrport_to_btcbytearray( src_ip, src_port) off += BTC_IP_ADDR_PORT_SIZE struct.pack_into('<Q%dpL' % (len(user_agent) + 1, ), buf, off, nonce, user_agent, start_height) off += 8 + len(user_agent) + 1 + 4 # we do not send or parse the relay boolean. if present, we benignly ignore it. BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._memoryview = memoryview(buf) self._version = self._services = self._timestamp = self._dst_ip = self._dst_port = None self._src_ip = self._src_port = self._nonce = self._start_height = self._user_agent = None
def __init__(self, magic=None, buf=None): if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 8) self.buf = buf off = BTC_HDR_COMMON_OFF struct.pack_into("<Q", buf, off, random.randint(0, sys.maxsize)) off += 8 BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._nonce = None
def test_parse_message_success_all_types(self): # TODO: pull these numbers into constants, along with all the BTC messages self.create_message_successfully(self.VERSION_BTC_MESSAGE, VersionBtcMessage) self.create_message_successfully(VerAckBtcMessage(self.MAGIC), VerAckBtcMessage) self.create_message_successfully(PingBtcMessage(self.MAGIC), PingBtcMessage) self.create_message_successfully(PongBtcMessage(self.MAGIC, 123), PongBtcMessage) self.create_message_successfully(GetAddrBtcMessage(self.MAGIC), GetAddrBtcMessage) self.create_message_successfully( AddrBtcMessage(self.MAGIC, [(int(time.time()), "127.0.0.1", 8000)]), AddrBtcMessage) inv_vector = [(1, self.HASH), (2, self.HASH)] self.create_message_successfully(InvBtcMessage(self.MAGIC, inv_vector), InvBtcMessage) self.create_message_successfully( GetDataBtcMessage(self.MAGIC, inv_vector), GetDataBtcMessage) self.create_message_successfully( NotFoundBtcMessage(self.MAGIC, inv_vector), NotFoundBtcMessage) hashes = [self.HASH, self.HASH] self.create_message_successfully( GetHeadersBtcMessage(self.MAGIC, self.VERSION, hashes, self.HASH), GetHeadersBtcMessage) self.create_message_successfully( GetBlocksBtcMessage(self.MAGIC, self.VERSION, hashes, self.HASH), GetBlocksBtcMessage) self.create_message_successfully( TxBtcMessage(self.MAGIC, self.VERSION, [], [], 0), TxBtcMessage) txs = [TxIn(buf=bytearray(10), length=10, off=0).rawbytes()] * 5 self.create_message_successfully( BlockBtcMessage(self.MAGIC, self.VERSION, self.HASH, self.HASH, 0, 0, 0, txs), BlockBtcMessage) self.create_message_successfully( HeadersBtcMessage(self.MAGIC, [helpers.generate_bytearray(81)] * 2), HeadersBtcMessage) self.create_message_successfully( RejectBtcMessage(self.MAGIC, b"a message", RejectBtcMessage.REJECT_MALFORMED, b"test break", helpers.generate_bytearray(10)), RejectBtcMessage) self.create_message_successfully(SendHeadersBtcMessage(self.MAGIC), SendHeadersBtcMessage) self.create_message_successfully( FeeFilterBtcMessage(self.MAGIC, fee_rate=100), FeeFilterBtcMessage) self.create_message_successfully( BtcMessage(self.MAGIC, b'xversion', 0, bytearray(30)), XversionBtcMessage)
def bx_tx_to_tx(self, tx_msg): if not isinstance(tx_msg, TxMessage): raise TypeError("tx_msg is expected to be of type TxMessage") buf = bytearray(btc_constants.BTC_HDR_COMMON_OFF) + tx_msg.tx_val() raw_btc_tx_msg = BtcMessage(self._btc_magic, TxBtcMessage.MESSAGE_TYPE, len(tx_msg.tx_val()), buf) btc_tx_msg = TxBtcMessage(buf=raw_btc_tx_msg.buf) return btc_tx_msg
def __init__(self, magic=None, headers=None, buf=None): if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 9 + len(headers) * 81) self.buf = buf off = BTC_HDR_COMMON_OFF off += pack_int_to_btc_varint(len(headers), buf, off) for header in headers: buf[off:off + 81] = header off += 81 BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(self.buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._headers = self._header_count = self._block_hash = None
def __init__(self, magic=None, nonce=None, buf=None): if buf is None: buf = bytearray(BTC_HDR_COMMON_OFF + 8) self.buf = buf off = BTC_HDR_COMMON_OFF if nonce != -1: struct.pack_into('<Q', buf, off, nonce) off += 8 BtcMessage.__init__(self, magic, self.MESSAGE_TYPE, off - BTC_HDR_COMMON_OFF, buf) else: self.buf = buf self._memoryview = memoryview(buf) self._magic = self._command = self._payload_len = self._checksum = None self._payload = None self._nonce = None