2020-10-13 16:56:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var mockIdentity = route.Vertex{1, 2, 3}
|
|
|
|
|
|
2023-02-03 12:46:00 +01:00
|
|
|
var testChannels = map[uint64]*channel{
|
|
|
|
|
2: {peer: route.Vertex{2}},
|
|
|
|
|
3: {peer: route.Vertex{3}, initiator: true},
|
2023-09-14 15:09:03 -04:00
|
|
|
4: {peer: route.Vertex{4}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// outgoingKey is an outgoing cirucit key for a channel that is contained in our set of open
|
|
|
|
|
// test channels.
|
|
|
|
|
var outgoingKey = circuitKey{
|
|
|
|
|
channel: 4,
|
|
|
|
|
htlc: 3,
|
2023-02-03 12:46:00 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-13 16:56:51 +02:00
|
|
|
type lndclientMock struct {
|
2023-01-03 13:01:26 +01:00
|
|
|
htlcEvents chan *resolvedEvent
|
|
|
|
|
htlcInterceptorRequests chan *interceptedEvent
|
|
|
|
|
htlcInterceptorResponses chan *interceptResponse
|
2023-02-03 12:46:00 +01:00
|
|
|
|
2023-10-02 13:50:25 -04:00
|
|
|
channels map[uint64]*channel
|
|
|
|
|
closedChannels map[uint64]*channel
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-02 13:50:25 -04:00
|
|
|
func newLndclientMock(channels, closedChannels map[uint64]*channel) *lndclientMock {
|
2020-10-13 16:56:51 +02:00
|
|
|
return &lndclientMock{
|
2023-01-03 13:01:26 +01:00
|
|
|
htlcEvents: make(chan *resolvedEvent),
|
|
|
|
|
htlcInterceptorRequests: make(chan *interceptedEvent),
|
|
|
|
|
htlcInterceptorResponses: make(chan *interceptResponse),
|
2023-02-03 12:46:00 +01:00
|
|
|
|
2023-10-02 13:50:25 -04:00
|
|
|
channels: channels,
|
|
|
|
|
closedChannels: closedChannels,
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:01:26 +01:00
|
|
|
func (l *lndclientMock) getInfo() (*info, error) {
|
|
|
|
|
return &info{
|
|
|
|
|
nodeKey: mockIdentity,
|
|
|
|
|
}, nil
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-02 08:58:21 +01:00
|
|
|
func (l *lndclientMock) listChannels() (map[uint64]*channel, error) {
|
2023-02-03 12:46:00 +01:00
|
|
|
return l.channels, nil
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-11 12:53:37 -04:00
|
|
|
func (l *lndclientMock) listClosedChannels() (map[uint64]*channel, error) {
|
2023-10-02 13:50:25 -04:00
|
|
|
return l.closedChannels, nil
|
2023-10-11 12:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
func (l *lndclientMock) subscribeHtlcEvents(ctx context.Context) (
|
|
|
|
|
htlcEventsClient, error) {
|
2020-10-13 16:56:51 +02:00
|
|
|
|
|
|
|
|
return &htlcEventsMock{
|
2022-11-29 11:42:18 +01:00
|
|
|
ctx: ctx,
|
2020-10-13 16:56:51 +02:00
|
|
|
htlcEvents: l.htlcEvents,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *lndclientMock) htlcInterceptor(ctx context.Context) (
|
2023-01-03 13:01:26 +01:00
|
|
|
htlcInterceptorClient, error) {
|
2020-10-13 16:56:51 +02:00
|
|
|
|
|
|
|
|
return &htlcInterceptorMock{
|
2022-11-29 11:42:18 +01:00
|
|
|
ctx: ctx,
|
2020-10-13 16:56:51 +02:00
|
|
|
htlcInterceptorRequests: l.htlcInterceptorRequests,
|
|
|
|
|
htlcInterceptorResponses: l.htlcInterceptorResponses,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *lndclientMock) getNodeAlias(key route.Vertex) (string, error) {
|
|
|
|
|
return "alias-" + key.String()[:6], nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
func (l *lndclientMock) getPendingIncomingHtlcs(ctx context.Context, peer *route.Vertex) (
|
2023-07-21 10:17:43 -04:00
|
|
|
map[route.Vertex]map[circuitKey]*inFlightHtlc, error) {
|
2022-11-29 12:03:36 +01:00
|
|
|
|
2023-07-21 10:17:43 -04:00
|
|
|
htlcs := make(map[route.Vertex]map[circuitKey]*inFlightHtlc)
|
2022-11-29 12:03:36 +01:00
|
|
|
|
2023-02-03 12:46:00 +01:00
|
|
|
for _, ch := range l.channels {
|
2023-07-21 10:17:43 -04:00
|
|
|
htlcs[ch.peer] = make(map[circuitKey]*inFlightHtlc)
|
2023-02-03 12:46:00 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 12:03:36 +01:00
|
|
|
return htlcs, nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-13 16:56:51 +02:00
|
|
|
type htlcEventsMock struct {
|
2022-12-02 12:10:50 +01:00
|
|
|
ctx context.Context //nolint:containedctx
|
2020-10-13 16:56:51 +02:00
|
|
|
routerrpc.Router_SubscribeHtlcEventsClient
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
htlcEvents chan *resolvedEvent
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
func (h *htlcEventsMock) recv() (*resolvedEvent, error) {
|
2022-11-29 11:42:18 +01:00
|
|
|
select {
|
|
|
|
|
case event := <-h.htlcEvents:
|
|
|
|
|
return event, nil
|
|
|
|
|
|
|
|
|
|
case <-h.ctx.Done():
|
|
|
|
|
return nil, h.ctx.Err()
|
|
|
|
|
}
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type htlcInterceptorMock struct {
|
2022-12-02 12:10:50 +01:00
|
|
|
ctx context.Context //nolint:containedctx
|
2020-10-13 16:56:51 +02:00
|
|
|
routerrpc.Router_HtlcInterceptorClient
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
htlcInterceptorRequests chan *interceptedEvent
|
|
|
|
|
htlcInterceptorResponses chan *interceptResponse
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
func (h *htlcInterceptorMock) send(resp *interceptResponse) error {
|
2022-11-29 11:42:18 +01:00
|
|
|
select {
|
|
|
|
|
case h.htlcInterceptorResponses <- resp:
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
case <-h.ctx.Done():
|
|
|
|
|
return h.ctx.Err()
|
|
|
|
|
}
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-03 13:01:26 +01:00
|
|
|
func (h *htlcInterceptorMock) recv() (*interceptedEvent, error) {
|
2022-11-29 11:42:18 +01:00
|
|
|
select {
|
|
|
|
|
case event := <-h.htlcInterceptorRequests:
|
|
|
|
|
return event, nil
|
|
|
|
|
|
|
|
|
|
case <-h.ctx.Done():
|
|
|
|
|
return nil, h.ctx.Err()
|
|
|
|
|
}
|
2020-10-13 16:56:51 +02:00
|
|
|
}
|