def __init__(self,
                 connection: AbstractGatewayBlockchainConnection) -> None:
        super(OntBaseConnectionProtocol,
              self).__init__(connection,
                             block_cleanup_poll_interval_s=ont_constants.
                             BLOCK_CLEANUP_NODE_BLOCK_LIST_POLL_INTERVAL_S)
        self.node = typing.cast(
            "bxgateway.connections.ont.ont_gateway_node.OntGatewayNode",
            connection.node)
        self.magic = self.node.opts.blockchain_net_magic
        self.version = self.node.opts.blockchain_version
        self.is_consensus = self.node.opts.is_consensus

        connection.hello_messages = ont_constants.ONT_HELLO_MESSAGES
        connection.header_size = ont_constants.ONT_HDR_COMMON_OFF
        connection.message_factory = ont_message_factory
        connection.message_handlers = {
            OntMessageType.PING: self.msg_ping,
            OntMessageType.PONG: self.msg_pong,
            OntMessageType.GET_ADDRESS: self.msg_getaddr
        }

        version_msg = VersionOntMessage(
            self.magic, self.version, self.node.opts.blockchain_port,
            self.node.opts.http_info_port, self.node.opts.consensus_port,
            ont_constants.STARTUP_CAP, self.node.opts.blockchain_nonce, 0,
            self.node.opts.relay, self.node.opts.is_consensus,
            f"{gateway_constants.GATEWAY_PEER_NAME} "
            f"{self.node.opts.source_version}".encode(
                constants.DEFAULT_TEXT_ENCODING),
            self.node.opts.blockchain_services)
        connection.enqueue_msg(version_msg)
    def test_version_and_verack(self):
        ver_msg = VersionOntMessage(123, 234, 20330, 20330, 20330, bytes(32), 123, 0,
                                    True, True, "v1.0.0".encode("utf-8"))
        self.sut.msg_version(ver_msg)

        verack_msg_bytes = self.sut.connection.get_bytes_to_send()
        verack_msg = VerAckOntMessage(buf=verack_msg_bytes)
        self.assertEqual(True, verack_msg.is_consensus())
Пример #3
0
class OntMessageFactoryTest(MessageFactoryTestCase):
    MAGIC = 12345
    VERSION = 111
    HASH = OntObjectHash(binary=crypto.double_sha256(b"123"))

    VERSION_ONT_MESSAGE = VersionOntMessage(MAGIC,
                                            VERSION, 20330, 20330, 20330,
                                            bytes(32), 123, 0, True, True,
                                            "v1.0.0".encode("utf-8"))

    def get_message_factory(self):
        return ont_message_factory

    def test_peek_message_success_all_types(self):
        self.get_message_preview_successfully(self.VERSION_ONT_MESSAGE,
                                              VersionOntMessage.MESSAGE_TYPE,
                                              83)
        self.get_message_preview_successfully(
            VerAckOntMessage(self.MAGIC, True), VerAckOntMessage.MESSAGE_TYPE,
            1)
        self.get_message_preview_successfully(PingOntMessage(self.MAGIC),
                                              PingOntMessage.MESSAGE_TYPE, 8)
        self.get_message_preview_successfully(PongOntMessage(self.MAGIC, 123),
                                              PongOntMessage.MESSAGE_TYPE, 8)
        self.get_message_preview_successfully(GetAddrOntMessage(self.MAGIC),
                                              GetAddrOntMessage.MESSAGE_TYPE,
                                              0)
        self.get_message_preview_successfully(
            AddrOntMessage(
                self.MAGIC,
                [(int(time.time()), 123, "127.0.0.1", 20300, 20200, 1234)]),
            AddrOntMessage.MESSAGE_TYPE, 52)
        self.get_message_preview_successfully(
            OntConsensusMessage(self.MAGIC, self.VERSION, bytes(20)),
            OntConsensusMessage.MESSAGE_TYPE, 24)

        self.get_message_preview_successfully(
            InvOntMessage(self.MAGIC, InventoryOntType.MSG_TX,
                          [self.HASH, self.HASH]), InvOntMessage.MESSAGE_TYPE,
            69)
        self.get_message_preview_successfully(
            GetDataOntMessage(self.MAGIC, 1, self.HASH),
            GetDataOntMessage.MESSAGE_TYPE, 33)
        self.get_message_preview_successfully(
            GetHeadersOntMessage(self.MAGIC, 1, self.HASH, self.HASH),
            GetHeadersOntMessage.MESSAGE_TYPE, 65)
        self.get_message_preview_successfully(
            GetBlocksOntMessage(self.MAGIC, 1, self.HASH, self.HASH),
            GetBlocksOntMessage.MESSAGE_TYPE, 65)
        self.get_message_preview_successfully(
            TxOntMessage(self.MAGIC, self.VERSION, bytes(20)),
            TxOntMessage.MESSAGE_TYPE, 21)
        self.get_message_preview_successfully(
            BlockOntMessage(self.MAGIC, self.VERSION, self.HASH, self.HASH,
                            self.HASH, 0, 0, 0, bytes(10), bytes(20),
                            [bytes(33)] * 5, [bytes(2)] * 3, [bytes(32)] * 5,
                            self.HASH), BlockOntMessage.MESSAGE_TYPE, 524)
        self.get_message_preview_successfully(
            HeadersOntMessage(self.MAGIC, [bytes(1)] * 2),
            HeadersOntMessage.MESSAGE_TYPE, 6)
        self.get_message_preview_successfully(
            NotFoundOntMessage(self.MAGIC, self.HASH),
            NotFoundOntMessage.MESSAGE_TYPE, 32)

    def test_peek_message_incomplete(self):
        is_full_message, command, payload_length = ont_message_factory.get_message_header_preview_from_input_buffer(
            create_input_buffer_with_bytes(
                self.VERSION_ONT_MESSAGE.rawbytes()[:-10]))
        self.assertFalse(is_full_message)
        self.assertEqual(b"version", command)
        self.assertEqual(83, payload_length)

        is_full_message, command, payload_length = ont_message_factory.get_message_header_preview_from_input_buffer(
            create_input_buffer_with_bytes(
                self.VERSION_ONT_MESSAGE.rawbytes()[:1]))
        self.assertFalse(is_full_message)
        self.assertIsNone(command)
        self.assertIsNone(payload_length)

    def test_parse_message_success_all_types(self):
        self.create_message_successfully(self.VERSION_ONT_MESSAGE,
                                         VersionOntMessage)
        self.create_message_successfully(VerAckOntMessage(self.MAGIC, False),
                                         VerAckOntMessage)
        self.create_message_successfully(PingOntMessage(self.MAGIC),
                                         PingOntMessage)
        self.create_message_successfully(PongOntMessage(self.MAGIC, 123),
                                         PongOntMessage)
        self.create_message_successfully(GetAddrOntMessage(self.MAGIC),
                                         GetAddrOntMessage)
        self.create_message_successfully(
            AddrOntMessage(
                self.MAGIC,
                [(int(time.time()), 123, "127.0.0.1", 20300, 20200, 1234)]),
            AddrOntMessage)
        self.create_message_successfully(
            OntConsensusMessage(self.MAGIC, self.VERSION, bytes(20)),
            OntConsensusMessage)
        self.create_message_successfully(
            InvOntMessage(self.MAGIC, InventoryOntType.MSG_TX,
                          [self.HASH, self.HASH]), InvOntMessage)
        self.create_message_successfully(
            GetDataOntMessage(self.MAGIC, 1, self.HASH), GetDataOntMessage)
        self.create_message_successfully(
            GetHeadersOntMessage(self.MAGIC, 1, self.HASH, self.HASH),
            GetHeadersOntMessage)
        self.create_message_successfully(
            GetBlocksOntMessage(self.MAGIC, 1, self.HASH, self.HASH),
            GetBlocksOntMessage)
        self.create_message_successfully(
            TxOntMessage(self.MAGIC, self.VERSION, bytes(20)), TxOntMessage)
        self.create_message_successfully(
            BlockOntMessage(self.MAGIC, self.VERSION, self.HASH, self.HASH,
                            self.HASH, 0, 0, 0, bytes(10), bytes(20),
                            [bytes(33)] * 5, [bytes(2)] * 3, [bytes(32)] * 5,
                            self.HASH), BlockOntMessage)
        self.create_message_successfully(
            HeadersOntMessage(self.MAGIC, [bytes(1)] * 2), HeadersOntMessage)
        self.create_message_successfully(
            NotFoundOntMessage(self.MAGIC, self.HASH), NotFoundOntMessage)

    def test_parse_message_incomplete(self):
        with self.assertRaises(PayloadLenError):
            ont_message_factory.create_message_from_buffer(
                PingOntMessage(self.MAGIC).rawbytes()[:-1])

        ping_message = PingOntMessage(self.MAGIC)
        for i in range(ONT_HEADER_MINUS_CHECKSUM, ONT_HDR_COMMON_OFF):
            ping_message.buf[i] = 0
        with self.assertRaises(ChecksumError):
            ont_message_factory.create_message_from_buffer(
                ping_message.rawbytes())