From a83be177c6dfce2cb29f8d1eb3185e0b2250ef7f Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 1 Nov 2019 12:23:48 +0100 Subject: [PATCH] cnct: move supplement method into resolvers --- contractcourt/channel_arbitrator.go | 98 +++---------------- contractcourt/contract_resolvers.go | 15 +++ .../htlc_incoming_contest_resolver.go | 14 ++- .../htlc_outgoing_contest_resolver.go | 2 +- contractcourt/htlc_success_resolver.go | 18 +++- contractcourt/htlc_timeout_resolver.go | 18 +++- 6 files changed, 76 insertions(+), 89 deletions(-) diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index 66730ed1..e9001d8e 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -501,9 +501,20 @@ func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet) error { "resolvers", c.cfg.ChanPoint, len(unresolvedContracts)) for _, resolver := range unresolvedContracts { - if err := c.supplementResolver(resolver, htlcMap); err != nil { - return err + htlcResolver, ok := resolver.(htlcContractResolver) + if !ok { + continue } + + htlcPoint := htlcResolver.HtlcPoint() + htlc, ok := htlcMap[htlcPoint] + if !ok { + return fmt.Errorf( + "htlc resolver %T unavailable", resolver, + ) + } + + htlcResolver.Supplement(*htlc) } c.launchResolvers(unresolvedContracts) @@ -511,89 +522,6 @@ func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet) error { return nil } -// supplementResolver takes a resolver as it is restored from the log and fills -// in missing data from the htlcMap. -func (c *ChannelArbitrator) supplementResolver(resolver ContractResolver, - htlcMap map[wire.OutPoint]*channeldb.HTLC) error { - - switch r := resolver.(type) { - - case *htlcSuccessResolver: - return c.supplementSuccessResolver(r, htlcMap) - - case *htlcIncomingContestResolver: - return c.supplementIncomingContestResolver(r, htlcMap) - - case *htlcTimeoutResolver: - return c.supplementTimeoutResolver(r, htlcMap) - - case *htlcOutgoingContestResolver: - return c.supplementTimeoutResolver( - &r.htlcTimeoutResolver, htlcMap, - ) - } - - return nil -} - -// supplementSuccessResolver takes a htlcIncomingContestResolver as it is -// restored from the log and fills in missing data from the htlcMap. -func (c *ChannelArbitrator) supplementIncomingContestResolver( - r *htlcIncomingContestResolver, - htlcMap map[wire.OutPoint]*channeldb.HTLC) error { - - res := r.htlcResolution - htlcPoint := res.HtlcPoint() - htlc, ok := htlcMap[htlcPoint] - if !ok { - return errors.New( - "htlc for incoming contest resolver unavailable", - ) - } - - r.htlcAmt = htlc.Amt - r.circuitKey = channeldb.CircuitKey{ - ChanID: c.cfg.ShortChanID, - HtlcID: htlc.HtlcIndex, - } - - return nil -} - -// supplementSuccessResolver takes a htlcSuccessResolver as it is restored from -// the log and fills in missing data from the htlcMap. -func (c *ChannelArbitrator) supplementSuccessResolver(r *htlcSuccessResolver, - htlcMap map[wire.OutPoint]*channeldb.HTLC) error { - - res := r.htlcResolution - htlcPoint := res.HtlcPoint() - htlc, ok := htlcMap[htlcPoint] - if !ok { - return errors.New( - "htlc for success resolver unavailable", - ) - } - r.htlcAmt = htlc.Amt - return nil -} - -// supplementTimeoutResolver takes a htlcSuccessResolver as it is restored from -// the log and fills in missing data from the htlcMap. -func (c *ChannelArbitrator) supplementTimeoutResolver(r *htlcTimeoutResolver, - htlcMap map[wire.OutPoint]*channeldb.HTLC) error { - - res := r.htlcResolution - htlcPoint := res.HtlcPoint() - htlc, ok := htlcMap[htlcPoint] - if !ok { - return errors.New( - "htlc for timeout resolver unavailable", - ) - } - r.htlcAmt = htlc.Amt - return nil -} - // Report returns htlc reports for the active resolvers. func (c *ChannelArbitrator) Report() []*ContractReport { c.activeResolversLock.RLock() diff --git a/contractcourt/contract_resolvers.go b/contractcourt/contract_resolvers.go index 7248bba0..2cda229a 100644 --- a/contractcourt/contract_resolvers.go +++ b/contractcourt/contract_resolvers.go @@ -4,6 +4,9 @@ import ( "encoding/binary" "errors" "io" + + "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/channeldb" ) var ( @@ -51,6 +54,18 @@ type ContractResolver interface { Stop() } +// htlcContractResolver is the required interface for htlc resolvers. +type htlcContractResolver interface { + ContractResolver + + // HtlcPoint returns the htlc's outpoint on the commitment tx. + HtlcPoint() wire.OutPoint + + // Supplement adds additional information to the resolver that is + // required before Resolve() is called. + Supplement(htlc channeldb.HTLC) +} + // reportingContractResolver is a ContractResolver that also exposes a report on // the resolution state of the contract. type reportingContractResolver interface { diff --git a/contractcourt/htlc_incoming_contest_resolver.go b/contractcourt/htlc_incoming_contest_resolver.go index 5a40369c..84ed63ea 100644 --- a/contractcourt/htlc_incoming_contest_resolver.go +++ b/contractcourt/htlc_incoming_contest_resolver.go @@ -338,6 +338,18 @@ func newIncomingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } +// Supplement adds additional information to the resolver that is required +// before Resolve() is called. +// +// NOTE: Part of the htlcContractResolver interface. +func (h *htlcIncomingContestResolver) Supplement(htlc channeldb.HTLC) { + h.htlcAmt = htlc.Amt + h.circuitKey = channeldb.CircuitKey{ + ChanID: h.ShortChanID, + HtlcID: htlc.HtlcIndex, + } +} + // A compile time assertion to ensure htlcIncomingContestResolver meets the // ContractResolver interface. -var _ ContractResolver = (*htlcIncomingContestResolver)(nil) +var _ htlcContractResolver = (*htlcIncomingContestResolver)(nil) diff --git a/contractcourt/htlc_outgoing_contest_resolver.go b/contractcourt/htlc_outgoing_contest_resolver.go index 7388b45a..12146f2f 100644 --- a/contractcourt/htlc_outgoing_contest_resolver.go +++ b/contractcourt/htlc_outgoing_contest_resolver.go @@ -215,4 +215,4 @@ func newOutgoingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) ( // A compile time assertion to ensure htlcOutgoingContestResolver meets the // ContractResolver interface. -var _ ContractResolver = (*htlcOutgoingContestResolver)(nil) +var _ htlcContractResolver = (*htlcOutgoingContestResolver)(nil) diff --git a/contractcourt/htlc_success_resolver.go b/contractcourt/htlc_success_resolver.go index 73bf5c5b..01244010 100644 --- a/contractcourt/htlc_success_resolver.go +++ b/contractcourt/htlc_success_resolver.go @@ -6,6 +6,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwallet" @@ -338,6 +339,21 @@ func newSuccessResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } +// Supplement adds additional information to the resolver that is required +// before Resolve() is called. +// +// NOTE: Part of the htlcContractResolver interface. +func (h *htlcSuccessResolver) Supplement(htlc channeldb.HTLC) { + h.htlcAmt = htlc.Amt +} + +// HtlcPoint returns the htlc's outpoint on the commitment tx. +// +// NOTE: Part of the htlcContractResolver interface. +func (h *htlcSuccessResolver) HtlcPoint() wire.OutPoint { + return h.htlcResolution.HtlcPoint() +} + // A compile time assertion to ensure htlcSuccessResolver meets the // ContractResolver interface. -var _ ContractResolver = (*htlcSuccessResolver)(nil) +var _ htlcContractResolver = (*htlcSuccessResolver)(nil) diff --git a/contractcourt/htlc_timeout_resolver.go b/contractcourt/htlc_timeout_resolver.go index 248cfa36..f36e8292 100644 --- a/contractcourt/htlc_timeout_resolver.go +++ b/contractcourt/htlc_timeout_resolver.go @@ -8,6 +8,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd/chainntnfs" + "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwallet" @@ -456,6 +457,21 @@ func newTimeoutResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } +// Supplement adds additional information to the resolver that is required +// before Resolve() is called. +// +// NOTE: Part of the htlcContractResolver interface. +func (h *htlcTimeoutResolver) Supplement(htlc channeldb.HTLC) { + h.htlcAmt = htlc.Amt +} + +// HtlcPoint returns the htlc's outpoint on the commitment tx. +// +// NOTE: Part of the htlcContractResolver interface. +func (h *htlcTimeoutResolver) HtlcPoint() wire.OutPoint { + return h.htlcResolution.HtlcPoint() +} + // A compile time assertion to ensure htlcTimeoutResolver meets the // ContractResolver interface. -var _ ContractResolver = (*htlcTimeoutResolver)(nil) +var _ htlcContractResolver = (*htlcTimeoutResolver)(nil)