Skip to content

Commit

Permalink
Use new method to create gRPC connections
Browse files Browse the repository at this point in the history
  • Loading branch information
aftermath2 committed May 18, 2024
1 parent b8e6cf3 commit 05033e6
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 29 deletions.
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log/slog"
"net"
"os"
"time"

"github.com/aftermath2/acceptlnd/policy"

Expand All @@ -15,7 +14,6 @@ import (
// Config is acceptLND's configuration schema.
type Config struct {
RPCAddress string `yaml:"rpc_address,omitempty"`
RPCTimeout *time.Duration `yaml:"rpc_timeout,omitempty"`
CertificatePath string `yaml:"certificate_path,omitempty"`
MacaroonPath string `yaml:"macaroon_path,omitempty"`
Policies []*policy.Policy `yaml:"policies,omitempty"`
Expand Down
3 changes: 0 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"testing"
"time"

"github.com/aftermath2/acceptlnd/policy"

Expand Down Expand Up @@ -51,7 +50,6 @@ func TestLoad(t *testing.T) {

func TestValidate(t *testing.T) {
tru := true
timeout := 40 * time.Second

cases := []struct {
desc string
Expand All @@ -62,7 +60,6 @@ func TestValidate(t *testing.T) {
desc: "Valid",
config: Config{
RPCAddress: "127.0.0.1:10001",
RPCTimeout: &timeout,
CertificatePath: "./testdata/tls.mock",
MacaroonPath: "./testdata/acceptlnd.mock",
Policies: []*policy.Policy{
Expand Down
1 change: 0 additions & 1 deletion config/testdata/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
rpc_address: 127.0.0.1:10001
rpc_timeout: 120s
certificate_path: ./testdata/tls.mock
macaroon_path: ./testdata/acceptlnd.mock
policies:
Expand Down
2 changes: 0 additions & 2 deletions examples/lnd.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
rpc_address: 127.0.0.1:10001
certificate_path: tls.cert
macaroon_path: acceptlnd.macaroon
rpc_timeout: 1m30s

--

rpc_address: 127.0.0.1:10001
certificate_path: /home/username/tls.cert
macaroon_path: /home/username/acceptlnd.macaroon
rpc_timeout: 90s
25 changes: 4 additions & 21 deletions lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"log/slog"
"os"
"time"

"github.com/aftermath2/acceptlnd/config"

Expand All @@ -32,23 +31,8 @@ func NewClient(config config.Config) (Client, error) {
return nil, errors.Wrap(err, "loading GRPC options")
}

if config.RPCTimeout == nil {
defaultTimeout := 60 * time.Second
config.RPCTimeout = &defaultTimeout
}

ctx, cancel := context.WithTimeout(context.Background(), *config.RPCTimeout)
defer cancel()

if *config.RPCTimeout == 0 {
ctx = context.Background()
}

slog.Info("Connecting to LND",
slog.String("address", config.RPCAddress),
slog.String("timeout", config.RPCTimeout.String()),
)
conn, err := grpc.DialContext(ctx, config.RPCAddress, opts...)
slog.Info("Connecting to LND", slog.String("address", config.RPCAddress))
conn, err := grpc.NewClient(config.RPCAddress, opts...)
if err != nil {
return nil, err
}
Expand All @@ -72,14 +56,13 @@ func loadGRPCOpts(config config.Config) ([]grpc.DialOption, error) {
return nil, errors.Wrap(err, "unmarshaling macaroon")
}

macaroon, err := macaroons.NewMacaroonCredential(mac)
macCred, err := macaroons.NewMacaroonCredential(mac)
if err != nil {
return nil, errors.Wrap(err, "creating macaroon credential")
}

return []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTransportCredentials(tlsCert),
grpc.WithPerRPCCredentials(macaroon),
grpc.WithPerRPCCredentials(macCred),
}, nil
}

0 comments on commit 05033e6

Please sign in to comment.