node

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2016 License: GPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Package node represents the Ethereum protocol stack container.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDatadirUsed    = errors.New("datadir already used")
	ErrNodeStopped    = errors.New("node not started")
	ErrNodeRunning    = errors.New("node already running")
	ErrServiceUnknown = errors.New("unknown service")
)

Functions

func DefaultHTTPEndpoint

func DefaultHTTPEndpoint() string

DefaultHTTPEndpoint returns the HTTP endpoint used by default.

func DefaultIPCEndpoint

func DefaultIPCEndpoint() string

DefaultIPCEndpoint returns the IPC path used by default.

func DefaultWSEndpoint

func DefaultWSEndpoint() string

DefaultWSEndpoint returns the websocket endpoint used by default.

Types

type Config

type Config struct {
	// DataDir is the file system folder the node should use for any data storage
	// requirements. The configured data directory will not be directly shared with
	// registered services, instead those can use utility methods to create/access
	// databases or flat files. This enables ephemeral nodes which can fully reside
	// in memory.
	DataDir string

	// IPCPath is the requested location to place the IPC endpoint. If the path is
	// a simple file name, it is placed inside the data directory (or on the root
	// pipe path on Windows), whereas if it's a resolvable path name (absolute or
	// relative), then that specific path is enforced. An empty path disables IPC.
	IPCPath string

	// This field should be a valid secp256k1 private key that will be used for both
	// remote peer identification as well as network traffic encryption. If no key
	// is configured, the preset one is loaded from the data dir, generating it if
	// needed.
	PrivateKey *ecdsa.PrivateKey

	// Name sets the node name of this server. Use common.MakeName to create a name
	// that follows existing conventions.
	Name string

	// NoDiscovery specifies whether the peer discovery mechanism should be started
	// or not. Disabling is usually useful for protocol debugging (manual topology).
	NoDiscovery bool

	// Bootstrap nodes used to establish connectivity with the rest of the network.
	BootstrapNodes []*discover.Node

	// Network interface address on which the node should listen for inbound peers.
	ListenAddr string

	// If set to a non-nil value, the given NAT port mapper is used to make the
	// listening port available to the Internet.
	NAT nat.Interface

	// If Dialer is set to a non-nil value, the given Dialer is used to dial outbound
	// peer connections.
	Dialer *net.Dialer

	// If NoDial is true, the node will not dial any peers.
	NoDial bool

	// MaxPeers is the maximum number of peers that can be connected. If this is
	// set to zero, then only the configured static and trusted peers can connect.
	MaxPeers int

	// MaxPendingPeers is the maximum number of peers that can be pending in the
	// handshake phase, counted separately for inbound and outbound connections.
	// Zero defaults to preset values.
	MaxPendingPeers int

	// HTTPHost is the host interface on which to start the HTTP RPC server. If this
	// field is empty, no HTTP API endpoint will be started.
	HTTPHost string

	// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
	// default zero value is/ valid and will pick a port number randomly (useful
	// for ephemeral nodes).
	HTTPPort int

	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
	// clients. Please be aware that CORS is a browser enforced security, it's fully
	// useless for custom HTTP clients.
	HTTPCors string

	// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
	// If the module list is empty, all RPC API endpoints designated public will be
	// exposed.
	HTTPModules []string

	// WSHost is the host interface on which to start the websocket RPC server. If
	// this field is empty, no websocket API endpoint will be started.
	WSHost string

	// WSPort is the TCP port number on which to start the websocket RPC server. The
	// default zero value is/ valid and will pick a port number randomly (useful for
	// ephemeral nodes).
	WSPort int

	// WSOrigins is the list of domain to accept websocket requests from. Please be
	// aware that the server can only act upon the HTTP request the client sends and
	// cannot verify the validity of the request header.
	WSOrigins string

	// WSModules is a list of API modules to expose via the websocket RPC interface.
	// If the module list is empty, all RPC API endpoints designated public will be
	// exposed.
	WSModules []string
}

Config represents a small collection of configuration values to fine tune the P2P network layer of a protocol stack. These values can be further extended by all registered services.

func (*Config) HTTPEndpoint

func (c *Config) HTTPEndpoint() string

HTTPEndpoint resolves an HTTP endpoint based on the configured host interface and port parameters.

func (*Config) IPCEndpoint

func (c *Config) IPCEndpoint() string

IPCEndpoint resolves an IPC endpoint based on a configured value, taking into account the set data folders as well as the designated platform we're currently running on.

func (*Config) NodeKey

func (c *Config) NodeKey() *ecdsa.PrivateKey

NodeKey retrieves the currently configured private key of the node, checking first any manually set key, falling back to the one found in the configured data folder. If no key can be found, a new one is generated.

func (*Config) StaticNodes

func (c *Config) StaticNodes() []*discover.Node

StaticNodes returns a list of node enode URLs configured as static nodes.

func (*Config) TrusterNodes

func (c *Config) TrusterNodes() []*discover.Node

TrusterNodes returns a list of node enode URLs configured as trusted nodes.

func (*Config) WSEndpoint

func (c *Config) WSEndpoint() string

WSEndpoint resolves an websocket endpoint based on the configured host interface and port parameters.

type DuplicateServiceError

type DuplicateServiceError struct {
	Kind reflect.Type
}

DuplicateServiceError is returned during Node startup if a registered service constructor returns a service of the same type that was already started.

func (*DuplicateServiceError) Error

func (e *DuplicateServiceError) Error() string

Error generates a textual representation of the duplicate service error.

type Node

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

Node represents a P2P node into which arbitrary (uniquely typed) services might be registered.

func New

func New(conf *Config) (*Node, error)

New creates a new P2P node, ready for protocol registration.

func (*Node) Attach

func (n *Node) Attach() (rpc.Client, error)

Attach creates an RPC client attached to an in-process API handler.

func (*Node) DataDir

func (n *Node) DataDir() string

DataDir retrieves the current datadir used by the protocol stack.

func (*Node) EventMux

func (n *Node) EventMux() *event.TypeMux

EventMux retrieves the event multiplexer used by all the network services in the current protocol stack.

func (*Node) HTTPEndpoint

func (n *Node) HTTPEndpoint() string

HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.

func (*Node) IPCEndpoint

func (n *Node) IPCEndpoint() string

IPCEndpoint retrieves the current IPC endpoint used by the protocol stack.

func (*Node) Register

func (n *Node) Register(constructor ServiceConstructor) error

Register injects a new service into the node's stack. The service created by the passed constructor must be unique in its type with regard to sibling ones.

func (*Node) Restart

func (n *Node) Restart() error

Restart terminates a running node and boots up a new one in its place. If the node isn't running, an error is returned.

func (*Node) Server

func (n *Node) Server() *p2p.Server

Server retrieves the currently running P2P network layer. This method is meant only to inspect fields of the currently running server, life cycle management should be left to this Node entity.

func (*Node) Service

func (n *Node) Service(service interface{}) error

Service retrieves a currently running service registered of a specific type.

func (*Node) Start

func (n *Node) Start() error

Start create a live P2P node and starts running it.

func (*Node) Stop

func (n *Node) Stop() error

Stop terminates a running node along with all it's services. In the node was not started, an error is returned.

func (*Node) WSEndpoint

func (n *Node) WSEndpoint() string

WSEndpoint retrieves the current WS endpoint used by the protocol stack.

func (*Node) Wait

func (n *Node) Wait()

Wait blocks the thread until the node is stopped. If the node is not running at the time of invocation, the method immediately returns.

type PrivateAdminAPI

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

PrivateAdminAPI is the collection of administrative API methods exposed only over a secure RPC channel.

func NewPrivateAdminAPI

func NewPrivateAdminAPI(node *Node) *PrivateAdminAPI

NewPrivateAdminAPI creates a new API definition for the private admin methods of the node itself.

func (*PrivateAdminAPI) AddPeer

func (api *PrivateAdminAPI) AddPeer(url string) (bool, error)

AddPeer requests connecting to a remote node, and also maintaining the new connection at all times, even reconnecting if it is lost.

func (*PrivateAdminAPI) StartRPC

func (api *PrivateAdminAPI) StartRPC(host *string, port *rpc.HexNumber, cors *string, apis *string) (bool, error)

StartRPC starts the HTTP RPC API server.

func (*PrivateAdminAPI) StartWS

func (api *PrivateAdminAPI) StartWS(host *string, port *rpc.HexNumber, allowedOrigins *string, apis *string) (bool, error)

StartWS starts the websocket RPC API server.

func (*PrivateAdminAPI) StopRPC

func (api *PrivateAdminAPI) StopRPC() (bool, error)

StopRPC terminates an already running HTTP RPC API endpoint.

func (*PrivateAdminAPI) StopWS

func (api *PrivateAdminAPI) StopWS() (bool, error)

StopRPC terminates an already running websocket RPC API endpoint.

type PublicAdminAPI

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

PublicAdminAPI is the collection of administrative API methods exposed over both secure and unsecure RPC channels.

func NewPublicAdminAPI

func NewPublicAdminAPI(node *Node) *PublicAdminAPI

NewPublicAdminAPI creates a new API definition for the public admin methods of the node itself.

func (*PublicAdminAPI) Datadir

func (api *PublicAdminAPI) Datadir() string

Datadir retrieves the current data directory the node is using.

func (*PublicAdminAPI) NodeInfo

func (api *PublicAdminAPI) NodeInfo() (*p2p.NodeInfo, error)

NodeInfo retrieves all the information we know about the host node at the protocol granularity.

func (*PublicAdminAPI) Peers

func (api *PublicAdminAPI) Peers() ([]*p2p.PeerInfo, error)

Peers retrieves all the information we know about each individual peer at the protocol granularity.

type PublicDebugAPI

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

PublicDebugAPI is the collection of debugging related API methods exposed over both secure and unsecure RPC channels.

func NewPublicDebugAPI

func NewPublicDebugAPI(node *Node) *PublicDebugAPI

NewPublicDebugAPI creates a new API definition for the public debug methods of the node itself.

func (*PublicDebugAPI) Metrics

func (api *PublicDebugAPI) Metrics(raw bool) (map[string]interface{}, error)

Metrics retrieves all the known system metric collected by the node.

type PublicWeb3API

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

PublicWeb3API offers helper utils

func NewPublicWeb3API

func NewPublicWeb3API(stack *Node) *PublicWeb3API

NewPublicWeb3API creates a new Web3Service instance

func (*PublicWeb3API) ClientVersion

func (s *PublicWeb3API) ClientVersion() string

ClientVersion returns the node name

func (*PublicWeb3API) Sha3

func (s *PublicWeb3API) Sha3(input string) string

Sha3 applies the ethereum sha3 implementation on the input. It assumes the input is hex encoded.

type Service

type Service interface {
	// Protocols retrieves the P2P protocols the service wishes to start.
	Protocols() []p2p.Protocol

	// APIs retrieves the list of RPC descriptors the service provides
	APIs() []rpc.API

	// Start is called after all services have been constructed and the networking
	// layer was also initialized to spawn any goroutines required by the service.
	Start(server *p2p.Server) error

	// Stop terminates all goroutines belonging to the service, blocking until they
	// are all terminated.
	Stop() error
}

Service is an individual protocol that can be registered into a node.

Notes:

  • Service life-cycle management is delegated to the node. The service is allowed to initialize itself upon creation, but no goroutines should be spun up outside of the Start method.
  • Restart logic is not required as the node will create a fresh instance every time a service is started.

type ServiceConstructor

type ServiceConstructor func(ctx *ServiceContext) (Service, error)

ServiceConstructor is the function signature of the constructors needed to be registered for service instantiation.

type ServiceContext

type ServiceContext struct {
	EventMux *event.TypeMux // Event multiplexer used for decoupled notifications
	// contains filtered or unexported fields
}

ServiceContext is a collection of service independent options inherited from the protocol stack, that is passed to all constructors to be optionally used; as well as utility methods to operate on the service environment.

func (*ServiceContext) OpenDatabase

func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ethdb.Database, error)

OpenDatabase opens an existing database with the given name (or creates one if no previous can be found) from within the node's data directory. If the node is an ephemeral one, a memory database is returned.

func (*ServiceContext) Service

func (ctx *ServiceContext) Service(service interface{}) error

Service retrieves a currently running service registered of a specific type.

type StopError

type StopError struct {
	Server   error
	Services map[reflect.Type]error
}

StopError is returned if a Node fails to stop either any of its registered services or itself.

func (*StopError) Error

func (e *StopError) Error() string

Error generates a textual representation of the stop error.

Jump to

Keyboard shortcuts

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