Documentation ¶
Overview ¶
Package node sets up multi-protocol PalletOne 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.
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 PalletOne 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 behavior 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, openening 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. Mode 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 A keystore/ -- account key store, used by both instances
Index ¶
- Constants
- Variables
- func DefaultDataDir() string
- func DefaultIPCEndpoint(clientIdentifier string) string
- func DefaultWSEndpoint() string
- type Config
- func (c *Config) AccountConfig() (int, int, string, error)
- func (c *Config) HTTPEndpoint() string
- func (c *Config) IPCEndpoint() string
- func (c *Config) NodeDB() string
- func (c *Config) NodeKey() *ecdsa.PrivateKey
- func (c *Config) NodeName() string
- func (c *Config) StaticNodes() []*discover.Node
- func (c *Config) TrustedNodes() []*discover.Node
- func (c *Config) WSEndpoint() string
- type DuplicateServiceError
- type Node
- func (n *Node) AccountManager() *accounts.Manager
- func (n *Node) Attach() (*rpc.Client, error)
- func (n *Node) Config() *Config
- func (n *Node) CorsServer() *p2p.Server
- func (n *Node) DataDir() string
- func (n *Node) EventMux() *event.TypeMux
- func (n *Node) GetKeyStore() *keystore.KeyStore
- func (n *Node) HTTPEndpoint() string
- func (n *Node) IPCEndpoint() string
- func (n *Node) InstanceDir() string
- func (n *Node) ListenAddr() string
- func (n *Node) OpenDatabase(path string, cache, handles int) (ptndb.Database, error)
- func (n *Node) RPCHandler() (*rpc.Server, error)
- func (n *Node) Register(constructor ServiceConstructor) error
- func (n *Node) ResolvePath(x string) string
- func (n *Node) Restart() error
- func (n *Node) Server() *p2p.Server
- func (n *Node) Service(service interface{}) error
- func (n *Node) Start() error
- func (n *Node) Stop() error
- func (n *Node) WSEndpoint() string
- func (n *Node) Wait()
- type PrivateAdminAPI
- func (api *PrivateAdminAPI) AddCorsPeer(url string) (bool, error)
- func (api *PrivateAdminAPI) AddPeer(url string) (bool, error)
- func (api *PrivateAdminAPI) AddTrustedPeer(url string) (bool, error)
- func (api *PrivateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription, error)
- func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error)
- func (api *PrivateAdminAPI) RemoveTrustedPeer(url string) (bool, error)
- func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error)
- func (api *PrivateAdminAPI) StartWS(host *string, port *int, allowedOrigins *string, apis *string) (bool, error)
- func (api *PrivateAdminAPI) StopRPC() (bool, error)
- func (api *PrivateAdminAPI) StopWS() (bool, error)
- type PublicAdminAPI
- func (api *PublicAdminAPI) CorsInfo() (*p2p.NodeInfo, error)
- func (api *PublicAdminAPI) CorsPeers(protocol string) ([]*p2p.PeerInfo, error)
- func (api *PublicAdminAPI) Datadir() string
- func (api *PublicAdminAPI) NodeInfo() (*p2p.NodeInfo, error)
- func (api *PublicAdminAPI) Peers() ([]*p2p.PeerInfo, error)
- type PublicDebugAPI
- type PublicWeb3API
- type Service
- type ServiceConstructor
- type ServiceContext
- 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 )
const (
GPTN = "gptn"
)
Variables ¶
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(), HTTPVirtualHosts: []string{"localhost"}, HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort, HTTPModules: []string{"net", "web3", "wallet", "dag", "personal", "mediator", "contract"}, HTTPs: false, HttpsCertFile: "", HttpsKeyFile: "", WSHost: DefaultWSHost, WSPort: DefaultWSPort, WSModules: []string{"net", "web3", "dag"}, WSExposeAll: false, }
DefaultConfig contains reasonable default settings.
Functions ¶
func DefaultDataDir ¶
func DefaultDataDir() string
DefaultDataDir is the default data directory to use for the databases and other persistence requirements. modified by Albert·Gou
func DefaultIPCEndpoint ¶
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 { // 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 gptn is "gptn". 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 IsTestNet bool `toml:"-"` // Configuration of peer-to-peer networking. P2P p2p.Config `toml:"-"` // 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"` // UseLightweightKDF lowers the memory and CPU requirements of the key store // scrypt KDF at the expense of security. UseLightweightKDF bool `toml:",omitempty"` // NoUSB disables hardware wallet monitoring and connectivity. NoUSB bool `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 `toml:",omitempty"` //HTTPS HTTPs bool //`toml:",omitempty"` //HttpsCAFile string HttpsCertFile string //`toml:",omitempty"` HttpsKeyFile string //`toml:",omitempty"` HttpsSecretKey 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 `toml:",omitempty"` // 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 `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 `toml:",omitempty"` // 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"` // 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 `toml:",omitempty"` // 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"` }
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) AccountConfig ¶
AccountConfig determines the settings for scrypt and keydirectory
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) 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 ¶
StaticNodes returns a list of node pnode URLs configured as static nodes.
func (*Config) TrustedNodes ¶
TrustedNodes returns a list of node pnode URLs configured as trusted nodes.
func (*Config) WSEndpoint ¶
WSEndpoint resolves a websocket endpoint based on the configured host interface and port parameters.
type DuplicateServiceError ¶
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 { CacheDb palletcache.ICache // global cache for use by other modules IsTestNet bool // contains filtered or unexported fields }
Node is a container on which services can be registered. 在区块链网络中,节点被定义为一个可以注册多种服务的容器。 node也可以理解为一个进程,整个区块链网络就是由运行在世界各地的很多中类型的node组成。 服务是一套独立的协议,可以基于P2P网络和PRC通信来提供具体的服务内容。 一个典型的node就是一个p2p的节点。根据节点的类型,运行了不同p2p网络协议, 运行了不同的业务层协议(以区别网络层协议。 参考p2p peer中的Protocol接口)。
func New ¶
New creates a new P2P node, ready for protocol registration. 创建一个 p2p 节点, 为协议(即service)注册做准备; 节点的初始化并不依赖其他的外部组件, 只依赖一个Config对象。
func (*Node) AccountManager ¶
AccountManager retrieves the account manager used by the protocol stack.
func (*Node) CorsServer ¶
func (*Node) EventMux ¶
EventMux retrieves the event multiplexer used by all the network services in the current protocol stack.
func (*Node) HTTPEndpoint ¶
HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.
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) OpenDatabase ¶
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) RPCHandler ¶
RPCHandler returns the in-process RPC request handler.
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) ResolvePath ¶
ResolvePath returns the absolute path of a resource in the instance directory.
func (*Node) Restart ¶
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 ¶
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) Start ¶
Start create a live P2P node and starts running it. 启动P2P服务,并且依次启动Register的各个serviceFuncs相关服务。
func (*Node) Stop ¶
Stop terminates a running node along with all it's services. In the node was not started, an error is returned.
func (*Node) WSEndpoint ¶
WSEndpoint retrieves the current WS endpoint used by the protocol stack.
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) AddCorsPeer ¶ added in v1.0.1
func (api *PrivateAdminAPI) AddCorsPeer(url string) (bool, error)
addCorsPeer
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) AddTrustedPeer ¶ added in v1.0.1
func (api *PrivateAdminAPI) AddTrustedPeer(url string) (bool, error)
AddTrustedPeer allows a remote node to always connect, even if slots are full
func (*PrivateAdminAPI) PeerEvents ¶
func (api *PrivateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription, error)
PeerEvents creates an RPC subscription which receives peer events from the node's p2p.Server
func (*PrivateAdminAPI) RemovePeer ¶
func (api *PrivateAdminAPI) RemovePeer(url string) (bool, error)
RemovePeer disconnects from a a remote node if the connection exists
func (*PrivateAdminAPI) RemoveTrustedPeer ¶ added in v1.0.1
func (api *PrivateAdminAPI) RemoveTrustedPeer(url string) (bool, error)
RemoveTrustedPeer removes a remote node from the trusted peer set, but it does not disconnect it automatically.
func (*PrivateAdminAPI) StartRPC ¶
func (api *PrivateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error)
StartRPC starts the HTTP RPC API server.
func (*PrivateAdminAPI) StartWS ¶
func (api *PrivateAdminAPI) StartWS(host *string, port *int, 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) CorsPeers ¶
func (api *PublicAdminAPI) CorsPeers(protocol string) ([]*p2p.PeerInfo, error)
func (*PublicAdminAPI) Datadir ¶
func (api *PublicAdminAPI) Datadir() string
Datadir retrieves the current data directory the node is using.
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.
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
type Service ¶
type Service interface { // Protocols retrieves the P2P protocols the service wishes to start. // 返回 service 要启动的 P2P 协议列表 Protocols() []p2p.Protocol CorsProtocols() []p2p.Protocol // APIs retrieves the list of RPC descriptors the service provides // 返回本 service 能提供的 RPC API 接口 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 方法是在所有服务构建之后和网络层初始化完后调用, // 在 start 方法用于开启大量 service 所需要的任何 goroutines Start(server *p2p.Server, corss *p2p.Server) error // Stop terminates all goroutines belonging to the service, blocking until they // are all terminated. // 停止service所有的goroutines,并阻塞线程直到所有goroutines都终止 Stop() error //GenesisHash GenesisHash() common.Hash }
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. Service是一个接口,定义了4个需要实现的函数。任何实现了这4个方法的类型,都可以称之为一个Service。 服务的生命周期管理已经代理给node管理。该服务允许在创建时自动初始化,但是在Start方法之外不应该启动goroutines。 重新启动逻辑不是必需的,因为节点将在每次启动服务时创建一个新的实例。
Example ¶
package main import ( "fmt" "log" "github.com/palletone/go-palletone/common" "github.com/palletone/go-palletone/common/p2p" "github.com/palletone/go-palletone/common/rpc" "github.com/palletone/go-palletone/core/node" ) // SampleService 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.Service: // - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on // - APIs() []rpc.API - api methods the service wants to expose on rpc channels // - Start() error - method invoked when the node is ready to start the service // - Stop() error - method invoked when the node terminates the service type SampleService struct{} func (s *SampleService) Protocols() []p2p.Protocol { return nil } func (s *SampleService) CorsProtocols() []p2p.Protocol { return nil } func (s *SampleService) GenesisHash() common.Hash { return common.Hash{} } func (s *SampleService) APIs() []rpc.API { return nil } func (s *SampleService) Start(*p2p.Server, *p2p.Server) error { fmt.Println("Service starting...") return nil } func (s *SampleService) 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) } // Create and register a simple network service. This is done through the definition // of a node.ServiceConstructor that will instantiate a node.Service. The reason for // the factory method approach is to support service restarts without relying on the // individual implementations' support for such operations. constructor := func(context *node.ServiceContext) (node.Service, error) { return new(SampleService), nil } if err := stack.Register(constructor); err != nil { log.Fatalf("Failed to register service: %v", err) } // 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.Restart(); err != nil { log.Fatalf("Failed to restart the protocol stack: %v", err) } if err := stack.Stop(); err != nil { log.Fatalf("Failed to stop the protocol stack: %v", err) } }
Output: Service starting... Service stopping... Service starting... Service stopping...
type ServiceConstructor ¶
type ServiceConstructor func(ctx *ServiceContext) (Service, error)
ServiceConstructor is the function signature of the constructors needed to be registered for service instantiation. ServiceConstructor是服务实例化时需要的构造函数的函数签名; 函数签名,如果两个函数的参数列表和返回值列表的变量类型能一一对应,那么这两个函数就有相同的签名。
type ServiceContext ¶
type ServiceContext struct { EventMux *event.TypeMux // Event multiplexer used for decoupled notifications AccountManager *accounts.Manager // Account manager created by the node. // 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. ServiceContext 是一些服务的集合,这些服务是从结点(或者叫协议栈)继承过来的,和具体服务无关, 具体服务的构造函数都被传递ServiceContext类型的参数以供选择使用; ServiceContext 也是在服务环境上运行的实用方法。
func (*ServiceContext) OpenDatabase ¶
func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ptndb.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) ResolvePath ¶
func (ctx *ServiceContext) ResolvePath(path string) string
ResolvePath resolves a user path into the data directory if that was relative and if the user actually uses persistent storage. It will return an empty string for emphemeral storage and the user's own input for absolute paths.
func (*ServiceContext) Service ¶
func (ctx *ServiceContext) Service(service interface{}) error
Service retrieves a currently running service registered of a specific type.