forked from TrueCloudLab/neoneo-go
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'.
This commit is contained in:
parent
16ce63e653
commit
d22286cbbc
5 changed files with 23 additions and 19 deletions
|
@ -22,11 +22,7 @@ const RPCEndpointFlag = "rpc-endpoint"
|
||||||
|
|
||||||
// Network is a set of flags for choosing the network to operate on
|
// Network is a set of flags for choosing the network to operate on
|
||||||
// (privnet/mainnet/testnet).
|
// (privnet/mainnet/testnet).
|
||||||
var Network = []cli.Flag{
|
var Network = RPC[2:]
|
||||||
cli.BoolFlag{Name: "privnet, p"},
|
|
||||||
cli.BoolFlag{Name: "mainnet, m"},
|
|
||||||
cli.BoolFlag{Name: "testnet, t"},
|
|
||||||
}
|
|
||||||
|
|
||||||
// RPC is a set of flags used for RPC connections (endpoint and timeout).
|
// RPC is a set of flags used for RPC connections (endpoint and timeout).
|
||||||
var RPC = []cli.Flag{
|
var RPC = []cli.Flag{
|
||||||
|
@ -35,9 +31,12 @@ var RPC = []cli.Flag{
|
||||||
Usage: "RPC node address",
|
Usage: "RPC node address",
|
||||||
},
|
},
|
||||||
cli.DurationFlag{
|
cli.DurationFlag{
|
||||||
Name: "timeout, t",
|
Name: "timeout, s",
|
||||||
Usage: "Timeout for the operation (10 seconds by default)",
|
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'")
|
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 {
|
if len(endpoint) == 0 {
|
||||||
return nil, cli.NewExitError(errNoEndpoint, 1)
|
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 {
|
if err != nil {
|
||||||
return nil, cli.NewExitError(err, 1)
|
return nil, cli.NewExitError(err, 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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/crypto/keys"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
||||||
|
@ -49,6 +50,7 @@ type Options struct {
|
||||||
CACert string
|
CACert string
|
||||||
DialTimeout time.Duration
|
DialTimeout time.Duration
|
||||||
RequestTimeout time.Duration
|
RequestTimeout time.Duration
|
||||||
|
Network netmode.Magic
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache stores cache values for the RPC client methods
|
// cache stores cache values for the RPC client methods
|
||||||
|
|
|
@ -5,13 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"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/encoding/address"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Example() {
|
func Example() {
|
||||||
endpoint := "http://seed5.bridgeprotocol.io:10332"
|
endpoint := "http://seed5.bridgeprotocol.io:10332"
|
||||||
opts := client.Options{}
|
opts := client.Options{Network: netmode.MainNet}
|
||||||
|
|
||||||
c, err := client.New(context.TODO(), endpoint, opts)
|
c, err := client.New(context.TODO(), endpoint, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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/block"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
"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()
|
defer srv.Close()
|
||||||
|
|
||||||
endpoint := srv.URL
|
endpoint := srv.URL
|
||||||
opts := Options{}
|
opts := Options{Network: netmode.UnitTestNet}
|
||||||
c, err := newClient(context.TODO(), endpoint, opts)
|
c, err := newClient(context.TODO(), endpoint, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -1136,7 +1137,7 @@ func testRPCClient(t *testing.T, newClient func(context.Context, string, Options
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
endpoint := srv.URL
|
endpoint := srv.URL
|
||||||
opts := Options{}
|
opts := Options{Network: netmode.UnitTestNet}
|
||||||
c, err := newClient(context.TODO(), endpoint, opts)
|
c, err := newClient(context.TODO(), endpoint, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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/rpc/request"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -17,7 +18,7 @@ import (
|
||||||
func TestWSClientClose(t *testing.T) {
|
func TestWSClientClose(t *testing.T) {
|
||||||
srv := initTestServer(t, "")
|
srv := initTestServer(t, "")
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
wsc.Close()
|
wsc.Close()
|
||||||
}
|
}
|
||||||
|
@ -42,7 +43,7 @@ func TestWSClientSubscription(t *testing.T) {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
id, err := f(wsc)
|
id, err := f(wsc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -55,7 +56,7 @@ func TestWSClientSubscription(t *testing.T) {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`)
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "error":{"code":-32602,"message":"Invalid Params"}}`)
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
_, err = f(wsc)
|
_, err = f(wsc)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
@ -104,7 +105,7 @@ func TestWSClientUnsubscription(t *testing.T) {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
srv := initTestServer(t, rc.response)
|
srv := initTestServer(t, rc.response)
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
rc.code(t, wsc)
|
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)
|
require.NoError(t, err)
|
||||||
for range events {
|
for range events {
|
||||||
select {
|
select {
|
||||||
|
@ -161,7 +162,7 @@ func TestWSExecutionVMStateCheck(t *testing.T) {
|
||||||
// Will answer successfully if request slips through.
|
// Will answer successfully if request slips through.
|
||||||
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
srv := initTestServer(t, `{"jsonrpc": "2.0", "id": 1, "result": "55aaff00"}`)
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
filter := "NONE"
|
filter := "NONE"
|
||||||
_, err = wsc.SubscribeForTransactionExecutions(&filter)
|
_, err = wsc.SubscribeForTransactionExecutions(&filter)
|
||||||
|
@ -291,7 +292,7 @@ func TestWSFilteredSubscriptions(t *testing.T) {
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
defer srv.Close()
|
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)
|
require.NoError(t, err)
|
||||||
c.clientCode(t, wsc)
|
c.clientCode(t, wsc)
|
||||||
wsc.Close()
|
wsc.Close()
|
||||||
|
@ -304,11 +305,11 @@ func TestNewWS(t *testing.T) {
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
t.Run("good", func(t *testing.T) {
|
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)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
t.Run("bad URL", func(t *testing.T) {
|
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)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue