5
5
node_announcement ,
6
6
gossip_store_channel_amount )
7
7
from pyln .proto import ShortChannelId , PublicKey
8
- from typing import Any , Dict , List , Optional
8
+ from typing import Any , Dict , List , Optional , Union , cast
9
9
10
10
import io
11
11
import struct
@@ -33,7 +33,7 @@ def __init__(self, buf: bytes):
33
33
34
34
class GossmapHalfchannel (object ):
35
35
"""One direction of a GossmapChannel."""
36
- def __init__ (self , channel : GossmapChannel , direction : int ,
36
+ def __init__ (self , channel : ' GossmapChannel' , direction : int ,
37
37
timestamp : int , cltv_expiry_delta : int ,
38
38
htlc_minimum_msat : int , htlc_maximum_msat : int ,
39
39
fee_base_msat : int , fee_proportional_millionths : int ):
@@ -71,12 +71,13 @@ def __hash__(self):
71
71
def __repr__ (self ):
72
72
return "GossmapNodeId[{}]" .format (self .nodeid .hex ())
73
73
74
- def from_str (self , s : str ):
74
+ @classmethod
75
+ def from_str (cls , s : str ):
75
76
if s .startswith ('0x' ):
76
77
s = s [2 :]
77
78
if len (s ) != 67 :
78
79
raise ValueError (f"{ s } is not a valid hexstring of a node_id" )
79
- return GossmapNodeId (bytes .fromhex (s ))
80
+ return cls (bytes .fromhex (s ))
80
81
81
82
82
83
class GossmapChannel (object ):
@@ -97,14 +98,14 @@ def __init__(self,
97
98
self .updates_fields : List [Optional [Dict [str , Any ]]] = [None , None ]
98
99
self .updates_offset : List [Optional [int ]] = [None , None ]
99
100
self .satoshis = None
100
- self .half_channels : List [GossmapHalfchannel ] = [None , None ]
101
+ self .half_channels : List [Optional [ GossmapHalfchannel ] ] = [None , None ]
101
102
102
103
def update_channel (self ,
103
104
direction : int ,
104
- fields : List [ Optional [ Dict [str , Any ]]] = [ None , None ],
105
- off : List [ Optional [ int ]] = [ None , None ] ):
105
+ fields : Dict [str , Any ],
106
+ off : int ):
106
107
self .updates_fields [direction ] = fields
107
- self .updates_offset = off
108
+ self .updates_offset [ direction ] = off
108
109
109
110
half = GossmapHalfchannel (self , direction ,
110
111
fields ['timestamp' ],
@@ -132,8 +133,8 @@ class GossmapNode(object):
132
133
"""
133
134
def __init__ (self , node_id : GossmapNodeId ):
134
135
self .announce_fields : Optional [Dict [str , Any ]] = None
135
- self .announce_offset = None
136
- self .channels = []
136
+ self .announce_offset : Optional [ int ] = None
137
+ self .channels : List [ GossmapChannel ] = []
137
138
self .node_id = node_id
138
139
139
140
def __repr__ (self ):
@@ -148,10 +149,10 @@ def __init__(self, store_filename: str = "gossip_store"):
148
149
self .store_buf = bytes ()
149
150
self .nodes : Dict [GossmapNodeId , GossmapNode ] = {}
150
151
self .channels : Dict [ShortChannelId , GossmapChannel ] = {}
151
- self ._last_scid : str = None
152
+ self ._last_scid : Optional [ str ] = None
152
153
version = self .store_file .read (1 )
153
154
if version [0 ] != GOSSIP_STORE_VERSION :
154
- raise ValueError ("Invalid gossip store version {}" .format (version ))
155
+ raise ValueError ("Invalid gossip store version {}" .format (int ( version ) ))
155
156
self .bytes_read = 1
156
157
self .refresh ()
157
158
@@ -205,11 +206,11 @@ def get_channel(self, short_channel_id: ShortChannelId):
205
206
short_channel_id = ShortChannelId .from_str (short_channel_id )
206
207
return self .channels .get (short_channel_id )
207
208
208
- def get_node (self , node_id : GossmapNodeId ):
209
+ def get_node (self , node_id : Union [ GossmapNodeId , str ] ):
209
210
""" Resolves a node by its public key node_id """
210
- if type (node_id ) == str :
211
+ if isinstance (node_id , str ) :
211
212
node_id = GossmapNodeId .from_str (node_id )
212
- return self .nodes .get (node_id )
213
+ return self .nodes .get (cast ( GossmapNodeId , node_id ) )
213
214
214
215
def update_channel (self , rec : bytes , off : int ):
215
216
fields = channel_update .read (io .BytesIO (rec [2 :]), {})
0 commit comments