File: network.py

package info (click to toggle)
python-duniterpy 1.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,228 kB
  • sloc: python: 10,624; makefile: 182; sh: 17
file content (181 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright  2014-2022 Vincent Texier <vit@free.fr>
#
# DuniterPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DuniterPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
from http.client import HTTPResponse

from duniterpy.api.client import RESPONSE_HTTP, Client

logger = logging.getLogger("duniter/network")

MODULE = "network"

PEERS_SCHEMA = {
    "type": "object",
    "properties": {
        "peers": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "version": {"type": ["number", "string"]},
                    "currency": {"type": "string"},
                    "status": {"type": "string"},
                    "first_down": {"type": ["null", "integer"]},
                    "last_try": {"type": ["null", "integer"]},
                    "pubkey": {"type": "string"},
                    "block": {"type": "string"},
                    "signature": {"type": "string"},
                    "endpoints": {"type": "array", "items": {"type": "string"}},
                },
                "required": [
                    "version",
                    "currency",
                    "status",
                    "first_down",
                    "last_try",
                    "pubkey",
                    "block",
                    "signature",
                    "endpoints",
                ],
            },
        }
    },
    "required": ["peers"],
}

PEERING_SCHEMA = {
    "type": "object",
    "properties": {
        "version": {"type": ["number", "string"]},
        "currency": {"type": "string"},
        "pubkey": {"type": "string"},
        "endpoints": {"type": "array", "items": {"type": "string"}},
        "signature": {"type": "string"},
    },
    "required": ["version", "currency", "pubkey", "endpoints", "signature"],
}

PEERING_PEERS_SCHEMA = {
    "type": ["object"],
    "properties": {
        "depth": {"type": "number"},
        "nodesCount": {"type": "number"},
        "leavesCount": {"type": "number"},
        "root": {"type": "string"},
        "hash": {"type": "string"},
        "value": {
            "type": "object",
            "properties": {
                "version": {"type": "string"},
                "currency": {"type": "string"},
                "pubkey": {"type": "string"},
                "endpoints": {"type": "array", "items": {"type": "string"}},
                "signature": {"type": "string"},
            },
            "required": ["version", "currency", "pubkey", "endpoints", "signature"],
        },
    },
    "oneOf": [
        {"required": ["depth", "nodesCount", "leavesCount", "root"]},
        {"required": ["hash", "value"]},
    ],
}

WS2P_HEADS_SCHEMA = {
    "type": "object",
    "properties": {
        "heads": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "message": {"type": "string"},
                    "sig": {"type": "string"},
                    "messageV2": {"type": "string"},
                    "sigV2": {"type": "string"},
                    "step": {"type": "number"},
                },
                "required": ["messageV2", "sigV2", "step"],
            },
        }
    },
    "required": ["heads"],
}


def peers(client: Client) -> dict:
    """
    GET the exhaustive list of peers known by the node

    :param client: Client to connect to the api
    :return:
    """

    return client.get(f"{MODULE}/peers", schema=PEERS_SCHEMA)


def peering(client: Client) -> dict:
    """
    GET peering information about a peer

    :param client: Client to connect to the api
    :return:
    """
    return client.get(f"{MODULE}/peering", schema=PEERING_SCHEMA)


def peering_peers(client: Client, leaves: bool = False, leaf: str = "") -> dict:
    """
    GET peering entries of every node inside the currency network

    :param client: Client to connect to the api
    :param leaves: True if leaves should be requested
    :param leaf: True if leaf should be requested
    :return:
    """
    if leaves is True:
        response = client.get(
            f"{MODULE}/peering/peers", {"leaves": "true"}, schema=PEERING_PEERS_SCHEMA
        )
    else:
        response = client.get(
            f"{MODULE}/peering/peers", {"leaf": leaf}, schema=PEERING_PEERS_SCHEMA
        )
    return response


def peer(client: Client, peer_signed_raw: str) -> HTTPResponse:
    """
    POST a Peer signed raw document

    :param client: Client to connect to the api
    :param peer_signed_raw: Peer signed raw document
    :return:
    """
    return client.post(
        f"{MODULE}/peering/peers", {"peer": peer_signed_raw}, rtype=RESPONSE_HTTP
    )


def ws2p_heads(client: Client) -> dict:
    """
    GET ws2p heads known by the node

    :param client: Client to connect to the api
    :rtype: dict
    """
    return client.get(f"{MODULE}/ws2p/heads", schema=WS2P_HEADS_SCHEMA)