htlcswitch: add an always on mode to interceptable switch

Co-authored-by: Juan Pablo Civile <elementohb@gmail.com>
This commit is contained in:
Joost JagerandJuan Pablo Civile committed 2022-03-17 17:39:37 +01:00
1 parent 169f0c0bf4
commit ae314ec794
10 files changed
+201 -54

No files matched your search

+88 -18
View File
@@ -3273,7 +3273,7 @@ func TestSwitchHoldForward(t *testing.T) {
t: t,
interceptedChan: make(chan InterceptedPacket),
}
switchForwardInterceptor := NewInterceptableSwitch(s)
switchForwardInterceptor := NewInterceptableSwitch(s, false)
require.NoError(t, switchForwardInterceptor.Start())
switchForwardInterceptor.SetInterceptor(forwardInterceptor.InterceptForwardHtlc)
@@ -3281,9 +3281,9 @@ func TestSwitchHoldForward(t *testing.T) {
// Test resume a hold forward.
assertNumCircuits(t, s, 0, 0)
if err := switchForwardInterceptor.ForwardPackets(linkQuit, ogPacket); err != nil {
t.Fatalf("can't forward htlc packet: %v", err)
}
err = switchForwardInterceptor.ForwardPackets(linkQuit, false, ogPacket)
require.NoError(t, err)
assertNumCircuits(t, s, 0, 0)
assertOutgoingLinkReceive(t, bobChannelLink, false)
@@ -3303,15 +3303,16 @@ func TestSwitchHoldForward(t *testing.T) {
PaymentPreimage: preimage,
},
}
if err := switchForwardInterceptor.ForwardPackets(linkQuit, settle); err != nil {
t.Fatalf("can't forward htlc packet: %v", err)
}
err = switchForwardInterceptor.ForwardPackets(linkQuit, false, settle)
require.NoError(t, err)
assertOutgoingLinkReceive(t, aliceChannelLink, true)
assertNumCircuits(t, s, 0, 0)
// Test resume a hold forward after disconnection.
err = switchForwardInterceptor.ForwardPackets(nil, ogPacket)
require.NoError(t, err)
require.NoError(t, switchForwardInterceptor.ForwardPackets(
linkQuit, false, ogPacket,
))
// Wait until the packet is offered to the interceptor.
_ = forwardInterceptor.getIntercepted()
@@ -3328,7 +3329,9 @@ func TestSwitchHoldForward(t *testing.T) {
// Settle the htlc to close the circuit.
settle.outgoingHTLCID = 1
require.NoError(t, switchForwardInterceptor.ForwardPackets(nil, settle))
require.NoError(t, switchForwardInterceptor.ForwardPackets(
linkQuit, false, settle,
))
assertOutgoingLinkReceive(t, aliceChannelLink, true)
assertNumCircuits(t, s, 0, 0)
@@ -3338,9 +3341,9 @@ func TestSwitchHoldForward(t *testing.T) {
forwardInterceptor.InterceptForwardHtlc,
)
if err := switchForwardInterceptor.ForwardPackets(linkQuit, ogPacket); err != nil {
t.Fatalf("can't forward htlc packet: %v", err)
}
require.NoError(t, switchForwardInterceptor.ForwardPackets(
linkQuit, false, ogPacket,
))
assertNumCircuits(t, s, 0, 0)
assertOutgoingLinkReceive(t, bobChannelLink, false)
@@ -3355,7 +3358,7 @@ func TestSwitchHoldForward(t *testing.T) {
// Test failing a hold forward with a failure message.
require.NoError(t,
switchForwardInterceptor.ForwardPackets(linkQuit, ogPacket),
switchForwardInterceptor.ForwardPackets(linkQuit, false, ogPacket),
)
assertNumCircuits(t, s, 0, 0)
assertOutgoingLinkReceive(t, bobChannelLink, false)
@@ -3375,7 +3378,7 @@ func TestSwitchHoldForward(t *testing.T) {
assertNumCircuits(t, s, 0, 0)
// Test failing a hold forward with a malformed htlc failure.
err = switchForwardInterceptor.ForwardPackets(linkQuit, ogPacket)
err = switchForwardInterceptor.ForwardPackets(linkQuit, false, ogPacket)
require.NoError(t, err)
assertNumCircuits(t, s, 0, 0)
@@ -3404,9 +3407,9 @@ func TestSwitchHoldForward(t *testing.T) {
assertNumCircuits(t, s, 0, 0)
// Test settling a hold forward
if err := switchForwardInterceptor.ForwardPackets(linkQuit, ogPacket); err != nil {
t.Fatalf("can't forward htlc packet: %v", err)
}
require.NoError(t, switchForwardInterceptor.ForwardPackets(
linkQuit, false, ogPacket,
))
assertNumCircuits(t, s, 0, 0)
assertOutgoingLinkReceive(t, bobChannelLink, false)
@@ -3420,6 +3423,73 @@ func TestSwitchHoldForward(t *testing.T) {
assertNumCircuits(t, s, 0, 0)
require.NoError(t, switchForwardInterceptor.Stop())
// Test always-on interception.
switchForwardInterceptor = NewInterceptableSwitch(s, true)
require.NoError(t, switchForwardInterceptor.Start())
// Forward a fresh packet. It is expected to be failed immediately,
// because there is no interceptor registered.
require.NoError(t, switchForwardInterceptor.ForwardPackets(
linkQuit, false, ogPacket,
))
assertOutgoingLinkReceive(t, bobChannelLink, false)
assertOutgoingLinkReceive(t, aliceChannelLink, true)
assertNumCircuits(t, s, 0, 0)
// Forward a replayed packet. It is expected to be held until the
// interceptor connects. To continue the test, it needs to be ran in a
// goroutine.
errChan := make(chan error)
go func() {
errChan <- switchForwardInterceptor.ForwardPackets(
linkQuit, true, ogPacket,
)
}()
// Assert that nothing is forward to the switch.
assertOutgoingLinkReceive(t, bobChannelLink, false)
assertNumCircuits(t, s, 0, 0)
// Register an interceptor.
switchForwardInterceptor.SetInterceptor(
forwardInterceptor.InterceptForwardHtlc,
)
// Expect the ForwardPackets call to unblock.
require.NoError(t, <-errChan)
// Now expect the queued packet to come through.
forwardInterceptor.getIntercepted()
// Disconnect and reconnect interceptor.
switchForwardInterceptor.SetInterceptor(nil)
switchForwardInterceptor.SetInterceptor(
forwardInterceptor.InterceptForwardHtlc,
)
// A replay of the held packet is expected.
intercepted := forwardInterceptor.getIntercepted()
// Settle the packet.
require.NoError(t, switchForwardInterceptor.resolve(&FwdResolution{
Key: intercepted.IncomingCircuit,
Action: FwdActionSettle,
Preimage: preimage,
}))
assertOutgoingLinkReceive(t, bobChannelLink, false)
assertOutgoingLinkReceive(t, aliceChannelLink, true)
assertNumCircuits(t, s, 0, 0)
require.NoError(t, switchForwardInterceptor.Stop())
select {
case <-forwardInterceptor.interceptedChan:
require.Fail(t, "unexpected interception")
default:
}
}
// TestSwitchDustForwarding tests that the switch properly fails HTLC's which