From d790eeb375e136cdb912a92018e5bc497c2cf537 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 9 Nov 2017 22:23:17 -0800 Subject: [PATCH] lnwallet: add [our|their]HtlcIndex to the commitment struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With these new fields, we’ll be able to properly reconstruct the log state after a restart, as each commitment will now note both the current HTLC and log index. --- lnwallet/channel.go | 48 +++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 2ec41127..85afabe1 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -4,7 +4,6 @@ import ( "bytes" "container/list" "crypto/sha256" - "errors" "fmt" "runtime" "sort" @@ -92,7 +91,7 @@ const ( // payments requested by the wallet/daemon. type PaymentHash [32]byte -// UpdateType is the exact type of an entry within the shared HTLC log. +// updateType is the exact type of an entry within the shared HTLC log. type updateType uint8 const ( @@ -107,10 +106,11 @@ const ( // which contains the Fail entry. Fail - // MalformedFail is an update type which removes a prior HTLC entry from the - // log. Adding a MalformedFail entry to ones log will modify the _remote_ - // parties update log once a new commitment view has been evaluated which - // contains the MalformedFail entry. The difference from Fail type lie in + // MalformedFail is an update type which removes a prior HTLC entry + // from the log. Adding a MalformedFail entry to ones log will modify + // the _remote_ parties update log once a new commitment view has been + // evaluated which contains the MalformedFail entry. The difference + // from Fail type lie in // the different data we have to store. MalformedFail @@ -184,7 +184,7 @@ type PaymentDescriptor struct { // localOutputIndex is the output index of this HTLc output in the // commitment transaction of the local node. // - // NOTE: If the output is dust from the PoV of the local comimtnet + // NOTE: If the output is dust from the PoV of the local commitment // chain, then this value will be -1. localOutputIndex int32 @@ -274,8 +274,8 @@ type commitment struct { // update number of this commitment. height uint64 - // isOurs indicates whether this is the local or remote node's version of - // the commitment. + // isOurs indicates whether this is the local or remote node's version + // of the commitment. isOurs bool // [our|their]MessageIndex are indexes into the HTLC log, up to which @@ -287,6 +287,14 @@ type commitment struct { ourMessageIndex uint64 theirMessageIndex uint64 + // [our|their]HtlcIndex are the current running counters for the HTLC's + // offered by either party. This value is incremented each time a party + // offers a new HTLC. The log update methods that consume HTLC's will + // reference these counters, rather than the running cumulative message + // counters. + ourHtlcIndex uint64 + theirHtlcIndex uint64 + // txn is the commitment transaction generated by including any HTLC // updates whose index are below the two indexes listed above. If this // commitment is being added to the remote chain, then this txn is @@ -305,16 +313,16 @@ type commitment struct { theirBalance lnwire.MilliSatoshi // fee is the amount that will be paid as fees for this commitment - // transaction. The fee is recorded here so that it can be added - // back and recalculated for each new update to the channel state. + // transaction. The fee is recorded here so that it can be added back + // and recalculated for each new update to the channel state. fee btcutil.Amount // feePerKw is the fee per kw used to calculate this commitment // transaction's fee. feePerKw btcutil.Amount - // dustLimit is the limit on the commitment transaction such that no output - // values should be below this amount. + // dustLimit is the limit on the commitment transaction such that no + // output values should be below this amount. dustLimit btcutil.Amount // outgoingHTLCs is a slice of all the outgoing HTLC's (from our PoV) @@ -327,12 +335,14 @@ type commitment struct { // [outgoing|incoming]HTLCIndex is an index that maps an output index // on the commitment transaction to the payment descriptor that - // represents the HTLC output. Note that these fields are only - // populated if this commitment state belongs to the local node. These - // maps are used when validating any HTLC signatures which are part of - // the local commitment state. We use this map in order to locate the - // details needed to validate an HTLC signature while iterating of the - // outputs int he local commitment view. + // represents the HTLC output. + // + // NOTE: that these fields are only populated if this commitment state + // belongs to the local node. These maps are used when validating any + // HTLC signatures which are part of the local commitment state. We use + // this map in order to locate the details needed to validate an HTLC + // signature while iterating of the outputs in the local commitment + // view. outgoingHTLCIndex map[int32]*PaymentDescriptor incomingHTLCIndex map[int32]*PaymentDescriptor }