forked from callebtc/electronwall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
173 lines (137 loc) · 4.69 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"errors"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
)
type lndclientMock struct {
htlcEvents chan *routerrpc.HtlcEvent
htlcInterceptorRequests chan *routerrpc.ForwardHtlcInterceptRequest
htlcInterceptorResponses chan *routerrpc.ForwardHtlcInterceptResponse
channelEvents chan *lnrpc.ChannelEventUpdate
channelAcceptorRequests chan *lnrpc.ChannelAcceptRequest
channelAcceptorResponses chan *lnrpc.ChannelAcceptResponse
}
func newLndclientMock() *lndclientMock {
return &lndclientMock{
htlcEvents: make(chan *routerrpc.HtlcEvent),
htlcInterceptorRequests: make(chan *routerrpc.ForwardHtlcInterceptRequest),
htlcInterceptorResponses: make(chan *routerrpc.ForwardHtlcInterceptResponse),
channelAcceptorRequests: make(chan *lnrpc.ChannelAcceptRequest),
channelAcceptorResponses: make(chan *lnrpc.ChannelAcceptResponse),
}
}
// --------------- Channel events mocks ---------------
type channelAcceptorMock struct {
lnrpc.Lightning_ChannelAcceptorClient
channelAcceptorRequests chan *lnrpc.ChannelAcceptRequest
channelAcceptorResponses chan *lnrpc.ChannelAcceptResponse
}
func (lnd *lndclientMock) channelAcceptor(ctx context.Context) (
lnrpc.Lightning_ChannelAcceptorClient, error) {
return &channelAcceptorMock{
channelAcceptorRequests: lnd.channelAcceptorRequests,
channelAcceptorResponses: lnd.channelAcceptorResponses,
}, nil
}
func (c *channelAcceptorMock) RecvMsg(m interface{}) error {
req := <-c.channelAcceptorRequests
*m.(*lnrpc.ChannelAcceptRequest) = *req
return nil
}
func (c *channelAcceptorMock) Send(m *lnrpc.ChannelAcceptResponse) error {
c.channelAcceptorResponses <- m
return nil
}
type channelEventsMock struct {
lnrpc.Lightning_SubscribeChannelEventsClient
channelEvents chan *lnrpc.ChannelEventUpdate
}
func (h *channelEventsMock) Recv() (*lnrpc.ChannelEventUpdate, error) {
event := <-h.channelEvents
return event, nil
}
func (l *lndclientMock) subscribeChannelEvents(ctx context.Context,
in *lnrpc.ChannelEventSubscription) (
lnrpc.Lightning_SubscribeChannelEventsClient, error) {
return &channelEventsMock{
channelEvents: l.channelEvents,
}, nil
}
// --------------- Node info mocks ---------------
// getNodeInfo returns the information of a node given a pubKey
func (lnd *lndclientMock) getNodeInfo(ctx context.Context, pubkey string) (
nodeInfo *lnrpc.NodeInfo, err error) {
info := &lnrpc.NodeInfo{
Node: &lnrpc.LightningNode{
Alias: "alias-" + trimPubKey([]byte(pubkey)),
},
NumChannels: 2,
TotalCapacity: 1234,
}
return info, nil
}
// getNodeAlias returns the alias of a node pubkey
func (lnd *lndclientMock) getNodeAlias(ctx context.Context, pubkey string) (
string, error) {
info, err := lnd.getNodeInfo(ctx, pubkey)
if err != nil {
return "", err
}
if info.Node == nil {
return "", errors.New("node info not available")
}
return info.Node.Alias, nil
}
func (lnd *lndclientMock) getMyInfo(ctx context.Context) (
*lnrpc.GetInfoResponse, error) {
info := &lnrpc.GetInfoResponse{
IdentityPubkey: "my-pubkey-is-very-long-for-trimming-pubkey",
Alias: "my-alias",
}
return info, nil
}
func (lnd *lndclientMock) getPubKeyFromChannel(ctx context.Context, chan_id uint64) (
*lnrpc.ChannelEdge, error) {
return &lnrpc.ChannelEdge{
Node1Pub: "my-pubkey-is-very-long-for-trimming-pubkey",
Node2Pub: "other-pubkey-is-very-long-for-trimming-pubkey",
}, nil
}
// --------------- HTLC events mock ---------------
type htlcEventsMock struct {
routerrpc.Router_SubscribeHtlcEventsClient
htlcEvents chan *routerrpc.HtlcEvent
}
func (h *htlcEventsMock) Recv() (*routerrpc.HtlcEvent, error) {
event := <-h.htlcEvents
return event, nil
}
type htlcInterceptorMock struct {
routerrpc.Router_HtlcInterceptorClient
htlcInterceptorRequests chan *routerrpc.ForwardHtlcInterceptRequest
htlcInterceptorResponses chan *routerrpc.ForwardHtlcInterceptResponse
}
func (h *htlcInterceptorMock) Send(resp *routerrpc.ForwardHtlcInterceptResponse) error {
h.htlcInterceptorResponses <- resp
return nil
}
func (h *htlcInterceptorMock) Recv() (*routerrpc.ForwardHtlcInterceptRequest, error) {
event := <-h.htlcInterceptorRequests
return event, nil
}
func (l *lndclientMock) subscribeHtlcEvents(ctx context.Context,
in *routerrpc.SubscribeHtlcEventsRequest) (
routerrpc.Router_SubscribeHtlcEventsClient, error) {
return &htlcEventsMock{
htlcEvents: l.htlcEvents,
}, nil
}
func (l *lndclientMock) htlcInterceptor(ctx context.Context) (
routerrpc.Router_HtlcInterceptorClient, error) {
return &htlcInterceptorMock{
htlcInterceptorRequests: l.htlcInterceptorRequests,
htlcInterceptorResponses: l.htlcInterceptorResponses,
}, nil
}