-
Notifications
You must be signed in to change notification settings - Fork 18
/
lndclient_mock.go
137 lines (106 loc) · 3.15 KB
/
lndclient_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/routing/route"
)
var mockIdentity = route.Vertex{1, 2, 3}
var testChannels = map[uint64]*channel{
2: {peer: route.Vertex{2}},
3: {peer: route.Vertex{3}, initiator: true},
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,
}
type lndclientMock struct {
htlcEvents chan *resolvedEvent
htlcInterceptorRequests chan *interceptedEvent
htlcInterceptorResponses chan *interceptResponse
channels map[uint64]*channel
closedChannels map[uint64]*channel
}
func newLndclientMock(channels, closedChannels map[uint64]*channel) *lndclientMock {
return &lndclientMock{
htlcEvents: make(chan *resolvedEvent),
htlcInterceptorRequests: make(chan *interceptedEvent),
htlcInterceptorResponses: make(chan *interceptResponse),
channels: channels,
closedChannels: closedChannels,
}
}
func (l *lndclientMock) getInfo() (*info, error) {
return &info{
nodeKey: mockIdentity,
}, nil
}
func (l *lndclientMock) listChannels() (map[uint64]*channel, error) {
return l.channels, nil
}
func (l *lndclientMock) listClosedChannels() (map[uint64]*channel, error) {
return l.closedChannels, nil
}
func (l *lndclientMock) subscribeHtlcEvents(ctx context.Context) (
htlcEventsClient, error) {
return &htlcEventsMock{
ctx: ctx,
htlcEvents: l.htlcEvents,
}, nil
}
func (l *lndclientMock) htlcInterceptor(ctx context.Context) (
htlcInterceptorClient, error) {
return &htlcInterceptorMock{
ctx: ctx,
htlcInterceptorRequests: l.htlcInterceptorRequests,
htlcInterceptorResponses: l.htlcInterceptorResponses,
}, nil
}
func (l *lndclientMock) getNodeAlias(key route.Vertex) (string, error) {
return "alias-" + key.String()[:6], nil
}
func (l *lndclientMock) getPendingIncomingHtlcs(ctx context.Context, peer *route.Vertex) (
map[route.Vertex]map[circuitKey]*inFlightHtlc, error) {
htlcs := make(map[route.Vertex]map[circuitKey]*inFlightHtlc)
for _, ch := range l.channels {
htlcs[ch.peer] = make(map[circuitKey]*inFlightHtlc)
}
return htlcs, nil
}
type htlcEventsMock struct {
ctx context.Context //nolint:containedctx
routerrpc.Router_SubscribeHtlcEventsClient
htlcEvents chan *resolvedEvent
}
func (h *htlcEventsMock) recv() (*resolvedEvent, error) {
select {
case event := <-h.htlcEvents:
return event, nil
case <-h.ctx.Done():
return nil, h.ctx.Err()
}
}
type htlcInterceptorMock struct {
ctx context.Context //nolint:containedctx
routerrpc.Router_HtlcInterceptorClient
htlcInterceptorRequests chan *interceptedEvent
htlcInterceptorResponses chan *interceptResponse
}
func (h *htlcInterceptorMock) send(resp *interceptResponse) error {
select {
case h.htlcInterceptorResponses <- resp:
return nil
case <-h.ctx.Done():
return h.ctx.Err()
}
}
func (h *htlcInterceptorMock) recv() (*interceptedEvent, error) {
select {
case event := <-h.htlcInterceptorRequests:
return event, nil
case <-h.ctx.Done():
return nil, h.ctx.Err()
}
}