Browse Source

follow-up prev: try to handle json db int key madness :/

hard-fail-on-bad-server-string
SomberNight 5 years ago
parent
commit
9a70b79eea
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 5
      electrum/json_db.py
  2. 8
      electrum/lnhtlc.py

5
electrum/json_db.py

@ -76,7 +76,10 @@ class StoredDict(dict):
self.__setitem__(k, v) self.__setitem__(k, v)
def convert_key(self, key): def convert_key(self, key):
# convert int, HTLCOwner to str """Convert int keys to str keys, as only those are allowed in json."""
# NOTE: this is evil. really hard to keep in mind and reason about. :(
# e.g.: imagine setting int keys everywhere, and then iterating over the dict:
# suddenly the keys are str...
return str(int(key)) if isinstance(key, int) else key return str(int(key)) if isinstance(key, int) else key
@locked @locked

8
electrum/lnhtlc.py

@ -1,5 +1,5 @@
from copy import deepcopy from copy import deepcopy
from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, Set
from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate
from .util import bh2u, bfh from .util import bh2u, bfh
@ -22,6 +22,7 @@ class HTLCManager:
'next_htlc_id': 0, 'next_htlc_id': 0,
'ctn': -1, # oldest unrevoked ctx of sub 'ctn': -1, # oldest unrevoked ctx of sub
} }
# note: "htlc_id" keys in dict are str! but due to json_db magic they can *almost* be treated as int...
log[LOCAL] = deepcopy(initial) log[LOCAL] = deepcopy(initial)
log[REMOTE] = deepcopy(initial) log[REMOTE] = deepcopy(initial)
log['unacked_local_updates2'] = {} log['unacked_local_updates2'] = {}
@ -207,7 +208,8 @@ class HTLCManager:
self._balance_delta -= htlc.amount_msat * htlc_proposer self._balance_delta -= htlc.amount_msat * htlc_proposer
def _init_maybe_active_htlc_ids(self): def _init_maybe_active_htlc_ids(self):
self._maybe_active_htlc_ids = {LOCAL: set(), REMOTE: set()} # first idx is "side who offered htlc" # first idx is "side who offered htlc":
self._maybe_active_htlc_ids = {LOCAL: set(), REMOTE: set()} # type: Dict[HTLCOwner, Set[int]]
# add all htlcs # add all htlcs
self._balance_delta = 0 # the balance delta of LOCAL since channel open self._balance_delta = 0 # the balance delta of LOCAL since channel open
for htlc_proposer in (LOCAL, REMOTE): for htlc_proposer in (LOCAL, REMOTE):
@ -263,6 +265,7 @@ class HTLCManager:
def is_htlc_active_at_ctn(self, *, ctx_owner: HTLCOwner, ctn: int, def is_htlc_active_at_ctn(self, *, ctx_owner: HTLCOwner, ctn: int,
htlc_proposer: HTLCOwner, htlc_id: int) -> bool: htlc_proposer: HTLCOwner, htlc_id: int) -> bool:
htlc_id = int(htlc_id)
if htlc_id >= self.get_next_htlc_id(htlc_proposer): if htlc_id >= self.get_next_htlc_id(htlc_proposer):
return False return False
settles = self.log[htlc_proposer]['settles'] settles = self.log[htlc_proposer]['settles']
@ -295,6 +298,7 @@ class HTLCManager:
else: # ctn is too old; need to consider full log (slow...) else: # ctn is too old; need to consider full log (slow...)
considered_htlc_ids = self.log[party]['locked_in'] considered_htlc_ids = self.log[party]['locked_in']
for htlc_id in considered_htlc_ids: for htlc_id in considered_htlc_ids:
htlc_id = int(htlc_id)
if self.is_htlc_active_at_ctn(ctx_owner=subject, ctn=ctn, htlc_proposer=party, htlc_id=htlc_id): if self.is_htlc_active_at_ctn(ctx_owner=subject, ctn=ctn, htlc_proposer=party, htlc_id=htlc_id):
d[htlc_id] = self.log[party]['adds'][htlc_id] d[htlc_id] = self.log[party]['adds'][htlc_id]
return d return d

Loading…
Cancel
Save