2
2
3
3
from pyln .spec .bolt7 import (channel_announcement , channel_update ,
4
4
node_announcement )
5
- from pyln .proto import ShortChannelId
5
+ from pyln .proto import ShortChannelId , PublicKey
6
6
from typing import Any , Dict , List , Optional
7
7
8
8
import io
@@ -29,18 +29,14 @@ def __init__(self, buf: bytes):
29
29
self .length = (length & GOSSIP_STORE_LEN_MASK )
30
30
31
31
32
- class point (bytes ):
33
- pass
34
-
35
-
36
32
class GossmapChannel (object ):
37
33
"""A channel: fields of channel_announcement are in .fields, optional updates are in .updates_fields, which can be None of there has been no channel update."""
38
34
def __init__ (self ,
39
35
fields : Dict [str , Any ],
40
36
announce_offset : int ,
41
37
scid ,
42
- node1_id : point ,
43
- node2_id : point ,
38
+ node1_id : bytes ,
39
+ node2_id : bytes ,
44
40
is_private : bool ):
45
41
self .fields = fields
46
42
self .announce_offset = announce_offset
@@ -52,12 +48,34 @@ def __init__(self,
52
48
self .updates_offset : List [Optional [int ]] = [None , None ]
53
49
54
50
51
+ class GossmapNodeId (object ):
52
+ def __init__ (self , buf : bytes ):
53
+ if len (buf ) != 33 or (buf [0 ] != 2 and buf [0 ] != 3 ):
54
+ raise ValueError ("{} is not a valid node_id" .format (buf .hex ))
55
+ self .nodeid = buf
56
+
57
+ def to_pubkey (self ) -> PublicKey :
58
+ return PublicKey (self .nodeid )
59
+
60
+ def __eq__ (self , other ):
61
+ if not isinstance (other , GossmapNodeId ):
62
+ return False
63
+
64
+ return self .nodeid == other .nodeid
65
+
66
+ def __hash__ (self ):
67
+ return self .nodeid .__hash__ ()
68
+
69
+ def __repr__ (self ):
70
+ return "GossmapNodeId[0x{}]" .format (self .nodeid .hex ())
71
+
72
+
55
73
class GossmapNode (object ):
56
74
"""A node: fields of node_announcement are in .announce_fields, which can be None of there has been no node announcement.
57
75
58
76
.channels is a list of the GossmapChannels attached to this node.
59
77
"""
60
- def __init__ (self , node_id : point ):
78
+ def __init__ (self , node_id : GossmapNodeId ):
61
79
self .announce_fields : Optional [Dict [str , Any ]] = None
62
80
self .announce_offset = None
63
81
self .channels = []
@@ -70,7 +88,7 @@ def __init__(self, store_filename: str = "gossip_store"):
70
88
self .store_filename = store_filename
71
89
self .store_file = open (store_filename , "rb" )
72
90
self .store_buf = bytes ()
73
- self .nodes : Dict [point , GossmapNode ] = {}
91
+ self .nodes : Dict [bytes , GossmapNode ] = {}
74
92
self .channels : Dict [ShortChannelId , GossmapChannel ] = {}
75
93
version = self .store_file .read (1 )
76
94
if version [0 ] != GOSSIP_STORE_VERSION :
@@ -82,8 +100,8 @@ def _new_channel(self,
82
100
fields : Dict [str , Any ],
83
101
announce_offset : int ,
84
102
scid : ShortChannelId ,
85
- node1_id : point ,
86
- node2_id : point ,
103
+ node1_id : GossmapNodeId ,
104
+ node2_id : GossmapNodeId ,
87
105
is_private : bool ):
88
106
c = GossmapChannel (fields , announce_offset ,
89
107
scid , node1_id , node2_id ,
@@ -113,7 +131,7 @@ def add_channel(self, rec: bytes, off: int, is_private: bool):
113
131
fields = channel_announcement .read (io .BytesIO (rec [2 :]), {})
114
132
self ._new_channel (fields , off ,
115
133
ShortChannelId .from_int (fields ['short_channel_id' ]),
116
- fields ['node_id_1' ], fields ['node_id_2' ],
134
+ GossmapNodeId ( fields ['node_id_1' ]), GossmapNodeId ( fields ['node_id_2' ]) ,
117
135
is_private )
118
136
119
137
def update_channel (self , rec : bytes , off : int ):
@@ -125,8 +143,9 @@ def update_channel(self, rec: bytes, off: int):
125
143
126
144
def add_node_announcement (self , rec : bytes , off : int ):
127
145
fields = node_announcement .read (io .BytesIO (rec [2 :]), {})
128
- self .nodes [fields ['node_id' ]].announce_fields = fields
129
- self .nodes [fields ['node_id' ]].announce_offset = off
146
+ node_id = GossmapNodeId (fields ['node_id' ])
147
+ self .nodes [node_id ].announce_fields = fields
148
+ self .nodes [node_id ].announce_offset = off
130
149
131
150
def reopen_store (self ):
132
151
"""FIXME: Implement!"""
0 commit comments