Skip to content

Commit

Permalink
pyln/client/gossmap: save deconstructed fields instead of raw msg bytes.
Browse files Browse the repository at this point in the history
We have to parse them anyway, so why not make them accessible.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell authored and cdecker committed Jun 21, 2021
1 parent bd59394 commit a68aeb2
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions contrib/pyln-client/pyln/client/gossmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pyln.spec.bolt7 import (channel_announcement, channel_update,
node_announcement)
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

import io
import struct
Expand Down Expand Up @@ -39,25 +39,25 @@ class point(bytes):

class GossmapChannel(object):
def __init__(self,
announce: bytes,
fields: Dict[str, Any],
announce_offset: int,
scid,
node1_id: point,
node2_id: point,
is_private: bool):
self.announce = announce
self.fields = fields
self.announce_offset = announce_offset
self.is_private = is_private
self.scid = scid
self.node1_id = node1_id
self.node2_id = node2_id
self.updates: List[Optional[bytes]] = [None, None]
self.updates_fields: List[Optional[Dict[str, Any]]] = [None, None]
self.updates_offset: List[Optional[int]] = [None, None]


class GossmapNode(object):
def __init__(self, node_id: point):
self.announce = None
self.announce_fields: Optional[Dict[str, Any]] = None
self.announce_offset = None
self.channels = []
self.node_id = node_id
Expand All @@ -78,13 +78,13 @@ def __init__(self, store_filename: str = "gossip_store"):
self.refresh()

def _new_channel(self,
announce: bytes,
fields: Dict[str, Any],
announce_offset: int,
scid: short_channel_id,
node1_id: point,
node2_id: point,
is_private: bool):
c = GossmapChannel(announce, announce_offset,
c = GossmapChannel(fields, announce_offset,
scid, node1_id, node2_id,
is_private)
if node1_id not in self.nodes:
Expand All @@ -110,20 +110,20 @@ def _del_channel(self, scid: short_channel_id):

def add_channel(self, rec: bytes, off: int, is_private: bool):
fields = channel_announcement.read(io.BytesIO(rec[2:]), {})
self._new_channel(rec, off, fields['short_channel_id'],
self._new_channel(fields, off, fields['short_channel_id'],
fields['node_id_1'], fields['node_id_2'],
is_private)

def update_channel(self, rec: bytes, off: int):
fields = channel_update.read(io.BytesIO(rec[2:]), {})
direction = fields['message_flags'] & 1
c = self.channels[fields['short_channel_id']]
c.updates[direction] = rec
c.updates_fields[direction] = fields
c.updates_offset = off

def add_node_announcement(self, rec: bytes, off: int):
fields = node_announcement.read(io.BytesIO(rec[2:]), {})
self.nodes[fields['node_id']].announce = rec
self.nodes[fields['node_id']].announce_fields = fields
self.nodes[fields['node_id']].announce_offset = off

def reopen_store(self):
Expand Down

0 comments on commit a68aeb2

Please sign in to comment.