Completed
Push — main ( 5e6d02...339f4c )
by
unknown
15s queued 12s
created

tests.test_tradehub_decentralized_client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 132
dl 0
loc 165
rs 10
c 0
b 0
f 0
wmc 6
1
from tradehub.decentralized_client import NetworkCrawlerClient
2
from tests import APITestCase
3
4
5
class TestNetworkCrawlerClient(APITestCase):
6
7
    def setUp(self) -> None:
8
        self.tradehub_client = NetworkCrawlerClient(network='mainnet')
9
10
    def test_validator_crawler_mp(self):
11
        self.tradehub_client.validator_crawler_mp()
12
        self.assertIsNotNone(self.tradehub_client.active_sentry_api_list)
13
        self.assertIsNotNone(self.tradehub_client.active_validator_list)
14
15
    def test_validator_status_request(self):
16
        expect: dict = {
17
            'moniker': str,
18
            'id': str,
19
            'ip': str,
20
            'version': str,
21
            'network': str,
22
            'latest_block_hash': str,
23
            'latest_block_height': str,
24
            'latest_block_time': str,
25
            'earliest_block_height': str,
26
            'earliest_block_time': str,
27
            'catching_up': bool,
28
            'validator_address': str,
29
            'validator_pub_key_type': str,
30
            'validator_pub_key': str,
31
            'validator_voting_power': str,
32
            'validator_status': str,
33
            'connected_nodes': [{
34
                'node_id': str,
35
                'node_ip': str,
36
                'node_full': str,
37
            }]
38
        }
39
40
        result = self.tradehub_client.validator_status_request(validator_ip=self.tradehub_client.active_sentry_api_ip)
41
        print(result)
42
        self.assertDictStructure(expect, result)
43
44
    def test_parse_validator_status(self):
45
        expect: dict = {
46
            'moniker': str,
47
            'id': str,
48
            'ip': str,
49
            'version': str,
50
            'network': str,
51
            'latest_block_hash': str,
52
            'latest_block_height': str,
53
            'latest_block_time': str,
54
            'earliest_block_height': str,
55
            'earliest_block_time': str,
56
            'catching_up': bool,
57
            'validator_address': str,
58
            'validator_pub_key_type': str,
59
            'validator_pub_key': str,
60
            'validator_voting_power': str,
61
        }
62
63
        validator_status_json: dict = {
64
            'jsonrpc': '2.0',
65
            'id': -1,
66
            'result': {
67
                'node_info': {
68
                    'protocol_version': {
69
                        'p2p': '7',
70
                        'block': '10',
71
                        'app': '0'
72
                    },
73
                    'id': '756dece3e0a00705c61a7de701f78f51f5e9a91b',
74
                    'listen_addr': 'tcp://0.0.0.0:26656',
75
                    'network': 'switcheo-tradehub-1',
76
                    'version': '0.33.7',
77
                    'channels': '4020212223303800',
78
                    'moniker': 'pike',
79
                    'other': {
80
                        'tx_index': 'on',
81
                        'rpc_address': 'tcp://0.0.0.0:26659'
82
                    }
83
                },
84
                'sync_info': {
85
                    'latest_block_hash': '3E385BDDF88024B1767BC5E19D4C4F92122FDF16BF93702CCACBD5D1E2299252',
86
                    'latest_app_hash': '0470CC9EA15482ECB5D497A6D70EF9C1181B649244AB589D8858941FB65EC7FB',
87
                    'latest_block_height': '7559634',
88
                    'latest_block_time': '2021-02-16T07:25:24.602077456Z',
89
                    'earliest_block_hash': 'B4AF1F3D3D3FD5795BDDB7A6A2E6CA4E34D06338505D6EC46DD8F99E72ADCDAB',
90
                    'earliest_app_hash': '',
91
                    'earliest_block_height': '1',
92
                    'earliest_block_time': '2020-08-14T07:32:27.856700491Z',
93
                    'catching_up': False
94
                },
95
                'validator_info': {
96
                    'address': 'A84B0CB22673DCBCEC8C9EB0A2E83AAC8DE297A2',
97
                    'pub_key': {
98
                        'type': 'tendermint/PubKeyEd25519',
99
                        'value': 'qjqeC/G3OABg7RVbtGDOAS0nWAy3AheJaI7s7OPb5o0='
100
                    },
101
                    'voting_power': '0'
102
                }
103
            }
104
        }
105
106
        result = self.tradehub_client.parse_validator_status(request_json=validator_status_json, validator_ip=self.tradehub_client.active_sentry_api_ip)
107
        self.assertDictStructure(expect, result)
108
109
    def test_sentry_status_request(self):
110
        self.tradehub_client.sentry_status_request()
111
        self.assertIsNotNone(self.tradehub_client.active_sentry_api_list)
112
113
    def test_tradehub_get_request(self):
114
        expect: dict = {
115
            "jsonrpc": str,
116
            "id": int,
117
            "result": {
118
                "node_info": {
119
                    "protocol_version": {
120
                        "p2p": str,
121
                        "block":  str,
122
                        "app":  str,
123
                    },
124
                    "id": str,
125
                    "listen_addr": str,
126
                    "network": str,
127
                    "version": str,
128
                    "channels": str,
129
                    "moniker": str,
130
                    "other": {
131
                        "tx_index": str,
132
                        "rpc_address": str,
133
                    }
134
                },
135
                "sync_info": {
136
                    "latest_block_hash": str,
137
                    "latest_app_hash": str,
138
                    "latest_block_height": str,
139
                    "latest_block_time": str,
140
                    "earliest_block_hash": str,
141
                    "earliest_app_hash": str,
142
                    "earliest_block_height": str,
143
                    "earliest_block_time": str,
144
                    "catching_up": bool
145
                },
146
                "validator_info": {
147
                    "address": str,
148
                    "pub_key": {
149
                        "type": str,
150
                        "value": str,
151
                    },
152
                    "voting_power": str,
153
                }
154
            }
155
        }
156
157
        result = self.tradehub_client.tradehub_get_request(path='/get_status')
158
        self.assertDictStructure(expect, result)
159
160
        # This Sentry IP should fail and retry with an existing Sentry IP in the active list.
161
        self.tradehub_client.active_sentry_api_ip = "116.202.216.145"
162
        self.tradehub_client.active_sentry_api_list.append("116.202.216.145")
163
        result = self.tradehub_client.tradehub_get_request(path='/get_status')
164
        self.assertDictStructure(expect, result)
165