Browse Source

walletdb: chan dict: small clean-up (incl db upgrade)

- "fail_htlc_reasons" was removed in 9b1c40e396
- "unfulfilled_htlcs": rm 2 dead items from the 4-tuple,
   and convert False value of forwarding_key
master
SomberNight 2 years ago
parent
commit
30c9f5b6b1
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 8
      electrum/lnchannel.py
  2. 8
      electrum/lnpeer.py
  3. 18
      electrum/wallet_db.py
  4. 1
      tests/test_lnchannel.py

8
electrum/lnchannel.py

@ -662,7 +662,8 @@ class Channel(AbstractChannel):
self.onion_keys = state['onion_keys'] # type: Dict[int, bytes]
self.data_loss_protect_remote_pcp = state['data_loss_protect_remote_pcp']
self.hm = HTLCManager(log=state['log'], initial_feerate=initial_feerate)
self.unfulfilled_htlcs = state["unfulfilled_htlcs"]
self.unfulfilled_htlcs = state["unfulfilled_htlcs"] # type: Dict[int, Tuple[str, Optional[str]]]
# ^ htlc_id -> onion_packet_hex, forwarding_key
self._state = ChannelState[state['state']]
self.peer_state = PeerState.DISCONNECTED
self._sweep_info = {}
@ -1058,11 +1059,8 @@ class Channel(AbstractChannel):
htlc = attr.evolve(htlc, htlc_id=self.hm.get_next_htlc_id(REMOTE))
with self.db_lock:
self.hm.recv_htlc(htlc)
local_ctn = self.get_latest_ctn(LOCAL)
remote_ctn = self.get_latest_ctn(REMOTE)
if onion_packet:
# TODO neither local_ctn nor remote_ctn are used anymore... no point storing them.
self.unfulfilled_htlcs[htlc.htlc_id] = local_ctn, remote_ctn, onion_packet.hex(), False
self.unfulfilled_htlcs[htlc.htlc_id] = onion_packet.hex(), None
self.logger.info("receive_htlc")
return htlc

8
electrum/lnpeer.py

@ -950,8 +950,7 @@ class Peer(Logger):
'onion_keys': {},
'data_loss_protect_remote_pcp': {},
"log": {},
"fail_htlc_reasons": {}, # htlc_id -> onion_packet
"unfulfilled_htlcs": {}, # htlc_id -> error_bytes, failure_message
"unfulfilled_htlcs": {},
"revocation_store": {},
"channel_type": channel_type,
}
@ -2610,10 +2609,9 @@ class Peer(Logger):
self.maybe_send_commitment(chan)
done = set()
unfulfilled = chan.unfulfilled_htlcs
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarding_key) in unfulfilled.items():
for htlc_id, (onion_packet_hex, forwarding_key) in unfulfilled.items():
if not chan.hm.is_htlc_irrevocably_added_yet(htlc_proposer=REMOTE, htlc_id=htlc_id):
continue
forwarding_key = forwarding_key or None # type: Optional[str]
htlc = chan.hm.get_htlc_by_id(REMOTE, htlc_id)
error_reason = None # type: Optional[OnionRoutingFailure]
error_bytes = None # type: Optional[bytes]
@ -2634,7 +2632,7 @@ class Peer(Logger):
onion_packet=onion_packet)
if _forwarding_key:
assert forwarding_key is None
unfulfilled[htlc_id] = local_ctn, remote_ctn, onion_packet_hex, _forwarding_key
unfulfilled[htlc_id] = onion_packet_hex, _forwarding_key
except OnionRoutingFailure as e:
error_bytes = construct_onion_error(e, onion_packet.public_key, our_onion_private_key=self.privkey)
if error_bytes:

18
electrum/wallet_db.py

@ -72,7 +72,7 @@ class WalletUnfinished(WalletFileException):
# seed_version is now used for the version of the wallet file
OLD_SEED_VERSION = 4 # electrum versions < 2.0
NEW_SEED_VERSION = 11 # electrum versions >= 2.0
FINAL_SEED_VERSION = 58 # electrum >= 2.7 will set this to prevent
FINAL_SEED_VERSION = 59 # electrum >= 2.7 will set this to prevent
# old versions from overwriting new format
@ -111,7 +111,7 @@ json_db.register_dict('contacts', tuple, None)
# register dicts that require key conversion
for key in [
'adds', 'locked_in', 'settles', 'fails', 'fee_updates', 'buckets',
'unacked_updates', 'unfulfilled_htlcs', 'fail_htlc_reasons', 'onion_keys']:
'unacked_updates', 'unfulfilled_htlcs', 'onion_keys']:
json_db.register_dict_key(key, int)
for key in ['log']:
json_db.register_dict_key(key, lambda x: HTLCOwner(int(x)))
@ -233,6 +233,7 @@ class WalletDBUpgrader(Logger):
self._convert_version_56()
self._convert_version_57()
self._convert_version_58()
self._convert_version_59()
self.put('seed_version', FINAL_SEED_VERSION) # just to be sure
def _convert_wallet_type(self):
@ -1132,6 +1133,19 @@ class WalletDBUpgrader(Logger):
self.put('prevouts_by_scripthash', prevouts_by_scripthash)
self.data['seed_version'] = 58
def _convert_version_59(self):
if not self._is_upgrade_method_needed(58, 58):
return
channels = self.data.get('channels', {})
for _key, chan in channels.items():
chan.pop('fail_htlc_reasons', {})
unfulfilled_htlcs = {}
for htlc_id, (local_ctn, remote_ctn, onion_packet_hex, forwarding_key) in chan['unfulfilled_htlcs'].items():
unfulfilled_htlcs[htlc_id] = (onion_packet_hex, forwarding_key or None)
chan['unfulfilled_htlcs'] = unfulfilled_htlcs
self.data['channels'] = channels
self.data['seed_version'] = 59
def _convert_imported(self):
if not self._is_upgrade_method_needed(0, 13):
return

1
tests/test_lnchannel.py

@ -108,7 +108,6 @@ def create_channel_state(funding_txid, funding_index, funding_sat, is_initiator,
'data_loss_protect_remote_pcp': {},
'state': 'PREOPENING',
'log': {},
'fail_htlc_reasons': {},
'unfulfilled_htlcs': {},
'revocation_store': {},
'channel_type': lnutil.ChannelType.OPTION_STATIC_REMOTEKEY

Loading…
Cancel
Save