Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: add dcr wallet client. #49

Merged
merged 7 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions client/wallet/dcr/apisemver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.

package dcr

import (
"fmt"

dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v2"
)

var (
// Expected api and daemon versions. Currently placeholders.
dcrd = &dcrdtypes.VersionResult{
VersionString: "1.5.0",
Major: 1,
Minor: 5,
Patch: 0,
}

dcrdjsonrpcapi = &dcrdtypes.VersionResult{
VersionString: "6.1.0",
Major: 6,
Minor: 1,
Patch: 0,
}

dcrwalletjsonrpcapi = &dcrdtypes.VersionResult{
VersionString: "6.2.0",
Major: 6,
Minor: 2,
Patch: 0,
}
)

// checkSemVer asserts the provided semantic version is at least equal to or
// better than the expected version.
func checkSemVer(id string, expected *dcrdtypes.VersionResult, provided *dcrdtypes.VersionResult) error {
if provided.Major < expected.Major {
return fmt.Errorf("%s's major version (%s) is lower than "+
"expected (%v)", id, provided.VersionString,
expected.VersionString)
}

if provided.Major == expected.Major {
if provided.Minor < expected.Minor {
return fmt.Errorf("%s's minor version (%s) is lower than "+
"expected (%v)", id, provided.VersionString,
expected.VersionString)
}

if provided.Minor == expected.Minor {
if provided.Patch < expected.Patch {
return fmt.Errorf("%s's patch version (%s) is lower than "+
"expected (%v)", id, provided.VersionString,
expected.VersionString)
}
}
}

return nil
}

// checkVersionInfo ensures the provided api version info are at least
// the expected or better.
func checkVersionInfo(versionInfo map[string]dcrdtypes.VersionResult, api ...string) error {
for _, id := range api {
semver, ok := versionInfo[id]
if !ok {
return fmt.Errorf("no version info found for %s", id)
}

var expected *dcrdtypes.VersionResult
switch id {
case "dcrd":
expected = dcrd

case "dcrdjsonrpcapi":
expected = dcrdjsonrpcapi

case "dcrwalletjsonrpcapi":
expected = dcrwalletjsonrpcapi

default:
return fmt.Errorf("unknown api id provided: %s", id)
}

err := checkSemVer(id, expected, &semver)
if err != nil {
return err
}
}

return nil
}
216 changes: 216 additions & 0 deletions client/wallet/dcr/apisemver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package dcr

import (
"testing"

dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v2"
)

func TestCheckSemVer(t *testing.T) {
tests := []struct {
expected *dcrdtypes.VersionResult
provided *dcrdtypes.VersionResult
wantErr bool
}{
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
wantErr: false,
},
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "1.5.2",
Major: 1,
Minor: 5,
Patch: 2,
},
wantErr: false,
},
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "1.5.0",
Major: 1,
Minor: 5,
Patch: 0,
},
wantErr: true,
},
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "1.4.1",
Major: 1,
Minor: 4,
Patch: 1,
},
wantErr: true,
},
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "0.4.1",
Major: 0,
Minor: 4,
Patch: 1,
},
wantErr: true,
},
{
expected: &dcrdtypes.VersionResult{
VersionString: "1.5.1",
Major: 1,
Minor: 5,
Patch: 1,
},
provided: &dcrdtypes.VersionResult{
VersionString: "2.0.0",
Major: 2,
Minor: 0,
Patch: 0,
},
wantErr: false,
},
}

for idx, tc := range tests {
err := checkSemVer("dcrd", tc.expected, tc.provided)
if (err != nil) != tc.wantErr {
t.Errorf("[checkSemVer] #%d: error: %v, wantErr: %v",
idx+1, err, tc.wantErr)
}
}
}

func TestCheckVersionInfo(t *testing.T) {
tests := []struct {
info map[string]dcrdtypes.VersionResult
wantErr bool
}{
{
info: map[string]dcrdtypes.VersionResult{
"dcrd": dcrdtypes.VersionResult{
VersionString: "1.5.0",
Major: 1,
Minor: 5,
Patch: 0,
},
"dcrdjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.1.0",
Major: 6,
Minor: 1,
Patch: 0,
},
"dcrwalletjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.2.0",
Major: 6,
Minor: 2,
Patch: 0,
},
},
wantErr: false,
},
{
info: map[string]dcrdtypes.VersionResult{
"wrongid": dcrdtypes.VersionResult{
VersionString: "1.5.0",
Major: 1,
Minor: 5,
Patch: 0,
},
"dcrdjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.1.0",
Major: 6,
Minor: 1,
Patch: 0,
},
"dcrwalletjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.2.0",
Major: 6,
Minor: 2,
Patch: 0,
},
},
wantErr: true,
},
{
info: map[string]dcrdtypes.VersionResult{
"dcrd": dcrdtypes.VersionResult{
VersionString: "1.4.0",
Major: 1,
Minor: 4,
Patch: 0,
},
"dcrdjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.1.0",
Major: 6,
Minor: 1,
Patch: 0,
},
"dcrwalletjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.2.0",
Major: 6,
Minor: 2,
Patch: 0,
},
},
wantErr: true,
},
{
info: map[string]dcrdtypes.VersionResult{
"dcrd": dcrdtypes.VersionResult{
VersionString: "1.5.0",
Major: 1,
Minor: 5,
Patch: 0,
},
"dcrwalletjsonrpcapi": dcrdtypes.VersionResult{
VersionString: "6.2.0",
Major: 6,
Minor: 2,
Patch: 0,
},
},
wantErr: true,
},
}

for idx, tc := range tests {
err := checkVersionInfo(tc.info, "dcrd", "dcrdjsonrpcapi", "dcrwalletjsonrpcapi")
if (err != nil) != tc.wantErr {
t.Errorf("[checkVersionInfo] #%d: error: %v, wantErr: %v",
idx+1, err, tc.wantErr)
}
}
}
Loading