contractcourt: fix concurrent access to resolved

This commit makes `resolved` an atomic bool to avoid data race. This
field is now defined in `contractResolverKit` to avoid code duplication.
This commit is contained in:
yyforyongyu committed 2024-12-20 17:54:06 +08:00
1 parent 47722292c5
commit 4f5ccb8650
11 files changed
+111 -129

No files matched your search

+17
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"sync/atomic"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btclog/v2"
@@ -119,6 +120,9 @@ type contractResolverKit struct {
// launched specifies whether the resolver has been launched. Calling
// `Launch` will be a no-op if this is true.
launched bool
// resolved reflects if the contract has been fully resolved or not.
resolved atomic.Bool
}
// newContractResolverKit instantiates the mix-in struct.
@@ -137,6 +141,19 @@ func (r *contractResolverKit) initLogger(prefix string) {
r.log = log.WithPrefix(logPrefix)
}
// IsResolved returns true if the stored state in the resolve is fully
// resolved. In this case the target output can be forgotten.
//
// NOTE: Part of the ContractResolver interface.
func (r *contractResolverKit) IsResolved() bool {
return r.resolved.Load()
}
// markResolved marks the resolver as resolved.
func (r *contractResolverKit) markResolved() {
r.resolved.Store(true)
}
var (
// errResolverShuttingDown is returned when the resolver stops
// progressing because it received the quit signal.