Documentation ¶
Overview ¶
Package node sets up multi-protocol Ethereum nodes.
In the model exposed by this package, a node is a collection of services which use shared resources to provide RPC APIs. Services can also offer devp2p protocols, which are wired up to the devp2p network when the node instance is started.
Node Lifecycle ¶
The Node object has a lifecycle consisting of three basic states, INITIALIZING, RUNNING and CLOSED.
●───────┐ New() │ ▼ INITIALIZING ────Start()─┐ │ │ │ ▼ Close() RUNNING │ │ ▼ │ CLOSED ◀──────Close()─┘
Creating a Node allocates basic resources such as the data directory and returns the node in its INITIALIZING state. Lifecycle objects, RPC APIs and peer-to-peer networking protocols can be registered in this state. Basic operations such as opening a key-value database are permitted while initializing.
Once everything is registered, the node can be started, which moves it into the RUNNING state. Starting the node starts all registered Lifecycle objects and enables RPC and peer-to-peer networking. Note that no additional Lifecycles, APIs or p2p protocols can be registered while the node is running.
Closing the node releases all held resources. The actions performed by Close depend on the state it was in. When closing a node in INITIALIZING state, resources related to the data directory are released. If the node was RUNNING, closing it also stops all Lifecycle objects and shuts down RPC and peer-to-peer networking.
You must always call Close on Node, even if the node was not started.
Resources Managed By Node ¶
All file-system resources used by a node instance are located in a directory called the data directory. The location of each resource can be overridden through additional node configuration. The data directory is optional. If it is not set and the location of a resource is otherwise unspecified, package node will create the resource in memory.
To access to the devp2p network, Node configures and starts p2p.Server. Each host on the devp2p network has a unique identifier, the node key. The Node instance persists this key across restarts. Node also loads static and trusted node lists and ensures that knowledge about other hosts is persisted.
JSON-RPC servers which run HTTP, WebSocket or IPC can be started on a Node. RPC modules offered by registered services will be offered on those endpoints. Users can restrict any endpoint to a subset of RPC modules. Node itself offers the "debug", "admin" and "web3" modules.
Service implementations can open LevelDB databases through the service context. Package node chooses the file system location of each database. If the node is configured to run without a data directory, databases are opened in memory instead.
Node also creates the shared store of encrypted Ethereum account keys. Services can access the account manager through the service context.
Sharing Data Directory Among Instances ¶
Multiple node instances can share a single data directory if they have distinct instance names (set through the Name config option). Sharing behaviour depends on the type of resource.
devp2p-related resources (node key, static/trusted node lists, known hosts database) are stored in a directory with the same name as the instance. Thus, multiple node instances using the same data directory will store this information in different subdirectories of the data directory.
LevelDB databases are also stored within the instance subdirectory. If multiple node instances use the same data directory, opening the databases with identical names will create one database for each instance.
The account key store is shared among all node instances using the same data directory unless its location is changed through the KeyStoreDir configuration option.
Data Directory Sharing Example ¶
In this example, two node instances named A and B are started with the same data directory. Node instance A opens the database "db", node instance B opens the databases "db" and "db-2". The following files will be created in the data directory:
data-directory/ A/ nodekey -- devp2p node key of instance A nodes/ -- devp2p discovery knowledge database of instance A db/ -- LevelDB content for "db" A.ipc -- JSON-RPC UNIX domain socket endpoint of instance A B/ nodekey -- devp2p node key of node B nodes/ -- devp2p discovery knowledge database of instance B static-nodes.json -- devp2p static node list of instance B db/ -- LevelDB content for "db" db-2/ -- LevelDB content for "db-2" B.ipc -- JSON-RPC UNIX domain socket endpoint of instance B keystore/ -- account key store, used by both instances
Index ¶
- Constants
- Variables
- func CheckTimeouts(timeouts *rpc.HTTPTimeouts)
- func DefaultDataDir() string
- func DefaultHTTPEndpoint() string
- func DefaultIPCEndpoint(clientIdentifier string) string
- func DefaultWSEndpoint() string
- func NewHTTPHandlerStack(srv http.Handler, cors []string, vhosts []string, jwtSecret []byte) http.Handler
- func NewWSHandlerStack(srv http.Handler, jwtSecret []byte) http.Handler
- func RegisterApis(apis []rpc.API, modules []string, srv *rpc.Server) error
- func StartHTTPEndpoint(endpoint string, timeouts rpc.HTTPTimeouts, handler http.Handler) (*http.Server, net.Addr, error)
- type Config
- func (c *Config) ExtRPCEnabled() bool
- func (c *Config) HTTPEndpoint() string
- func (c *Config) IPCEndpoint() string
- func (c *Config) KeyDirConfig() (string, error)
- func (c *Config) NodeDB() string
- func (c *Config) NodeKey() *ecdsa.PrivateKey
- func (c *Config) NodeName() string
- func (c *Config) ResolvePath(path string) string
- func (c *Config) StaticNodes() []*enode.Node
- func (c *Config) TrustedNodes() []*enode.Node
- func (c *Config) WSEndpoint() string
- type Lifecycle
- type Node
- func (n *Node) AccountManager() *accounts.Manager
- func (n *Node) Attach() (*rpc.Client, error)
- func (n *Node) Close() error
- func (n *Node) Config() *Config
- func (n *Node) DataDir() string
- func (n *Node) EventMux() *event.TypeMux
- func (n *Node) GetAPIs() (unauthenticated, all []rpc.API)
- func (n *Node) HTTPEndpoint() string
- func (n *Node) IPCEndpoint() string
- func (n *Node) InstanceDir() string
- func (n *Node) KeyStoreDir() string
- func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, readonly bool) (ethdb.Database, error)
- func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, ancient string, namespace string, ...) (ethdb.Database, error)
- func (n *Node) RPCHandler() (*rpc.Server, error)
- func (n *Node) RegisterAPIs(apis []rpc.API)
- func (n *Node) RegisterHandler(name, path string, handler http.Handler)
- func (n *Node) RegisterLifecycle(lifecycle Lifecycle)
- func (n *Node) RegisterProtocols(protocols []p2p.Protocol)
- func (n *Node) ResolveAncient(name string, ancient string) string
- func (n *Node) ResolvePath(x string) string
- func (n *Node) Server() *p2p.Server
- func (n *Node) Start() error
- func (n *Node) WSEndpoint() string
- func (n *Node) Wait()
- type StopError
Examples ¶
Constants ¶
const ( DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server DefaultWSHost = "localhost" // Default host interface for the websocket RPC server DefaultWSPort = 8546 // Default TCP port for the websocket RPC server DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server DefaultGraphQLPort = 8547 // Default TCP port for the GraphQL server DefaultAuthHost = "localhost" // Default host interface for the authenticated apis DefaultAuthPort = 8551 // Default port for the authenticated apis )
Variables ¶
var ( DefaultAuthCors = []string{"localhost"} // Default cors domain for the authenticated apis DefaultAuthVhosts = []string{"localhost"} // Default virtual hosts for the authenticated apis DefaultAuthOrigins = []string{"localhost"} // Default origins for the authenticated apis DefaultAuthPrefix = "" // Default prefix for the authenticated apis DefaultAuthModules = []string{"eth", "engine"} )
var ( ErrDatadirUsed = errors.New("datadir already used by another process") ErrNodeStopped = errors.New("node not started") ErrNodeRunning = errors.New("node already running") ErrServiceUnknown = errors.New("unknown service") )
var DefaultConfig = Config{ DataDir: DefaultDataDir(), HTTPPort: DefaultHTTPPort, AuthAddr: DefaultAuthHost, AuthPort: DefaultAuthPort, AuthVirtualHosts: DefaultAuthVhosts, HTTPModules: []string{"net", "web3"}, HTTPVirtualHosts: []string{"localhost"}, HTTPTimeouts: rpc.DefaultHTTPTimeouts, WSPort: DefaultWSPort, WSModules: []string{"net", "web3"}, GraphQLVirtualHosts: []string{"localhost"}, P2P: p2p.Config{ ListenAddr: ":30303", MaxPeers: 50, NAT: nat.Any(), }, }
DefaultConfig contains reasonable default settings.
Functions ¶
func CheckTimeouts ¶
func CheckTimeouts(timeouts *rpc.HTTPTimeouts)
CheckTimeouts ensures that timeout values are meaningful
func DefaultDataDir ¶
func DefaultDataDir() string
DefaultDataDir is the default data directory to use for the databases and other persistence requirements.
func DefaultHTTPEndpoint ¶
func DefaultHTTPEndpoint() string
DefaultHTTPEndpoint returns the HTTP endpoint used by default.
func DefaultIPCEndpoint ¶
DefaultIPCEndpoint returns the IPC path used by default.
func DefaultWSEndpoint ¶
func DefaultWSEndpoint() string
DefaultWSEndpoint returns the websocket endpoint used by default.
func NewHTTPHandlerStack ¶
func NewHTTPHandlerStack(srv http.Handler, cors []string, vhosts []string, jwtSecret []byte) http.Handler
NewHTTPHandlerStack returns wrapped http-related handlers
func NewWSHandlerStack ¶
NewWSHandlerStack returns a wrapped ws-related handler.
func RegisterApis ¶
RegisterApis checks the given modules' availability, generates an allowlist based on the allowed modules, and then registers all of the APIs exposed by the services.
Types ¶
type Config ¶
type Config struct { // Name sets the instance name of the node. It must not contain the / character and is // used in the devp2p node identifier. The instance name of geth is "geth". If no // value is specified, the basename of the current executable is used. Name string `toml:"-"` // UserIdent, if set, is used as an additional component in the devp2p node identifier. UserIdent string `toml:",omitempty"` // Version should be set to the version number of the program. It is used // in the devp2p node identifier. Version string `toml:"-"` // 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 // Configuration of peer-to-peer networking. P2P p2p.Config // KeyStoreDir is the file system folder that contains private keys. The directory can // be specified as a relative path, in which case it is resolved relative to the // current directory. // // If KeyStoreDir is empty, the default location is the "keystore" subdirectory of // DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory // is created by New and destroyed when the node is stopped. KeyStoreDir string `toml:",omitempty"` // ExternalSigner specifies an external URI for a clef-type signer ExternalSigner string `toml:",omitempty"` // UseLightweightKDF lowers the memory and CPU requirements of the key store // scrypt KDF at the expense of security. UseLightweightKDF bool `toml:",omitempty"` // InsecureUnlockAllowed allows user to unlock accounts in unsafe http environment. InsecureUnlockAllowed bool `toml:",omitempty"` // NoUSB disables hardware wallet monitoring and connectivity. // Deprecated: USB monitoring is disabled by default and must be enabled explicitly. NoUSB bool `toml:",omitempty"` // USB enables hardware wallet monitoring and connectivity. USB bool `toml:",omitempty"` // SmartCardDaemonPath is the path to the smartcard daemon's socket SmartCardDaemonPath string `toml:",omitempty"` // 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 // 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 `toml:",omitempty"` // 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 `toml:",omitempty"` // HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests. // This is by default {'localhost'}. Using this prevents attacks like // DNS rebinding, which bypasses SOP by simply masquerading as being within the same // origin. These attacks do not utilize CORS, since they are not cross-domain. // By explicitly checking the Host-header, the server will not allow requests // made against the server with a malicious host domain. // Requests using ip address directly are not affected HTTPVirtualHosts []string `toml:",omitempty"` // 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 // HTTPTimeouts allows for customization of the timeout values used by the HTTP RPC // interface. HTTPTimeouts rpc.HTTPTimeouts // HTTPPathPrefix specifies a path prefix on which http-rpc is to be served. HTTPPathPrefix string `toml:",omitempty"` // AuthAddr is the listening address on which authenticated APIs are provided. AuthAddr string `toml:",omitempty"` // AuthPort is the port number on which authenticated APIs are provided. AuthPort int `toml:",omitempty"` // AuthVirtualHosts is the list of virtual hostnames which are allowed on incoming requests // for the authenticated api. This is by default {'localhost'}. AuthVirtualHosts []string `toml:",omitempty"` // 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 `toml:",omitempty"` // WSPathPrefix specifies a path prefix on which ws-rpc is to be served. WSPathPrefix string `toml:",omitempty"` // 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 `toml:",omitempty"` // 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 // WSExposeAll exposes all API modules via the WebSocket RPC interface rather // than just the public ones. // // *WARNING* Only set this if the node is running in a trusted network, exposing // private APIs to untrusted users is a major security risk. WSExposeAll bool `toml:",omitempty"` // GraphQLCors 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. GraphQLCors []string `toml:",omitempty"` // GraphQLVirtualHosts is the list of virtual hostnames which are allowed on incoming requests. // This is by default {'localhost'}. Using this prevents attacks like // DNS rebinding, which bypasses SOP by simply masquerading as being within the same // origin. These attacks do not utilize CORS, since they are not cross-domain. // By explicitly checking the Host-header, the server will not allow requests // made against the server with a malicious host domain. // Requests using ip address directly are not affected GraphQLVirtualHosts []string `toml:",omitempty"` // Logger is a custom logger to use with the p2p.Server. Logger log.Logger `toml:",omitempty"` // AllowUnprotectedTxs allows non EIP-155 protected transactions to be send over RPC. AllowUnprotectedTxs bool `toml:",omitempty"` // JWTSecret is the hex-encoded jwt secret. JWTSecret string `toml:",omitempty"` // contains filtered or unexported fields }
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) ExtRPCEnabled ¶
ExtRPCEnabled returns the indicator whether node enables the external RPC(http, ws or graphql).
func (*Config) HTTPEndpoint ¶
HTTPEndpoint resolves an HTTP endpoint based on the configured host interface and port parameters.
func (*Config) IPCEndpoint ¶
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) KeyDirConfig ¶
KeyDirConfig determines the settings for keydirectory
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) ResolvePath ¶
ResolvePath resolves path in the instance directory.
func (*Config) StaticNodes ¶
StaticNodes returns a list of node enode URLs configured as static nodes.
func (*Config) TrustedNodes ¶
TrustedNodes returns a list of node enode URLs configured as trusted nodes.
func (*Config) WSEndpoint ¶
WSEndpoint resolves a websocket endpoint based on the configured host interface and port parameters.
type Lifecycle ¶
type Lifecycle interface { // 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() error // Stop terminates all goroutines belonging to the service, blocking until they // are all terminated. Stop() error }
Lifecycle encompasses the behavior of services that can be started and stopped on the node. Lifecycle management is delegated to the node, but it is the responsibility of the service-specific package to configure and register the service on the node using the `RegisterLifecycle` method.
Example ¶
package main import ( "fmt" "log" "github.com/qydata/go-ctereum/node" ) // SampleLifecycle is a trivial network service that can be attached to a node for // life cycle management. // // The following methods are needed to implement a node.Lifecycle: // - Start() error - method invoked when the node is ready to start the service // - Stop() error - method invoked when the node terminates the service type SampleLifecycle struct{} func (s *SampleLifecycle) Start() error { fmt.Println("Service starting..."); return nil } func (s *SampleLifecycle) Stop() error { fmt.Println("Service stopping..."); return nil } func main() { // Create a network node to run protocols with the default values. stack, err := node.New(&node.Config{}) if err != nil { log.Fatalf("Failed to create network node: %v", err) } defer stack.Close() // Create and register a simple network Lifecycle. service := new(SampleLifecycle) stack.RegisterLifecycle(service) // Boot up the entire protocol stack, do a restart and terminate if err := stack.Start(); err != nil { log.Fatalf("Failed to start the protocol stack: %v", err) } if err := stack.Close(); err != nil { log.Fatalf("Failed to stop the protocol stack: %v", err) } }
Output: Service starting... Service stopping...
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node is a container on which services can be registered.
func (*Node) AccountManager ¶
AccountManager retrieves the account manager used by the protocol stack.
func (*Node) DataDir ¶
DataDir retrieves the current datadir used by the protocol stack. Deprecated: No files should be stored in this directory, use InstanceDir instead.
func (*Node) EventMux ¶
EventMux retrieves the event multiplexer used by all the network services in the current protocol stack.
func (*Node) GetAPIs ¶
GetAPIs return two sets of APIs, both the ones that do not require authentication, and the complete set
func (*Node) HTTPEndpoint ¶
HTTPEndpoint returns the URL of the HTTP server. Note that this URL does not contain the JSON-RPC path prefix set by HTTPPathPrefix.
func (*Node) IPCEndpoint ¶
IPCEndpoint retrieves the current IPC endpoint used by the protocol stack.
func (*Node) InstanceDir ¶
InstanceDir retrieves the instance directory used by the protocol stack.
func (*Node) KeyStoreDir ¶
KeyStoreDir retrieves the key directory
func (*Node) OpenDatabase ¶
func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, readonly bool) (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 instance directory. If the node is ephemeral, a memory database is returned.
func (*Node) OpenDatabaseWithFreezer ¶
func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, ancient string, namespace string, readonly bool) (ethdb.Database, error)
OpenDatabaseWithFreezer opens an existing database with the given name (or creates one if no previous can be found) from within the node's data directory, also attaching a chain freezer to it that moves ancient chain data from the database to immutable append-only files. If the node is an ephemeral one, a memory database is returned.
func (*Node) RPCHandler ¶
RPCHandler returns the in-process RPC request handler.
func (*Node) RegisterAPIs ¶
RegisterAPIs registers the APIs a service provides on the node.
func (*Node) RegisterHandler ¶
RegisterHandler mounts a handler on the given path on the canonical HTTP server.
The name of the handler is shown in a log message when the HTTP server starts and should be a descriptive term for the service provided by the handler.
func (*Node) RegisterLifecycle ¶
RegisterLifecycle registers the given Lifecycle on the node.
func (*Node) RegisterProtocols ¶
RegisterProtocols adds backend's protocols to the node's p2p server.
func (*Node) ResolveAncient ¶
ResolveAncient returns the absolute path of the root ancient directory.
func (*Node) ResolvePath ¶
ResolvePath returns the absolute path of a resource in the instance directory.
func (*Node) Server ¶
Server retrieves the currently running P2P network layer. This method is meant only to inspect fields of the currently running server. Callers should not start or stop the returned server.
func (*Node) Start ¶
Start starts all registered lifecycles, RPC services and p2p networking. Node can only be started once.
func (*Node) WSEndpoint ¶
WSEndpoint returns the current JSON-RPC over WebSocket endpoint.