htlcswitch+lntest: modify Switch to persist resolution messages

Include unit tests for the Switch, and integration tests that
exercise the persistence logic.
This commit is contained in:
eugene
2022-02-11 13:38:49 -05:00
parent bfed7a088f
commit f7b3da4bb2
8 changed files with 518 additions and 26 deletions

View File

@@ -2,6 +2,7 @@ package htlcswitch_test
import (
"bytes"
"fmt"
"io/ioutil"
"reflect"
"testing"
@@ -97,8 +98,9 @@ func newOnionProcessor(t *testing.T) *hop.OnionProcessor {
}
// newCircuitMap creates a new htlcswitch.CircuitMap using a temp db and a
// fresh sphinx router.
func newCircuitMap(t *testing.T) (*htlcswitch.CircuitMapConfig,
// fresh sphinx router. When resMsg is set to true, CheckResolutionMsg will
// always return nil. Otherwise it will always return an error.
func newCircuitMap(t *testing.T, resMsg bool) (*htlcswitch.CircuitMapConfig,
htlcswitch.CircuitMap) {
onionProcessor := newOnionProcessor(t)
@@ -111,6 +113,18 @@ func newCircuitMap(t *testing.T) (*htlcswitch.CircuitMapConfig,
ExtractErrorEncrypter: onionProcessor.ExtractErrorEncrypter,
}
if resMsg {
checkRes := func(out *htlcswitch.CircuitKey) error {
return nil
}
circuitMapCfg.CheckResolutionMsg = checkRes
} else {
checkRes := func(out *htlcswitch.CircuitKey) error {
return fmt.Errorf("not found")
}
circuitMapCfg.CheckResolutionMsg = checkRes
}
circuitMap, err := htlcswitch.NewCircuitMap(circuitMapCfg)
if err != nil {
t.Fatalf("unable to create persistent circuit map: %v", err)
@@ -124,7 +138,7 @@ func newCircuitMap(t *testing.T) (*htlcswitch.CircuitMapConfig,
func TestCircuitMapInit(t *testing.T) {
t.Parallel()
cfg, _ := newCircuitMap(t)
cfg, _ := newCircuitMap(t, false)
restartCircuitMap(t, cfg)
}
@@ -231,7 +245,7 @@ func TestCircuitMapPersistence(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := circuitMap.LookupCircuit(htlcswitch.CircuitKey{
ChanID: chan1,
@@ -649,6 +663,7 @@ func restartCircuitMap(t *testing.T, cfg *htlcswitch.CircuitMapConfig) (
FetchAllOpenChannels: db.ChannelStateDB().FetchAllOpenChannels,
FetchClosedChannels: db.ChannelStateDB().FetchClosedChannels,
ExtractErrorEncrypter: cfg.ExtractErrorEncrypter,
CheckResolutionMsg: cfg.CheckResolutionMsg,
}
cm2, err := htlcswitch.NewCircuitMap(cfg2)
if err != nil {
@@ -671,7 +686,7 @@ func TestCircuitMapCommitCircuits(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{
@@ -767,7 +782,7 @@ func TestCircuitMapOpenCircuits(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{
@@ -973,7 +988,7 @@ func TestCircuitMapTrimOpenCircuits(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
const nCircuits = 10
const firstTrimIndex = 7
@@ -1122,7 +1137,7 @@ func TestCircuitMapCloseOpenCircuits(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{
@@ -1215,7 +1230,7 @@ func TestCircuitMapCloseUnopenedCircuit(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{
@@ -1272,7 +1287,7 @@ func TestCircuitMapDeleteUnopenedCircuit(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{
@@ -1331,7 +1346,7 @@ func TestCircuitMapDeleteOpenCircuit(t *testing.T) {
err error
)
cfg, circuitMap := newCircuitMap(t)
cfg, circuitMap := newCircuitMap(t, false)
circuit := &htlcswitch.PaymentCircuit{
Incoming: htlcswitch.CircuitKey{