mirror of https://github.com/lightninglabs/loop
Merge pull request #181 from guggero/lnd-version-aware
Make loop aware of lnd's versionpull/167/head
commit
e99202ced5
@ -0,0 +1,158 @@
|
||||
package lndclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type mockVersioner struct {
|
||||
version *verrpc.Version
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *mockVersioner) GetVersion(_ context.Context) (*verrpc.Version, error) {
|
||||
return m.version, m.err
|
||||
}
|
||||
|
||||
// TestCheckVersionCompatibility makes sure the correct error is returned if an
|
||||
// old lnd is connected that doesn't implement the version RPC, has an older
|
||||
// version or if an lnd with not all subservers enabled is connected.
|
||||
func TestCheckVersionCompatibility(t *testing.T) {
|
||||
// Make sure a version check against a node that doesn't implement the
|
||||
// version RPC always fails.
|
||||
unimplemented := &mockVersioner{
|
||||
err: status.Error(codes.Unimplemented, "missing"),
|
||||
}
|
||||
_, err := checkVersionCompatibility(unimplemented, &verrpc.Version{
|
||||
AppMajor: 0,
|
||||
AppMinor: 10,
|
||||
AppPatch: 0,
|
||||
})
|
||||
if err != ErrVersionCheckNotImplemented {
|
||||
t.Fatalf("unexpected error. got '%v' wanted '%v'", err,
|
||||
ErrVersionCheckNotImplemented)
|
||||
}
|
||||
|
||||
// Next, make sure an older version than what we want is rejected.
|
||||
oldVersion := &mockVersioner{
|
||||
version: &verrpc.Version{
|
||||
AppMajor: 0,
|
||||
AppMinor: 10,
|
||||
AppPatch: 0,
|
||||
},
|
||||
}
|
||||
_, err = checkVersionCompatibility(oldVersion, &verrpc.Version{
|
||||
AppMajor: 0,
|
||||
AppMinor: 11,
|
||||
AppPatch: 0,
|
||||
})
|
||||
if err != ErrVersionIncompatible {
|
||||
t.Fatalf("unexpected error. got '%v' wanted '%v'", err,
|
||||
ErrVersionIncompatible)
|
||||
}
|
||||
|
||||
// Finally, make sure we also get the correct error when trying to run
|
||||
// against an lnd that doesn't have all required build tags enabled.
|
||||
buildTagsMissing := &mockVersioner{
|
||||
version: &verrpc.Version{
|
||||
AppMajor: 0,
|
||||
AppMinor: 10,
|
||||
AppPatch: 0,
|
||||
BuildTags: []string{"dev", "lntest", "btcd", "signrpc"},
|
||||
},
|
||||
}
|
||||
_, err = checkVersionCompatibility(buildTagsMissing, &verrpc.Version{
|
||||
AppMajor: 0,
|
||||
AppMinor: 10,
|
||||
AppPatch: 0,
|
||||
BuildTags: []string{"signrpc", "walletrpc"},
|
||||
})
|
||||
if err != ErrBuildTagsMissing {
|
||||
t.Fatalf("unexpected error. got '%v' wanted '%v'", err,
|
||||
ErrVersionIncompatible)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLndVersionCheckComparison makes sure the version check comparison works
|
||||
// correctly and considers all three version levels.
|
||||
func TestLndVersionCheckComparison(t *testing.T) {
|
||||
actual := &verrpc.Version{
|
||||
AppMajor: 1,
|
||||
AppMinor: 2,
|
||||
AppPatch: 3,
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectMajor uint32
|
||||
expectMinor uint32
|
||||
expectPatch uint32
|
||||
actual *verrpc.Version
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "no expectation",
|
||||
expectMajor: 0,
|
||||
expectMinor: 0,
|
||||
expectPatch: 0,
|
||||
actual: actual,
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "expect exact same version",
|
||||
expectMajor: 1,
|
||||
expectMinor: 2,
|
||||
expectPatch: 3,
|
||||
actual: actual,
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "ignore patch if minor is bigger",
|
||||
expectMajor: 12,
|
||||
expectMinor: 9,
|
||||
expectPatch: 14,
|
||||
actual: &verrpc.Version{
|
||||
AppMajor: 12,
|
||||
AppMinor: 22,
|
||||
AppPatch: 0,
|
||||
},
|
||||
expectedErr: nil,
|
||||
},
|
||||
{
|
||||
name: "all fields different",
|
||||
expectMajor: 3,
|
||||
expectMinor: 2,
|
||||
expectPatch: 1,
|
||||
actual: actual,
|
||||
expectedErr: ErrVersionIncompatible,
|
||||
},
|
||||
{
|
||||
name: "patch version different",
|
||||
expectMajor: 1,
|
||||
expectMinor: 2,
|
||||
expectPatch: 4,
|
||||
actual: actual,
|
||||
expectedErr: ErrVersionIncompatible,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := assertVersionCompatible(
|
||||
tc.actual, &verrpc.Version{
|
||||
AppMajor: tc.expectMajor,
|
||||
AppMinor: tc.expectMinor,
|
||||
AppPatch: tc.expectPatch,
|
||||
},
|
||||
)
|
||||
if err != tc.expectedErr {
|
||||
t.Fatalf("unexpected error, got '%v' wanted "+
|
||||
"'%v'", err, tc.expectedErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
@ -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…
Reference in New Issue