contractcourt: fix concurrent access to launched

This commit is contained in:
yyforyongyu 2024-07-11 16:19:01 +08:00
parent 4f5ccb8650
commit d2e81a19fd
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
8 changed files with 27 additions and 14 deletions

View File

@ -178,13 +178,13 @@ var _ ContractResolver = (*anchorResolver)(nil)
// Launch offers the anchor output to the sweeper.
func (c *anchorResolver) Launch() error {
if c.launched {
if c.isLaunched() {
c.log.Tracef("already launched")
return nil
}
c.log.Debugf("launching resolver...")
c.launched = true
c.markLaunched()
// If we're already resolved, then we can exit early.
if c.IsResolved() {

View File

@ -130,13 +130,13 @@ var _ ContractResolver = (*breachResolver)(nil)
//
// TODO(yy): implement it once the outputs are offered to the sweeper.
func (b *breachResolver) Launch() error {
if b.launched {
if b.isLaunched() {
b.log.Tracef("already launched")
return nil
}
b.log.Debugf("launching resolver...")
b.launched = true
b.markLaunched()
return nil
}

View File

@ -368,13 +368,13 @@ var _ reportingContractResolver = (*commitSweepResolver)(nil)
// Launch constructs a commit input and offers it to the sweeper.
func (c *commitSweepResolver) Launch() error {
if c.launched {
if c.isLaunched() {
c.log.Tracef("already launched")
return nil
}
c.log.Debugf("launching resolver...")
c.launched = true
c.markLaunched()
// If we're already resolved, then we can exit early.
if c.IsResolved() {

View File

@ -118,8 +118,11 @@ type contractResolverKit struct {
sweepResultChan chan sweep.Result
// launched specifies whether the resolver has been launched. Calling
// `Launch` will be a no-op if this is true.
launched bool
// `Launch` will be a no-op if this is true. This value is not saved to
// db, as it's fine to relaunch a resolver after a restart. It's only
// used to avoid resending requests to the sweeper when a new blockbeat
// is received.
launched atomic.Bool
// resolved reflects if the contract has been fully resolved or not.
resolved atomic.Bool
@ -154,6 +157,16 @@ func (r *contractResolverKit) markResolved() {
r.resolved.Store(true)
}
// isLaunched returns true if the resolver has been launched.
func (r *contractResolverKit) isLaunched() bool {
return r.launched.Load()
}
// markLaunched marks the resolver as launched.
func (r *contractResolverKit) markLaunched() {
r.launched.Store(true)
}
var (
// errResolverShuttingDown is returned when the resolver stops
// progressing because it received the quit signal.

View File

@ -83,7 +83,7 @@ func (h *htlcIncomingContestResolver) processFinalHtlcFail() error {
func (h *htlcIncomingContestResolver) Launch() error {
// NOTE: we don't mark this resolver as launched as the inner resolver
// will set it when it's launched.
if h.launched {
if h.isLaunched() {
h.log.Tracef("already launched")
return nil
}

View File

@ -40,7 +40,7 @@ func newOutgoingContestResolver(res lnwallet.OutgoingHtlcResolution,
func (h *htlcOutgoingContestResolver) Launch() error {
// NOTE: we don't mark this resolver as launched as the inner resolver
// will set it when it's launched.
if h.launched {
if h.isLaunched() {
h.log.Tracef("already launched")
return nil
}

View File

@ -730,13 +730,13 @@ func (h *htlcSuccessResolver) resolveSuccessTxOutput(op wire.OutPoint) error {
// Launch creates an input based on the details of the incoming htlc resolution
// and offers it to the sweeper.
func (h *htlcSuccessResolver) Launch() error {
if h.launched {
if h.isLaunched() {
h.log.Tracef("already launched")
return nil
}
h.log.Debugf("launching resolver...")
h.launched = true
h.markLaunched()
switch {
// If we're already resolved, then we can exit early.

View File

@ -1270,13 +1270,13 @@ func (h *htlcTimeoutResolver) resolveTimeoutTxOutput(op wire.OutPoint) error {
// Launch creates an input based on the details of the outgoing htlc resolution
// and offers it to the sweeper.
func (h *htlcTimeoutResolver) Launch() error {
if h.launched {
if h.isLaunched() {
h.log.Tracef("already launched")
return nil
}
h.log.Debugf("launching resolver...")
h.launched = true
h.markLaunched()
switch {
// If we're already resolved, then we can exit early.