Skip to content

Commit

Permalink
Merge pull request #8452 from ProofOfKeags/refactor/contractcourt/nam…
Browse files Browse the repository at this point in the history
…ing-consistency

contractcourt: homogenize naming convention
  • Loading branch information
yyforyongyu authored Feb 7, 2024
2 parents 771d1f0 + 0f245bf commit 9651ee6
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ var (
// for the first and one for the second level).
taprootRetributionBucket = []byte("tap-retribution")

// errBrarShuttingDown is an error returned if the breacharbiter has
// errBrarShuttingDown is an error returned if the BreachArbitrator has
// been signalled to exit.
errBrarShuttingDown = errors.New("breacharbiter shutting down")
errBrarShuttingDown = errors.New("BreachArbitrator shutting down")
)

// ContractBreachEvent is an event the BreachArbiter will receive in case a
// ContractBreachEvent is an event the BreachArbitrator will receive in case a
// contract breach is observed on-chain. It contains the necessary information
// to handle the breach, and a ProcessACK closure we will use to ACK the event
// when we have safely stored all the necessary information.
Expand All @@ -71,7 +71,7 @@ type ContractBreachEvent struct {
// store. In case storing the information to the store fails, a non-nil
// error should be used. When this closure returns, it means that the
// contract court has marked the channel pending close in the DB, and
// it is safe for the BreachArbiter to carry on its duty.
// it is safe for the BreachArbitrator to carry on its duty.
ProcessACK func(error)

// BreachRetribution is the information needed to act on this contract
Expand All @@ -94,11 +94,12 @@ const (
)

// RetributionStorer provides an interface for managing a persistent map from
// wire.OutPoint -> retributionInfo. Upon learning of a breach, a BreachArbiter
// should record the retributionInfo for the breached channel, which serves a
// checkpoint in the event that retribution needs to be resumed after failure.
// A RetributionStore provides an interface for managing the persisted set, as
// well as mapping user defined functions over the entire on-disk contents.
// wire.OutPoint -> retributionInfo. Upon learning of a breach, a
// BreachArbitrator should record the retributionInfo for the breached channel,
// which serves a checkpoint in the event that retribution needs to be resumed
// after failure. A RetributionStore provides an interface for managing the
// persisted set, as well as mapping user defined functions over the entire
// on-disk contents.
//
// Calls to RetributionStore may occur concurrently. A concrete instance of
// RetributionStore should use appropriate synchronization primitives, or
Expand All @@ -125,7 +126,8 @@ type RetributionStorer interface {
}

// BreachConfig bundles the required subsystems used by the breach arbiter. An
// instance of BreachConfig is passed to newBreachArbiter during instantiation.
// instance of BreachConfig is passed to NewBreachArbitrator during
// instantiation.
type BreachConfig struct {
// CloseLink allows the breach arbiter to shutdown any channel links for
// which it detects a breach, ensuring now further activity will
Expand Down Expand Up @@ -154,9 +156,9 @@ type BreachConfig struct {
// transaction to the network.
PublishTransaction func(*wire.MsgTx, string) error

// ContractBreaches is a channel where the BreachArbiter will receive
// ContractBreaches is a channel where the BreachArbitrator will receive
// notifications in the event of a contract breach being observed. A
// ContractBreachEvent must be ACKed by the BreachArbiter, such that
// ContractBreachEvent must be ACKed by the BreachArbitrator, such that
// the sending subsystem knows that the event is properly handed off.
ContractBreaches <-chan *ContractBreachEvent

Expand All @@ -171,15 +173,15 @@ type BreachConfig struct {
Store RetributionStorer
}

// BreachArbiter is a special subsystem which is responsible for watching and
// BreachArbitrator is a special subsystem which is responsible for watching and
// acting on the detection of any attempted uncooperative channel breaches by
// channel counterparties. This file essentially acts as deterrence code for
// those attempting to launch attacks against the daemon. In practice it's
// expected that the logic in this file never gets executed, but it is
// important to have it in place just in case we encounter cheating channel
// counterparties.
// TODO(roasbeef): closures in config for subsystem pointers to decouple?
type BreachArbiter struct {
type BreachArbitrator struct {
started sync.Once
stopped sync.Once

Expand All @@ -192,19 +194,19 @@ type BreachArbiter struct {
sync.Mutex
}

// NewBreachArbiter creates a new instance of a BreachArbiter initialized with
// its dependent objects.
func NewBreachArbiter(cfg *BreachConfig) *BreachArbiter {
return &BreachArbiter{
// NewBreachArbitrator creates a new instance of a BreachArbitrator initialized
// with its dependent objects.
func NewBreachArbitrator(cfg *BreachConfig) *BreachArbitrator {
return &BreachArbitrator{
cfg: cfg,
subscriptions: make(map[wire.OutPoint]chan struct{}),
quit: make(chan struct{}),
}
}

// Start is an idempotent method that officially starts the BreachArbiter along
// with all other goroutines it needs to perform its functions.
func (b *BreachArbiter) Start() error {
// Start is an idempotent method that officially starts the BreachArbitrator
// along with all other goroutines it needs to perform its functions.
func (b *BreachArbitrator) Start() error {
var err error
b.started.Do(func() {
brarLog.Info("Breach arbiter starting")
Expand All @@ -213,7 +215,7 @@ func (b *BreachArbiter) Start() error {
return err
}

func (b *BreachArbiter) start() error {
func (b *BreachArbitrator) start() error {
// Load all retributions currently persisted in the retribution store.
var breachRetInfos map[wire.OutPoint]retributionInfo
if err := b.cfg.Store.ForAll(func(ret *retributionInfo) error {
Expand Down Expand Up @@ -305,10 +307,10 @@ func (b *BreachArbiter) start() error {
return nil
}

// Stop is an idempotent method that signals the BreachArbiter to execute a
// Stop is an idempotent method that signals the BreachArbitrator to execute a
// graceful shutdown. This function will block until all goroutines spawned by
// the BreachArbiter have gracefully exited.
func (b *BreachArbiter) Stop() error {
// the BreachArbitrator have gracefully exited.
func (b *BreachArbitrator) Stop() error {
b.stopped.Do(func() {
brarLog.Infof("Breach arbiter shutting down...")
defer brarLog.Debug("Breach arbiter shutdown complete")
Expand All @@ -321,13 +323,13 @@ func (b *BreachArbiter) Stop() error {

// IsBreached queries the breach arbiter's retribution store to see if it is
// aware of any channel breaches for a particular channel point.
func (b *BreachArbiter) IsBreached(chanPoint *wire.OutPoint) (bool, error) {
func (b *BreachArbitrator) IsBreached(chanPoint *wire.OutPoint) (bool, error) {
return b.cfg.Store.IsBreached(chanPoint)
}

// SubscribeBreachComplete is used by outside subsystems to be notified of a
// successful breach resolution.
func (b *BreachArbiter) SubscribeBreachComplete(chanPoint *wire.OutPoint,
func (b *BreachArbitrator) SubscribeBreachComplete(chanPoint *wire.OutPoint,
c chan struct{}) (bool, error) {

breached, err := b.cfg.Store.IsBreached(chanPoint)
Expand All @@ -353,9 +355,9 @@ func (b *BreachArbiter) SubscribeBreachComplete(chanPoint *wire.OutPoint,
return false, nil
}

// notifyBreachComplete is used by the BreachArbiter to notify outside
// notifyBreachComplete is used by the BreachArbitrator to notify outside
// subsystems that the breach resolution process is complete.
func (b *BreachArbiter) notifyBreachComplete(chanPoint *wire.OutPoint) {
func (b *BreachArbitrator) notifyBreachComplete(chanPoint *wire.OutPoint) {
b.Lock()
defer b.Unlock()
if c, ok := b.subscriptions[*chanPoint]; ok {
Expand All @@ -366,15 +368,15 @@ func (b *BreachArbiter) notifyBreachComplete(chanPoint *wire.OutPoint) {
delete(b.subscriptions, *chanPoint)
}

// contractObserver is the primary goroutine for the BreachArbiter. This
// contractObserver is the primary goroutine for the BreachArbitrator. This
// goroutine is responsible for handling breach events coming from the
// contractcourt on the ContractBreaches channel. If a channel breach is
// detected, then the contractObserver will execute the retribution logic
// required to sweep ALL outputs from a contested channel into the daemon's
// wallet.
//
// NOTE: This MUST be run as a goroutine.
func (b *BreachArbiter) contractObserver() {
func (b *BreachArbitrator) contractObserver() {
defer b.wg.Done()

brarLog.Infof("Starting contract observer, watching for breaches.")
Expand Down Expand Up @@ -406,7 +408,7 @@ type spend struct {
// returns the spend details for those outputs. The spendNtfns map is a cache
// used to store registered spend subscriptions, in case we must call this
// method multiple times.
func (b *BreachArbiter) waitForSpendEvent(breachInfo *retributionInfo,
func (b *BreachArbitrator) waitForSpendEvent(breachInfo *retributionInfo,
spendNtfns map[wire.OutPoint]*chainntnfs.SpendEvent) ([]spend, error) {

inputs := breachInfo.breachedOutputs
Expand Down Expand Up @@ -684,8 +686,10 @@ func updateBreachInfo(breachInfo *retributionInfo, spends []spend) (
// the lingering funds within the channel into the daemon's wallet.
//
// NOTE: This MUST be run as a goroutine.
func (b *BreachArbiter) exactRetribution(confChan *chainntnfs.ConfirmationEvent,
breachInfo *retributionInfo) {
//
//nolint:funlen
func (b *BreachArbitrator) exactRetribution(
confChan *chainntnfs.ConfirmationEvent, breachInfo *retributionInfo) {

defer b.wg.Done()

Expand Down Expand Up @@ -916,7 +920,7 @@ Loop:

// cleanupBreach marks the given channel point as fully resolved and removes the
// retribution for that the channel from the retribution store.
func (b *BreachArbiter) cleanupBreach(chanPoint *wire.OutPoint) error {
func (b *BreachArbitrator) cleanupBreach(chanPoint *wire.OutPoint) error {
// With the channel closed, mark it in the database as such.
err := b.cfg.DB.MarkChanFullyClosed(chanPoint)
if err != nil {
Expand All @@ -943,15 +947,17 @@ func (b *BreachArbiter) cleanupBreach(chanPoint *wire.OutPoint) error {
}

// handleBreachHandoff handles a new breach event, by writing it to disk, then
// notifies the BreachArbiter contract observer goroutine that a channel's
// notifies the BreachArbitrator contract observer goroutine that a channel's
// contract has been breached by the prior counterparty. Once notified the
// BreachArbiter will attempt to sweep ALL funds within the channel using the
// BreachArbitrator will attempt to sweep ALL funds within the channel using the
// information provided within the BreachRetribution generated due to the
// breach of channel contract. The funds will be swept only after the breaching
// transaction receives a necessary number of confirmations.
//
// NOTE: This MUST be run as a goroutine.
func (b *BreachArbiter) handleBreachHandoff(breachEvent *ContractBreachEvent) {
func (b *BreachArbitrator) handleBreachHandoff(
breachEvent *ContractBreachEvent) {

defer b.wg.Done()

chanPoint := breachEvent.ChanPoint
Expand Down Expand Up @@ -1367,7 +1373,7 @@ type justiceTxVariants struct {
// the funds within the channel which we are now entitled to due to a breach of
// the channel's contract by the counterparty. This function returns a *fully*
// signed transaction with the witness for each input fully in place.
func (b *BreachArbiter) createJusticeTx(
func (b *BreachArbitrator) createJusticeTx(
breachedOutputs []breachedOutput) (*justiceTxVariants, error) {

var (
Expand Down Expand Up @@ -1442,7 +1448,7 @@ func (b *BreachArbiter) createJusticeTx(
}

// createSweepTx creates a tx that sweeps the passed inputs back to our wallet.
func (b *BreachArbiter) createSweepTx(inputs ...input.Input) (*wire.MsgTx,
func (b *BreachArbitrator) createSweepTx(inputs ...input.Input) (*wire.MsgTx,
error) {

if len(inputs) == 0 {
Expand Down Expand Up @@ -1498,7 +1504,7 @@ func (b *BreachArbiter) createSweepTx(inputs ...input.Input) (*wire.MsgTx,

// sweepSpendableOutputsTxn creates a signed transaction from a sequence of
// spendable outputs by sweeping the funds into a single p2wkh output.
func (b *BreachArbiter) sweepSpendableOutputsTxn(txWeight int64,
func (b *BreachArbitrator) sweepSpendableOutputsTxn(txWeight int64,
inputs ...input.Input) (*wire.MsgTx, error) {

// First, we obtain a new public key script from the wallet which we'll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ restartCheck:
}
}

func initBreachedState(t *testing.T) (*BreachArbiter,
func initBreachedState(t *testing.T) (*BreachArbitrator,
*lnwallet.LightningChannel, *lnwallet.LightningChannel,
*lnwallet.LocalForceCloseSummary, chan *ContractBreachEvent) {

Expand Down Expand Up @@ -1338,7 +1338,7 @@ func getSpendTransactions(signer input.Signer, chanPoint *wire.OutPoint,
},
}

// In order for the breacharbiter to detect that it is being spent
// In order for the BreachArbitrator to detect that it is being spent
// using the revocation key, it will inspect the witness. Therefore
// sign and add the witness to the HTLC sweep.
retInfo := newRetributionInfo(chanPoint, retribution)
Expand Down Expand Up @@ -1684,7 +1684,7 @@ func testBreachSpends(t *testing.T, test breachTest) {
}

// We also keep a map of those remaining outputs we expect the
// breacharbiter to try and sweep.
// BreachArbitrator to try and sweep.
inputsToSweep := map[wire.OutPoint]struct{}{
htlcOutpoint: {},
localOutpoint: {},
Expand Down Expand Up @@ -1892,7 +1892,7 @@ func TestBreachDelayedJusticeConfirmation(t *testing.T) {
}

// Now mine another block without the justice tx confirming. This
// should lead to the breacharbiter publishing the split justice tx
// should lead to the BreachArbitrator publishing the split justice tx
// variants.
notifier.EpochChan <- &chainntnfs.BlockEpoch{
Height: blockHeight + 4,
Expand Down Expand Up @@ -1988,7 +1988,7 @@ func findInputIndex(t *testing.T, op wire.OutPoint, tx *wire.MsgTx) int {

// assertArbiterBreach checks that the breach arbiter has persisted the breach
// information for a particular channel.
func assertArbiterBreach(t *testing.T, brar *BreachArbiter,
func assertArbiterBreach(t *testing.T, brar *BreachArbitrator,
chanPoint *wire.OutPoint) {

t.Helper()
Expand All @@ -2008,7 +2008,7 @@ func assertArbiterBreach(t *testing.T, brar *BreachArbiter,

// assertNoArbiterBreach checks that the breach arbiter has not persisted the
// breach information for a particular channel.
func assertNoArbiterBreach(t *testing.T, brar *BreachArbiter,
func assertNoArbiterBreach(t *testing.T, brar *BreachArbitrator,
chanPoint *wire.OutPoint) {

t.Helper()
Expand All @@ -2027,7 +2027,7 @@ func assertNoArbiterBreach(t *testing.T, brar *BreachArbiter,

// assertBrarCleanup blocks until the given channel point has been removed the
// retribution store and the channel is fully closed in the database.
func assertBrarCleanup(t *testing.T, brar *BreachArbiter,
func assertBrarCleanup(t *testing.T, brar *BreachArbitrator,
chanPoint *wire.OutPoint, db *channeldb.ChannelStateDB) {

t.Helper()
Expand Down Expand Up @@ -2108,7 +2108,7 @@ func assertNotPendingClosed(t *testing.T, c *lnwallet.LightningChannel) {
// createTestArbiter instantiates a breach arbiter with a failing retribution
// store, so that controlled failures can be tested.
func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,
db *channeldb.DB) (*BreachArbiter, error) {
db *channeldb.DB) (*BreachArbitrator, error) {

// Create a failing retribution store, that wraps a normal one.
store := newFailingRetributionStore(func() RetributionStorer {
Expand All @@ -2120,7 +2120,7 @@ func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,

// Assemble our test arbiter.
notifier := mock.MakeMockSpendNotifier()
ba := NewBreachArbiter(&BreachConfig{
ba := NewBreachArbitrator(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ ChannelCloseType) {},
DB: db.ChannelStateDB(),
Estimator: chainfee.NewStaticEstimator(12500, 0),
Expand Down
9 changes: 5 additions & 4 deletions contractcourt/breach_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

// breachResolver is a resolver that will handle breached closes. In the
// future, this will likely take over the duties the current breacharbiter has.
// future, this will likely take over the duties the current BreachArbitrator
// has.
type breachResolver struct {
// resolved reflects if the contract has been fully resolved or not.
resolved bool

// subscribed denotes whether or not the breach resolver has subscribed
// to the breacharbiter for breach resolution.
// to the BreachArbitrator for breach resolution.
subscribed bool

// replyChan is closed when the breach arbiter has completed serving
Expand Down Expand Up @@ -42,8 +43,8 @@ func (b *breachResolver) ResolverKey() []byte {
return key[:]
}

// Resolve queries the breacharbiter to see if the justice transaction has been
// broadcast.
// Resolve queries the BreachArbitrator to see if the justice transaction has
// been broadcast.
func (b *breachResolver) Resolve() (ContractResolver, error) {
if !b.subscribed {
complete, err := b.SubscribeBreachComplete(
Expand Down
6 changes: 3 additions & 3 deletions contractcourt/chain_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ type ChainArbitratorConfig struct {
MarkLinkInactive func(wire.OutPoint) error

// ContractBreach is a function closure that the ChainArbitrator will
// use to notify the breachArbiter about a contract breach. It should
// only return a non-nil error when the breachArbiter has preserved
// use to notify the BreachArbitrator about a contract breach. It should
// only return a non-nil error when the BreachArbitrator has preserved
// the necessary breach info for this channel point. Once the breach
// resolution is persisted in the channel arbitrator, it will be safe
// resolution is persisted in the ChannelArbitrator, it will be safe
// to mark the channel closed.
ContractBreach func(wire.OutPoint, *lnwallet.BreachRetribution) error

Expand Down
Loading

0 comments on commit 9651ee6

Please sign in to comment.