add rpc.discover (openrpc)

This commit is contained in:
Luke Williams 2019-09-14 19:39:24 +02:00
parent a6903c886a
commit 2a31f979cb
8 changed files with 2026 additions and 15 deletions

View file

@ -35,9 +35,11 @@ import (
"github.com/ubiq/go-ubiq/eth" "github.com/ubiq/go-ubiq/eth"
"github.com/ubiq/go-ubiq/ethclient" "github.com/ubiq/go-ubiq/ethclient"
"github.com/ubiq/go-ubiq/internal/debug" "github.com/ubiq/go-ubiq/internal/debug"
"github.com/ubiq/go-ubiq/internal/openrpc"
"github.com/ubiq/go-ubiq/log" "github.com/ubiq/go-ubiq/log"
"github.com/ubiq/go-ubiq/metrics" "github.com/ubiq/go-ubiq/metrics"
"github.com/ubiq/go-ubiq/node" "github.com/ubiq/go-ubiq/node"
"github.com/ubiq/go-ubiq/rpc"
cli "gopkg.in/urfave/cli.v1" cli "gopkg.in/urfave/cli.v1"
) )
@ -248,6 +250,10 @@ func init() {
console.Stdin.Close() // Resets terminal mode. console.Stdin.Close() // Resets terminal mode.
return nil return nil
} }
if err := rpc.SetDefaultOpenRPCSchemaRaw(openrpc.OpenRPCSchema); err != nil {
log.Crit("Setting OpenRPC default", "error", err)
}
} }
func main() { func main() {

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,14 @@
package openrpc_test
import (
"testing"
"github.com/ubiq/go-ubiq/internal/openrpc"
"github.com/ubiq/go-ubiq/rpc"
)
func TestDefaultSchema(t *testing.T) {
if err := rpc.SetDefaultOpenRPCSchemaRaw(openrpc.OpenRPCSchema); err != nil {
t.Fatal(err)
}
}

View file

@ -22,7 +22,7 @@ var Modules = map[string]string{
"admin": Admin_JS, "admin": Admin_JS,
"chequebook": Chequebook_JS, "chequebook": Chequebook_JS,
"clique": Clique_JS, "clique": Clique_JS,
"ubqhash": Ubqhash_JS, "ubqhash": Ubqhash_JS,
"debug": Debug_JS, "debug": Debug_JS,
"eth": Eth_JS, "eth": Eth_JS,
"miner": Miner_JS, "miner": Miner_JS,
@ -625,7 +625,13 @@ web3._extend({
const RPC_JS = ` const RPC_JS = `
web3._extend({ web3._extend({
property: 'rpc', property: 'rpc',
methods: [], methods: [
new web3._extend.Method({
name: 'discover',
call: 'rpc.discover',
params: 0
}),
],
properties: [ properties: [
new web3._extend.Property({ new web3._extend.Property({
name: 'modules', name: 'modules',

View file

@ -31,7 +31,6 @@ import (
const ( const (
vsn = "2.0" vsn = "2.0"
serviceMethodSeparator = "_"
subscribeMethodSuffix = "_subscribe" subscribeMethodSuffix = "_subscribe"
unsubscribeMethodSuffix = "_unsubscribe" unsubscribeMethodSuffix = "_unsubscribe"
notificationMethodSuffix = "_subscription" notificationMethodSuffix = "_subscription"
@ -39,7 +38,20 @@ const (
defaultWriteTimeout = 10 * time.Second // used if context has no deadline defaultWriteTimeout = 10 * time.Second // used if context has no deadline
) )
var null = json.RawMessage("null") var (
null = json.RawMessage("null")
serviceMethodSeparators = []string{"_", "."}
errInvalidMethodName = errors.New("invalid method name")
)
func elementizeMethodName(methodName string) (module, method string, err error) {
for _, sep := range serviceMethodSeparators {
if s := strings.SplitN(methodName, sep, 2); len(s) == 2 {
return s[0], s[1], nil
}
}
return "", "", errInvalidMethodName
}
type subscriptionResult struct { type subscriptionResult struct {
ID string `json:"subscription"` ID string `json:"subscription"`
@ -82,8 +94,8 @@ func (msg *jsonrpcMessage) isUnsubscribe() bool {
} }
func (msg *jsonrpcMessage) namespace() string { func (msg *jsonrpcMessage) namespace() string {
elem := strings.SplitN(msg.Method, serviceMethodSeparator, 2) module, _, _ := elementizeMethodName(msg.Method)
return elem[0] return module // even if err != nil, empty string is returned so err can be ignored
} }
func (msg *jsonrpcMessage) String() string { func (msg *jsonrpcMessage) String() string {

25
rpc/openrpc.go Normal file
View file

@ -0,0 +1,25 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package rpc
type OpenRPCDiscoverSchemaT struct {
OpenRPC string `json:"openrpc"`
Info map[string]interface{} `json:"info"`
Servers []map[string]interface{} `json:"servers"`
Methods []map[string]interface{} `json:"methods"`
Components map[string]interface{} `json:"components"`
}

View file

@ -18,6 +18,9 @@ package rpc
import ( import (
"context" "context"
"encoding/json"
"errors"
"fmt"
"io" "io"
"sync/atomic" "sync/atomic"
@ -27,6 +30,16 @@ import (
const MetadataApi = "rpc" const MetadataApi = "rpc"
var (
// defaultOpenRPCSchemaRaw can be used to establish a default (package-wide) OpenRPC schema from raw JSON.
// Methods will be cross referenced with actual registered method names in order to serve
// only server-enabled methods, enabling user and on-the-fly server endpoint availability configuration.
defaultOpenRPCSchemaRaw string
errOpenRPCDiscoverUnavailable = errors.New("openrpc discover data unavailable")
errOpenRPCDiscoverSchemaInvalid = errors.New("openrpc discover data invalid")
)
// CodecOption specifies which type of messages a codec supports. // CodecOption specifies which type of messages a codec supports.
// //
// Deprecated: this option is no longer honored by Server. // Deprecated: this option is no longer honored by Server.
@ -42,15 +55,21 @@ const (
// Server is an RPC server. // Server is an RPC server.
type Server struct { type Server struct {
services serviceRegistry services serviceRegistry
idgen func() ID idgen func() ID
run int32 run int32
codecs mapset.Set codecs mapset.Set
OpenRPCSchemaRaw string
} }
// NewServer creates a new server instance with no registered handlers. // NewServer creates a new server instance with no registered handlers.
func NewServer() *Server { func NewServer() *Server {
server := &Server{idgen: randomIDGenerator(), codecs: mapset.NewSet(), run: 1} server := &Server{
idgen: randomIDGenerator(),
codecs: mapset.NewSet(),
run: 1,
OpenRPCSchemaRaw: defaultOpenRPCSchemaRaw,
}
// Register the default service providing meta information about the RPC service such // Register the default service providing meta information about the RPC service such
// as the services and methods it offers. // as the services and methods it offers.
rpcService := &RPCService{server} rpcService := &RPCService{server}
@ -58,6 +77,35 @@ func NewServer() *Server {
return server return server
} }
func validateOpenRPCSchemaRaw(schemaJSON string) error {
if schemaJSON == "" {
return errOpenRPCDiscoverSchemaInvalid
}
var schema OpenRPCDiscoverSchemaT
if err := json.Unmarshal([]byte(schemaJSON), &schema); err != nil {
return fmt.Errorf("%v: %v", errOpenRPCDiscoverSchemaInvalid, err)
}
return nil
}
// SetDefaultOpenRPCSchemaRaw validates and sets the package-wide OpenRPC schema data.
func SetDefaultOpenRPCSchemaRaw(schemaJSON string) error {
if err := validateOpenRPCSchemaRaw(schemaJSON); err != nil {
return err
}
defaultOpenRPCSchemaRaw = schemaJSON
return nil
}
// SetOpenRPCSchemaRaw validates and sets the raw OpenRPC schema data for a server.
func (s *Server) SetOpenRPCSchemaRaw(schemaJSON string) error {
if err := validateOpenRPCSchemaRaw(schemaJSON); err != nil {
return err
}
s.OpenRPCSchemaRaw = schemaJSON
return nil
}
// RegisterName creates a service for the given receiver type under the given name. When no // RegisterName creates a service for the given receiver type under the given name. When no
// methods on the given receiver match the criteria to be either a RPC method or a // methods on the given receiver match the criteria to be either a RPC method or a
// subscription an error is returned. Otherwise a new service is created and added to the // subscription an error is returned. Otherwise a new service is created and added to the
@ -145,3 +193,66 @@ func (s *RPCService) Modules() map[string]string {
} }
return modules return modules
} }
func (s *RPCService) methods() map[string][]string {
s.server.services.mu.Lock()
defer s.server.services.mu.Unlock()
methods := make(map[string][]string)
for name, ser := range s.server.services.services {
for s := range ser.callbacks {
_, ok := methods[name]
if !ok {
methods[name] = []string{s}
} else {
methods[name] = append(methods[name], s)
}
}
}
return methods
}
// Discover returns a configured schema that is audited for actual server availability.
// Only methods that the server makes available are included in the 'methods' array of
// the discover schema. Components are not audited.
func (s *RPCService) Discover() (schema *OpenRPCDiscoverSchemaT, err error) {
if s.server.OpenRPCSchemaRaw == "" {
return nil, errOpenRPCDiscoverUnavailable
}
schema = &OpenRPCDiscoverSchemaT{
Servers: make([]map[string]interface{}, 0),
}
err = json.Unmarshal([]byte(s.server.OpenRPCSchemaRaw), schema)
if err != nil {
log.Crit("openrpc json umarshal", "error", err)
}
// Audit documented schema methods vs. actual server availability
// This removes methods described in the OpenRPC JSON schema document
// which are not currently exposed on the server's API.
// This is done on the fly (as opposed to at servre init or schema setting)
// because it's possible that exposed APIs could be modified in proc.
schemaMethodsAvailable := []map[string]interface{}{}
serverMethodsAvailable := s.methods()
for _, m := range schema.Methods {
module, path, err := elementizeMethodName(m["name"].(string))
if err != nil {
return nil, err
}
paths, ok := serverMethodsAvailable[module]
if !ok {
continue
}
// the module exists, does the path exist?
for _, pa := range paths {
if pa == path {
schemaMethodsAvailable = append(schemaMethodsAvailable, m)
break
}
}
}
schema.Methods = schemaMethodsAvailable
return
}

View file

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"runtime" "runtime"
"strings"
"sync" "sync"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
@ -95,13 +94,13 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {
// callback returns the callback corresponding to the given RPC method name. // callback returns the callback corresponding to the given RPC method name.
func (r *serviceRegistry) callback(method string) *callback { func (r *serviceRegistry) callback(method string) *callback {
elem := strings.SplitN(method, serviceMethodSeparator, 2) module, mthd, err := elementizeMethodName(method)
if len(elem) != 2 { if err != nil {
return nil return nil
} }
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock() defer r.mu.Unlock()
return r.services[elem[0]].callbacks[elem[1]] return r.services[module].callbacks[mthd]
} }
// subscription returns a subscription callback in the given service. // subscription returns a subscription callback in the given service.