diff --git a/contractcourt/briefcase.go b/contractcourt/briefcase.go index 88a743ff..91a02af3 100644 --- a/contractcourt/briefcase.go +++ b/contractcourt/briefcase.go @@ -440,7 +440,7 @@ func (b *boltArbitratorLog) CommitState(s ArbitratorState) error { // // NOTE: Part of the ContractResolver interface. func (b *boltArbitratorLog) FetchUnresolvedContracts() ([]ContractResolver, error) { - resKit := ResolverKit{ + resolverCfg := ResolverConfig{ ChannelArbitratorConfig: b.cfg, Checkpoint: b.checkpointContract, } @@ -516,8 +516,7 @@ func (b *boltArbitratorLog) FetchUnresolvedContracts() ([]ContractResolver, erro return fmt.Errorf("unknown resolver type: %v", resType) } - resKit.quit = make(chan struct{}) - res.AttachResolverKit(resKit) + res.AttachConfig(resolverCfg) contracts = append(contracts, res) return nil }) diff --git a/contractcourt/channel_arbitrator.go b/contractcourt/channel_arbitrator.go index dec845ee..48987597 100644 --- a/contractcourt/channel_arbitrator.go +++ b/contractcourt/channel_arbitrator.go @@ -1685,7 +1685,7 @@ func (c *ChannelArbitrator) prepContractResolutions( // We'll create the resolver kit that we'll be cloning for each // resolver so they each can do their duty. - resKit := ResolverKit{ + resolverCfg := ResolverConfig{ ChannelArbitratorConfig: c.cfg, Checkpoint: func(res ContractResolver) error { return c.log.InsertUnresolvedContracts(res) @@ -1733,13 +1733,13 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit.quit = make(chan struct{}) + resKit := newContractResolverKit(resolverCfg) resolver := &htlcSuccessResolver{ - htlcResolution: resolution, - broadcastHeight: height, - payHash: htlc.RHash, - htlcAmt: htlc.Amt, - ResolverKit: resKit, + htlcResolution: resolution, + broadcastHeight: height, + payHash: htlc.RHash, + htlcAmt: htlc.Amt, + contractResolverKit: *resKit, } htlcResolvers = append(htlcResolvers, resolver) } @@ -1761,13 +1761,13 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit.quit = make(chan struct{}) + resKit := newContractResolverKit(resolverCfg) resolver := &htlcTimeoutResolver{ - htlcResolution: resolution, - broadcastHeight: height, - htlcIndex: htlc.HtlcIndex, - htlcAmt: htlc.Amt, - ResolverKit: resKit, + htlcResolution: resolution, + broadcastHeight: height, + htlcIndex: htlc.HtlcIndex, + htlcAmt: htlc.Amt, + contractResolverKit: *resKit, } htlcResolvers = append(htlcResolvers, resolver) } @@ -1798,16 +1798,16 @@ func (c *ChannelArbitrator) prepContractResolutions( ChanID: c.cfg.ShortChanID, } - resKit.quit = make(chan struct{}) + resKit := newContractResolverKit(resolverCfg) resolver := &htlcIncomingContestResolver{ htlcExpiry: htlc.RefundTimeout, circuitKey: circuitKey, htlcSuccessResolver: htlcSuccessResolver{ - htlcResolution: resolution, - broadcastHeight: height, - payHash: htlc.RHash, - htlcAmt: htlc.Amt, - ResolverKit: resKit, + htlcResolution: resolution, + broadcastHeight: height, + payHash: htlc.RHash, + htlcAmt: htlc.Amt, + contractResolverKit: *resKit, }, } htlcResolvers = append(htlcResolvers, resolver) @@ -1831,14 +1831,14 @@ func (c *ChannelArbitrator) prepContractResolutions( continue } - resKit.quit = make(chan struct{}) + resKit := newContractResolverKit(resolverCfg) resolver := &htlcOutgoingContestResolver{ htlcTimeoutResolver: htlcTimeoutResolver{ - htlcResolution: resolution, - broadcastHeight: height, - htlcIndex: htlc.HtlcIndex, - htlcAmt: htlc.Amt, - ResolverKit: resKit, + htlcResolution: resolution, + broadcastHeight: height, + htlcIndex: htlc.HtlcIndex, + htlcAmt: htlc.Amt, + contractResolverKit: *resKit, }, } htlcResolvers = append(htlcResolvers, resolver) @@ -1850,12 +1850,12 @@ func (c *ChannelArbitrator) prepContractResolutions( // a resolver to sweep our commitment output (but only if it wasn't // trimmed). if contractResolutions.CommitResolution != nil { - resKit.quit = make(chan struct{}) + resKit := newContractResolverKit(resolverCfg) resolver := &commitSweepResolver{ - commitResolution: *contractResolutions.CommitResolution, - broadcastHeight: height, - chanPoint: c.cfg.ChanPoint, - ResolverKit: resKit, + commitResolution: *contractResolutions.CommitResolution, + broadcastHeight: height, + chanPoint: c.cfg.ChanPoint, + contractResolverKit: *resKit, } htlcResolvers = append(htlcResolvers, resolver) diff --git a/contractcourt/commit_sweep_resolver.go b/contractcourt/commit_sweep_resolver.go index 12d90fc5..147892ea 100644 --- a/contractcourt/commit_sweep_resolver.go +++ b/contractcourt/commit_sweep_resolver.go @@ -36,7 +36,7 @@ type commitSweepResolver struct { // chanPoint is the channel point of the original contract. chanPoint wire.OutPoint - ResolverKit + contractResolverKit } // ResolverKey returns an identifier which should be globally unique for this @@ -293,13 +293,13 @@ func (c *commitSweepResolver) Decode(r io.Reader) error { return nil } -// AttachResolverKit should be called once a resolved is successfully decoded -// from its stored format. This struct delivers a generic tool kit that +// 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) AttachResolverKit(r ResolverKit) { - c.ResolverKit = r +func (c *commitSweepResolver) AttachConfig(r ResolverConfig) { + c.contractResolverKit = *newContractResolverKit(r) } // A compile time assertion to ensure commitSweepResolver meets the diff --git a/contractcourt/contract_resolvers.go b/contractcourt/contract_resolvers.go index fc5b324d..709fc3a5 100644 --- a/contractcourt/contract_resolvers.go +++ b/contractcourt/contract_resolvers.go @@ -46,10 +46,10 @@ type ContractResolver interface { // passed Writer. Encode(w io.Writer) error - // AttachResolverKit should be called once a resolved is successfully - // decoded from its stored format. This struct delivers a generic tool - // kit that resolvers need to complete their duty. - AttachResolverKit(ResolverKit) + // 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. @@ -64,10 +64,9 @@ type reportingContractResolver interface { report() *ContractReport } -// ResolverKit is meant to be used as a mix-in struct to be embedded within a -// given ContractResolver implementation. It contains all the items that a -// resolver requires to carry out its duties. -type ResolverKit struct { +// ResolverConfig contains the externally supplied configuration items that are +// required by a ContractResolver implementation. +type ResolverConfig struct { // ChannelArbitratorConfig contains all the interfaces and closures // required for the resolver to interact with outside sub-systems. ChannelArbitratorConfig @@ -76,10 +75,25 @@ type ResolverKit struct { // should write the state of the resolver to persistent storage, and // return a non-nil error upon success. Checkpoint func(ContractResolver) error +} + +// contractResolverKit is meant to be used as a mix-in struct to be embedded within a +// given ContractResolver implementation. It contains all the common items that +// a resolver requires to carry out its duties. +type contractResolverKit struct { + ResolverConfig quit chan struct{} } +// newContractResolverKit instantiates the mix-in struct. +func newContractResolverKit(cfg ResolverConfig) *contractResolverKit { + return &contractResolverKit{ + ResolverConfig: cfg, + quit: make(chan struct{}), + } +} + var ( // errResolverShuttingDown is returned when the resolver stops // progressing because it received the quit signal. diff --git a/contractcourt/htlc_incoming_contest_resolver.go b/contractcourt/htlc_incoming_contest_resolver.go index a9fdd78a..131c77be 100644 --- a/contractcourt/htlc_incoming_contest_resolver.go +++ b/contractcourt/htlc_incoming_contest_resolver.go @@ -310,13 +310,13 @@ func (h *htlcIncomingContestResolver) Decode(r io.Reader) error { return h.htlcSuccessResolver.Decode(r) } -// AttachResolverKit should be called once a resolved is successfully decoded -// from its stored format. This struct delivers a generic tool kit that +// 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) AttachResolverKit(r ResolverKit) { - h.ResolverKit = r +func (h *htlcIncomingContestResolver) AttachConfig(r ResolverConfig) { + h.htlcSuccessResolver.AttachConfig(r) } // A compile time assertion to ensure htlcIncomingContestResolver meets the diff --git a/contractcourt/htlc_incoming_resolver_test.go b/contractcourt/htlc_incoming_resolver_test.go index 43d01817..1f28f0a5 100644 --- a/contractcourt/htlc_incoming_resolver_test.go +++ b/contractcourt/htlc_incoming_resolver_test.go @@ -199,17 +199,18 @@ func newIncomingResolverTestContext(t *testing.T) *incomingResolverTestContext { }, } + cfg := ResolverConfig{ + ChannelArbitratorConfig: chainCfg, + Checkpoint: func(_ ContractResolver) error { + checkPointChan <- struct{}{} + return nil + }, + } resolver := &htlcIncomingContestResolver{ htlcSuccessResolver: htlcSuccessResolver{ - ResolverKit: ResolverKit{ - ChannelArbitratorConfig: chainCfg, - Checkpoint: func(_ ContractResolver) error { - checkPointChan <- struct{}{} - return nil - }, - }, - htlcResolution: lnwallet.IncomingHtlcResolution{}, - payHash: testResHash, + contractResolverKit: *newContractResolverKit(cfg), + htlcResolution: lnwallet.IncomingHtlcResolution{}, + payHash: testResHash, }, htlcExpiry: testHtlcExpiry, } diff --git a/contractcourt/htlc_outgoing_contest_resolver.go b/contractcourt/htlc_outgoing_contest_resolver.go index ed092cca..3abe7e1b 100644 --- a/contractcourt/htlc_outgoing_contest_resolver.go +++ b/contractcourt/htlc_outgoing_contest_resolver.go @@ -189,13 +189,13 @@ func (h *htlcOutgoingContestResolver) Decode(r io.Reader) error { return h.htlcTimeoutResolver.Decode(r) } -// AttachResolverKit should be called once a resolved is successfully decoded -// from its stored format. This struct delivers a generic tool kit that +// 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) AttachResolverKit(r ResolverKit) { - h.ResolverKit = r +func (h *htlcOutgoingContestResolver) AttachConfig(r ResolverConfig) { + h.htlcTimeoutResolver.AttachConfig(r) } // A compile time assertion to ensure htlcOutgoingContestResolver meets the diff --git a/contractcourt/htlc_outgoing_contest_resolver_test.go b/contractcourt/htlc_outgoing_contest_resolver_test.go index 244b438d..1f7dbed1 100644 --- a/contractcourt/htlc_outgoing_contest_resolver_test.go +++ b/contractcourt/htlc_outgoing_contest_resolver_test.go @@ -122,16 +122,18 @@ func newOutgoingResolverTestContext(t *testing.T) *outgoingResolverTestContext { }, } + cfg := ResolverConfig{ + ChannelArbitratorConfig: chainCfg, + Checkpoint: func(_ ContractResolver) error { + checkPointChan <- struct{}{} + return nil + }, + } + resolver := &htlcOutgoingContestResolver{ htlcTimeoutResolver: htlcTimeoutResolver{ - ResolverKit: ResolverKit{ - ChannelArbitratorConfig: chainCfg, - Checkpoint: func(_ ContractResolver) error { - checkPointChan <- struct{}{} - return nil - }, - }, - htlcResolution: outgoingRes, + contractResolverKit: *newContractResolverKit(cfg), + htlcResolution: outgoingRes, }, } diff --git a/contractcourt/htlc_success_resolver.go b/contractcourt/htlc_success_resolver.go index 6d6d58e2..aa50c533 100644 --- a/contractcourt/htlc_success_resolver.go +++ b/contractcourt/htlc_success_resolver.go @@ -52,7 +52,7 @@ type htlcSuccessResolver struct { // account any fees that may have to be paid if it goes on chain. htlcAmt lnwire.MilliSatoshi - ResolverKit + contractResolverKit } // ResolverKey returns an identifier which should be globally unique for this @@ -318,13 +318,13 @@ func (h *htlcSuccessResolver) Decode(r io.Reader) error { return nil } -// AttachResolverKit should be called once a resolved is successfully decoded -// from its stored format. This struct delivers a generic tool kit that +// 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) AttachResolverKit(r ResolverKit) { - h.ResolverKit = r +func (h *htlcSuccessResolver) AttachConfig(r ResolverConfig) { + h.contractResolverKit = *newContractResolverKit(r) } // A compile time assertion to ensure htlcSuccessResolver meets the diff --git a/contractcourt/htlc_timeout_resolver.go b/contractcourt/htlc_timeout_resolver.go index 2907aea6..1a245bc4 100644 --- a/contractcourt/htlc_timeout_resolver.go +++ b/contractcourt/htlc_timeout_resolver.go @@ -48,7 +48,7 @@ type htlcTimeoutResolver struct { // account any fees that may have to be paid if it goes on chain. htlcAmt lnwire.MilliSatoshi - ResolverKit + contractResolverKit } // ResolverKey returns an identifier which should be globally unique for this @@ -436,13 +436,13 @@ func (h *htlcTimeoutResolver) Decode(r io.Reader) error { return nil } -// AttachResolverKit should be called once a resolved is successfully decoded -// from its stored format. This struct delivers a generic tool kit that +// 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) AttachResolverKit(r ResolverKit) { - h.ResolverKit = r +func (h *htlcTimeoutResolver) AttachConfig(r ResolverConfig) { + h.contractResolverKit = *newContractResolverKit(r) } // A compile time assertion to ensure htlcTimeoutResolver meets the diff --git a/contractcourt/htlc_timeout_resolver_test.go b/contractcourt/htlc_timeout_resolver_test.go index 7dcf025a..981cefc4 100644 --- a/contractcourt/htlc_timeout_resolver_test.go +++ b/contractcourt/htlc_timeout_resolver_test.go @@ -237,15 +237,18 @@ func TestHtlcTimeoutResolver(t *testing.T) { }, } - resolver := &htlcTimeoutResolver{ - ResolverKit: ResolverKit{ - ChannelArbitratorConfig: chainCfg, - Checkpoint: func(_ ContractResolver) error { - checkPointChan <- struct{}{} - return nil - }, + cfg := ResolverConfig{ + ChannelArbitratorConfig: chainCfg, + Checkpoint: func(_ ContractResolver) error { + checkPointChan <- struct{}{} + return nil }, } + resolver := &htlcTimeoutResolver{ + contractResolverKit: *newContractResolverKit( + cfg, + ), + } resolver.htlcResolution.SweepSignDesc = *fakeSignDesc // If the test case needs the remote commitment to be