rpc

package
v0.0.3-hotfix1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 22 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 HexEncodeBig

func HexEncodeBig(bigint *big.Int) string

HexEncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.

func HexIsValid

func HexIsValid(s string) bool

HexIsValid checks if the provided string is a valid hexadecimal value

Types

type ArgBig added in v0.0.3

type ArgBig big.Int

ArgBig helps to marshal big number values provided in the RPC requests

func (ArgBig) Hex added in v0.0.3

func (b ArgBig) Hex() string

Hex returns a hexadecimal representation

func (ArgBig) MarshalText added in v0.0.3

func (a ArgBig) MarshalText() ([]byte, error)

MarshalText marshals an array of bytes into an instance of ArgBig

func (*ArgBig) UnmarshalText added in v0.0.3

func (a *ArgBig) UnmarshalText(input []byte) error

UnmarshalText unmarshals an instance of ArgBig into an array of bytes

type ArgBytes

type ArgBytes []byte

ArgBytes helps to marshal byte array values provided in the RPC requests

func ArgBytesPtr

func ArgBytesPtr(b []byte) *ArgBytes

ArgBytesPtr helps to marshal byte array values provided in the RPC requests

func (ArgBytes) Hex

func (b ArgBytes) Hex() string

Hex returns a hexadecimal representation

func (ArgBytes) MarshalText

func (b ArgBytes) MarshalText() ([]byte, error)

MarshalText marshals into text

func (*ArgBytes) UnmarshalText

func (b *ArgBytes) UnmarshalText(input []byte) error

UnmarshalText unmarshals from text

type ArgHash

type ArgHash common.Hash

ArgHash represents a common.Hash that accepts strings shorter than 64 bytes, like 0x00

func (*ArgHash) Hash

func (arg *ArgHash) Hash() common.Hash

Hash returns an instance of common.Hash

func (*ArgHash) UnmarshalText

func (arg *ArgHash) UnmarshalText(input []byte) error

UnmarshalText unmarshals from text

type ArgUint64

type ArgUint64 uint64

ArgUint64 helps to marshal uint64 values provided in the RPC requests

func ArgUint64Ptr

func ArgUint64Ptr(a ArgUint64) *ArgUint64

ArgUint64Ptr returns the pointer of the provided ArgUint64

func (ArgUint64) Hex

func (b ArgUint64) Hex() string

Hex returns a hexadecimal representation

func (ArgUint64) MarshalText

func (b ArgUint64) MarshalText() ([]byte, error)

MarshalText marshals into text

func (*ArgUint64) UnmarshalText

func (b *ArgUint64) UnmarshalText(input []byte) error

UnmarshalText unmarshals from text

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 DBTxManager

type DBTxManager struct{}

DBTxManager allows to do scopped DB txs

func (*DBTxManager) NewDbTxScope

func (f *DBTxManager) NewDbTxScope(db DBTxer, scopedFn DBTxScopedFn) (interface{}, Error)

NewDbTxScope function to initiate DB scopped txs

type DBTxScopedFn

type DBTxScopedFn func(ctx context.Context, dbTx pgx.Tx) (interface{}, Error)

DBTxScopedFn function to do scopped DB txs

type DBTxer

type DBTxer interface {
	BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
}

DBTxer interface to begin DB txs

type Error

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

Error interface

func RPCErrorResponse

func RPCErrorResponse(code int, message string, err error) (interface{}, Error)

RPCErrorResponse formats error to be returned through RPC

func RPCErrorResponseWithData

func RPCErrorResponseWithData(code int, message string, data *[]byte, err error) (interface{}, Error)

RPCErrorResponseWithData formats error to be returned through RPC

type ErrorObject

type ErrorObject struct {
	Code    int       `json:"code"`
	Message string    `json:"message"`
	Data    *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
	ID      interface{}
	Result  json.RawMessage
	Error   *ErrorObject
}

Response is a jsonrpc success response

func JSONRPCCall

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

JSONRPCCall 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