rpc

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 17 Imported by: 14

README

Cardinal RPC

Cardinal RPC is a framework for creating Web3 compatible RPC servers, built for the Cardinal Blockchain Paraclient.

It uses reflection to convert Go structs into JSON-RPC methods, allowing developers to write Go code without putting much effort on serialization, deserialization, and other HTTP handling concerns.

It currently supports an HTTP transport, with Websockets coming in the future.

Usage

The easiest way to get started is with the transport manager:

import (
  "github.com/openrelayxyz/cardinal-rpc/transports"
)

func main() {
  tm := transports.NewTransportManager(concurrencyLevel)
  tm.Register("namespace", &myService{})
  tm.AddHTTPServer(8000)
  if err := tm.Run(); err != nil { panic(err.Error()) }
}

The transport manager specifies a concurrencyLevel, which is the number of goroutines which can concurrently execute RPC calls.

Documentation

Index

Constants

View Source
const (
	PendingBlockNumber  = BlockNumber(-2)
	LatestBlockNumber   = BlockNumber(-1)
	EarliestBlockNumber = BlockNumber(0)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockNumber added in v1.1.0

type BlockNumber int64

func (BlockNumber) MarshalJSON added in v1.1.0

func (bn BlockNumber) MarshalJSON() ([]byte, error)

func (*BlockNumber) UnmarshalJSON added in v1.1.0

func (bn *BlockNumber) UnmarshalJSON(data []byte) error

type Call added in v0.0.18

type Call struct {
	Version string            `json:"jsonrpc"`
	ID      json.RawMessage   `json:"id"`
	Method  string            `json:"method"`
	Params  []json.RawMessage `json:"params"`
}

func NewCall added in v0.0.18

func NewCall(method string, params ...interface{}) (*Call, error)

func NewCallParams added in v0.0.18

func NewCallParams(method string, rawparams []json.RawMessage) (*Call, error)

type CallContext added in v0.0.4

type CallContext struct {
	Latest int64
	Await  func(int64) bool
	// contains filtered or unexported fields
}

func (*CallContext) Context added in v0.0.4

func (c *CallContext) Context() context.Context

func (*CallContext) Get added in v0.0.6

func (cm *CallContext) Get(key string) (interface{}, bool)

func (*CallContext) Metadata added in v0.0.4

func (c *CallContext) Metadata() *CallMetadata

func (*CallContext) Set added in v0.0.6

func (cm *CallContext) Set(key string, value interface{})

type CallMetadata

type CallMetadata struct {
	Hash     types.Hash
	Compute  *big.Int
	Duration time.Duration
}

func (*CallMetadata) AddBigCompute added in v0.0.4

func (cm *CallMetadata) AddBigCompute(x *big.Int)

func (*CallMetadata) AddCompute added in v0.0.4

func (cm *CallMetadata) AddCompute(x uint64)

type Check added in v0.0.6

type Check struct {
	Frequency             int64  `yaml:"frequency"`
	Params                string `yaml:"params"`
	Method                string `yaml:"method"`
	MaxHeavyResponseTime  int64  `yaml:"max_heavy_response_time_ms"`
	MaxNormalResponseTime int64  `yaml:"max_normal_response_time_ms"`
	// contains filtered or unexported fields
}

type Checks added in v0.0.6

type Checks []Check

func (Checks) Healthy added in v0.0.6

func (c Checks) Healthy() HealthStatus

func (Checks) Start added in v0.0.6

func (c Checks) Start(r RegistryCallable)

type HealthCheck added in v0.0.6

type HealthCheck interface {
	Healthy() HealthStatus
}

type HealthStatus added in v0.0.6

type HealthStatus int
const (
	Healthy HealthStatus = iota
	Warning
	Unavailable
)

type Middleware added in v0.0.6

type Middleware interface {
	Enter(*CallContext, string, []json.RawMessage) (interface{}, *RPCError)
	Exit(*CallContext, interface{}, *RPCError) (interface{}, *RPCError)
}

type RPCError

type RPCError struct {
	C    int         `json:"code"`
	Msg  string      `json:"message"`
	Data interface{} `json:"data,omitempty"`
}

func NewRPCError

func NewRPCError(code int, msg string) *RPCError

func NewRPCErrorWithData added in v0.0.13

func NewRPCErrorWithData(code int, msg string, data interface{}) *RPCError

func (*RPCError) Code

func (err *RPCError) Code() int

func (*RPCError) Error

func (err *RPCError) Error() string

type RawResponse added in v0.0.18

type RawResponse struct {
	Version string          `json:"jsonrpc"`
	ID      json.RawMessage `json:"id"`
	Error   *RPCError       `json:"error,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Meta    *CallMetadata   `json:"-"`
}

type Registry

type Registry interface {
	RegistryCallable
	Register(namespace string, service interface{})
	RegisterMiddleware(Middleware)
	RegisterHeightFeed(<-chan int64)
	SetBlockWaitDuration(time.Duration)
	OnMissing(func(*CallContext, string, []json.RawMessage) (interface{}, *RPCError, *CallMetadata))
	Disconnect(context.Context)
}

func NewRegistry

func NewRegistry(concurrency int) Registry

type RegistryCallable added in v0.0.7

type RegistryCallable interface {
	Call(ctx context.Context, method string, args []json.RawMessage, output chan interface{}, latestNumber int64) (interface{}, *RPCError, *CallMetadata)
}

type Response added in v0.0.18

type Response struct {
	Version string          `json:"jsonrpc"`
	ID      json.RawMessage `json:"id"`
	Error   *RPCError       `json:"error,omitempty"`
	Result  interface{}     `json:"result,omitempty"`
	Params  interface{}     `json:"params,omitempty"`
	Meta    *CallMetadata   `json:"-"`
}

type SubscriptionResponse added in v0.0.19

type SubscriptionResponse struct {
	Version string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  struct {
		ID     hexutil.Uint64 `json:"subscription"`
		Result interface{}    `json:"result"`
	} `json:"params"`
}

type SubscriptionResponseRaw added in v0.0.19

type SubscriptionResponseRaw struct {
	Version string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  struct {
		ID     hexutil.Uint64  `json:"subscription"`
		Result json.RawMessage `json:"result"`
	} `json:"params"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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