tdo not duplicate ctn in channel log and config - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit 82491ff083368fe57821998aeea7f5f4b1f4e5a8 (DIR) parent 8d28188d246e63e5bb734bfdc0cf76d78410f48f (HTM) Author: ThomasV <thomasv@electrum.org> Date: Thu, 7 Mar 2019 12:44:22 +0100 do not duplicate ctn in channel log and config Diffstat: M electrum/lnchannel.py | 5 +++-- M electrum/lnhtlc.py | 48 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 26 deletions(-) --- (DIR) diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py t@@ -151,7 +151,8 @@ class Channel(PrintError): self.remote_commitment_to_be_revoked = Transaction(state["remote_commitment_to_be_revoked"]) self.remote_commitment_to_be_revoked.deserialize(True) - self.hm = HTLCManager(state.get('log')) + log = state.get('log') + self.hm = HTLCManager(self.config[LOCAL].ctn if log else 0, self.config[REMOTE].ctn if log else 0, log) self.name = name t@@ -535,7 +536,7 @@ class Channel(PrintError): pending outgoing HTLCs, is used in the UI. """ assert type(subject) is HTLCOwner - ctn = self.hm.log[subject]['ctn'] + 1 + ctn = self.hm.ctn[subject] + 1 return self.balance(subject, ctn)\ - htlcsum(self.hm.htlcs_by_direction(subject, SENT, ctn)) (DIR) diff --git a/electrum/lnhtlc.py b/electrum/lnhtlc.py t@@ -3,19 +3,19 @@ from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Dir from .util import bh2u class HTLCManager: - def __init__(self, log=None): + def __init__(self, local_ctn=0, remote_ctn=0, log=None): + self.ctn = {LOCAL:local_ctn, REMOTE: remote_ctn} self.expect_sig = {SENT: False, RECEIVED: False} if log is None: - initial = {'ctn': 0, 'adds': {}, 'locked_in': {}, 'settles': {}, 'fails': {}} + initial = {'adds': {}, 'locked_in': {}, 'settles': {}, 'fails': {}} log = {LOCAL: deepcopy(initial), REMOTE: deepcopy(initial)} else: assert type(log) is dict log = {HTLCOwner(int(x)): y for x, y in deepcopy(log).items()} - # log[sub]['ctn'] is the ctn for the oldest unrevoked ctx of sub + # self.ctn[sub] is the ctn for the oldest unrevoked ctx of sub for sub in (LOCAL, REMOTE): log[sub]['adds'] = {int(x): UpdateAddHtlc(*y) for x, y in log[sub]['adds'].items()} coerceHtlcOwner2IntMap = lambda x: {HTLCOwner(int(y)): z for y, z in x.items()} - # "side who offered htlc" -> action -> htlc_id -> whose ctx -> ctn log[sub]['locked_in'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['locked_in'].items()} log[sub]['settles'] = {int(x): coerceHtlcOwner2IntMap(y) for x, y in log[sub]['settles'].items()} t@@ -37,47 +37,47 @@ class HTLCManager: adds = self.log[LOCAL]['adds'] assert type(adds) is not str adds[htlc_id] = htlc - self.log[LOCAL]['locked_in'][htlc_id] = {LOCAL: None, REMOTE: self.log[REMOTE]['ctn']+1} + self.log[LOCAL]['locked_in'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE]+1} self.expect_sig[SENT] = True return htlc def recv_htlc(self, htlc): htlc_id = htlc.htlc_id self.log[REMOTE]['adds'][htlc_id] = htlc - l = self.log[REMOTE]['locked_in'][htlc_id] = {LOCAL: self.log[LOCAL]['ctn']+1, REMOTE: None} + l = self.log[REMOTE]['locked_in'][htlc_id] = {LOCAL: self.ctn[LOCAL]+1, REMOTE: None} self.expect_sig[RECEIVED] = True def send_ctx(self): - next_ctn = self.log[REMOTE]['ctn'] + 1 + next_ctn = self.ctn[REMOTE] + 1 for locked_in in self.log[REMOTE]['locked_in'].values(): if locked_in[REMOTE] is None: locked_in[REMOTE] = next_ctn self.expect_sig[SENT] = False def recv_ctx(self): - next_ctn = self.log[LOCAL]['ctn'] + 1 + next_ctn = self.ctn[LOCAL] + 1 for locked_in in self.log[LOCAL]['locked_in'].values(): if locked_in[LOCAL] is None: locked_in[LOCAL] = next_ctn self.expect_sig[RECEIVED] = False def send_rev(self): - self.log[LOCAL]['ctn'] += 1 + self.ctn[LOCAL] += 1 for htlc_id, ctns in self.log[LOCAL]['settles'].items(): if ctns[REMOTE] is None: - ctns[REMOTE] = self.log[REMOTE]['ctn'] + 1 + ctns[REMOTE] = self.ctn[REMOTE] + 1 def recv_rev(self): - self.log[REMOTE]['ctn'] += 1 + self.ctn[REMOTE] += 1 did_set_htlc_height = False for htlc_id, ctns in self.log[LOCAL]['locked_in'].items(): if ctns[LOCAL] is None: did_set_htlc_height = True - assert ctns[REMOTE] == self.log[REMOTE]['ctn'] - ctns[LOCAL] = self.log[LOCAL]['ctn'] + 1 + assert ctns[REMOTE] == self.ctn[REMOTE] + ctns[LOCAL] = self.ctn[LOCAL] + 1 for htlc_id, ctns in self.log[REMOTE]['settles'].items(): if ctns[LOCAL] is None: - ctns[LOCAL] = self.log[LOCAL]['ctn'] + 1 + ctns[LOCAL] = self.ctn[LOCAL] + 1 return did_set_htlc_height def htlcs_by_direction(self, subject, direction, ctn=None): t@@ -87,7 +87,7 @@ class HTLCManager: assert type(subject) is HTLCOwner assert type(direction) is Direction if ctn is None: - ctn = self.log[subject]['ctn'] + ctn = self.ctn[subject] l = [] if direction == SENT and subject == LOCAL: party = LOCAL t@@ -113,7 +113,7 @@ class HTLCManager: def htlcs(self, subject, ctn=None): assert type(subject) is HTLCOwner if ctn is None: - ctn = self.log[subject]['ctn'] + ctn = self.ctn[subject] l = [] l += [(SENT, x) for x in self.htlcs_by_direction(subject, SENT, ctn)] l += [(RECEIVED, x) for x in self.htlcs_by_direction(subject, RECEIVED, ctn)] t@@ -121,24 +121,24 @@ class HTLCManager: def current_htlcs(self, subject): assert type(subject) is HTLCOwner - ctn = self.log[subject]['ctn'] + ctn = self.ctn[subject] return self.htlcs(subject, ctn) def pending_htlcs(self, subject): assert type(subject) is HTLCOwner - ctn = self.log[subject]['ctn'] + 1 + ctn = self.ctn[subject] + 1 return self.htlcs(subject, ctn) def send_settle(self, htlc_id): - self.log[REMOTE]['settles'][htlc_id] = {LOCAL: None, REMOTE: self.log[REMOTE]['ctn'] + 1} + self.log[REMOTE]['settles'][htlc_id] = {LOCAL: None, REMOTE: self.ctn[REMOTE] + 1} def recv_settle(self, htlc_id): - self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.log[LOCAL]['ctn'] + 1, REMOTE: None} + self.log[LOCAL]['settles'][htlc_id] = {LOCAL: self.ctn[LOCAL] + 1, REMOTE: None} def settled_htlcs_by(self, subject, ctn=None): assert type(subject) is HTLCOwner if ctn is None: - ctn = self.log[subject]['ctn'] + ctn = self.ctn[subject] d = [] for htlc_id, ctns in self.log[subject]['settles'].items(): if ctns[subject] <= ctn: t@@ -148,7 +148,7 @@ class HTLCManager: def settled_htlcs(self, subject, ctn=None): assert type(subject) is HTLCOwner if ctn is None: - ctn = self.log[subject]['ctn'] + ctn = self.ctn[subject] sent = [(SENT, x) for x in self.settled_htlcs_by(subject, ctn)] other = subject.inverted() received = [(RECEIVED, x) for x in self.settled_htlcs_by(other, ctn)] t@@ -165,7 +165,7 @@ class HTLCManager: if ctns[LOCAL] == ctn] def send_fail(self, htlc_id): - self.log[REMOTE]['fails'][htlc_id] = self.log[REMOTE]['ctn'] + 1 + self.log[REMOTE]['fails'][htlc_id] = self.ctn[REMOTE] + 1 def recv_fail(self, htlc_id): - self.log[LOCAL]['fails'][htlc_id] = self.log[LOCAL]['ctn'] + 1 + self.log[LOCAL]['fails'][htlc_id] = self.ctn[LOCAL] + 1