-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
312 lines (263 loc) · 8.8 KB
/
api.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package lightningapi
import (
"context"
"fmt"
"math"
entities "github.com/bolt-observer/go_common/entities"
"github.com/getsentry/sentry-go"
"github.com/golang/glog"
)
// APIType enum
type APIType int
// ApiTypes
const (
LndGrpc APIType = iota
LndRest
ClnSocket
)
// GetAPIType from integer
func GetAPIType(t *int) (*APIType, error) {
if t == nil {
return nil, fmt.Errorf("no api type specified")
}
if *t != int(LndGrpc) && *t != int(LndRest) && *t != int(ClnSocket) {
return nil, fmt.Errorf("invalid api type specified")
}
ret := APIType(*t)
return &ret, nil
}
// InfoAPI struct
type InfoAPI struct {
IdentityPubkey string
Alias string
Chain string
Network string
Version string
IsSyncedToGraph bool
IsSyncedToChain bool
}
// ChannelsAPI struct
type ChannelsAPI struct {
Channels []ChannelAPI
}
// ChannelAPI struct
type ChannelAPI struct {
Private bool
Active bool
RemotePubkey string
Initiator bool
CommitFee uint64
ChanID uint64
RemoteBalance uint64
LocalBalance uint64
Capacity uint64
PendingHtlcs []HtlcAPI
TotalSatoshisSent uint64
TotalSatoshisReceived uint64
NumUpdates uint64
}
// HtlcAPI struct
type HtlcAPI struct {
Amount uint64
Incoming bool
ForwardingChannel uint64
ForwardingHtlcIndex uint64
}
// DescribeGraphAPI struct
type DescribeGraphAPI struct {
Nodes []DescribeGraphNodeAPI
Channels []NodeChannelAPI
}
// DescribeGraphNodeAPI struct
type DescribeGraphNodeAPI struct {
PubKey string `json:"pub_key,omitempty"`
Alias string `json:"alias,omitempty"`
Color string `json:"color,omitempty"`
Addresses []NodeAddressAPI `json:"addresses,omitempty"`
Features map[string]NodeFeatureAPI `json:"features,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
}
// NodeAddressAPI struct
type NodeAddressAPI struct {
Network string `json:"network,omitempty"`
Addr string `json:"addr,omitempty"`
}
// NodeFeatureAPI struct
type NodeFeatureAPI struct {
Name string `json:"name,omitempty"`
IsRequired bool `json:"is_required,omitempty"`
IsKnown bool `json:"is_known,omitempty"`
}
// NodeChannelAPI struct
type NodeChannelAPI struct {
ChannelID uint64 `json:"channel_id,omitempty"`
ChanPoint string `json:"chan_point"`
Node1Pub string `json:"node1_pub,omitempty"`
Node2Pub string `json:"node2_pub,omitempty"`
Capacity uint64 `json:"capacity,omitempty"`
Node1Policy *RoutingPolicyAPI `json:"node1_policy,omitempty"`
Node2Policy *RoutingPolicyAPI `json:"node2_policy,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
}
// RoutingPolicyAPI struct
type RoutingPolicyAPI struct {
TimeLockDelta uint32 `json:"time_lock_delta"`
MinHtlc uint64 `json:"min_htlc"`
BaseFee uint64 `json:"fee_base_msat"`
FeeRate uint64 `json:"fee_rate_milli_msat"`
Disabled bool `json:"disabled,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
MaxHtlc uint64 `json:"max_htlc_msat"`
}
// NodeInfoAPI struct
type NodeInfoAPI struct {
Node DescribeGraphNodeAPI `json:"node,omitempty"`
Channels []NodeChannelAPI `json:"channels"`
NumChannels uint32 `json:"num_channels"`
TotalCapacity uint64 `json:"total_capacity"`
}
// NodeChannelAPIExtended struct
type NodeChannelAPIExtended struct {
Private bool `json:"private,omitempty"`
NodeChannelAPI
}
// NodeInfoAPIExtended struct
type NodeInfoAPIExtended struct {
NodeInfoAPI
Channels []NodeChannelAPIExtended `json:"channels"`
}
// LightningAPI - generic API settings
type LightningAPI struct {
GetNodeInfoFullThreshUseDescribeGraph int // If node has more than that number of channels use DescribeGraph else do GetChanInfo for each one
}
// GetNodeInfoFull - GetNodeInfoFull API (GRPC interface)
func (l *LndGrpcLightningAPI) GetNodeInfoFull(ctx context.Context, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
return getNodeInfoFullTemplate(ctx, l, l.GetNodeInfoFullThreshUseDescribeGraph, channels, unnanounced)
}
// GetNodeInfoFull - GetNodeInfoFull API (REST interface)
func (l *LndRestLightningAPI) GetNodeInfoFull(ctx context.Context, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
return getNodeInfoFullTemplate(ctx, l, l.GetNodeInfoFullThreshUseDescribeGraph, channels, unnanounced)
}
// getNodeInfoFullTemplate returns info for local node possibly including unnanounced channels (as soon as that can be obtained via GetNodeInfo this method is useless)
func getNodeInfoFullTemplate(ctx context.Context, l LightingAPICalls, threshUseDescribeGraph int, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
info, err := l.GetInfo(ctx)
if err != nil {
return nil, err
}
nodeInfo, err := l.GetNodeInfo(ctx, info.IdentityPubkey, channels)
if err != nil {
return nil, err
}
extendedNodeInfo := &NodeInfoAPIExtended{NodeInfoAPI: *nodeInfo}
if !unnanounced {
// We have full info already (fast bailout)
capacity := uint64(0)
all := make([]NodeChannelAPIExtended, 0)
for _, ch := range nodeInfo.Channels {
all = append(all, NodeChannelAPIExtended{NodeChannelAPI: ch, Private: false})
capacity += ch.Capacity
}
extendedNodeInfo.Channels = all
extendedNodeInfo.NumChannels = uint32(len(all))
extendedNodeInfo.TotalCapacity = capacity
return extendedNodeInfo, err
}
// Else the channel stats are wrong (unnanounced channels did not count)
chans, err := l.GetChannels(ctx)
if err != nil {
// TODO: Bit of a hack but nodeInfo is pretty much correct
return extendedNodeInfo, err
}
numChans := 0
totalCapacity := uint64(0)
privateMapping := make(map[uint64]bool)
for _, ch := range chans.Channels {
if ch.Private && !unnanounced {
continue
}
privateMapping[ch.ChanID] = ch.Private
totalCapacity += ch.Capacity
numChans++
}
extendedNodeInfo.NumChannels = uint32(numChans)
extendedNodeInfo.TotalCapacity = totalCapacity
if !channels {
return extendedNodeInfo, nil
}
extendedNodeInfo.Channels = make([]NodeChannelAPIExtended, 0)
if len(chans.Channels) <= threshUseDescribeGraph {
for _, ch := range chans.Channels {
if ch.Private && !unnanounced {
continue
}
c, err := l.GetChanInfo(ctx, ch.ChanID)
if err != nil {
glog.Warningf("Could not get channel info for %v: %v", ch.ChanID, err)
extendedNodeInfo.NumChannels--
continue
}
private, ok := privateMapping[ch.ChanID]
extendedNodeInfo.Channels = append(extendedNodeInfo.Channels, NodeChannelAPIExtended{NodeChannelAPI: *c, Private: ok && private})
}
} else {
graph, err := l.DescribeGraph(ctx, unnanounced)
if err != nil {
// This could happen due to too big response (btcpay example with limited nginx), retry with other mode
return getNodeInfoFullTemplate(ctx, l, math.MaxInt, channels, unnanounced)
}
for _, one := range graph.Channels {
if one.Node1Pub != info.IdentityPubkey && one.Node2Pub != info.IdentityPubkey {
continue
}
// No need to filter private channels (since we used unnanounced in DescribeGraph)
private, ok := privateMapping[one.ChannelID]
extendedNodeInfo.Channels = append(extendedNodeInfo.Channels, NodeChannelAPIExtended{NodeChannelAPI: one, Private: ok && private})
}
}
return extendedNodeInfo, nil
}
// LightingAPICalls is the interface for lightning API
type LightingAPICalls interface {
Cleanup()
GetInfo(ctx context.Context) (*InfoAPI, error)
GetChannels(ctx context.Context) (*ChannelsAPI, error)
DescribeGraph(ctx context.Context, unannounced bool) (*DescribeGraphAPI, error)
GetNodeInfoFull(ctx context.Context, channels, unannounced bool) (*NodeInfoAPIExtended, error)
GetNodeInfo(ctx context.Context, pubKey string, channels bool) (*NodeInfoAPI, error)
GetChanInfo(ctx context.Context, chanID uint64) (*NodeChannelAPI, error)
}
// GetDataCall - signature of function for retrieving data
type GetDataCall func() (*entities.Data, error)
// NewAPI - gets new lightning API
func NewAPI(apiType APIType, getData GetDataCall) LightingAPICalls {
if getData == nil {
sentry.CaptureMessage("getData was nil")
return nil
}
data, err := getData()
if err != nil {
sentry.CaptureException(err)
return nil
}
t := LndGrpc
if data.ApiType != nil {
foo, err := GetAPIType(data.ApiType)
if err != nil {
t = LndGrpc
} else {
t = *foo
}
} else {
t = apiType
}
switch t {
case LndGrpc:
return NewLndGrpcLightningAPI(getData)
case LndRest:
return NewLndRestLightningAPI(getData)
case ClnSocket:
return NewClnSocketLightningAPI(getData)
}
sentry.CaptureMessage("Invalid api type")
return nil
}