sweep: create wallet interface
We need access to additional wallet functionality. This commit creates an interface to prevent passing in multiple function pointers.
This commit is contained in:
parent
b325aae4f2
commit
34c9193bfc
@ -795,7 +795,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
|
|||||||
FeeEstimator: cc.feeEstimator,
|
FeeEstimator: cc.feeEstimator,
|
||||||
GenSweepScript: newSweepPkScriptGen(cc.wallet),
|
GenSweepScript: newSweepPkScriptGen(cc.wallet),
|
||||||
Signer: cc.wallet.Cfg.Signer,
|
Signer: cc.wallet.Cfg.Signer,
|
||||||
PublishTransaction: cc.wallet.PublishTransaction,
|
Wallet: cc.wallet,
|
||||||
NewBatchTimer: func() <-chan time.Time {
|
NewBatchTimer: func() <-chan time.Time {
|
||||||
return time.NewTimer(sweep.DefaultBatchWindowDuration).C
|
return time.NewTimer(sweep.DefaultBatchWindowDuration).C
|
||||||
},
|
},
|
||||||
|
@ -2,6 +2,8 @@ package sweep
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
@ -11,6 +13,8 @@ import (
|
|||||||
// mockBackend simulates a chain backend for realistic behaviour in unit tests
|
// mockBackend simulates a chain backend for realistic behaviour in unit tests
|
||||||
// around double spends.
|
// around double spends.
|
||||||
type mockBackend struct {
|
type mockBackend struct {
|
||||||
|
t *testing.T
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
notifier *MockNotifier
|
notifier *MockNotifier
|
||||||
@ -19,14 +23,18 @@ type mockBackend struct {
|
|||||||
|
|
||||||
unconfirmedTxes map[chainhash.Hash]*wire.MsgTx
|
unconfirmedTxes map[chainhash.Hash]*wire.MsgTx
|
||||||
unconfirmedSpendInputs map[wire.OutPoint]struct{}
|
unconfirmedSpendInputs map[wire.OutPoint]struct{}
|
||||||
|
|
||||||
|
publishChan chan wire.MsgTx
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMockBackend(notifier *MockNotifier) *mockBackend {
|
func newMockBackend(t *testing.T, notifier *MockNotifier) *mockBackend {
|
||||||
return &mockBackend{
|
return &mockBackend{
|
||||||
|
t: t,
|
||||||
notifier: notifier,
|
notifier: notifier,
|
||||||
unconfirmedTxes: make(map[chainhash.Hash]*wire.MsgTx),
|
unconfirmedTxes: make(map[chainhash.Hash]*wire.MsgTx),
|
||||||
confirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
confirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
||||||
unconfirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
unconfirmedSpendInputs: make(map[wire.OutPoint]struct{}),
|
||||||
|
publishChan: make(chan wire.MsgTx, 2),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +73,17 @@ func (b *mockBackend) publishTransaction(tx *wire.MsgTx) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *mockBackend) PublishTransaction(tx *wire.MsgTx) error {
|
||||||
|
log.Tracef("Publishing tx %v", tx.TxHash())
|
||||||
|
err := b.publishTransaction(tx)
|
||||||
|
select {
|
||||||
|
case b.publishChan <- *tx:
|
||||||
|
case <-time.After(defaultTestTimeout):
|
||||||
|
b.t.Fatalf("unexpected tx published")
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (b *mockBackend) deleteUnconfirmed(txHash chainhash.Hash) {
|
func (b *mockBackend) deleteUnconfirmed(txHash chainhash.Hash) {
|
||||||
b.lock.Lock()
|
b.lock.Lock()
|
||||||
defer b.lock.Unlock()
|
defer b.lock.Unlock()
|
||||||
|
12
sweep/interface.go
Normal file
12
sweep/interface.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package sweep
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Wallet contains all wallet related functionality required by sweeper.
|
||||||
|
type Wallet interface {
|
||||||
|
// PublishTransaction performs cursory validation (dust checks, etc) and
|
||||||
|
// broadcasts the passed transaction to the Bitcoin network.
|
||||||
|
PublishTransaction(tx *wire.MsgTx) error
|
||||||
|
}
|
@ -210,9 +210,8 @@ type UtxoSweeperConfig struct {
|
|||||||
// transaction.
|
// transaction.
|
||||||
FeeEstimator chainfee.Estimator
|
FeeEstimator chainfee.Estimator
|
||||||
|
|
||||||
// PublishTransaction facilitates the process of broadcasting a signed
|
// Wallet contains the wallet functions that sweeper requires.
|
||||||
// transaction to the appropriate network.
|
Wallet Wallet
|
||||||
PublishTransaction func(*wire.MsgTx) error
|
|
||||||
|
|
||||||
// NewBatchTimer creates a channel that will be sent on when a certain
|
// NewBatchTimer creates a channel that will be sent on when a certain
|
||||||
// time window has passed. During this time window, new inputs can still
|
// time window has passed. During this time window, new inputs can still
|
||||||
@ -321,7 +320,7 @@ func (s *UtxoSweeper) Start() error {
|
|||||||
|
|
||||||
// Error can be ignored. Because we are starting up, there are
|
// Error can be ignored. Because we are starting up, there are
|
||||||
// no pending inputs to update based on the publish result.
|
// no pending inputs to update based on the publish result.
|
||||||
err := s.cfg.PublishTransaction(lastTx)
|
err := s.cfg.Wallet.PublishTransaction(lastTx)
|
||||||
if err != nil && err != lnwallet.ErrDoubleSpend {
|
if err != nil && err != lnwallet.ErrDoubleSpend {
|
||||||
log.Errorf("last tx publish: %v", err)
|
log.Errorf("last tx publish: %v", err)
|
||||||
}
|
}
|
||||||
@ -886,7 +885,7 @@ func (s *UtxoSweeper) sweep(inputs inputSet, feeRate chainfee.SatPerKWeight,
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
err = s.cfg.PublishTransaction(tx)
|
err = s.cfg.Wallet.PublishTransaction(tx)
|
||||||
|
|
||||||
// In case of an unexpected error, don't try to recover.
|
// In case of an unexpected error, don't try to recover.
|
||||||
if err != nil && err != lnwallet.ErrDoubleSpend {
|
if err != nil && err != lnwallet.ErrDoubleSpend {
|
||||||
|
@ -98,14 +98,13 @@ func createSweeperTestContext(t *testing.T) *sweeperTestContext {
|
|||||||
|
|
||||||
store := NewMockSweeperStore()
|
store := NewMockSweeperStore()
|
||||||
|
|
||||||
backend := newMockBackend(notifier)
|
backend := newMockBackend(t, notifier)
|
||||||
|
|
||||||
estimator := newMockFeeEstimator(10000, chainfee.FeePerKwFloor)
|
estimator := newMockFeeEstimator(10000, chainfee.FeePerKwFloor)
|
||||||
|
|
||||||
publishChan := make(chan wire.MsgTx, 2)
|
|
||||||
ctx := &sweeperTestContext{
|
ctx := &sweeperTestContext{
|
||||||
notifier: notifier,
|
notifier: notifier,
|
||||||
publishChan: publishChan,
|
publishChan: backend.publishChan,
|
||||||
t: t,
|
t: t,
|
||||||
estimator: estimator,
|
estimator: estimator,
|
||||||
backend: backend,
|
backend: backend,
|
||||||
@ -116,16 +115,7 @@ func createSweeperTestContext(t *testing.T) *sweeperTestContext {
|
|||||||
var outputScriptCount byte
|
var outputScriptCount byte
|
||||||
ctx.sweeper = New(&UtxoSweeperConfig{
|
ctx.sweeper = New(&UtxoSweeperConfig{
|
||||||
Notifier: notifier,
|
Notifier: notifier,
|
||||||
PublishTransaction: func(tx *wire.MsgTx) error {
|
Wallet: backend,
|
||||||
log.Tracef("Publishing tx %v", tx.TxHash())
|
|
||||||
err := backend.publishTransaction(tx)
|
|
||||||
select {
|
|
||||||
case publishChan <- *tx:
|
|
||||||
case <-time.After(defaultTestTimeout):
|
|
||||||
t.Fatalf("unexpected tx published")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
NewBatchTimer: func() <-chan time.Time {
|
NewBatchTimer: func() <-chan time.Time {
|
||||||
c := make(chan time.Time, 1)
|
c := make(chan time.Time, 1)
|
||||||
ctx.timeoutChan <- c
|
ctx.timeoutChan <- c
|
||||||
|
Loading…
Reference in New Issue
Block a user