From 74eeb95e8c584cab3a09c937357a6d21c701ccaf Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 14 Oct 2022 14:25:25 +0200 Subject: [PATCH] htlcswitch: add error return value to NewInterceptableSwitch Prepares for parameter validation. --- htlcswitch/interceptable_switch.go | 6 ++++-- htlcswitch/switch_test.go | 11 +++++++---- peer/test_utils.go | 31 +++++++++++++++++------------- server.go | 5 ++++- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/htlcswitch/interceptable_switch.go b/htlcswitch/interceptable_switch.go index 9675c3a4b..dd8e69239 100644 --- a/htlcswitch/interceptable_switch.go +++ b/htlcswitch/interceptable_switch.go @@ -146,7 +146,9 @@ type InterceptableSwitchConfig struct { } // NewInterceptableSwitch returns an instance of InterceptableSwitch. -func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) *InterceptableSwitch { +func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) ( + *InterceptableSwitch, error) { + return &InterceptableSwitch{ htlcSwitch: cfg.Switch, intercepted: make(chan *interceptedPackets), @@ -159,7 +161,7 @@ func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) *InterceptableSwitch notifier: cfg.Notifier, quit: make(chan struct{}), - } + }, nil } // SetInterceptor sets the ForwardInterceptor to be used. A nil argument diff --git a/htlcswitch/switch_test.go b/htlcswitch/switch_test.go index 27ebaa471..6a3d6fdb9 100644 --- a/htlcswitch/switch_test.go +++ b/htlcswitch/switch_test.go @@ -3885,13 +3885,14 @@ func TestSwitchHoldForward(t *testing.T) { } notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight} - switchForwardInterceptor := NewInterceptableSwitch( + switchForwardInterceptor, err := NewInterceptableSwitch( &InterceptableSwitchConfig{ Switch: c.s, CltvRejectDelta: c.cltvRejectDelta, Notifier: notifier, }, ) + require.NoError(t, err) require.NoError(t, switchForwardInterceptor.Start()) switchForwardInterceptor.SetInterceptor(c.forwardInterceptor.InterceptForwardHtlc) @@ -3901,7 +3902,7 @@ func TestSwitchHoldForward(t *testing.T) { packet := c.createTestPacket() packet.incomingTimeout = testStartingHeight + c.cltvRejectDelta - 1 - err := switchForwardInterceptor.ForwardPackets(linkQuit, false, packet) + err = switchForwardInterceptor.ForwardPackets(linkQuit, false, packet) require.NoError(t, err, "can't forward htlc packet") assertOutgoingLinkReceive(t, c.bobChannelLink, false) assertOutgoingLinkReceiveIntercepted(t, c.aliceChannelLink) @@ -4086,7 +4087,7 @@ func TestSwitchHoldForward(t *testing.T) { } notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight} - switchForwardInterceptor = NewInterceptableSwitch( + switchForwardInterceptor, err = NewInterceptableSwitch( &InterceptableSwitchConfig{ Switch: c.s, CltvRejectDelta: c.cltvRejectDelta, @@ -4094,6 +4095,7 @@ func TestSwitchHoldForward(t *testing.T) { Notifier: notifier, }, ) + require.NoError(t, err) require.NoError(t, switchForwardInterceptor.Start()) // Forward a fresh packet. It is expected to be failed immediately, @@ -5391,12 +5393,13 @@ func testSwitchAliasInterceptFail(t *testing.T, zeroConf bool) { } notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight} - interceptSwitch := NewInterceptableSwitch( + interceptSwitch, err := NewInterceptableSwitch( &InterceptableSwitchConfig{ Switch: s, Notifier: notifier, }, ) + require.NoError(t, err) require.NoError(t, interceptSwitch.Start()) interceptSwitch.SetInterceptor(forwardInterceptor.InterceptForwardHtlc) diff --git a/peer/test_utils.go b/peer/test_utils.go index 7ba6474c8..723a439bd 100644 --- a/peer/test_utils.go +++ b/peer/test_utils.go @@ -366,6 +366,16 @@ func createTestPeer(t *testing.T, notifier chainntnfs.ChainNotifier, Height: 1, } + interceptableSwitch, err := htlcswitch.NewInterceptableSwitch( + &htlcswitch.InterceptableSwitchConfig{ + CltvRejectDelta: testCltvRejectDelta, + Notifier: interceptableSwitchNotifier, + }, + ) + if err != nil { + return nil, nil, err + } + cfg := &Config{ Addr: cfgAddr, PubKeyBytes: pubKey, @@ -373,19 +383,14 @@ func createTestPeer(t *testing.T, notifier chainntnfs.ChainNotifier, ChainIO: chainIO, Switch: mockSwitch, ChanActiveTimeout: chanActiveTimeout, - InterceptSwitch: htlcswitch.NewInterceptableSwitch( - &htlcswitch.InterceptableSwitchConfig{ - CltvRejectDelta: testCltvRejectDelta, - Notifier: interceptableSwitchNotifier, - }, - ), - ChannelDB: dbAlice.ChannelStateDB(), - FeeEstimator: estimator, - Wallet: wallet, - ChainNotifier: notifier, - ChanStatusMgr: chanStatusMgr, - Features: lnwire.NewFeatureVector(nil, lnwire.Features), - DisconnectPeer: func(b *btcec.PublicKey) error { return nil }, + InterceptSwitch: interceptableSwitch, + ChannelDB: dbAlice.ChannelStateDB(), + FeeEstimator: estimator, + Wallet: wallet, + ChainNotifier: notifier, + ChanStatusMgr: chanStatusMgr, + Features: lnwire.NewFeatureVector(nil, lnwire.Features), + DisconnectPeer: func(b *btcec.PublicKey) error { return nil }, } alicePeer := NewBrontide(*cfg) diff --git a/server.go b/server.go index 1022b2ad5..372e349ce 100644 --- a/server.go +++ b/server.go @@ -666,7 +666,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr, if err != nil { return nil, err } - s.interceptableSwitch = htlcswitch.NewInterceptableSwitch( + s.interceptableSwitch, err = htlcswitch.NewInterceptableSwitch( &htlcswitch.InterceptableSwitchConfig{ Switch: s.htlcSwitch, CltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta, @@ -674,6 +674,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr, Notifier: s.cc.ChainNotifier, }, ) + if err != nil { + return nil, err + } s.witnessBeacon = newPreimageBeacon( dbs.ChanStateDB.NewWitnessCache(),