htlcswitch: update mockServer to be able to fail test directly
This commit is contained in:
parent
a48ceac98f
commit
9873d4ece8
@ -60,9 +60,9 @@ func messageToString(msg lnwire.Message) string {
|
||||
return spew.Sdump(msg)
|
||||
}
|
||||
|
||||
// expectedMessage struct hols the message which travels from one peer to
|
||||
// another, and additional information like, should this message we skipped
|
||||
// for handling.
|
||||
// expectedMessage struct holds the message which travels from one peer to
|
||||
// another, and additional information like, should this message we skipped for
|
||||
// handling.
|
||||
type expectedMessage struct {
|
||||
from string
|
||||
to string
|
||||
@ -111,8 +111,9 @@ func createInterceptorFunc(prefix, receiver string, messages []expectedMessage,
|
||||
|
||||
if messageChanID == chanID {
|
||||
if len(expectToReceive) == 0 {
|
||||
return false, errors.Errorf("received unexpected message out "+
|
||||
"of range: %v", m.MsgType())
|
||||
return false, errors.Errorf("%v received "+
|
||||
"unexpected message out of range: %v",
|
||||
receiver, m.MsgType())
|
||||
}
|
||||
|
||||
expectedMessage := expectToReceive[0]
|
||||
@ -160,16 +161,15 @@ func TestChannelLinkSingleHopPayment(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer n.stop()
|
||||
|
||||
bobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
||||
aliceBandwidthBefore := n.aliceChannelLink.Bandwidth()
|
||||
bobBandwidthBefore := n.firstBobChannelLink.Bandwidth()
|
||||
|
||||
debug := false
|
||||
if debug {
|
||||
@ -209,7 +209,7 @@ func TestChannelLinkSingleHopPayment(t *testing.T) {
|
||||
// links was changed.
|
||||
invoice, err := receiver.registry.LookupInvoice(rhash)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get inveoice: %v", err)
|
||||
t.Fatalf("unable to get invoice: %v", err)
|
||||
}
|
||||
if !invoice.Terms.Settled {
|
||||
t.Fatal("alice invoice wasn't settled")
|
||||
@ -221,7 +221,9 @@ func TestChannelLinkSingleHopPayment(t *testing.T) {
|
||||
}
|
||||
|
||||
if bobBandwidthBefore+amount != n.firstBobChannelLink.Bandwidth() {
|
||||
t.Fatal("bob bandwidth isn't match")
|
||||
t.Fatalf("bob bandwidth isn't match: expected %v, got %v",
|
||||
bobBandwidthBefore+amount,
|
||||
n.firstBobChannelLink.Bandwidth())
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,9 +241,8 @@ func TestChannelLinkBidirectionalOneHopPayments(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -366,9 +367,8 @@ func TestChannelLinkMultiHopPayment(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -474,9 +474,8 @@ func TestExitNodeTimelockPayloadMismatch(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -527,9 +526,8 @@ func TestExitNodeAmountPayloadMismatch(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -572,9 +570,8 @@ func TestLinkForwardTimelockPolicyMismatch(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -628,9 +625,8 @@ func TestLinkForwardFeePolicyMismatch(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -685,9 +681,8 @@ func TestLinkForwardMinHTLCPolicyMismatch(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -743,9 +738,8 @@ func TestUpdateForwardingPolicy(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -847,9 +841,8 @@ func TestChannelLinkMultiHopInsufficientPayment(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatalf("unable to start three hop network: %v", err)
|
||||
}
|
||||
@ -930,9 +923,8 @@ func TestChannelLinkMultiHopUnknownPaymentHash(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatalf("unable to start three hop network: %v", err)
|
||||
}
|
||||
@ -1020,9 +1012,8 @@ func TestChannelLinkMultiHopUnknownNextHop(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -1037,7 +1028,7 @@ func TestChannelLinkMultiHopUnknownNextHop(t *testing.T) {
|
||||
htlcAmt, totalTimelock, hops := generateHops(amount, testStartingHeight,
|
||||
n.firstBobChannelLink, n.carolChannelLink)
|
||||
|
||||
davePub := newMockServer("save", serverErr).PubKey()
|
||||
davePub := newMockServer(t, "dave").PubKey()
|
||||
receiver := n.bobServer
|
||||
rhash, err := n.makePayment(n.aliceServer, n.bobServer, davePub, hops,
|
||||
amount, htlcAmt, totalTimelock).Wait(10 * time.Second)
|
||||
@ -1096,9 +1087,8 @@ func TestChannelLinkMultiHopDecodeError(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatalf("unable to start three hop network: %v", err)
|
||||
}
|
||||
@ -1188,10 +1178,9 @@ func TestChannelLinkExpiryTooSoonExitNode(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
const startingHeight = 200
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, startingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, startingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatalf("unable to start three hop network: %v", err)
|
||||
}
|
||||
@ -1245,10 +1234,9 @@ func TestChannelLinkExpiryTooSoonMidNode(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
const startingHeight = 200
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, startingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, startingHeight)
|
||||
if err := n.start(); err != nil {
|
||||
t.Fatalf("unable to start three hop network: %v", err)
|
||||
}
|
||||
@ -1301,9 +1289,8 @@ func TestChannelLinkSingleHopMessageOrdering(t *testing.T) {
|
||||
}
|
||||
defer cleanUp()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
|
||||
chanID := n.aliceChannelLink.ChanID()
|
||||
|
||||
@ -1358,8 +1345,7 @@ func TestChannelLinkSingleHopMessageOrdering(t *testing.T) {
|
||||
// * settle request to be sent back from bob to alice.
|
||||
// * alice<->bob commitment state to be updated.
|
||||
// * user notification to be sent.
|
||||
receiver := n.bobServer
|
||||
_, err = n.makePayment(n.aliceServer, receiver,
|
||||
_, err = n.makePayment(n.aliceServer, n.bobServer,
|
||||
n.bobServer.PubKey(), hops, amount, htlcAmt,
|
||||
totalTimelock).Wait(10 * time.Second)
|
||||
if err != nil {
|
||||
@ -1406,7 +1392,7 @@ func newSingleLinkTestHarness(chanAmt btcutil.Amount) (ChannelLink, func(), erro
|
||||
}
|
||||
|
||||
chanID := lnwire.NewShortChanIDFromInt(4)
|
||||
aliceChannel, _, fCleanUp, err := createTestChannel(
|
||||
aliceChannel, _, fCleanUp, _, err := createTestChannel(
|
||||
alicePrivKey, bobPrivKey, chanAmt, chanAmt, chanID,
|
||||
)
|
||||
if err != nil {
|
||||
@ -1833,8 +1819,11 @@ func TestChannelRetransmission(t *testing.T) {
|
||||
"bob", messages, chanID, true)
|
||||
|
||||
// Add interceptor to check the order of Bob and Alice messages.
|
||||
n := newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
n := newThreeHopNetwork(t,
|
||||
channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob,
|
||||
testStartingHeight,
|
||||
)
|
||||
n.aliceServer.intersect(aliceInterceptor)
|
||||
n.bobServer.intersect(bobInterceptor)
|
||||
if err := n.start(); err != nil {
|
||||
@ -1872,7 +1861,7 @@ func TestChannelRetransmission(t *testing.T) {
|
||||
}
|
||||
|
||||
n = newThreeHopNetwork(t, channels.aliceToBob, channels.bobToAlice,
|
||||
channels.bobToCarol, channels.carolToBob, serverErr, testStartingHeight)
|
||||
channels.bobToCarol, channels.carolToBob, testStartingHeight)
|
||||
n.firstBobChannelLink.cfg.Registry = bobRegistry
|
||||
n.aliceServer.intersect(aliceInterceptor)
|
||||
n.bobServer.intersect(bobInterceptor)
|
||||
@ -1894,8 +1883,8 @@ func TestChannelRetransmission(t *testing.T) {
|
||||
t.Fatalf("server error: %v", serverErr)
|
||||
}
|
||||
|
||||
// Check that alice invoice wasn't settled and bandwidth of htlc
|
||||
// links hasn't been changed.
|
||||
// Check that alice invoice wasn't settled and
|
||||
// bandwidth of htlc links hasn't been changed.
|
||||
invoice, err = receiver.registry.LookupInvoice(rhash)
|
||||
if err != nil {
|
||||
err = errors.Errorf("unable to get invoice: %v", err)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"io"
|
||||
"sync/atomic"
|
||||
@ -29,6 +30,8 @@ type mockServer struct {
|
||||
wg sync.WaitGroup
|
||||
quit chan struct{}
|
||||
|
||||
t *testing.T
|
||||
|
||||
name string
|
||||
messages chan lnwire.Message
|
||||
|
||||
@ -43,13 +46,13 @@ type mockServer struct {
|
||||
|
||||
var _ Peer = (*mockServer)(nil)
|
||||
|
||||
func newMockServer(name string, errChan chan error) *mockServer {
|
||||
func newMockServer(t *testing.T, name string) *mockServer {
|
||||
var id [33]byte
|
||||
h := sha256.Sum256([]byte(name))
|
||||
copy(id[:], h[:])
|
||||
|
||||
return &mockServer{
|
||||
errChan: errChan,
|
||||
t: t,
|
||||
id: id,
|
||||
name: name,
|
||||
messages: make(chan lnwire.Message, 3000),
|
||||
@ -85,8 +88,8 @@ func (s *mockServer) Start() error {
|
||||
for _, interceptor := range s.interceptorFuncs {
|
||||
skip, err := interceptor(msg)
|
||||
if err != nil {
|
||||
s.fail(errors.Errorf("%v: error in the "+
|
||||
"interceptor: %v", s.name, err))
|
||||
s.t.Fatalf("%v: error in the "+
|
||||
"interceptor: %v", s.name, err)
|
||||
return
|
||||
}
|
||||
shouldSkip = shouldSkip || skip
|
||||
@ -97,7 +100,7 @@ func (s *mockServer) Start() error {
|
||||
}
|
||||
|
||||
if err := s.readHandler(msg); err != nil {
|
||||
s.fail(err)
|
||||
s.t.Fatal(err)
|
||||
return
|
||||
}
|
||||
case <-s.quit:
|
||||
@ -109,16 +112,6 @@ func (s *mockServer) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *mockServer) fail(err error) {
|
||||
go func() {
|
||||
s.Stop()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
s.errChan <- errors.Errorf("%v server error: %v", s.name, err)
|
||||
}()
|
||||
}
|
||||
|
||||
// mockHopIterator represents the test version of hop iterator which instead
|
||||
// of encrypting the path in onion blob just stores the path as a list of hops.
|
||||
type mockHopIterator struct {
|
||||
@ -311,7 +304,6 @@ func (s *mockServer) readHandler(message lnwire.Message) error {
|
||||
case *lnwire.ChannelReestablish:
|
||||
targetChan = msg.ChanID
|
||||
default:
|
||||
return errors.New("unknown message type")
|
||||
}
|
||||
|
||||
// Dispatch the commitment update message to the proper
|
||||
@ -323,15 +315,6 @@ func (s *mockServer) readHandler(message lnwire.Message) error {
|
||||
|
||||
// Create goroutine for this, in order to be able to properly stop
|
||||
// the server when handler stacked (server unavailable)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
link.HandleChannelUpdate(message)
|
||||
done <- struct{}{}
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-s.quit:
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -343,7 +326,6 @@ func (s *mockServer) PubKey() [33]byte {
|
||||
func (s *mockServer) Disconnect(reason error) {
|
||||
fmt.Printf("server %v disconnected due to %v\n", s.name, reason)
|
||||
|
||||
s.fail(errors.Errorf("server %v was disconnected: %v", s.name, reason))
|
||||
}
|
||||
|
||||
func (s *mockServer) WipeChannel(*lnwallet.LightningChannel) error {
|
||||
|
@ -34,9 +34,8 @@ func TestSwitchForward(t *testing.T) {
|
||||
|
||||
var packet *htlcPacket
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
alicePeer := newMockServer("alice", serverErr)
|
||||
bobPeer := newMockServer("bob", serverErr)
|
||||
alicePeer := newMockServer(t, "alice")
|
||||
bobPeer := newMockServer(t, "bob")
|
||||
|
||||
aliceChannelLink := newMockChannelLink(chanID1, aliceChanID, alicePeer)
|
||||
bobChannelLink := newMockChannelLink(chanID2, bobChanID, bobPeer)
|
||||
@ -113,9 +112,8 @@ func TestSwitchCancel(t *testing.T) {
|
||||
|
||||
var request *htlcPacket
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
alicePeer := newMockServer("alice", serverErr)
|
||||
bobPeer := newMockServer("bob", serverErr)
|
||||
alicePeer := newMockServer(t, "alice")
|
||||
bobPeer := newMockServer(t, "bob")
|
||||
|
||||
aliceChannelLink := newMockChannelLink(chanID1, aliceChanID, alicePeer)
|
||||
bobChannelLink := newMockChannelLink(chanID2, bobChanID, bobPeer)
|
||||
@ -190,9 +188,8 @@ func TestSwitchAddSamePayment(t *testing.T) {
|
||||
|
||||
var request *htlcPacket
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
alicePeer := newMockServer("alice", serverErr)
|
||||
bobPeer := newMockServer("bob", serverErr)
|
||||
alicePeer := newMockServer(t, "alice")
|
||||
bobPeer := newMockServer(t, "bob")
|
||||
|
||||
aliceChannelLink := newMockChannelLink(chanID1, aliceChanID, alicePeer)
|
||||
bobChannelLink := newMockChannelLink(chanID2, bobChanID, bobPeer)
|
||||
@ -290,8 +287,7 @@ func TestSwitchAddSamePayment(t *testing.T) {
|
||||
func TestSwitchSendPayment(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
serverErr := make(chan error, 4)
|
||||
alicePeer := newMockServer("alice", serverErr)
|
||||
alicePeer := newMockServer(t, "alice")
|
||||
aliceChannelLink := newMockChannelLink(chanID1, aliceChanID, alicePeer)
|
||||
|
||||
s := New(Config{})
|
||||
|
@ -673,13 +673,12 @@ func createClusterChannels(aliceToBob, bobToCarol btcutil.Amount) (
|
||||
//
|
||||
func newThreeHopNetwork(t *testing.T, aliceChannel, firstBobChannel,
|
||||
secondBobChannel, carolChannel *lnwallet.LightningChannel,
|
||||
serverErr chan error,
|
||||
startingHeight uint32) *threeHopNetwork {
|
||||
|
||||
// Create three peers/servers.
|
||||
aliceServer := newMockServer("alice", serverErr)
|
||||
bobServer := newMockServer("bob", serverErr)
|
||||
carolServer := newMockServer("carol", serverErr)
|
||||
aliceServer := newMockServer(t, "alice")
|
||||
bobServer := newMockServer(t, "bob")
|
||||
carolServer := newMockServer(t, "carol")
|
||||
|
||||
// Create mock decoder instead of sphinx one in order to mock the
|
||||
// route which htlc should follow.
|
||||
|
Loading…
Reference in New Issue
Block a user