node

package
v1.3.1-testnet Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RouteKeyCacheSize       = 5000
	RouteCacheExpirationTTL = 60 * time.Second
)

Variables

View Source
var (
	ErrClientUnavailable = errors.New("no full node available")
)

Functions

func CfxUrlConfig

func CfxUrlConfig() map[Group]UrlConfig

func Config

func Config() *config

func EthFactory

func EthFactory() *factory

EthFactory returns evm space instance factory

func EthUrlConfig

func EthUrlConfig() map[Group]UrlConfig

func Factory

func Factory() *factory

Factory returns core space instance factory

func MustNewServer

func MustNewServer(db *mysql.MysqlStore, nf nodeFactory, grpConf map[Group]UrlConfig) *rpc.Server

MustNewServer creates node management RPC server

Types

type CfxClientProvider

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

CfxClientProvider provides core space client by router.

func NewCfxClientProvider

func NewCfxClientProvider(db *mysql.MysqlStore, router Router) *CfxClientProvider

func (*CfxClientProvider) GetClient

func (p *CfxClientProvider) GetClient(key string, groups ...Group) (sdk.ClientOperator, error)

GetClient gets client of specific group (or use normal HTTP group as default.

func (*CfxClientProvider) GetClientByIP

func (p *CfxClientProvider) GetClientByIP(ctx context.Context, groups ...Group) (sdk.ClientOperator, error)

GetClientByIP gets client of specific group (or use normal HTTP group as default) by remote IP address.

func (CfxClientProvider) GetRouteGroup

func (p CfxClientProvider) GetRouteGroup(key string) (grp Group, ok bool)

getRouteGroup get custom route group for specific route key

type CfxNode

type CfxNode struct {
	sdk.ClientOperator
	// contains filtered or unexported fields
}

CfxNode represents a core space fullnode with friendly name and health status.

func NewCfxNode

func NewCfxNode(group Group, name, url string, hm HealthMonitor) (*CfxNode, error)

NewCfxNode creates an instance of core space fullnode and start to monitor node health in a separate goroutine until node closed.

func (*CfxNode) Close

func (n *CfxNode) Close()

func (*CfxNode) LatestEpochNumber

func (n *CfxNode) LatestEpochNumber() (uint64, error)

LatestEpochNumber returns the latest epoch height of the core space fullnode

func (CfxNode) Name

func (n CfxNode) Name() string

func (CfxNode) Status

func (n CfxNode) Status() Status

func (CfxNode) String

func (n CfxNode) String() string

func (CfxNode) Url

func (n CfxNode) Url() string

type EthClientProvider

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

EthClientProvider provides evm space client by router.

func NewEthClientProvider

func NewEthClientProvider(db *mysql.MysqlStore, router Router) *EthClientProvider

func (*EthClientProvider) GetClient

func (p *EthClientProvider) GetClient(key string, groups ...Group) (*Web3goClient, error)

GetClient gets client of specific group (or use normal HTTP group as default).

func (*EthClientProvider) GetClientByIP

func (p *EthClientProvider) GetClientByIP(ctx context.Context, groups ...Group) (*Web3goClient, error)

GetClientByIP gets client of specific group (or use normal HTTP group as default) by remote IP address.

func (*EthClientProvider) GetClientRandom

func (p *EthClientProvider) GetClientRandom() (*Web3goClient, error)

func (EthClientProvider) GetRouteGroup

func (p EthClientProvider) GetRouteGroup(key string) (grp Group, ok bool)

getRouteGroup get custom route group for specific route key

type EthNode

type EthNode struct {
	*web3go.Client
	// contains filtered or unexported fields
}

EthNode represents an evm space node with friendly name and health status.

func NewEthNode

func NewEthNode(group Group, name, url string, hm HealthMonitor) (*EthNode, error)

NewEthNode creates an instance of evm space node and start to monitor node health in a separate goroutine until node closed.

func (EthNode) Close

func (n EthNode) Close()

func (*EthNode) LatestEpochNumber

func (n *EthNode) LatestEpochNumber() (uint64, error)

LatestEpochNumber returns the latest block height of the evm space fullnode

func (EthNode) Name

func (n EthNode) Name() string

func (EthNode) Status

func (n EthNode) Status() Status

func (EthNode) String

func (n EthNode) String() string

func (EthNode) Url

func (n EthNode) Url() string

type Group

type Group string

Group allows to manage full nodes in multiple groups.

const (

	// core space fullnode groups
	GroupCfxHttp      Group = "cfxhttp"
	GroupCfxFullState Group = "cfxfullstate"
	GroupCfxWs        Group = "cfxws"
	GroupCfxFilter    Group = "cfxfilter"
	GroupCfxLogs      Group = "cfxlog"
	GroupCfxArchives  Group = "cfxarchives"

	// evm space fullnode groups
	GroupEthHttp      Group = "ethhttp"
	GroupEthFullState Group = "ethfullstate"
	GroupEthWs        Group = "ethws"
	GroupEthFilter    Group = "ethfilter"
	GroupEthLogs      Group = "ethlogs"
)

func (Group) Space

func (g Group) Space() string

Space parses space from group name

func (Group) String

func (g Group) String() string

type HealthMonitor

type HealthMonitor interface {
	// HealthyEpoch returns the healthy epoch number among full nodes.
	// Usually, it is the middle epoch number of all full nodes.
	HealthyEpoch() uint64

	// ReportEpoch fired when epoch changes.
	ReportEpoch(nodeName string, epoch uint64)

	// ReportUnhealthy fired when full node becomes unhealthy or unrecovered for a long time.
	ReportUnhealthy(nodeName string, remind bool, reason error)

	// ReportHealthy fired when full node becomes healthy.
	ReportHealthy(nodeName string)
}

HealthMonitor is implemented by any objects that support to monitor full node health.

type LocalRouter

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

LocalRouter routes RPC requests based on local hash ring.

func NewLocalRouter

func NewLocalRouter(group2Urls map[Group][]string) *LocalRouter

func NewLocalRouterFromNodeRPC

func NewLocalRouterFromNodeRPC(client *rpc.Client) (*LocalRouter, error)

func (*LocalRouter) Route

func (r *LocalRouter) Route(group Group, key []byte) string

type Manager

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

Manager manages full node cluster, including: 1. Monitor node health and disable/enable full node automatically. 2. Implements Router interface to route RPC requests to different full nodes in manner of consistent hashing.

func NewManager

func NewManager(group Group) *Manager

func NewManagerWithRepartition

func NewManagerWithRepartition(group Group, resolver RepartitionResolver) *Manager

func (*Manager) Add

func (m *Manager) Add(nodes ...Node)

Add adds fullnode to monitor

func (*Manager) Close

func (m *Manager) Close()

Close closes the manager to reclaim resources

func (*Manager) Distribute

func (m *Manager) Distribute(key []byte) Node

Distribute distributes a full node by specified key.

func (*Manager) Get

func (m *Manager) Get(nodeName string) (Node, bool)

Get gets monitored fullnode from url

func (*Manager) HealthyEpoch

func (m *Manager) HealthyEpoch() uint64

HealthyEpoch returns the middle epoch height collected from managed cluster nodes, which is also regarded as the overall health epoch height.

func (*Manager) List

func (m *Manager) List() []Node

List lists all monitored fullnodes

func (*Manager) Remove

func (m *Manager) Remove(nodeNames ...string)

Remove removes monitored fullnode

func (*Manager) ReportEpoch

func (m *Manager) ReportEpoch(nodeName string, epoch uint64)

ReportEpoch reports latest epoch height of managed node to manager.

func (*Manager) ReportHealthy

func (m *Manager) ReportHealthy(nodeName string)

ReportHealthy reports healthy status of managed node to manager.

func (*Manager) ReportUnhealthy

func (m *Manager) ReportUnhealthy(nodeName string, remind bool, reason error)

ReportUnhealthy reports unhealthy status of managed node to manager.

func (*Manager) Route

func (m *Manager) Route(key []byte) string

Route implements the Router interface.

func (*Manager) String

func (m *Manager) String() string

String implements stringer interface

type Node

type Node interface {
	consistent.Member

	Name() string
	Url() string
	Status() Status

	LatestEpochNumber() (uint64, error)

	Close()
}

Node represents a full node with friendly name and health status.

type NodeRpcRouter

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

NodeRpcRouter routes RPC requests via node management RPC service.

func NewNodeRpcRouter

func NewNodeRpcRouter(client *rpc.Client) *NodeRpcRouter

func (*NodeRpcRouter) Route

func (r *NodeRpcRouter) Route(group Group, key []byte) string

type RedisRepartitionResolver

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

RedisRepartitionResolver implements RepartitionResolver

func NewRedisRepartitionResolver

func NewRedisRepartitionResolver(client *redis.Client, ttl time.Duration, keyPrefix string) *RedisRepartitionResolver

func (*RedisRepartitionResolver) Get

func (r *RedisRepartitionResolver) Get(key uint64) (string, bool)

func (*RedisRepartitionResolver) Put

func (r *RedisRepartitionResolver) Put(key uint64, value string)

type RedisRouter

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

RedisRouter routes RPC requests via redis. It should be used together with RedisRepartitionResolver.

func NewRedisRouter

func NewRedisRouter(client *redis.Client) *RedisRouter

func (*RedisRouter) Route

func (r *RedisRouter) Route(group Group, key []byte) string

type RepartitionResolver

type RepartitionResolver interface {
	Get(key uint64) (string, bool)
	Put(key uint64, value string)
}

RepartitionResolver is implemented to support repartition when item added or removed in the consistent hash ring.

type Router

type Router interface {
	// Route returns the full node URL for specified group and key.
	Route(group Group, key []byte) string
}

Router is used to route RPC requests to multiple full nodes.

func MustNewRouter

func MustNewRouter(redisURL string, nodeRPCURL string, groupConf map[Group]UrlConfig) Router

MustNewRouter creates an instance of Router.

func NewChainedRouter

func NewChainedRouter(groupConf map[Group]UrlConfig, routers ...Router) Router

type SimpleRepartitionResolver

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

func NewSimpleRepartitionResolver

func NewSimpleRepartitionResolver(ttl time.Duration) *SimpleRepartitionResolver

func (*SimpleRepartitionResolver) Get

func (*SimpleRepartitionResolver) Put

func (r *SimpleRepartitionResolver) Put(key uint64, value string)

type Status

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

Status represents the node status, including current epoch number and health status.

func NewStatus

func NewStatus(group Group, nodeName string) Status

func (*Status) Close

func (s *Status) Close()

func (*Status) MarshalJSON

func (s *Status) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON.

func (*Status) Update

func (s *Status) Update(n Node, monitor HealthMonitor)

Update heartbeats with node and updates health status.

type UrlConfig

type UrlConfig struct {
	Nodes    []string
	Failover string
}

type Web3goClient

type Web3goClient struct {
	*web3go.Client

	URL string
}

func (*Web3goClient) NodeName

func (w3c *Web3goClient) NodeName() string

Jump to

Keyboard shortcuts

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