-
Notifications
You must be signed in to change notification settings - Fork 32
/
faucet.go
758 lines (645 loc) · 22.9 KB
/
faucet.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
macaroon "gopkg.in/macaroon.v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
// maxChannelSize is the larget channel that the faucet will create to
// another peer.
maxChannelSize int64 = (1 << 24)
// minChannelSize is the smallest channel that the faucet will extend
// to a peer.
minChannelSize int64 = 50000
)
var (
lndHomeDir = btcutil.AppDataDir("lnd", false)
defaultTLSCertFilename = "tls.cert"
tlsCertPath = filepath.Join(lndHomeDir, defaultTLSCertFilename)
defaultMacaroonFilename = "admin.macaroon"
defaultMacaroonPath = filepath.Join(
lndHomeDir, "data", "chain", "bitcoin", "testnet",
defaultMacaroonFilename,
)
)
// chanCreationError is an enum which describes the exact nature of an error
// encountered when a user attempts to create a channel with the faucet. This
// enum is used within the templates to determine at which input item the error
// occurred and also to generate an error string unique to the error.
type chanCreationError uint8
const (
// NoError is the default error which indicates either the form hasn't
// yet been submitted or no errors have arisen.
NoError chanCreationError = iota
// InvalidAddress indicates that the passed node address is invalid.
InvalidAddress
// NotConnected indicates that the target peer isn't connected to the
// faucet.
NotConnected
// ChanAmountNotNumber indicates that the amount specified for the
// amount to fund the channel with isn't actually a number.
ChanAmountNotNumber
// ChannelTooLarge indicates that the amounts specified to fund the
// channel with is greater than maxChannelSize.
ChannelTooLarge
// ChannelTooSmall indicates that the channel size required is below
// minChannelSize.
ChannelTooSmall
// PushIncorrect indicates that the amount specified to push to the
// other end of the channel is greater-than-or-equal-to the local
// funding amount.
PushIncorrect
// ChannelOpenFail indicates some error occurred when attempting to
// open a channel with the target peer.
ChannelOpenFail
// HaveChannel indicates that the faucet already has a channel open
// with the target node.
HaveChannel
)
// String returns a human readable string describing the chanCreationError.
// This string is used in the templates in order to display the error to the
// user.
func (c chanCreationError) String() string {
switch c {
case NoError:
return ""
case InvalidAddress:
return "node address is invalid"
case NotConnected:
return "not connected to node"
case ChanAmountNotNumber:
return "amount must be int"
case ChannelTooLarge:
return "channel value is too large"
case ChannelTooSmall:
return fmt.Sprintf("min channel size is is %x sat",
minChannelSize)
case PushIncorrect:
return "push amount is incorrect"
case ChannelOpenFail:
return "unable to open channel"
case HaveChannel:
return "node has channel with faucet already"
default:
return fmt.Sprintf("%v", uint8(c))
}
}
// lightningFaucet is a Bitcoin Channel Faucet. The faucet itself is a web app
// that is capable of programmatically opening channels with users with the
// size of the channel parametrized by the user. The faucet required a
// connection to a local lnd node in order to operate properly. The faucet
// implements the constrains on the channel size, and also will only open a
// single channel to a particular node. Finally, the faucet will periodically
// close channels based on their age as the faucet will only open up 100
// channels total at any given time.
type lightningFaucet struct {
lnd lnrpc.LightningClient
btcdClient *rpcclient.Client
templates *template.Template
network string
openChanMtx sync.RWMutex
openChannels map[wire.OutPoint]time.Time
}
// newLightningFaucet creates a new channel faucet that's bound to a cluster of
// lnd nodes, and uses the passed templates to render the web page.
func newLightningFaucet(lndHost string,
templates *template.Template, network string) (*lightningFaucet, error) {
// First attempt to establish a connection to lnd's RPC sever.
creds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
if err != nil {
return nil, fmt.Errorf("unable to read cert file: %v", err)
}
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
// Load the specified macaroon file.
macPath := cleanAndExpandPath(defaultMacaroonPath)
macBytes, err := ioutil.ReadFile(macPath)
if err != nil {
return nil, err
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(macBytes); err != nil {
return nil, err
}
// Now we append the macaroon credentials to the dial options.
opts = append(
opts,
grpc.WithPerRPCCredentials(macaroons.NewMacaroonCredential(mac)),
)
conn, err := grpc.Dial(*lndNodes, opts...)
if err != nil {
return nil, fmt.Errorf("unable to dial to lnd's gRPC server: ",
err)
}
// If we're able to connect out to the lnd node, then we can start up
// the faucet safely.
lnd := lnrpc.NewLightningClient(conn)
// As we may need to grab fee estimates, we'll also establish a
// connection to the running btcd node so we can query it and wrap its
// responses.
var btcdRpcCert []byte
btcdCertFile, err := os.Open(defaultBtcdRPCCertFile)
if err != nil {
return nil, err
}
btcdRpcCert, err = ioutil.ReadAll(btcdCertFile)
if err != nil {
return nil, err
}
btcdCertFile.Close()
config := &rpcclient.ConnConfig{
Host: "localhost:18334",
Endpoint: "ws",
User: *btcdRpcUser,
Pass: *btcdRpcPass,
Certificates: btcdRpcCert,
}
rpcClient, err := rpcclient.New(config, nil)
if err != nil {
return nil, err
}
return &lightningFaucet{
btcdClient: rpcClient,
lnd: lnd,
templates: templates,
network: network,
}, nil
}
// Start launches all the goroutines necessary for routine operation of the
// lightning faucet.
func (l *lightningFaucet) Start() error {
//go l.zombieChanSweeper()
return nil
}
// zombieChanSweeper is a goroutine that is tasked with cleaning up "zombie"
// channels. A zombie channel is a channel in which the peer we have the
// channel open with hasn't been online for greater than 48 hours. We'll
// periodically perform a sweep every hour to close out any lingering zombie
// channels.
//
// NOTE: This MUST be run as a goroutine.
func (l *lightningFaucet) zombieChanSweeper() {
fmt.Println("zombie chan sweeper active")
// Any channel peer that hasn't been online in more than 48 hours past
// from now will have their channels closed out.
timeCutOff := time.Now().Add(-time.Hour * 48)
// Upon initial boot, we'll do a scan to close out any channels that
// are now considered zombies while we were down.
l.sweepZombieChans(timeCutOff)
// Every hour we'll consume a new tick and perform a sweep to close out
// any zombies channels.
zombieTicker := time.NewTicker(time.Hour * 1)
for _ = range zombieTicker.C {
log.Println("Performing zombie channel sweep!")
// In order to ensure we close out the proper channels, we also
// calculate the 48 hour offset from the point of our next
// tick.
timeCutOff = time.Now().Add(-time.Hour * 48)
// With the time cut off calculated, we'll force close any
// channels that are now considered "zombies".
l.sweepZombieChans(timeCutOff)
}
}
// strPointToChanPoint concerts a string outpoint (txid:index) into an lnrpc
// ChannelPoint object.
func strPointToChanPoint(stringPoint string) (*lnrpc.ChannelPoint, error) {
s := strings.Split(stringPoint, ":")
txid, err := chainhash.NewHashFromStr(s[0])
if err != nil {
return nil, err
}
index, err := strconv.Atoi(s[1])
if err != nil {
return nil, err
}
return &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
txid[:],
},
OutputIndex: uint32(index),
}, nil
}
// sweepZombieChans performs a sweep of the set of channels that the faucet has
// active to close out any channels that are now considered to be a "zombie". A
// channel is a zombie if the peer with have the channel open is currently
// offline, and we haven't detected them as being online since timeCutOff.
//
// TODO(roasbeef): after removing the node ANN on startup, will need to rely on
// LinkNode information.
func (l *lightningFaucet) sweepZombieChans(timeCutOff time.Time) {
// Fetch all the facuet's currently open channels.
openChanReq := &lnrpc.ListChannelsRequest{}
openChannels, err := l.lnd.ListChannels(ctxb, openChanReq)
if err != nil {
log.Println("unable to fetch open channels: %v", err)
return
}
for _, channel := range openChannels.Channels {
// For each channel we'll first fetch the announcement
// information for the peer that we have the channel open with.
nodeInfoResp, err := l.lnd.GetNodeInfo(ctxb,
&lnrpc.NodeInfoRequest{
PubKey: channel.RemotePubkey,
})
if err != nil {
log.Println("unable to get node pubkey: %v", err)
continue
}
// Convert the unix time stamp into a time.Time object.
lastSeen := time.Unix(int64(nodeInfoResp.Node.LastUpdate), 0)
// If the last time we saw this peer online was _before_ our
// time cutoff, and the peer isn't currently online, then we'll
// force close out the channel.
if lastSeen.Before(timeCutOff) && !channel.Active {
fmt.Println("ChannelPoint(%v) is a zombie, last seen: %v",
channel.ChannelPoint, lastSeen)
chanPoint, err := strPointToChanPoint(channel.ChannelPoint)
if err != nil {
log.Println("unable to get chan point: %v", err)
continue
}
txid, err := l.closeChannel(chanPoint, true)
if err != nil {
fmt.Println("unable to close zombie chan: %v", err)
continue
}
fmt.Println("closed zombie chan, txid: %v", txid)
}
}
}
// closeChannel closes out a target channel optionally executing a force close.
// This function will block until the closing transaction has been broadcast.
func (l *lightningFaucet) closeChannel(chanPoint *lnrpc.ChannelPoint,
force bool) (*chainhash.Hash, error) {
closeReq := &lnrpc.CloseChannelRequest{
ChannelPoint: chanPoint,
Force: force,
}
stream, err := l.lnd.CloseChannel(ctxb, closeReq)
if err != nil {
fmt.Println("unable to start channel close: %v", err)
}
// Consume the first response which'll be sent once the closing
// transaction has been broadcast.
resp, err := stream.Recv()
if err != nil {
return nil, fmt.Errorf("unable to close chan: %v", err)
}
update, ok := resp.Update.(*lnrpc.CloseStatusUpdate_ClosePending)
if !ok {
return nil, fmt.Errorf("didn't get a pending update")
}
// Convert the raw bytes into a new chainhash so we gain access to its
// utility methods.
closingHash := update.ClosePending.Txid
return chainhash.NewHash(closingHash)
}
// homePageContext defines the initial context required for rendering home
// page. The home page displays some basic statistics, errors in the case of an
// invalid channel submission, and finally a splash page upon successful
// creation of a channel.
type homePageContext struct {
// NumCoins is the number of coins in BTC that the faucet has available
// for channel creation.
NumCoins float64
// NumChannels is the number of active channels that the faucet has
// open at a given time.
NumChannels uint32
// GitCommitHash is the git HEAD's commit hash of
// $GOPATH/src/github.com/lightningnetwork/lnd
GitCommitHash string
// NodeAddr is the full <pubkey>@host:port where the faucet can be
// connect to.
NodeAddr string
// SubmissionError is a enum that stores if any error took place during
// the creation of a channel.
SubmissionError chanCreationError
// ChannelTxid is the txid of the created funding channel. If this
// field is an empty string, then that indicates the channel hasn't yet
// been created.
ChannelTxid string
// NumConfs is the number of confirmations required for the channel to
// open up.
NumConfs uint32
// Network is the network the faucet is running on.
Network string
}
// fetchHomeState is helper functions that populates the homePageContext with
// the latest state from the local lnd node.
func (l *lightningFaucet) fetchHomeState() (*homePageContext, error) {
// First query for the general information from the lnd node, this'll
// be used to populate the number of active channel as well as the
// identity of the node.
infoReq := &lnrpc.GetInfoRequest{}
nodeInfo, err := l.lnd.GetInfo(ctxb, infoReq)
if err != nil {
return nil, err
}
// Next obtain the wallet's available balance which indicates how much
// we can allocate towards channels.
balReq := &lnrpc.WalletBalanceRequest{}
walletBalance, err := l.lnd.WalletBalance(ctxb, balReq)
if err != nil {
return nil, err
}
cmd := exec.Command("git", "log", "--pretty=format:'%H'", "-n", "1")
cmd.Dir = os.Getenv("GOPATH") + "/src/github.com/lightningnetwork/lnd"
gitHash, err := cmd.Output()
if err != nil {
gitHash = []byte{}
}
nodeAddr := fmt.Sprintf("%v@%v", nodeInfo.IdentityPubkey, *lndIP)
return &homePageContext{
NumCoins: btcutil.Amount(walletBalance.ConfirmedBalance).ToBTC(),
NumChannels: nodeInfo.NumActiveChannels,
GitCommitHash: strings.Replace(string(gitHash), "'", "", -1),
NodeAddr: nodeAddr,
NumConfs: 3,
Network: l.network,
}, nil
}
// faucetHome renders the main home page for the faucet. This includes the form
// to create channels, the network statistics, and the splash page upon channel
// success.
//
// NOTE: This method implements the http.Handler interface.
func (l *lightningFaucet) faucetHome(w http.ResponseWriter, r *http.Request) {
// First obtain the home template from our cache of pre-compiled
// templates.
homeTemplate := l.templates.Lookup("index.html")
// In order to render the home template we'll need the necessary
// context, so we'll grab that from the lnd daemon now in order to get
// the most up to date state.
homeInfo, err := l.fetchHomeState()
if err != nil {
log.Println("unable to fetch home state: ", err)
homeTemplate.Execute(w, nil)
return
}
// If the method is GET, then we'll render the home page with the form
// itself.
switch {
case r.Method == http.MethodGet:
if err := homeTemplate.Execute(w, homeInfo); err != nil {
log.Println("unable to render home page: %v", err)
}
// Otherwise, if the method is POST, then the user is submitting the
// form to open a channel, so we'll pass that off to the openChannel
// handler.
case r.Method == http.MethodPost:
l.openChannel(homeTemplate, homeInfo, w, r)
// If the method isn't either of those, then this is an error as we
// only support the two methods above.
default:
http.Error(w, "", http.StatusMethodNotAllowed)
}
return
}
// channelExistsWithNode return true if the faucet already has a channel open
// with the target node, and false otherwise.
func (l *lightningFaucet) channelExistsWithNode(nodePub string) bool {
listChanReq := &lnrpc.ListChannelsRequest{}
resp, err := l.lnd.ListChannels(ctxb, listChanReq)
if err != nil {
return false
}
for _, channel := range resp.Channels {
if channel.RemotePubkey == nodePub {
return true
}
}
return false
}
// connectedToNode returns true if the faucet is connected to the node, and
// false otherwise.
func (l *lightningFaucet) connectedToNode(nodePub string) bool {
peersReq := &lnrpc.ListPeersRequest{}
resp, err := l.lnd.ListPeers(ctxb, peersReq)
if err != nil {
return false
}
for _, peer := range resp.Peers {
if peer.PubKey == nodePub {
return true
}
}
return false
}
// openChannel is a hybrid http.Handler that handles: the validation of the
// channel creation form, rendering errors to the form, and finally creating
// channels if all the parameters check out.
func (l *lightningFaucet) openChannel(homeTemplate *template.Template,
homeState *homePageContext, w http.ResponseWriter, r *http.Request) {
// Before we can obtain the values the user entered in the form, we
// need to parse all parameters. First attempt to establish a
// connection with the
if err := r.ParseForm(); err != nil {
http.Error(w, "unable to parse form", 500)
return
}
nodePubStr := r.FormValue("node")
// With the forms details parsed, extract out the public key of the
// target peer.
nodePub, err := hex.DecodeString(nodePubStr)
if err != nil {
homeState.SubmissionError = InvalidAddress
homeTemplate.Execute(w, homeState)
return
}
// If we already have a channel with this peer, then we'll fail the
// request as we have a policy of only one channel per node.
if l.channelExistsWithNode(nodePubStr) {
homeState.SubmissionError = HaveChannel
homeTemplate.Execute(w, homeState)
}
// If we're not connected to the node, then we won't be able to extend
// a channel to them. So we'll exit early with an error here.
if !l.connectedToNode(nodePubStr) {
homeState.SubmissionError = NotConnected
homeTemplate.Execute(w, homeState)
return
}
// With the connection established (or already present) with the target
// peer, we'll now parse out the rest of the fields, performing
// validation and exiting early if any field is invalid.
chanSize, err := strconv.ParseInt(r.FormValue("amt"), 10, 64)
if err != nil {
homeState.SubmissionError = ChanAmountNotNumber
homeTemplate.Execute(w, homeState)
return
}
pushAmt, err := strconv.ParseInt(r.FormValue("bal"), 10, 64)
if err != nil {
homeState.SubmissionError = PushIncorrect
homeTemplate.Execute(w, homeState)
return
}
// With the initial validation complete, we'll now ensure the channel
// size and push amt meet our constraints.
switch {
// The target channel can't be below the constant min channel size.
case chanSize < minChannelSize:
homeState.SubmissionError = ChannelTooSmall
homeTemplate.Execute(w, homeState)
return
// The target channel can't be above the max channel size.
case chanSize > maxChannelSize:
homeState.SubmissionError = ChannelTooLarge
homeTemplate.Execute(w, homeState)
return
// The amount pushed to the other side as part of the channel creation
// MUST be less than the size of the channel itself.
case pushAmt >= chanSize:
homeState.SubmissionError = PushIncorrect
homeTemplate.Execute(w, homeState)
return
}
// If we were able to connect to the peer successfully, and all the
// parameters check out, then we'll parse out the remaining channel
// parameters and initiate the funding workflow.
openChanReq := &lnrpc.OpenChannelRequest{
NodePubkey: nodePub,
LocalFundingAmount: chanSize,
PushSat: pushAmt,
}
log.Printf("attempting to create channel with params: %v",
spew.Sdump(openChanReq))
openChanStream, err := l.lnd.OpenChannel(ctxb, openChanReq)
if err != nil {
http.Error(w, "unable to open channel", 500)
return
}
// Consume the first update from the open channel stream which
// indicates that the channel has been broadcast to the network.
chanUpdate, err := openChanStream.Recv()
if err != nil {
http.Error(w, "unable to open channel", 500)
return
}
pendingUpdate := chanUpdate.Update.(*lnrpc.OpenStatusUpdate_ChanPending).ChanPending
fundingTXID, _ := chainhash.NewHash(pendingUpdate.Txid)
log.Printf("channel created with txid: %v", fundingTXID)
homeState.ChannelTxid = fundingTXID.String()
if err := homeTemplate.Execute(w, homeState); err != nil {
log.Printf("unable to render home page: %v", err)
}
}
// activeChannels returns a page describing all the currently active channels
// the faucet is maintaining.
func (l *lightningFaucet) activeChannels(w http.ResponseWriter, r *http.Request) {
openChanReq := &lnrpc.ListChannelsRequest{}
openChannels, err := l.lnd.ListChannels(ctxb, openChanReq)
if err != nil {
http.Error(w, "unable to fetch channels", 500)
return
}
l.templates.Lookup("active.html").Execute(w, openChannels)
}
// estimateFee...
func (l *lightningFaucet) estimateFee(w http.ResponseWriter, r *http.Request) {
// If the user didn't specify a conf target, then we'll fall back to
// the default of 3.
confTarget := int64(3)
var err error
queryConfTarget, ok := r.URL.Query()["numBlocks"]
if ok && len(queryConfTarget) > 0 {
target, err := strconv.ParseInt(queryConfTarget[0], 10, 32)
if err != nil {
http.Error(w, "incorrect value for numBlocks passed", 500)
return
}
confTarget = target
}
// First, we'll fetch the estimate for our confirmation target.
btcPerKB, err := l.btcdClient.EstimateFee(confTarget)
if err != nil {
log.Printf("unable to estimate fee: %v", err)
http.Error(w, "unable to estimate fee", 500)
return
}
// Next, we'll convert the returned value to satoshis, as it's
// currently returned in BTC.
satPerKB, err := btcutil.NewAmount(btcPerKB)
if err != nil {
log.Printf("unable to convert fee: %v", err)
http.Error(w, "unable to estimate fee", 500)
return
}
// Now that we have our response, we can craft a JSON blob to send back
// to the end user.
type resp struct {
FeePerKb int64 `json:"feePerKb"`
}
jsonBlob, err := json.Marshal(&resp{int64(satPerKB)})
if err != nil {
log.Printf("unable to encode fee: %v", err)
http.Error(w, "unable to estimate fee", 500)
return
}
fmt.Println("blob resp: ", string(jsonBlob))
w.Header().Set("Content-Type", "application/json")
w.Write(jsonBlob)
}
// CloseAllChannels attempt unconditionally close ALL of the faucet's currently
// open channels. In the case that a channel is active a cooperative closure
// will be executed, in the case that a channel is inactive, a force close will
// be attempted.
func (l *lightningFaucet) CloseAllChannels() error {
openChanReq := &lnrpc.ListChannelsRequest{}
openChannels, err := l.lnd.ListChannels(ctxb, openChanReq)
if err != nil {
return fmt.Errorf("unable to fetch open channels: %v", err)
}
for _, channel := range openChannels.Channels {
fmt.Println("Attempting to close channel: %c", channel.ChannelPoint)
chanPoint, err := strPointToChanPoint(channel.ChannelPoint)
if err != nil {
log.Println("unable to get chan point: %v", err)
continue
}
forceClose := !channel.Active
if forceClose {
log.Println("Attempting force close")
}
closeTxid, err := l.closeChannel(chanPoint, forceClose)
if err != nil {
log.Println("unable to close channel: %v", err)
continue
}
log.Println("closing txid: ", closeTxid)
}
return nil
}
// cleanAndExpandPath expands environment variables and leading ~ in the passed
// path, cleans the result, and returns it.
// This function is taken from https://github.com/btcsuite/btcd
func cleanAndExpandPath(path string) string {
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(lndHomeDir)
path = strings.Replace(path, "~", homeDir, 1)
}
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
// but the variables can still be expanded via POSIX-style $VARIABLE.
return filepath.Clean(os.ExpandEnv(path))
}