|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2021 ICON Foundation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import json |
| 17 | +from unittest import main |
| 18 | +from unittest.mock import patch |
| 19 | + |
| 20 | +import requests_mock |
| 21 | + |
| 22 | +from iconsdk.exception import DataTypeException, JSONRPCException |
| 23 | +from iconsdk.utils.hexadecimal import remove_0x_prefix, add_cx_prefix |
| 24 | +from iconsdk.utils.validation import is_transaction_result |
| 25 | +from tests.api_send.test_send_super import TestSendSuper |
| 26 | + |
| 27 | + |
| 28 | +@patch('iconsdk.providers.http_provider.HTTPProvider._make_id', return_value=1234) |
| 29 | +class TestWaitTransactionResult(TestSendSuper): |
| 30 | + def test_wait_transaction_result(self, _make_id): |
| 31 | + tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238" |
| 32 | + with requests_mock.Mocker() as m: |
| 33 | + expected_request = { |
| 34 | + 'id': 1234, |
| 35 | + 'jsonrpc': '2.0', |
| 36 | + 'method': 'icx_waitTransactionResult', |
| 37 | + 'params': { |
| 38 | + 'txHash': tx_hash |
| 39 | + } |
| 40 | + } |
| 41 | + response_json = { |
| 42 | + "jsonrpc": "2.0", |
| 43 | + "result": { |
| 44 | + "txHash": "0x33db06f38424207daa69c9df153649fd3913c21e162f16f4839c9c3318e44388", |
| 45 | + "blockHeight": "0x13f", |
| 46 | + "blockHash": "0x069e8a2431ae2c7e55924af477be87518476aa1eb1b2e7d1ee8d61d7874ea907", |
| 47 | + "txIndex": "0x1", |
| 48 | + "to": "cx0000000000000000000000000000000000000000", |
| 49 | + "stepUsed": "0x263b8", |
| 50 | + "stepPrice": "0x2540be400", |
| 51 | + "cumulativeStepUsed": "0x263b8", |
| 52 | + "eventLogs": [ |
| 53 | + { |
| 54 | + "scoreAddress": "cx0000000000000000000000000000000000000000", |
| 55 | + "indexed": [ |
| 56 | + "PRepSet(Address)" |
| 57 | + ], |
| 58 | + "data": [ |
| 59 | + "hx86aba2210918a9b116973f3c4b27c41a54d5dafe" |
| 60 | + ] |
| 61 | + } |
| 62 | + ], |
| 63 | + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000080000000000000000000000000000000000000000000000000000000000020000000000000008000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", |
| 64 | + "status": "0x1" |
| 65 | + }, |
| 66 | + "id": 1234 |
| 67 | + } |
| 68 | + m.post(self.matcher, json=response_json) |
| 69 | + self.icon_service.wait_transaction_result(tx_hash) |
| 70 | + actual_request = json.loads(m._adapter.last_request.text) |
| 71 | + self.assertEqual(expected_request, actual_request) |
| 72 | + |
| 73 | + def test_wait_transaction_result_invalid(self, _make_id): |
| 74 | + invalid_tx_hash: str = "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238" |
| 75 | + # case 1: when tx_hash is invalid - no prefixed |
| 76 | + self.assertRaises( |
| 77 | + DataTypeException, |
| 78 | + self.icon_service.wait_transaction_result, |
| 79 | + remove_0x_prefix(invalid_tx_hash) |
| 80 | + ) |
| 81 | + # case 2: when tx_hash is invalid - wrong prefixed |
| 82 | + self.assertRaises( |
| 83 | + DataTypeException, |
| 84 | + self.icon_service.wait_transaction_result, |
| 85 | + add_cx_prefix(remove_0x_prefix(invalid_tx_hash)) |
| 86 | + ) |
| 87 | + # case 3: when tx_hash is invalid - too short |
| 88 | + self.assertRaises( |
| 89 | + DataTypeException, |
| 90 | + self.icon_service.wait_transaction_result, |
| 91 | + invalid_tx_hash[:15] |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments