rpc

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DefaultErrorCode rpc default error code
	DefaultErrorCode = -32000
	// RevertedErrorCode error code for reverted txs
	RevertedErrorCode = 3
	// InvalidRequestErrorCode error code for invalid requests
	InvalidRequestErrorCode = -32600
	// NotFoundErrorCode error code for not found objects
	NotFoundErrorCode = -32601
	// InvalidParamsErrorCode error code for invalid parameters
	InvalidParamsErrorCode = -32602
	// ParserErrorCode error code for parsing errors
	ParserErrorCode = -32700
	// AccessDeniedCode error code when requests are denied
	AccessDeniedCode = -32800
)

Variables

This section is empty.

Functions

func BuildJsonHTTPRequest added in v0.0.4

func BuildJsonHTTPRequest(ctx context.Context, url, method string, parameters ...interface{}) (*http.Request, error)

BuildJsonHTTPRequest creates JSON RPC http request using provided url, method and parameters

func BuildJsonHttpRequestWithBody added in v0.0.4

func BuildJsonHttpRequestWithBody(ctx context.Context, url string, reqBody []byte) (*http.Request, error)

BuildJsonHttpRequestWithBody creates JSON RPC http request using provided url and request body

Types

type Config

type Config struct {
	// Host defines the network adapter that will be used to serve the HTTP requests
	Host string `mapstructure:"Host"`

	// Port defines the port to serve the endpoints via HTTP
	Port int `mapstructure:"Port"`

	// ReadTimeout is the HTTP server read timeout
	// check net/http.server.ReadTimeout and net/http.server.ReadHeaderTimeout
	ReadTimeout types.Duration `mapstructure:"ReadTimeout"`

	// WriteTimeout is the HTTP server write timeout
	// check net/http.server.WriteTimeout
	WriteTimeout types.Duration `mapstructure:"WriteTimeout"`

	// MaxRequestsPerIPAndSecond defines how much requests a single IP can
	// send within a single second
	MaxRequestsPerIPAndSecond float64 `mapstructure:"MaxRequestsPerIPAndSecond"`
}

Config represents the configuration of the json rpc

type Error

type Error interface {
	Error() string
	ErrorCode() int
	ErrorData() *[]byte
}

Error interface

type ErrorObject

type ErrorObject struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    *types.ArgBytes `json:"data,omitempty"`
}

ErrorObject is a jsonrpc error

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler manage services to handle jsonrpc requests

Services are public structures containing public methods matching the name of the jsonrpc method.

Services must be registered with a prefix to identify the service and its methods, for example a service registered with a prefix `eth` will have all the public methods exposed as eth_<methodName> through the json rpc server.

Go public methods requires the first char of its name to be in uppercase, but the exposition of the method will consider it to lower case, for example a method `func MyMethod()` provided by the service registered with `eth` prefix will be triggered when the method eth_myMethod is specified

the public methods must follow the conventions: - return interface{}, rpcError - if the method depend on a Web Socket connection, it must be the first parameters as f(*websocket.Conn) - parameter types must match the type of the data provided for the method

check the `eth.go` file for more example on how the methods are implemented

func (*Handler) Handle

func (h *Handler) Handle(req handleRequest) Response

Handle is the function that knows which and how a function should be executed when a JSON RPC request is received

func (*Handler) HandleWs

func (h *Handler) HandleWs(reqBody []byte, wsConn *websocket.Conn, httpReq *http.Request) ([]byte, error)

HandleWs handle websocket requests

type RPCError

type RPCError struct {
	// contains filtered or unexported fields
}

RPCError represents an error returned by a JSON RPC endpoint.

func NewRPCError

func NewRPCError(code int, err string, args ...interface{}) *RPCError

NewRPCError creates a new error instance to be returned by the RPC endpoints

func NewRPCErrorWithData

func NewRPCErrorWithData(code int, err string, data *[]byte, args ...interface{}) *RPCError

NewRPCErrorWithData creates a new error instance with data to be returned by the RPC endpoints

func (*RPCError) Error

func (e *RPCError) Error() string

Error returns the error message.

func (*RPCError) ErrorCode

func (e *RPCError) ErrorCode() int

ErrorCode returns the error code.

func (*RPCError) ErrorData

func (e *RPCError) ErrorData() *[]byte

ErrorData returns the error data.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Request is a jsonrpc request

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      interface{}     `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *ErrorObject    `json:"error,omitempty"`
}

Response is a jsonrpc success response

func JSONRPCCall

func JSONRPCCall(url, method string, params ...interface{}) (Response, error)

JSONRPCCall calls JSONRPCCallWithContext with the default context

func JSONRPCCallWithContext added in v0.0.4

func JSONRPCCallWithContext(ctx context.Context, url, method string, parameters ...interface{}) (Response, error)

JSONRPCCallWithContext executes a 2.0 JSON RPC HTTP Post Request to the provided URL with the provided method and parameters, which is compatible with the Ethereum JSON RPC Server.

func NewResponse

func NewResponse(req Request, reply []byte, err Error) Response

NewResponse returns Success/Error response object

func (Response) Bytes

func (s Response) Bytes() ([]byte, error)

Bytes return the serialized response

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is an API backend to handle RPC requests

func NewServer

func NewServer(
	cfg Config,
	services []Service,
) *Server

NewServer returns the JsonRPC server

func (*Server) Start

func (s *Server) Start() error

Start initializes the JSON RPC server to listen for request

func (*Server) Stop

func (s *Server) Stop() error

Stop shutdown the rpc server

type Service

type Service struct {
	Name    string
	Service interface{}
}

Service implementation of a service an it's name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL