def tx_to_bx_txs(self, ont_tx_msg: TxOntMessage, network_num: int, quota_type: Optional[QuotaType] = None) -> \
            List[Tuple[TxMessage, Sha256Hash, Union[bytearray, memoryview]]]:
        tx_msg = TxMessage(ont_tx_msg.tx_hash(),
                           network_num,
                           tx_val=ont_tx_msg.tx(),
                           quota_type=quota_type)

        return [(tx_msg, ont_tx_msg.tx_hash(), ont_tx_msg.tx())]
 def _get_node_tx_message(
     self
 ) -> Tuple[Union[TxOntMessage, TransactionsEthProtocolMessage], List[Tuple[
         Any, Any]]]:
     magic = 123456
     version = 1
     tx_contents = helpers.generate_bytearray(200)
     msg = TxOntMessage(magic, version, tx_contents)
     tx_contents = msg.payload()
     return msg, [(msg.tx_hash(), tx_contents)]
Example #3
0
    def test_transaction_propagation(self):
        initial_message = TxOntMessage(12345, 123,
                                       helpers.generate_bytearray(250))
        self.gateway_1.account_id = "12345"
        transaction_hash = initial_message.tx_hash()
        transaction_key = self.gateway_1._tx_service.get_transaction_key(
            transaction_hash)
        self.gateway_1_receive_message_from_blockchain(initial_message)

        time.time = MagicMock(return_value=time.time() + 1)
        self.gateway_1.alarm_queue.fire_alarms()

        self.assertTrue(
            self.gateway_1._tx_service.has_transaction_contents_by_key(
                transaction_key))
        self.assertFalse(
            self.gateway_1._tx_service.has_transaction_short_id_by_key(
                transaction_key))
        messages_for_relay = self.gateway_1_get_queued_messages_for_relay()
        self.assertEqual(1, len(messages_for_relay))

        tx_message = messages_for_relay[0]
        self.assertIsInstance(tx_message, TxMessage)
        self.assertEqual(tx_message.tx_hash(), transaction_hash)

        tx_message_with_short_id = TxMessage(tx_message.message_hash(),
                                             tx_message.network_num(),
                                             short_id=10,
                                             tx_val=tx_message.tx_val())
        self.gateway_2_receive_message_from_relay(tx_message_with_short_id)

        self.assertTrue(
            self.gateway_2._tx_service.has_transaction_contents_by_key(
                transaction_key))
        self.assertTrue(
            self.gateway_2._tx_service.has_transaction_short_id_by_key(
                transaction_key))
        self.assertEqual(
            10,
            self.gateway_2._tx_service.get_short_id_by_key(transaction_key))

        messages_for_blockchain = self.gateway_2_get_queued_messages_for_blockchain(
        )
        self.assertEqual(1, len(messages_for_blockchain))

        tx_ont_message = messages_for_blockchain[0]
        self.assertIsInstance(tx_ont_message, TxOntMessage)
        self.assertEqual(tx_ont_message.tx_hash(), transaction_hash)
        self.assertEqual(tx_ont_message.tx(), initial_message.tx())
Example #4
0
    def bx_tx_to_tx(self, tx_msg: TxMessage):
        # pyre-fixme[6]: Expected `bytes` for 1st param but got `memoryview`.
        buf = bytearray(ont_constants.ONT_HDR_COMMON_OFF) + tx_msg.tx_val()
        raw_ont_tx_msg = OntMessage(self._ont_magic, TxOntMessage.MESSAGE_TYPE, len(tx_msg.tx_val()), buf)
        ont_tx_msg = TxOntMessage(buf=raw_ont_tx_msg.buf)

        return ont_tx_msg
    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_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)
Example #7
0
 def ont_transactions(self,
                      block: BlockOntMessage = None) -> List[memoryview]:
     txs = block.txns()
     return [TxOntMessage(self.magic, self.version, tx[1:]) for tx in txs]