From d22286cbbc4185dadb6aaaec24945731b5f8fa2c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 18 Jun 2020 09:43:37 +0300 Subject: [PATCH] client/cli: add network option to the RPC client It doesn't affect anything yet, but it's going to be used in the future for network-specific behavior. It also renames short '--timeout' form to '-s' avoiding conlict with '-t' used for '--testnet'. --- cli/options/options.go | 13 ++++++------- pkg/rpc/client/client.go | 2 ++ pkg/rpc/client/doc_test.go | 3 ++- pkg/rpc/client/rpc_test.go | 5 +++-- pkg/rpc/client/wsclient_test.go | 19 ++++++++++--------- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cli/options/options.go b/cli/options/options.go index 568106e63..19420db0f 100644 --- a/cli/options/options.go +++ b/cli/options/options.go @@ -22,11 +22,7 @@ const RPCEndpointFlag = "rpc-endpoint" // Network is a set of flags for choosing the network to operate on // (privnet/mainnet/testnet). -var Network = []cli.Flag{ - cli.BoolFlag{Name: "privnet, p"}, - cli.BoolFlag{Name: "mainnet, m"}, - cli.BoolFlag{Name: "testnet, t"}, -} +var Network = RPC[2:] // RPC is a set of flags used for RPC connections (endpoint and timeout). var RPC = []cli.Flag{ @@ -35,9 +31,12 @@ var RPC = []cli.Flag{ Usage: "RPC node address", }, cli.DurationFlag{ - Name: "timeout, t", + Name: "timeout, s", Usage: "Timeout for the operation (10 seconds by default)", }, + cli.BoolFlag{Name: "privnet, p"}, + cli.BoolFlag{Name: "mainnet, m"}, + cli.BoolFlag{Name: "testnet, t"}, } var errNoEndpoint = errors.New("no RPC endpoint specified, use option '--" + RPCEndpointFlag + "' or '-r'") @@ -70,7 +69,7 @@ func GetRPCClient(gctx context.Context, ctx *cli.Context) (*client.Client, cli.E if len(endpoint) == 0 { return nil, cli.NewExitError(errNoEndpoint, 1) } - c, err := client.New(gctx, endpoint, client.Options{}) + c, err := client.New(gctx, endpoint, client.Options{Network: GetNetwork(ctx)}) if err != nil { return nil, cli.NewExitError(err, 1) } diff --git a/pkg/rpc/client/client.go b/pkg/rpc/client/client.go index 1f2c1cad2..a5110b29c 100644 --- a/pkg/rpc/client/client.go +++ b/pkg/rpc/client/client.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/rpc/request" "github.com/nspcc-dev/neo-go/pkg/rpc/response" @@ -49,6 +50,7 @@ type Options struct { CACert string DialTimeout time.Duration RequestTimeout time.Duration + Network netmode.Magic } // cache stores cache values for the RPC client methods diff --git a/pkg/rpc/client/doc_test.go b/pkg/rpc/client/doc_test.go index 496ca8d4d..e3db66617 100644 --- a/pkg/rpc/client/doc_test.go +++ b/pkg/rpc/client/doc_test.go @@ -5,13 +5,14 @@ import ( "fmt" "os" + "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/rpc/client" ) func Example() { endpoint := "http://seed5.bridgeprotocol.io:10332" - opts := client.Options{} + opts := client.Options{Network: netmode.MainNet} c, err := client.New(context.TODO(), endpoint, opts) if err != nil { diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index e7102c8bf..b0bb369cc 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -1112,7 +1113,7 @@ func testRPCClient(t *testing.T, newClient func(context.Context, string, Options defer srv.Close() endpoint := srv.URL - opts := Options{} + opts := Options{Network: netmode.UnitTestNet} c, err := newClient(context.TODO(), endpoint, opts) if err != nil { t.Fatal(err) @@ -1136,7 +1137,7 @@ func testRPCClient(t *testing.T, newClient func(context.Context, string, Options defer srv.Close() endpoint := srv.URL - opts := Options{} + opts := Options{Network: netmode.UnitTestNet} c, err := newClient(context.TODO(), endpoint, opts) if err != nil { t.Fatal(err) diff --git a/pkg/rpc/client/wsclient_test.go b/pkg/rpc/client/wsclient_test.go index 95094d7af..a69c09cdd 100644 --- a/pkg/rpc/client/wsclient_test.go +++ b/pkg/rpc/client/wsclient_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/gorilla/websocket" + "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/rpc/request" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/stretchr/testify/require" @@ -17,7 +18,7 @@ import ( func TestWSClientClose(t *testing.T) { srv := initTestServer(t, "") defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) wsc.Close() } @@ -42,7 +43,7 @@ func TestWSClientSubscription(t *testing.T) { t.Run(name, func(t *testing.T) { srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`) defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) id, err := f(wsc) require.NoError(t, err) @@ -55,7 +56,7 @@ func TestWSClientSubscription(t *testing.T) { t.Run(name, func(t *testing.T) { srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`) defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) _, err = f(wsc) require.Error(t, err) @@ -104,7 +105,7 @@ func TestWSClientUnsubscription(t *testing.T) { t.Run(name, func(t *testing.T) { srv := initTestServer(t, rc.response) defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) rc.code(t, wsc) }) @@ -138,7 +139,7 @@ func TestWSClientEvents(t *testing.T) { } })) - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) for range events { select { @@ -161,7 +162,7 @@ func TestWSExecutionVMStateCheck(t *testing.T) { // Will answer successfully if request slips through. srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`) defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) filter := "NONE" _, err = wsc.SubscribeForTransactionExecutions(&filter) @@ -291,7 +292,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { } })) defer srv.Close() - wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + wsc, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) c.clientCode(t, wsc) wsc.Close() @@ -304,11 +305,11 @@ func TestNewWS(t *testing.T) { defer srv.Close() t.Run("good", func(t *testing.T) { - _, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{}) + _, err := NewWS(context.TODO(), httpURLtoWS(srv.URL), Options{Network: netmode.UnitTestNet}) require.NoError(t, err) }) t.Run("bad URL", func(t *testing.T) { - _, err := NewWS(context.TODO(), strings.Trim(srv.URL, "http://"), Options{}) + _, err := NewWS(context.TODO(), strings.Trim(srv.URL, "http://"), Options{Network: netmode.UnitTestNet}) require.Error(t, err) }) }