From b6ed405d8c5e3ed13e834bd208bd2dae0661f3fd Mon Sep 17 00:00:00 2001
From: Michael Schmoock <michael@schmoock.net>
Date: Wed, 8 Sep 2021 06:24:57 +0930
Subject: [PATCH] pyln-client/gossmap: adds channel satoshi capacity

This reads the `gossip_store_channel_amount` that always follows the
`channel_announcement` messages. Therefore it uses an internal variable
_last_scid to know what channel has been added last time.
---
 contrib/pyln-client/pyln/client/gossmap.py | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/contrib/pyln-client/pyln/client/gossmap.py b/contrib/pyln-client/pyln/client/gossmap.py
index 0e796e2bd..f10f490f0 100755
--- a/contrib/pyln-client/pyln/client/gossmap.py
+++ b/contrib/pyln-client/pyln/client/gossmap.py
@@ -20,6 +20,7 @@ WIRE_GOSSIP_STORE_PRIVATE_CHANNEL = 4104
 WIRE_GOSSIP_STORE_PRIVATE_UPDATE = 4102
 WIRE_GOSSIP_STORE_DELETE_CHAN = 4103
 WIRE_GOSSIP_STORE_ENDED = 4105
+WIRE_GOSSIP_STORE_CHANNEL_AMOUNT = 4101
 
 
 class GossipStoreHeader(object):
@@ -60,8 +61,7 @@ class GossmapChannel(object):
         self.node2_id = node2_id
         self.updates_fields: List[Optional[Dict[str, Any]]] = [None, None]
         self.updates_offset: List[Optional[int]] = [None, None]
-
-        self.capacity = None  # TODO: where do we get this?
+        self.satoshis = None
         self.half_channels: List[GossmapHalfchannel] = [None, None]
 
     def update_channel(self,
@@ -135,6 +135,7 @@ class Gossmap(object):
         self.store_buf = bytes()
         self.nodes: Dict[bytes, GossmapNode] = {}
         self.channels: Dict[ShortChannelId, GossmapChannel] = {}
+        self._last_scid: str = None
         version = self.store_file.read(1)
         if version[0] != GOSSIP_STORE_VERSION:
             raise ValueError("Invalid gossip store version {}".format(version))
@@ -156,6 +157,7 @@ class Gossmap(object):
         if node2_id not in self.nodes:
             self.nodes[node2_id] = GossmapNode(node2_id)
 
+        self._last_scid = scid
         self.channels[scid] = c
         self.nodes[node1_id].channels.append(c)
         self.nodes[node2_id].channels.append(c)
@@ -179,6 +181,11 @@ class Gossmap(object):
                           GossmapNodeId(fields['node_id_1']), GossmapNodeId(fields['node_id_2']),
                           is_private)
 
+    def _set_channel_amount(self, rec: bytes):
+        """ Sets channel capacity of last added channel """
+        sats, = struct.unpack(">Q", rec[2:])
+        self.channels[self._last_scid].satoshis = sats
+
     def get_channel(self, short_channel_id: ShortChannelId):
         """ Resolves a channel by its short channel id """
         if type(short_channel_id) == str:
@@ -252,6 +259,8 @@ class Gossmap(object):
                 self.add_channel(rec, off, False)
             elif rectype == WIRE_GOSSIP_STORE_PRIVATE_CHANNEL:
                 self.add_channel(rec[2 + 8 + 2:], off + 2 + 8 + 2, True)
+            elif rectype == WIRE_GOSSIP_STORE_CHANNEL_AMOUNT:
+                self._set_channel_amount(rec)
             elif rectype == channel_update.number:
                 self.update_channel(rec, off)
             elif rectype == WIRE_GOSSIP_STORE_PRIVATE_UPDATE: