Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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
Пример #4
0
    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
Пример #5
0
    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
Пример #6
0
    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
Пример #7
0
    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
Пример #8
0
    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
Пример #9
0
    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