Skip to content

Commit

Permalink
multi: move breach arbiter and utxo nursery into contractcourt package
Browse files Browse the repository at this point in the history
In this commit, we take an initial step towards converting the existing
breach arbiter and utxo nursery logic into contract resolvers by moving
the files as is, into the `contractcourt` pacakge.

This commit is primarily move only, though we had to massage some
interfaces and config names along the way to make things compile and the
tests run properly.
  • Loading branch information
Roasbeef committed Sep 28, 2021
1 parent be2566c commit 7bde166
Show file tree
Hide file tree
Showing 15 changed files with 388 additions and 348 deletions.
236 changes: 125 additions & 111 deletions breacharbiter.go → contractcourt/breacharbiter.go

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions breacharbiter_test.go → contractcourt/breacharbiter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//go:build !rpctest
// +build !rpctest

package lnd
package contractcourt

import (
"bytes"
Expand All @@ -26,7 +27,6 @@ import (
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntest/channels"
Expand Down Expand Up @@ -338,7 +338,7 @@ func init() {
// modifications to the entries are made between calls or through side effects,
// and (2) that the database is actually being persisted between actions.
type FailingRetributionStore interface {
RetributionStore
RetributionStorer

Restart()
}
Expand All @@ -350,18 +350,18 @@ type FailingRetributionStore interface {
type failingRetributionStore struct {
mu sync.Mutex

rs RetributionStore
rs RetributionStorer

nextAddErr error

restart func() RetributionStore
restart func() RetributionStorer
}

// newFailingRetributionStore creates a new failing retribution store. The given
// restart closure should ensure that it is reloading its contents from the
// persistent source.
func newFailingRetributionStore(
restart func() RetributionStore) *failingRetributionStore {
restart func() RetributionStorer) *failingRetributionStore {

return &failingRetributionStore{
mu: sync.Mutex{},
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestMockRetributionStore(t *testing.T) {
func(tt *testing.T) {
mrs := newMockRetributionStore()
frs := newFailingRetributionStore(
func() RetributionStore { return mrs },
func() RetributionStorer { return mrs },
)
test.test(frs, tt)
},
Expand Down Expand Up @@ -677,7 +677,7 @@ func TestChannelDBRetributionStore(t *testing.T) {
defer db.Close()
defer cleanUp()

restartDb := func() RetributionStore {
restartDb := func() RetributionStorer {
// Close and reopen channeldb
if err = db.Close(); err != nil {
t.Fatalf("unable to close "+
Expand All @@ -691,7 +691,7 @@ func TestChannelDBRetributionStore(t *testing.T) {
"channeldb: %v", err)
}

return newRetributionStore(db)
return NewRetributionStore(db)
}

frs := newFailingRetributionStore(restartDb)
Expand All @@ -703,7 +703,7 @@ func TestChannelDBRetributionStore(t *testing.T) {

// countRetributions uses a retribution store's ForAll to count the number of
// elements emitted from the store.
func countRetributions(t *testing.T, rs RetributionStore) int {
func countRetributions(t *testing.T, rs RetributionStorer) int {
count := 0
err := rs.ForAll(func(_ *retributionInfo) error {
count++
Expand Down Expand Up @@ -971,7 +971,7 @@ restartCheck:
}
}

func initBreachedState(t *testing.T) (*breachArbiter,
func initBreachedState(t *testing.T) (*BreachArbiter,
*lnwallet.LightningChannel, *lnwallet.LightningChannel,
*lnwallet.LocalForceCloseSummary, chan *ContractBreachEvent,
func(), func()) {
Expand Down Expand Up @@ -2035,7 +2035,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 *BreachArbiter,
chanPoint *wire.OutPoint) {

t.Helper()
Expand All @@ -2055,7 +2055,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 *BreachArbiter,
chanPoint *wire.OutPoint) {

t.Helper()
Expand All @@ -2074,7 +2074,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 *BreachArbiter,
chanPoint *wire.OutPoint, db *channeldb.DB) {

t.Helper()
Expand Down Expand Up @@ -2159,11 +2159,11 @@ 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, func(), error) {
db *channeldb.DB) (*BreachArbiter, func(), error) {

// Create a failing retribution store, that wraps a normal one.
store := newFailingRetributionStore(func() RetributionStore {
return newRetributionStore(db)
store := newFailingRetributionStore(func() RetributionStorer {
return NewRetributionStore(db)
})

aliceKeyPriv, _ := btcec.PrivKeyFromBytes(btcec.S256(),
Expand All @@ -2172,8 +2172,8 @@ func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,

// Assemble our test arbiter.
notifier := mock.MakeMockSpendNotifier()
ba := newBreachArbiter(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ htlcswitch.ChannelCloseType) {},
ba := NewBreachArbiter(&BreachConfig{
CloseLink: func(_ *wire.OutPoint, _ ChannelCloseType) {},
DB: db,
Estimator: chainfee.NewStaticEstimator(12500, 0),
GenSweepScript: func() ([]byte, error) { return nil, nil },
Expand Down
2 changes: 0 additions & 2 deletions contractcourt/channel_arbitrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
)

const (
defaultTimeout = time.Second * 5

// stateTimeout is the timeout we allow when waiting for state
// transitions.
stateTimeout = time.Second * 15
Expand Down
32 changes: 28 additions & 4 deletions contractcourt/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import (
"github.com/lightningnetwork/lnd/build"
)

// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var log btclog.Logger
var (
// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
log btclog.Logger

// brarLog is the logger used by the breach arb.
brarLog btclog.Logger

// utxnLog is the logger used by the utxo nursary.
utxnLog btclog.Logger
)

// The default amount of logging is none.
func init() {
UseLogger(build.NewSubLogger("CNCT", nil))
UseBreachLogger(build.NewSubLogger("BRAR", nil))
UseNurseryLogger(build.NewSubLogger("UTXN", nil))
}

// DisableLog disables all library log output. Logging output is disabled
Expand All @@ -28,6 +38,20 @@ func UseLogger(logger btclog.Logger) {
log = logger
}

// UseBreachLogger uses a specified Logger to output package logging info.
// This should be used in preference to SetLogWriter if the caller is also
// using btclog.
func UseBreachLogger(logger btclog.Logger) {
brarLog = logger
}

// UseNurseryLogger uses a specified Logger to output package logging info.
// This should be used in preference to SetLogWriter if the caller is also
// using btclog.
func UseNurseryLogger(logger btclog.Logger) {
utxnLog = logger
}

// logClosure is used to provide a closure over expensive logging operations so
// don't have to be performed when the logging level doesn't warrant it.
type logClosure func() string
Expand Down
Loading

0 comments on commit 7bde166

Please sign in to comment.