htlcswitch: add error return value to NewInterceptableSwitch
Prepares for parameter validation.
This commit is contained in:
4 files changed
+33
-20
No files matched your search
@@ -146,7 +146,9 @@ type InterceptableSwitchConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewInterceptableSwitch returns an instance of InterceptableSwitch.
|
// NewInterceptableSwitch returns an instance of InterceptableSwitch.
|
||||||
func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) *InterceptableSwitch {
|
func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) (
|
||||||
|
*InterceptableSwitch, error) {
|
||||||
|
|
||||||
return &InterceptableSwitch{
|
return &InterceptableSwitch{
|
||||||
htlcSwitch: cfg.Switch,
|
htlcSwitch: cfg.Switch,
|
||||||
intercepted: make(chan *interceptedPackets),
|
intercepted: make(chan *interceptedPackets),
|
||||||
@@ -159,7 +161,7 @@ func NewInterceptableSwitch(cfg *InterceptableSwitchConfig) *InterceptableSwitch
|
|||||||
notifier: cfg.Notifier,
|
notifier: cfg.Notifier,
|
||||||
|
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetInterceptor sets the ForwardInterceptor to be used. A nil argument
|
// SetInterceptor sets the ForwardInterceptor to be used. A nil argument
|
||||||
|
|||||||
@@ -3885,13 +3885,14 @@ func TestSwitchHoldForward(t *testing.T) {
|
|||||||
}
|
}
|
||||||
notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight}
|
notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight}
|
||||||
|
|
||||||
switchForwardInterceptor := NewInterceptableSwitch(
|
switchForwardInterceptor, err := NewInterceptableSwitch(
|
||||||
&InterceptableSwitchConfig{
|
&InterceptableSwitchConfig{
|
||||||
Switch: c.s,
|
Switch: c.s,
|
||||||
CltvRejectDelta: c.cltvRejectDelta,
|
CltvRejectDelta: c.cltvRejectDelta,
|
||||||
Notifier: notifier,
|
Notifier: notifier,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
require.NoError(t, switchForwardInterceptor.Start())
|
require.NoError(t, switchForwardInterceptor.Start())
|
||||||
|
|
||||||
switchForwardInterceptor.SetInterceptor(c.forwardInterceptor.InterceptForwardHtlc)
|
switchForwardInterceptor.SetInterceptor(c.forwardInterceptor.InterceptForwardHtlc)
|
||||||
@@ -3901,7 +3902,7 @@ func TestSwitchHoldForward(t *testing.T) {
|
|||||||
packet := c.createTestPacket()
|
packet := c.createTestPacket()
|
||||||
packet.incomingTimeout = testStartingHeight + c.cltvRejectDelta - 1
|
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")
|
require.NoError(t, err, "can't forward htlc packet")
|
||||||
assertOutgoingLinkReceive(t, c.bobChannelLink, false)
|
assertOutgoingLinkReceive(t, c.bobChannelLink, false)
|
||||||
assertOutgoingLinkReceiveIntercepted(t, c.aliceChannelLink)
|
assertOutgoingLinkReceiveIntercepted(t, c.aliceChannelLink)
|
||||||
@@ -4086,7 +4087,7 @@ func TestSwitchHoldForward(t *testing.T) {
|
|||||||
}
|
}
|
||||||
notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight}
|
notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight}
|
||||||
|
|
||||||
switchForwardInterceptor = NewInterceptableSwitch(
|
switchForwardInterceptor, err = NewInterceptableSwitch(
|
||||||
&InterceptableSwitchConfig{
|
&InterceptableSwitchConfig{
|
||||||
Switch: c.s,
|
Switch: c.s,
|
||||||
CltvRejectDelta: c.cltvRejectDelta,
|
CltvRejectDelta: c.cltvRejectDelta,
|
||||||
@@ -4094,6 +4095,7 @@ func TestSwitchHoldForward(t *testing.T) {
|
|||||||
Notifier: notifier,
|
Notifier: notifier,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
require.NoError(t, switchForwardInterceptor.Start())
|
require.NoError(t, switchForwardInterceptor.Start())
|
||||||
|
|
||||||
// Forward a fresh packet. It is expected to be failed immediately,
|
// 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}
|
notifier.EpochChan <- &chainntnfs.BlockEpoch{Height: testStartingHeight}
|
||||||
|
|
||||||
interceptSwitch := NewInterceptableSwitch(
|
interceptSwitch, err := NewInterceptableSwitch(
|
||||||
&InterceptableSwitchConfig{
|
&InterceptableSwitchConfig{
|
||||||
Switch: s,
|
Switch: s,
|
||||||
Notifier: notifier,
|
Notifier: notifier,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
require.NoError(t, interceptSwitch.Start())
|
require.NoError(t, interceptSwitch.Start())
|
||||||
interceptSwitch.SetInterceptor(forwardInterceptor.InterceptForwardHtlc)
|
interceptSwitch.SetInterceptor(forwardInterceptor.InterceptForwardHtlc)
|
||||||
|
|
||||||
|
|||||||
+18
-13
@@ -366,6 +366,16 @@ func createTestPeer(t *testing.T, notifier chainntnfs.ChainNotifier,
|
|||||||
Height: 1,
|
Height: 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interceptableSwitch, err := htlcswitch.NewInterceptableSwitch(
|
||||||
|
&htlcswitch.InterceptableSwitchConfig{
|
||||||
|
CltvRejectDelta: testCltvRejectDelta,
|
||||||
|
Notifier: interceptableSwitchNotifier,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
Addr: cfgAddr,
|
Addr: cfgAddr,
|
||||||
PubKeyBytes: pubKey,
|
PubKeyBytes: pubKey,
|
||||||
@@ -373,19 +383,14 @@ func createTestPeer(t *testing.T, notifier chainntnfs.ChainNotifier,
|
|||||||
ChainIO: chainIO,
|
ChainIO: chainIO,
|
||||||
Switch: mockSwitch,
|
Switch: mockSwitch,
|
||||||
ChanActiveTimeout: chanActiveTimeout,
|
ChanActiveTimeout: chanActiveTimeout,
|
||||||
InterceptSwitch: htlcswitch.NewInterceptableSwitch(
|
InterceptSwitch: interceptableSwitch,
|
||||||
&htlcswitch.InterceptableSwitchConfig{
|
ChannelDB: dbAlice.ChannelStateDB(),
|
||||||
CltvRejectDelta: testCltvRejectDelta,
|
FeeEstimator: estimator,
|
||||||
Notifier: interceptableSwitchNotifier,
|
Wallet: wallet,
|
||||||
},
|
ChainNotifier: notifier,
|
||||||
),
|
ChanStatusMgr: chanStatusMgr,
|
||||||
ChannelDB: dbAlice.ChannelStateDB(),
|
Features: lnwire.NewFeatureVector(nil, lnwire.Features),
|
||||||
FeeEstimator: estimator,
|
DisconnectPeer: func(b *btcec.PublicKey) error { return nil },
|
||||||
Wallet: wallet,
|
|
||||||
ChainNotifier: notifier,
|
|
||||||
ChanStatusMgr: chanStatusMgr,
|
|
||||||
Features: lnwire.NewFeatureVector(nil, lnwire.Features),
|
|
||||||
DisconnectPeer: func(b *btcec.PublicKey) error { return nil },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
alicePeer := NewBrontide(*cfg)
|
alicePeer := NewBrontide(*cfg)
|
||||||
|
|||||||
@@ -666,7 +666,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.interceptableSwitch = htlcswitch.NewInterceptableSwitch(
|
s.interceptableSwitch, err = htlcswitch.NewInterceptableSwitch(
|
||||||
&htlcswitch.InterceptableSwitchConfig{
|
&htlcswitch.InterceptableSwitchConfig{
|
||||||
Switch: s.htlcSwitch,
|
Switch: s.htlcSwitch,
|
||||||
CltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta,
|
CltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta,
|
||||||
@@ -674,6 +674,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
|
|||||||
Notifier: s.cc.ChainNotifier,
|
Notifier: s.cc.ChainNotifier,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
s.witnessBeacon = newPreimageBeacon(
|
s.witnessBeacon = newPreimageBeacon(
|
||||||
dbs.ChanStateDB.NewWitnessCache(),
|
dbs.ChanStateDB.NewWitnessCache(),
|
||||||
|
|||||||
Reference in new issue
Block a user