From 32249cb72e8a1982566d4f22cebcd193efe16608 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 1 Nov 2019 11:04:28 +0100 Subject: [PATCH] cnct: add new methods for resolvers --- contractcourt/channel_arbitrator.go | 67 ++++++------------- contractcourt/commit_sweep_resolver.go | 22 +++--- contractcourt/contract_resolvers.go | 5 -- .../htlc_incoming_contest_resolver.go | 32 ++++++--- .../htlc_outgoing_contest_resolver.go | 26 ++++--- contractcourt/htlc_success_resolver.go | 24 ++++--- contractcourt/htlc_timeout_resolver.go | 24 ++++--- 7 files changed, 102 insertions(+), 98 deletions(-) diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index 48987597..66730ed1 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -1733,14 +1733,10 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit := newContractResolverKit(resolverCfg) - resolver := &htlcSuccessResolver{ - htlcResolution: resolution, - broadcastHeight: height, - payHash: htlc.RHash, - htlcAmt: htlc.Amt, - contractResolverKit: *resKit, - } + resolver := newSuccessResolver( + resolution, height, + htlc.RHash, htlc.Amt, resolverCfg, + ) htlcResolvers = append(htlcResolvers, resolver) } @@ -1761,14 +1757,10 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit := newContractResolverKit(resolverCfg) - resolver := &htlcTimeoutResolver{ - htlcResolution: resolution, - broadcastHeight: height, - htlcIndex: htlc.HtlcIndex, - htlcAmt: htlc.Amt, - contractResolverKit: *resKit, - } + resolver := newTimeoutResolver( + resolution, height, htlc.HtlcIndex, + htlc.Amt, resolverCfg, + ) htlcResolvers = append(htlcResolvers, resolver) } @@ -1798,18 +1790,11 @@ func (c *ChannelArbitrator) prepContractResolutions( ChanID: c.cfg.ShortChanID, } - resKit := newContractResolverKit(resolverCfg) - resolver := &htlcIncomingContestResolver{ - htlcExpiry: htlc.RefundTimeout, - circuitKey: circuitKey, - htlcSuccessResolver: htlcSuccessResolver{ - htlcResolution: resolution, - broadcastHeight: height, - payHash: htlc.RHash, - htlcAmt: htlc.Amt, - contractResolverKit: *resKit, - }, - } + resolver := newIncomingContestResolver( + htlc.RefundTimeout, circuitKey, + resolution, height, htlc.RHash, + htlc.Amt, resolverCfg, + ) htlcResolvers = append(htlcResolvers, resolver) } @@ -1831,16 +1816,10 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit := newContractResolverKit(resolverCfg) - resolver := &htlcOutgoingContestResolver{ - htlcTimeoutResolver: htlcTimeoutResolver{ - htlcResolution: resolution, - broadcastHeight: height, - htlcIndex: htlc.HtlcIndex, - htlcAmt: htlc.Amt, - contractResolverKit: *resKit, - }, - } + resolver := newOutgoingContestResolver( + resolution, height, htlc.HtlcIndex, + htlc.Amt, resolverCfg, + ) htlcResolvers = append(htlcResolvers, resolver) } } @@ -1850,14 +1829,10 @@ func (c *ChannelArbitrator) prepContractResolutions( // a resolver to sweep our commitment output (but only if it wasn't // trimmed). if contractResolutions.CommitResolution != nil { - resKit := newContractResolverKit(resolverCfg) - resolver := &commitSweepResolver{ - commitResolution: *contractResolutions.CommitResolution, - broadcastHeight: height, - chanPoint: c.cfg.ChanPoint, - contractResolverKit: *resKit, - } - + resolver := newCommitSweepResolver( + *contractResolutions.CommitResolution, + height, c.cfg.ChanPoint, resolverCfg, + ) htlcResolvers = append(htlcResolvers, resolver) } diff --git a/contractcourt/commit_sweep_resolver.go b/contractcourt/commit_sweep_resolver.go index 26bc42ce..3b7f3b85 100644 --- a/contractcourt/commit_sweep_resolver.go +++ b/contractcourt/commit_sweep_resolver.go @@ -39,6 +39,19 @@ type commitSweepResolver struct { contractResolverKit } +// newCommitSweepResolver instantiates a new direct commit output resolver. +func newCommitSweepResolver(res lnwallet.CommitOutputResolution, + broadcastHeight uint32, + chanPoint wire.OutPoint, resCfg ResolverConfig) *commitSweepResolver { + + return &commitSweepResolver{ + contractResolverKit: *newContractResolverKit(resCfg), + commitResolution: res, + broadcastHeight: broadcastHeight, + chanPoint: chanPoint, + } +} + // ResolverKey returns an identifier which should be globally unique for this // particular resolver within the chain the original contract resides within. func (c *commitSweepResolver) ResolverKey() []byte { @@ -298,15 +311,6 @@ func newCommitSweepResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return c, nil } -// AttachConfig should be called once a resolved is successfully decoded from -// its stored format. This struct delivers the configuration items that -// resolvers need to complete their duty. -// -// NOTE: Part of the ContractResolver interface. -func (c *commitSweepResolver) AttachConfig(r ResolverConfig) { - c.contractResolverKit = *newContractResolverKit(r) -} - // A compile time assertion to ensure commitSweepResolver meets the // ContractResolver interface. var _ ContractResolver = (*commitSweepResolver)(nil) diff --git a/contractcourt/contract_resolvers.go b/contractcourt/contract_resolvers.go index 709fc3a5..7248bba0 100644 --- a/contractcourt/contract_resolvers.go +++ b/contractcourt/contract_resolvers.go @@ -46,11 +46,6 @@ type ContractResolver interface { // passed Writer. Encode(w io.Writer) error - // AttachConfig should be called once a resolved is successfully decoded - // from its stored format. This struct delivers the configuration items - // that resolvers need to complete their duty. - AttachConfig(ResolverConfig) - // Stop signals the resolver to cancel any current resolution // processes, and suspend. Stop() diff --git a/contractcourt/htlc_incoming_contest_resolver.go b/contractcourt/htlc_incoming_contest_resolver.go index 00993e24..5a40369c 100644 --- a/contractcourt/htlc_incoming_contest_resolver.go +++ b/contractcourt/htlc_incoming_contest_resolver.go @@ -5,11 +5,12 @@ import ( "errors" "io" + "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/invoices" - - "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/lntypes" + "github.com/lightningnetwork/lnd/lnwallet" + "github.com/lightningnetwork/lnd/lnwire" ) // htlcIncomingContestResolver is a ContractResolver that's able to resolve an @@ -34,6 +35,24 @@ type htlcIncomingContestResolver struct { htlcSuccessResolver } +// newIncomingContestResolver instantiates a new incoming htlc contest resolver. +func newIncomingContestResolver(htlcExpiry uint32, + circuitKey channeldb.CircuitKey, res lnwallet.IncomingHtlcResolution, + broadcastHeight uint32, payHash lntypes.Hash, + htlcAmt lnwire.MilliSatoshi, + resCfg ResolverConfig) *htlcIncomingContestResolver { + + success := newSuccessResolver( + res, broadcastHeight, payHash, htlcAmt, resCfg, + ) + + return &htlcIncomingContestResolver{ + htlcExpiry: htlcExpiry, + circuitKey: circuitKey, + htlcSuccessResolver: *success, + } +} + // Resolve attempts to resolve this contract. As we don't yet know of the // preimage for the contract, we'll wait for one of two things to happen: // @@ -319,15 +338,6 @@ func newIncomingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } -// AttachConfig should be called once a resolved is successfully decoded from -// its stored format. This struct delivers the configuration items that -// resolvers need to complete their duty. -// -// NOTE: Part of the ContractResolver interface. -func (h *htlcIncomingContestResolver) AttachConfig(r ResolverConfig) { - h.htlcSuccessResolver.AttachConfig(r) -} - // A compile time assertion to ensure htlcIncomingContestResolver meets the // ContractResolver interface. var _ ContractResolver = (*htlcIncomingContestResolver)(nil) diff --git a/contractcourt/htlc_outgoing_contest_resolver.go b/contractcourt/htlc_outgoing_contest_resolver.go index 3f4981af..7388b45a 100644 --- a/contractcourt/htlc_outgoing_contest_resolver.go +++ b/contractcourt/htlc_outgoing_contest_resolver.go @@ -5,6 +5,8 @@ import ( "io" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/lnwallet" + "github.com/lightningnetwork/lnd/lnwire" ) // htlcOutgoingContestResolver is a ContractResolver that's able to resolve an @@ -18,6 +20,21 @@ type htlcOutgoingContestResolver struct { htlcTimeoutResolver } +// newOutgoingContestResolver instantiates a new outgoing contested htlc +// resolver. +func newOutgoingContestResolver(res lnwallet.OutgoingHtlcResolution, + broadcastHeight uint32, htlcIndex uint64, htlcAmt lnwire.MilliSatoshi, + resCfg ResolverConfig) *htlcOutgoingContestResolver { + + timeout := newTimeoutResolver( + res, broadcastHeight, htlcIndex, htlcAmt, resCfg, + ) + + return &htlcOutgoingContestResolver{ + htlcTimeoutResolver: *timeout, + } +} + // Resolve commences the resolution of this contract. As this contract hasn't // yet timed out, we'll wait for one of two things to happen // @@ -196,15 +213,6 @@ func newOutgoingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } -// AttachConfig should be called once a resolved is successfully decoded from -// its stored format. This struct delivers the configuration items that -// resolvers need to complete their duty. -// -// NOTE: Part of the ContractResolver interface. -func (h *htlcOutgoingContestResolver) AttachConfig(r ResolverConfig) { - h.htlcTimeoutResolver.AttachConfig(r) -} - // A compile time assertion to ensure htlcOutgoingContestResolver meets the // ContractResolver interface. var _ ContractResolver = (*htlcOutgoingContestResolver)(nil) diff --git a/contractcourt/htlc_success_resolver.go b/contractcourt/htlc_success_resolver.go index 8645f24d..73bf5c5b 100644 --- a/contractcourt/htlc_success_resolver.go +++ b/contractcourt/htlc_success_resolver.go @@ -55,6 +55,21 @@ type htlcSuccessResolver struct { contractResolverKit } +// newSuccessResolver instanties a new htlc success resolver. +func newSuccessResolver(res lnwallet.IncomingHtlcResolution, + broadcastHeight uint32, payHash lntypes.Hash, + htlcAmt lnwire.MilliSatoshi, + resCfg ResolverConfig) *htlcSuccessResolver { + + return &htlcSuccessResolver{ + contractResolverKit: *newContractResolverKit(resCfg), + htlcResolution: res, + broadcastHeight: broadcastHeight, + payHash: payHash, + htlcAmt: htlcAmt, + } +} + // ResolverKey returns an identifier which should be globally unique for this // particular resolver within the chain the original contract resides within. // @@ -323,15 +338,6 @@ func newSuccessResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } -// AttachConfig should be called once a resolved is successfully decoded from -// its stored format. This struct delivers the configuration items that -// resolvers need to complete their duty. -// -// NOTE: Part of the ContractResolver interface. -func (h *htlcSuccessResolver) AttachConfig(r ResolverConfig) { - h.contractResolverKit = *newContractResolverKit(r) -} - // A compile time assertion to ensure htlcSuccessResolver meets the // ContractResolver interface. var _ ContractResolver = (*htlcSuccessResolver)(nil) diff --git a/contractcourt/htlc_timeout_resolver.go b/contractcourt/htlc_timeout_resolver.go index 5edc21a0..248cfa36 100644 --- a/contractcourt/htlc_timeout_resolver.go +++ b/contractcourt/htlc_timeout_resolver.go @@ -51,6 +51,21 @@ type htlcTimeoutResolver struct { contractResolverKit } +// newTimeoutResolver instantiates a new timeout htlc resolver. +func newTimeoutResolver(res lnwallet.OutgoingHtlcResolution, + broadcastHeight uint32, htlcIndex uint64, + htlcAmt lnwire.MilliSatoshi, + resCfg ResolverConfig) *htlcTimeoutResolver { + + return &htlcTimeoutResolver{ + contractResolverKit: *newContractResolverKit(resCfg), + htlcResolution: res, + broadcastHeight: broadcastHeight, + htlcIndex: htlcIndex, + htlcAmt: htlcAmt, + } +} + // ResolverKey returns an identifier which should be globally unique for this // particular resolver within the chain the original contract resides within. // @@ -441,15 +456,6 @@ func newTimeoutResolverFromReader(r io.Reader, resCfg ResolverConfig) ( return h, nil } -// AttachConfig should be called once a resolved is successfully decoded from -// its stored format. This struct delivers the configuration items that -// resolvers need to complete their duty. -// -// NOTE: Part of the ContractResolver interface. -func (h *htlcTimeoutResolver) AttachConfig(r ResolverConfig) { - h.contractResolverKit = *newContractResolverKit(r) -} - // A compile time assertion to ensure htlcTimeoutResolver meets the // ContractResolver interface. var _ ContractResolver = (*htlcTimeoutResolver)(nil)