fundingmgr+server: don't use global cfg

This commit is contained in:
Oliver Gugger 2020-05-14 13:53:33 +02:00
parent d44f205e3f
commit 7ce70321ac
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
3 changed files with 20 additions and 9 deletions

View File

@ -369,6 +369,14 @@ type fundingConfig struct {
// NotifyPendingOpenChannelEvent informs the ChannelNotifier when channels
// enter a pending state.
NotifyPendingOpenChannelEvent func(wire.OutPoint, *channeldb.OpenChannel)
// EnableUpfrontShutdown specifies whether the upfront shutdown script
// is enabled.
EnableUpfrontShutdown bool
// RegisteredChains keeps track of all chains that have been registered
// with the daemon.
RegisteredChains *chainRegistry
}
// fundingManager acts as an orchestrator/bridge between the wallet's
@ -1322,7 +1330,7 @@ func (f *fundingManager) handleFundingOpen(fmsg *fundingOpenMsg) {
// A nil address is set in place of user input, because this channel open
// was not initiated by the user.
shutdown, err := getUpfrontShutdownScript(
fmsg.peer, nil,
f.cfg.EnableUpfrontShutdown, fmsg.peer, nil,
func() (lnwire.DeliveryAddress, error) {
addr, err := f.cfg.Wallet.NewAddress(lnwallet.WitnessPubKey, false)
if err != nil {
@ -2981,7 +2989,8 @@ func (f *fundingManager) initFundingWorkflow(peer lnpeer.Peer, req *openChanReq)
// our peer does support the feature, we will return the user provided script
// if non-zero, or a freshly generated script if our node is configured to set
// upfront shutdown scripts automatically.
func getUpfrontShutdownScript(peer lnpeer.Peer, script lnwire.DeliveryAddress,
func getUpfrontShutdownScript(enableUpfrontShutdown bool, peer lnpeer.Peer,
script lnwire.DeliveryAddress,
getScript func() (lnwire.DeliveryAddress, error)) (lnwire.DeliveryAddress,
error) {
@ -3010,7 +3019,7 @@ func getUpfrontShutdownScript(peer lnpeer.Peer, script lnwire.DeliveryAddress,
// If we do not have setting of upfront shutdown script enabled, return
// an empty script.
if !cfg.EnableUpfrontShutdown {
if !enableUpfrontShutdown {
return nil, nil
}
@ -3030,7 +3039,7 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
// We'll determine our dust limit depending on which chain is active.
var ourDustLimit btcutil.Amount
switch cfg.registeredChains.PrimaryChain() {
switch f.cfg.RegisteredChains.PrimaryChain() {
case bitcoinChain:
ourDustLimit = lnwallet.DefaultDustLimit()
case litecoinChain:
@ -3084,7 +3093,8 @@ func (f *fundingManager) handleInitFundingMsg(msg *initFundingMsg) {
// address from the wallet if our node is configured to set shutdown
// address by default).
shutdown, err := getUpfrontShutdownScript(
msg.peer, msg.openChanReq.shutdownScript,
f.cfg.EnableUpfrontShutdown, msg.peer,
msg.openChanReq.shutdownScript,
func() (lnwire.DeliveryAddress, error) {
addr, err := f.cfg.Wallet.NewAddress(
lnwallet.WitnessPubKey, false,

View File

@ -418,6 +418,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey,
NotifyOpenChannelEvent: evt.NotifyOpenChannelEvent,
OpenChannelPredicate: chainedAcceptor,
NotifyPendingOpenChannelEvent: evt.NotifyPendingOpenChannelEvent,
RegisteredChains: newChainRegistry(),
}
for _, op := range options {
@ -3084,11 +3085,9 @@ func TestGetUpfrontShutdownScript(t *testing.T) {
}
}
// Set the command line option in config as needed.
cfg = &Config{EnableUpfrontShutdown: test.localEnabled}
addr, err := getUpfrontShutdownScript(
&mockPeer, test.upfrontScript, test.getScript,
test.localEnabled, &mockPeer, test.upfrontScript,
test.getScript,
)
if err != test.expectedErr {
t.Fatalf("got: %v, expected error: %v", err, test.expectedErr)

View File

@ -1144,6 +1144,8 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB,
NotifyOpenChannelEvent: s.channelNotifier.NotifyOpenChannelEvent,
OpenChannelPredicate: chanPredicate,
NotifyPendingOpenChannelEvent: s.channelNotifier.NotifyPendingOpenChannelEvent,
EnableUpfrontShutdown: cfg.EnableUpfrontShutdown,
RegisteredChains: cfg.registeredChains,
})
if err != nil {
return nil, err