def test_convert_tx_to_jsonrpc_request_for_message_transaction(self):
     # Message send
     msg_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
         .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
     tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
         msg_transaction)
     self.assertTrue(is_message_transaction(tx_dict))
Exemplo n.º 2
0
    def test_send_message(self, _make_id):
        message_transaction = MessageTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .data(self.setting["data"]) \
            .timestamp(self.setting["timestamp"]) \
            .build()

        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            message_transaction)
        self.assertTrue(is_message_transaction(tx_dict))
        signed_transaction = SignedTransaction(message_transaction,
                                               self.wallet)

        with requests_mock.Mocker() as m:
            tx_hash = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"
            expected_request = {
                'id': 1234,
                'jsonrpc': '2.0',
                'method': 'icx_sendTransaction',
                'params': {
                    'data':
                    self.setting["data"],
                    'dataType':
                    'message',
                    'from':
                    self.setting["from"],
                    'nid':
                    hex(self.setting["nid"]),
                    'nonce':
                    hex(self.setting["nonce"]),
                    'timestamp':
                    hex(self.setting["timestamp"]),
                    'signature':
                    signed_transaction.signed_transaction_dict["signature"],
                    'stepLimit':
                    hex(self.setting["step_limit"]),
                    'to':
                    self.setting["to"],
                    'version':
                    hex(3)
                }
            }

            response_json = {'jsonrpc': '2.0', 'result': tx_hash, 'id': 1234}

            m.post(self.matcher, json=response_json)
            result = self.icon_service.send_transaction(signed_transaction)
            self.assertTrue(is_T_HASH(result))
            actual_request = json.loads(m._adapter.last_request.text)
            self.assertEqual(expected_request, actual_request)
Exemplo n.º 3
0
    def test_to_dict(self):
        # Transfer
        # When having an optional property, nonce
        icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            icx_transaction)
        self.assertTrue(is_icx_transaction(tx_dict))
        # When not having an optional property, nonce
        icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .value(self.setting["value"]).step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            icx_transaction)
        self.assertTrue(is_icx_transaction(tx_dict))
        # When not having an required property, value
        icx_transaction = TransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            icx_transaction)
        self.assertFalse(is_icx_transaction(tx_dict))

        # Update SCORE
        deploy_transaction = DeployTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).content_type(self.setting["content_type"]) \
            .content(self.setting["content_update"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            deploy_transaction)
        self.assertTrue(is_deploy_transaction(tx_dict))

        # Install SCORE
        deploy_transaction = DeployTransactionBuilder().from_(self.setting["from"]).to(self.setting["to_install"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).nonce(self.setting["nonce"]) \
            .content_type(self.setting["content_type"]).content(self.setting["content_install"]) \
            .params(self.setting["params_install"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            deploy_transaction)
        self.assertTrue(is_deploy_transaction(tx_dict))

        # SCORE method call
        call_transaction = CallTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).nonce(self.setting["nonce"]) \
            .method(self.setting["method"]).params(self.setting["params_call"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            call_transaction)
        self.assertTrue(is_call_transaction(tx_dict))

        # Message send
        msg_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(
            msg_transaction)
        self.assertTrue(is_message_transaction(tx_dict))
    def test_send_message(self):

        # Checks if making an instance of message transaction correctly
        message_transaction = MessageTransactionBuilder()\
            .from_(self.setting["from"])\
            .to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"])\
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"])\
            .data(self.setting["data"]).build()
        tx_dict = SignedTransaction.to_dict(message_transaction)
        self.assertTrue(is_message_transaction(tx_dict))

        # Checks if sending transaction correctly
        signed_transaction_dict = SignedTransaction(message_transaction,
                                                    self.wallet)
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))

        # When having an optional property, nonce
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction_dict = SignedTransaction(message_transaction,
                                                    self.wallet)
        result = self.icon_service.send_transaction(signed_transaction_dict)
        self.assertTrue(is_T_HASH(result))

        # When address is wrong
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"][2:]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction_dict = SignedTransaction(message_transaction,
                                                    self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction,
                          signed_transaction_dict)

        # When not having a required property, nid
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"][2:]) \
            .step_limit(self.setting["step_limit"]).data(self.setting["data"]).build()
        signed_transaction_dict = SignedTransaction(message_transaction,
                                                    self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction,
                          signed_transaction_dict)

        # When a sending address is wrong - not the wallet's address
        message_transaction = MessageTransactionBuilder().from_(self.setting["to"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction_dict = SignedTransaction(message_transaction,
                                                    self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction,
                          signed_transaction_dict)
Exemplo n.º 5
0
    def test_send_message(self):
        sleep_time = 2

        # Checks if making an instance of message transaction correctly
        message_transaction = MessageTransactionBuilder()\
            .from_(self.setting["from"])\
            .to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"])\
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"])\
            .data(self.setting["data"]).build()
        tx_dict = SignedTransaction.convert_tx_to_jsonrpc_request(message_transaction)
        self.assertTrue(is_message_transaction(tx_dict))

        # Checks if sending transaction correctly
        signed_transaction = SignedTransaction(message_transaction, self.wallet)
        result = self.icon_service.send_transaction(signed_transaction)
        self.assertTrue(is_T_HASH(result))

        # When having an optional property, nonce
        sleep(sleep_time)
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction = SignedTransaction(message_transaction, self.wallet)
        result = self.icon_service.send_transaction(signed_transaction)
        self.assertTrue(is_T_HASH(result))

        # When the data is hex string
        sleep(sleep_time)
        tx_result = self.icon_service.get_transaction_result(result)
        tx = self.icon_service.get_transaction(result)

        # Checks the transaction
        self.assertEqual(tx["data"], self.setting["data"])
        block = self.icon_service.get_block(int(tx_result["blockHeight"]))

        # Checks the block's transaction
        self.assertEqual(block["confirmed_transaction_list"][0]["data"], self.setting["data"])

        # When data is not hex string
        message_transaction = MessageTransactionBuilder() \
            .from_(self.setting["from"]) \
            .to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]) \
            .nid(self.setting["nid"]) \
            .nonce(self.setting["nonce"]) \
            .data("test")

        # Raise DataTypeException
        self.assertRaises(DataTypeException, message_transaction.build)

        # When address is wrong
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"][2:]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction = SignedTransaction(message_transaction, self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction, signed_transaction)

        # When not having a required property, nid
        message_transaction = MessageTransactionBuilder().from_(self.setting["from"]).to(self.setting["to"][2:]) \
            .step_limit(self.setting["step_limit"]).data(self.setting["data"]).build()
        signed_transaction = SignedTransaction(message_transaction, self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction, signed_transaction)

        # When a sending address is wrong - not the wallet's address
        message_transaction = MessageTransactionBuilder().from_(self.setting["to"]).to(self.setting["to"]) \
            .step_limit(self.setting["step_limit"]).nid(self.setting["nid"]).data(self.setting["data"]).build()
        signed_transaction = SignedTransaction(message_transaction, self.wallet)
        self.assertRaises(JSONRPCException, self.icon_service.send_transaction, signed_transaction)