From ed4f77871a42b626c95b4130b4cae6c70404eae9 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 23 Apr 2018 13:21:02 -0700 Subject: [PATCH] htlcswitch/circuit_map: trim using NextLocalHtlcIndex --- htlcswitch/circuit_map.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/htlcswitch/circuit_map.go b/htlcswitch/circuit_map.go index 844a93169..1fa0a0422 100644 --- a/htlcswitch/circuit_map.go +++ b/htlcswitch/circuit_map.go @@ -343,8 +343,9 @@ func (cm *circuitMap) decodeCircuit(v []byte) (*PaymentCircuit, error) { } // trimAllOpenCircuits reads the set of active channels from disk and trims -// keystones for any non-pending channels. This method is intended to be called -// on startup. Each link will also trim it's own circuits upon startup. +// keystones for any non-pending channels using the next unallocated htlc index. +// This method is intended to be called on startup. Each link will also trim +// it's own circuits upon startup. // // NOTE: This operation will be applied to the persistent state of all active // channels. Therefore, it must be called before any links are created to avoid @@ -356,9 +357,31 @@ func (cm *circuitMap) trimAllOpenCircuits() error { } for _, activeChannel := range activeChannels { + if activeChannel.IsPending { + continue + } + + // First, skip any channels that have not been assigned their + // final channel identifier, otherwise we would try to trim + // htlcs belonging to the all-zero, sourceHop ID. chanID := activeChannel.ShortChanID - start := activeChannel.LocalCommitment.LocalHtlcIndex - if err := cm.TrimOpenCircuits(chanID, start); err != nil { + if chanID == sourceHop { + continue + } + + // Next, retrieve the next unallocated htlc index, which bounds + // the cutoff of confirmed htlc indexes. + start, err := activeChannel.NextLocalHtlcIndex() + if err != nil { + return err + } + + // Finally, remove all pending circuits above at or above the + // next unallocated local htlc indexes. This has the effect of + // reverting any circuits that have either not been locked in, + // or had not been included in a pending commitment. + err = cm.TrimOpenCircuits(chanID, start) + if err != nil { return err } }