rpc: move client-related code to a separate package
This includes Client struct with RPC methods and BalanceGetter implementation with NeoSCAN.
This commit is contained in:
parent
b6bc4e580a
commit
d24c6d1d9e
6 changed files with 86 additions and 80 deletions
|
@ -14,7 +14,7 @@ import (
|
|||
"github.com/CityOfZion/neo-go/pkg/compiler"
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||
"github.com/CityOfZion/neo-go/pkg/rpc"
|
||||
"github.com/CityOfZion/neo-go/pkg/rpc/client"
|
||||
"github.com/CityOfZion/neo-go/pkg/rpc/request"
|
||||
"github.com/CityOfZion/neo-go/pkg/rpc/response"
|
||||
"github.com/CityOfZion/neo-go/pkg/smartcontract"
|
||||
|
@ -400,15 +400,15 @@ func invokeInternal(ctx *cli.Context, withMethod bool, signAndPush bool) error {
|
|||
return err
|
||||
}
|
||||
}
|
||||
client, err := rpc.NewClient(context.TODO(), endpoint, rpc.ClientOptions{})
|
||||
c, err := client.New(context.TODO(), endpoint, client.Options{})
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
||||
if withMethod {
|
||||
resp, err = client.InvokeFunction(script, operation, params)
|
||||
resp, err = c.InvokeFunction(script, operation, params)
|
||||
} else {
|
||||
resp, err = client.Invoke(script, params)
|
||||
resp, err = c.Invoke(script, params)
|
||||
}
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
|
@ -421,7 +421,7 @@ func invokeInternal(ctx *cli.Context, withMethod bool, signAndPush bool) error {
|
|||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("bad script returned from the RPC node: %v", err), 1)
|
||||
}
|
||||
txHash, err := client.SignAndPushInvocationTx(script, wif, gas)
|
||||
txHash, err := c.SignAndPushInvocationTx(script, wif, gas)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1)
|
||||
}
|
||||
|
@ -453,13 +453,13 @@ func testInvokeScript(ctx *cli.Context) error {
|
|||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
||||
client, err := rpc.NewClient(context.TODO(), endpoint, rpc.ClientOptions{})
|
||||
c, err := client.New(context.TODO(), endpoint, client.Options{})
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
||||
scriptHex := hex.EncodeToString(b)
|
||||
resp, err := client.InvokeScript(scriptHex)
|
||||
resp, err := c.InvokeScript(scriptHex)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -573,7 +573,7 @@ func contractDeploy(ctx *cli.Context) error {
|
|||
return cli.NewExitError(fmt.Errorf("bad config: %v", err), 1)
|
||||
}
|
||||
|
||||
client, err := rpc.NewClient(context.TODO(), endpoint, rpc.ClientOptions{})
|
||||
c, err := client.New(context.TODO(), endpoint, client.Options{})
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -583,7 +583,7 @@ func contractDeploy(ctx *cli.Context) error {
|
|||
return cli.NewExitError(fmt.Errorf("failed to create deployment script: %v", err), 1)
|
||||
}
|
||||
|
||||
txHash, err := client.SignAndPushInvocationTx(txScript, wif, gas)
|
||||
txHash, err := c.SignAndPushInvocationTx(txScript, wif, gas)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %v", err), 1)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package rpc
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -41,10 +41,10 @@ type Client struct {
|
|||
balancer request.BalanceGetter
|
||||
}
|
||||
|
||||
// ClientOptions defines options for the RPC client.
|
||||
// Options defines options for the RPC client.
|
||||
// All Values are optional. If any duration is not specified
|
||||
// a default of 3 seconds will be used.
|
||||
type ClientOptions struct {
|
||||
type Options struct {
|
||||
Cert string
|
||||
Key string
|
||||
CACert string
|
||||
|
@ -56,8 +56,8 @@ type ClientOptions struct {
|
|||
Version string
|
||||
}
|
||||
|
||||
// NewClient returns a new Client ready to use.
|
||||
func NewClient(ctx context.Context, endpoint string, opts ClientOptions) (*Client, error) {
|
||||
// New returns a new Client ready to use.
|
||||
func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
||||
url, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
69
pkg/rpc/client/doc.go
Normal file
69
pkg/rpc/client/doc.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Package client implements NEO-specific JSON-RPC 2.0 client.
|
||||
This package is currently in alpha and is subject to change.
|
||||
|
||||
Client
|
||||
|
||||
After creating a client instance with or without a ClientConfig
|
||||
you can interact with the NEO blockchain by its exposed methods.
|
||||
|
||||
Some of the methods also allow to pass a verbose bool. This will
|
||||
return a more pretty printed response from the server instead of
|
||||
a raw hex string.
|
||||
|
||||
An example:
|
||||
endpoint := "http://seed5.bridgeprotocol.io:10332"
|
||||
opts := client.Options{}
|
||||
|
||||
c, err := client.New(context.TODO(), endpoint, opts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := c.Ping(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := c.GetAccountState("ATySFJAbLW7QHsZGHScLhxq6EyNBxx3eFP")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println(resp.Result.ScriptHash)
|
||||
log.Println(resp.Result.Balances)
|
||||
|
||||
TODO:
|
||||
Merge structs so can be used by both server and client.
|
||||
Add missing methods to client.
|
||||
Allow client to connect using client cert.
|
||||
More in-depth examples.
|
||||
|
||||
Supported methods
|
||||
|
||||
getblock
|
||||
getaccountstate
|
||||
getunspents
|
||||
invokescript
|
||||
invokefunction
|
||||
sendrawtransaction
|
||||
invoke
|
||||
getrawtransaction
|
||||
|
||||
Unsupported methods
|
||||
|
||||
validateaddress
|
||||
getblocksysfee
|
||||
getcontractstate
|
||||
getrawmempool
|
||||
getstorage
|
||||
submitblock
|
||||
gettxout
|
||||
getassetstate
|
||||
getpeers
|
||||
getversion
|
||||
getconnectioncount
|
||||
getblockhash
|
||||
getblockcount
|
||||
getbestblockhash
|
||||
|
||||
*/
|
||||
package client
|
|
@ -1,4 +1,4 @@
|
|||
package rpc
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
|
@ -1,4 +1,4 @@
|
|||
package rpc
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
|
@ -1,70 +1,7 @@
|
|||
/*
|
||||
Package rpc implements NEO-specific JSON-RPC 2.0 client and server.
|
||||
Package rpc implements NEO-specific JSON-RPC 2.0 server.
|
||||
This package is currently in alpha and is subject to change.
|
||||
|
||||
Client
|
||||
|
||||
After creating a client instance with or without a ClientConfig
|
||||
you can interact with the NEO blockchain by its exposed methods.
|
||||
|
||||
Some of the methods also allow to pass a verbose bool. This will
|
||||
return a more pretty printed response from the server instead of
|
||||
a raw hex string.
|
||||
|
||||
An example:
|
||||
endpoint := "http://seed5.bridgeprotocol.io:10332"
|
||||
opts := rpc.ClientOptions{}
|
||||
|
||||
client, err := rpc.NewClient(context.TODO(), endpoint, opts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := client.Ping(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := client.GetAccountState("ATySFJAbLW7QHsZGHScLhxq6EyNBxx3eFP")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println(resp.Result.ScriptHash)
|
||||
log.Println(resp.Result.Balances)
|
||||
|
||||
TODO:
|
||||
Merge structs so can be used by both server and client.
|
||||
Add missing methods to client.
|
||||
Allow client to connect using client cert.
|
||||
More in-depth examples.
|
||||
|
||||
Supported methods
|
||||
|
||||
getblock
|
||||
getaccountstate
|
||||
getunspents
|
||||
invokescript
|
||||
invokefunction
|
||||
sendrawtransaction
|
||||
invoke
|
||||
getrawtransaction
|
||||
|
||||
Unsupported methods
|
||||
|
||||
validateaddress
|
||||
getblocksysfee
|
||||
getcontractstate
|
||||
getrawmempool
|
||||
getstorage
|
||||
submitblock
|
||||
gettxout
|
||||
getassetstate
|
||||
getpeers
|
||||
getversion
|
||||
getconnectioncount
|
||||
getblockhash
|
||||
getblockcount
|
||||
getbestblockhash
|
||||
|
||||
Server
|
||||
|
||||
The server is written to support as much of the JSON-RPC 2.0 Spec as possible.
|
||||
|
|
Loading…
Reference in a new issue