mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
lndclient+test: add versioner client and mock
This commit is contained in:
parent
a8e6118cfb
commit
44d05f284b
4 changed files with 124 additions and 0 deletions
|
|
@ -49,6 +49,7 @@ type LndServices struct {
|
|||
Signer SignerClient
|
||||
Invoices InvoicesClient
|
||||
Router RouterClient
|
||||
Versioner VersionerClient
|
||||
|
||||
ChainParams *chaincfg.Params
|
||||
|
||||
|
|
@ -157,6 +158,7 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) {
|
|||
walletKitClient := newWalletKitClient(conn, macaroons.walletKitMac)
|
||||
invoicesClient := newInvoicesClient(conn, macaroons.invoiceMac)
|
||||
routerClient := newRouterClient(conn, macaroons.routerMac)
|
||||
versionerClient := newVersionerClient(conn, macaroons.readonlyMac)
|
||||
|
||||
cleanup := func() {
|
||||
log.Debugf("Closing lnd connection")
|
||||
|
|
@ -185,6 +187,7 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) {
|
|||
Signer: signerClient,
|
||||
Invoices: invoicesClient,
|
||||
Router: routerClient,
|
||||
Versioner: versionerClient,
|
||||
ChainParams: chainParams,
|
||||
macaroons: macaroons,
|
||||
},
|
||||
|
|
|
|||
68
lndclient/versioner_client.go
Normal file
68
lndclient/versioner_client.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package lndclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// VersionerClient exposes the version of lnd.
|
||||
type VersionerClient interface {
|
||||
// GetVersion returns the version and build information of the lnd
|
||||
// daemon.
|
||||
GetVersion(ctx context.Context) (*verrpc.Version, error)
|
||||
}
|
||||
|
||||
type versionerClient struct {
|
||||
client verrpc.VersionerClient
|
||||
readonlyMac serializedMacaroon
|
||||
}
|
||||
|
||||
func newVersionerClient(conn *grpc.ClientConn,
|
||||
readonlyMac serializedMacaroon) *versionerClient {
|
||||
|
||||
return &versionerClient{
|
||||
client: verrpc.NewVersionerClient(conn),
|
||||
readonlyMac: readonlyMac,
|
||||
}
|
||||
}
|
||||
|
||||
// GetVersion returns the version and build information of the lnd
|
||||
// daemon.
|
||||
//
|
||||
// NOTE: This method is part of the VersionerClient interface.
|
||||
func (v *versionerClient) GetVersion(ctx context.Context) (*verrpc.Version,
|
||||
error) {
|
||||
|
||||
rpcCtx, cancel := context.WithTimeout(
|
||||
v.readonlyMac.WithMacaroonAuth(ctx), rpcTimeout,
|
||||
)
|
||||
defer cancel()
|
||||
return v.client.GetVersion(rpcCtx, &verrpc.VersionRequest{})
|
||||
}
|
||||
|
||||
// VersionString returns a nice, human readable string of a version returned by
|
||||
// the VersionerClient, including all build tags.
|
||||
func VersionString(version *verrpc.Version) string {
|
||||
short := VersionStringShort(version)
|
||||
enabledTags := strings.Join(version.BuildTags, ",")
|
||||
return fmt.Sprintf("%s, build tags '%s'", short, enabledTags)
|
||||
}
|
||||
|
||||
// VersionStringShort returns a nice, human readable string of a version
|
||||
// returned by the VersionerClient.
|
||||
func VersionStringShort(version *verrpc.Version) string {
|
||||
versionStr := fmt.Sprintf(
|
||||
"v%d.%d.%d", version.AppMajor, version.AppMinor,
|
||||
version.AppPatch,
|
||||
)
|
||||
if version.AppPreRelease != "" {
|
||||
versionStr = fmt.Sprintf(
|
||||
"%s-%s", versionStr, version.AppPreRelease,
|
||||
)
|
||||
}
|
||||
return versionStr
|
||||
}
|
||||
|
|
@ -34,6 +34,7 @@ func NewMockLnd() *LndMockServices {
|
|||
signer := &mockSigner{}
|
||||
invoices := &mockInvoices{}
|
||||
router := &mockRouter{}
|
||||
versioner := newMockVersioner()
|
||||
|
||||
lnd := LndMockServices{
|
||||
LndServices: lndclient.LndServices{
|
||||
|
|
@ -44,6 +45,7 @@ func NewMockLnd() *LndMockServices {
|
|||
Invoices: invoices,
|
||||
Router: router,
|
||||
ChainParams: &chaincfg.TestNet3Params,
|
||||
Versioner: versioner,
|
||||
},
|
||||
SendPaymentChannel: make(chan PaymentChannelMessage),
|
||||
ConfChannel: make(chan *chainntnfs.TxConfirmation),
|
||||
|
|
|
|||
51
test/versioner_mock.go
Normal file
51
test/versioner_mock.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/lightninglabs/loop/lndclient"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMockCommit = "v0.99.9-beta"
|
||||
defaultMockCommitHash = "0000000000000000000000000000000000000000"
|
||||
defaultMockVersion = "v0.99.9-beta"
|
||||
defaultMockAppMajor = 0
|
||||
defaultMockAppMinor = 99
|
||||
defaultMockAppPatch = 9
|
||||
defaultMockAppPrerelease = "beta"
|
||||
defaultMockAppGoVersion = "go1.99.9"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultMockBuildTags = []string{
|
||||
"signrpc", "walletrpc", "chainrpc", "invoicesrpc",
|
||||
}
|
||||
)
|
||||
|
||||
type mockVersioner struct {
|
||||
version *verrpc.Version
|
||||
}
|
||||
|
||||
var _ lndclient.VersionerClient = (*mockVersioner)(nil)
|
||||
|
||||
func newMockVersioner() *mockVersioner {
|
||||
return &mockVersioner{
|
||||
version: &verrpc.Version{
|
||||
Commit: defaultMockCommit,
|
||||
CommitHash: defaultMockCommitHash,
|
||||
Version: defaultMockVersion,
|
||||
AppMajor: defaultMockAppMajor,
|
||||
AppMinor: defaultMockAppMinor,
|
||||
AppPatch: defaultMockAppPatch,
|
||||
AppPreRelease: defaultMockAppPrerelease,
|
||||
BuildTags: defaultMockBuildTags,
|
||||
GoVersion: defaultMockAppGoVersion,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (v *mockVersioner) GetVersion(_ context.Context) (*verrpc.Version, error) {
|
||||
return v.version, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue