mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-26 23:13:25 +00:00
lnhtlc: add all_htlcs_ever, get_htlc_by_id, was_htlc_failed and use them
towards encapsulation of hm.log
This commit is contained in:
parent
51f42a25f9
commit
aba2e0f55a
3 changed files with 41 additions and 32 deletions
|
@ -647,29 +647,25 @@ class Channel(AbstractChannel):
|
||||||
|
|
||||||
def get_payments(self):
|
def get_payments(self):
|
||||||
out = []
|
out = []
|
||||||
for subject in LOCAL, REMOTE:
|
for direction, htlc in self.hm.all_htlcs_ever():
|
||||||
log = self.hm.log[subject]
|
htlc_proposer = LOCAL if direction is SENT else REMOTE
|
||||||
for htlc_id, htlc in log.get('adds', {}).items():
|
if self.hm.was_htlc_failed(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
|
||||||
if htlc_id in log.get('fails',{}):
|
status = 'failed'
|
||||||
status = 'failed'
|
elif self.hm.was_htlc_preimage_released(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
|
||||||
elif htlc_id in log.get('settles',{}):
|
status = 'settled'
|
||||||
status = 'settled'
|
else:
|
||||||
else:
|
status = 'inflight'
|
||||||
status = 'inflight'
|
rhash = htlc.payment_hash.hex()
|
||||||
direction = SENT if subject is LOCAL else RECEIVED
|
out.append((rhash, self.channel_id, htlc, direction, status))
|
||||||
rhash = bh2u(htlc.payment_hash)
|
|
||||||
out.append((rhash, self.channel_id, htlc, direction, status))
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def get_settled_payments(self):
|
def get_settled_payments(self):
|
||||||
out = defaultdict(list)
|
out = defaultdict(list)
|
||||||
for subject in LOCAL, REMOTE:
|
for direction, htlc in self.hm.all_htlcs_ever():
|
||||||
log = self.hm.log[subject]
|
htlc_proposer = LOCAL if direction is SENT else REMOTE
|
||||||
for htlc_id, htlc in log.get('adds', {}).items():
|
if self.hm.was_htlc_preimage_released(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
|
||||||
if htlc_id in log.get('settles',{}):
|
rhash = htlc.payment_hash.hex()
|
||||||
direction = SENT if subject is LOCAL else RECEIVED
|
out[rhash].append((self.channel_id, htlc, direction))
|
||||||
rhash = bh2u(htlc.payment_hash)
|
|
||||||
out[rhash].append((self.channel_id, htlc, direction))
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:
|
def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:
|
||||||
|
@ -1206,15 +1202,13 @@ class Channel(AbstractChannel):
|
||||||
"""
|
"""
|
||||||
self.logger.info("settle_htlc")
|
self.logger.info("settle_htlc")
|
||||||
assert self.can_send_ctx_updates(), f"cannot update channel. {self.get_state()!r} {self.peer_state!r}"
|
assert self.can_send_ctx_updates(), f"cannot update channel. {self.get_state()!r} {self.peer_state!r}"
|
||||||
log = self.hm.log[REMOTE]
|
htlc = self.hm.get_htlc_by_id(REMOTE, htlc_id)
|
||||||
htlc = log['adds'][htlc_id]
|
|
||||||
assert htlc.payment_hash == sha256(preimage)
|
assert htlc.payment_hash == sha256(preimage)
|
||||||
assert htlc_id not in log['settles']
|
assert htlc_id not in self.hm.log[REMOTE]['settles']
|
||||||
self.hm.send_settle(htlc_id)
|
self.hm.send_settle(htlc_id)
|
||||||
|
|
||||||
def get_payment_hash(self, htlc_id: int) -> bytes:
|
def get_payment_hash(self, htlc_id: int) -> bytes:
|
||||||
log = self.hm.log[LOCAL]
|
htlc = self.hm.get_htlc_by_id(LOCAL, htlc_id)
|
||||||
htlc = log['adds'][htlc_id] # type: UpdateAddHtlc
|
|
||||||
return htlc.payment_hash
|
return htlc.payment_hash
|
||||||
|
|
||||||
def decode_onion_error(self, reason: bytes, route: Sequence['RouteEdge'],
|
def decode_onion_error(self, reason: bytes, route: Sequence['RouteEdge'],
|
||||||
|
@ -1230,10 +1224,9 @@ class Channel(AbstractChannel):
|
||||||
Action must be initiated by REMOTE.
|
Action must be initiated by REMOTE.
|
||||||
"""
|
"""
|
||||||
self.logger.info("receive_htlc_settle")
|
self.logger.info("receive_htlc_settle")
|
||||||
log = self.hm.log[LOCAL]
|
htlc = self.hm.get_htlc_by_id(LOCAL, htlc_id)
|
||||||
htlc = log['adds'][htlc_id]
|
|
||||||
assert htlc.payment_hash == sha256(preimage)
|
assert htlc.payment_hash == sha256(preimage)
|
||||||
assert htlc_id not in log['settles']
|
assert htlc_id not in self.hm.log[LOCAL]['settles']
|
||||||
with self.db_lock:
|
with self.db_lock:
|
||||||
self.hm.recv_settle(htlc_id)
|
self.hm.recv_settle(htlc_id)
|
||||||
|
|
||||||
|
@ -1419,7 +1412,7 @@ class Channel(AbstractChannel):
|
||||||
(REMOTE, SENT, self.get_oldest_unrevoked_ctn(LOCAL)),
|
(REMOTE, SENT, self.get_oldest_unrevoked_ctn(LOCAL)),
|
||||||
(REMOTE, SENT, self.get_latest_ctn(LOCAL)),):
|
(REMOTE, SENT, self.get_latest_ctn(LOCAL)),):
|
||||||
for htlc_id, htlc in self.hm.htlcs_by_direction(subject=sub, direction=dir, ctn=ctn).items():
|
for htlc_id, htlc in self.hm.htlcs_by_direction(subject=sub, direction=dir, ctn=ctn).items():
|
||||||
if not self.hm.was_htlc_preimage_released(htlc_id=htlc_id, htlc_sender=REMOTE):
|
if not self.hm.was_htlc_preimage_released(htlc_id=htlc_id, htlc_proposer=REMOTE):
|
||||||
continue
|
continue
|
||||||
if htlc.cltv_expiry - recv_htlc_deadline > local_height:
|
if htlc.cltv_expiry - recv_htlc_deadline > local_height:
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -294,6 +294,9 @@ class HTLCManager:
|
||||||
|
|
||||||
##### Queries re HTLCs:
|
##### Queries re HTLCs:
|
||||||
|
|
||||||
|
def get_htlc_by_id(self, htlc_proposer: HTLCOwner, htlc_id: int) -> UpdateAddHtlc:
|
||||||
|
return self.log[htlc_proposer]['adds'][htlc_id]
|
||||||
|
|
||||||
@with_lock
|
@with_lock
|
||||||
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:
|
||||||
|
@ -365,11 +368,18 @@ class HTLCManager:
|
||||||
ctn = self.ctn_latest(subject) + 1
|
ctn = self.ctn_latest(subject) + 1
|
||||||
return self.htlcs(subject, ctn)
|
return self.htlcs(subject, ctn)
|
||||||
|
|
||||||
def was_htlc_preimage_released(self, *, htlc_id: int, htlc_sender: HTLCOwner) -> bool:
|
def was_htlc_preimage_released(self, *, htlc_id: int, htlc_proposer: HTLCOwner) -> bool:
|
||||||
settles = self.log[htlc_sender]['settles']
|
settles = self.log[htlc_proposer]['settles']
|
||||||
if htlc_id not in settles:
|
if htlc_id not in settles:
|
||||||
return False
|
return False
|
||||||
return settles[htlc_id][htlc_sender] is not None
|
return settles[htlc_id][htlc_proposer] is not None
|
||||||
|
|
||||||
|
def was_htlc_failed(self, *, htlc_id: int, htlc_proposer: HTLCOwner) -> bool:
|
||||||
|
"""Returns whether an HTLC has been (or will be if we already know) failed."""
|
||||||
|
fails = self.log[htlc_proposer]['fails']
|
||||||
|
if htlc_id not in fails:
|
||||||
|
return False
|
||||||
|
return fails[htlc_id][htlc_proposer] is not None
|
||||||
|
|
||||||
@with_lock
|
@with_lock
|
||||||
def all_settled_htlcs_ever_by_direction(self, subject: HTLCOwner, direction: Direction,
|
def all_settled_htlcs_ever_by_direction(self, subject: HTLCOwner, direction: Direction,
|
||||||
|
@ -402,6 +412,12 @@ class HTLCManager:
|
||||||
received = [(RECEIVED, x) for x in self.all_settled_htlcs_ever_by_direction(subject, RECEIVED, ctn)]
|
received = [(RECEIVED, x) for x in self.all_settled_htlcs_ever_by_direction(subject, RECEIVED, ctn)]
|
||||||
return sent + received
|
return sent + received
|
||||||
|
|
||||||
|
@with_lock
|
||||||
|
def all_htlcs_ever(self) -> Sequence[Tuple[Direction, UpdateAddHtlc]]:
|
||||||
|
sent = [(SENT, htlc) for htlc in self.log[LOCAL]['adds'].values()]
|
||||||
|
received = [(RECEIVED, htlc) for htlc in self.log[LOCAL]['adds'].values()]
|
||||||
|
return sent + received
|
||||||
|
|
||||||
@with_lock
|
@with_lock
|
||||||
def get_balance_msat(self, whose: HTLCOwner, *, ctx_owner=HTLCOwner.LOCAL, ctn: int = None,
|
def get_balance_msat(self, whose: HTLCOwner, *, ctx_owner=HTLCOwner.LOCAL, ctn: int = None,
|
||||||
initial_balance_msat: int) -> int:
|
initial_balance_msat: int) -> int:
|
||||||
|
|
|
@ -1541,7 +1541,7 @@ class Peer(Logger):
|
||||||
if chan.get_oldest_unrevoked_ctn(REMOTE) <= remote_ctn:
|
if chan.get_oldest_unrevoked_ctn(REMOTE) <= remote_ctn:
|
||||||
continue
|
continue
|
||||||
chan.logger.info(f'found unfulfilled htlc: {htlc_id}')
|
chan.logger.info(f'found unfulfilled htlc: {htlc_id}')
|
||||||
htlc = chan.hm.log[REMOTE]['adds'][htlc_id]
|
htlc = chan.hm.get_htlc_by_id(REMOTE, htlc_id)
|
||||||
payment_hash = htlc.payment_hash
|
payment_hash = htlc.payment_hash
|
||||||
error_reason = None # type: Optional[OnionRoutingFailureMessage]
|
error_reason = None # type: Optional[OnionRoutingFailureMessage]
|
||||||
error_bytes = None # type: Optional[bytes]
|
error_bytes = None # type: Optional[bytes]
|
||||||
|
|
Loading…
Add table
Reference in a new issue