Documentation ¶
Index ¶
- func RegisterLifecycles(lifecycles LifecycleConstructors)
- type ExecAdapter
- type ExecNode
- func (n *ExecNode) Addr() []byte
- func (n *ExecNode) Client() (*rpc.Client, error)
- func (n *ExecNode) NodeInfo() *p2p.NodeInfo
- func (n *ExecNode) ServeRPC(clientConn *websocket.Conn) error
- func (n *ExecNode) Snapshots() (map[string][]byte, error)
- func (n *ExecNode) Start(snapshots map[string][]byte) (err error)
- func (n *ExecNode) Stop() error
- type LifecycleConstructor
- type LifecycleConstructors
- type Node
- type NodeAdapter
- type NodeConfig
- type RPCDialer
- type ServiceContext
- type SimAdapter
- func (s *SimAdapter) Dial(ctx context.Context, dest *enode.Node) (conn net.Conn, err error)
- func (s *SimAdapter) DialRPC(id enode.ID) (*rpc.Client, error)
- func (s *SimAdapter) GetNode(id enode.ID) (*SimNode, bool)
- func (s *SimAdapter) Name() string
- func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error)
- type SimNode
- func (sn *SimNode) Addr() []byte
- func (sn *SimNode) Client() (*rpc.Client, error)
- func (sn *SimNode) Close() error
- func (sn *SimNode) Node() *enode.Node
- func (sn *SimNode) NodeInfo() *p2p.NodeInfo
- func (sn *SimNode) ServeRPC(conn *websocket.Conn) error
- func (sn *SimNode) Server() *p2p.Server
- func (sn *SimNode) Service(name string) node.Lifecycle
- func (sn *SimNode) ServiceMap() map[string]node.Lifecycle
- func (sn *SimNode) Services() []node.Lifecycle
- func (sn *SimNode) Snapshots() (map[string][]byte, error)
- func (sn *SimNode) Start(snapshots map[string][]byte) error
- func (sn *SimNode) Stop() error
- func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription
- type SnapshotAPI
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterLifecycles ¶
func RegisterLifecycles(lifecycles LifecycleConstructors)
RegisterLifecycles registers the given Services which can then be used to start devp2p nodes using either the Exec or Docker adapters.
It should be called in an init function so that it has the opportunity to execute the services before main() is called. 为Exec或者Docker类型注册多个服务,这里注册的服务不对Sim类型生效 Sim类型注册服务在创建适配器的时候进行,adapters.NewSimAdapter(services)
Types ¶
type ExecAdapter ¶
type ExecAdapter struct { // BaseDir is the directory under which the data directories for each // simulation node are created. BaseDir string // contains filtered or unexported fields }
ExecAdapter is a NodeAdapter which runs simulation nodes by executing the current binary as a child process. 实现了NodeAdapter接口,需要实现Name和NewNode方法
func NewExecAdapter ¶
func NewExecAdapter(baseDir string) *ExecAdapter
NewExecAdapter returns an ExecAdapter which stores node data in subdirectories of the given base directory
func (*ExecAdapter) Name ¶
func (e *ExecAdapter) Name() string
Name returns the name of the adapter for logging purposes
func (*ExecAdapter) NewNode ¶
func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error)
NewNode returns a new ExecNode using the given config
type ExecNode ¶
type ExecNode struct { ID enode.ID Dir string Config *execNodeConfig Cmd *exec.Cmd Info *p2p.NodeInfo // contains filtered or unexported fields }
ExecNode starts a simulation node by exec'ing the current binary and running the configured services 由ExecAdapter.NewNode创建的节点就是ExecNode对象
func (*ExecNode) Client ¶
Client returns an rpc.Client which can be used to communicate with the underlying services (it is set once the node has started)
func (*ExecNode) ServeRPC ¶
ServeRPC serves RPC requests over the given connection by dialling the node's WebSocket address and joining the two connections
func (*ExecNode) Snapshots ¶
Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method
type LifecycleConstructor ¶
LifecycleConstructor allows a Lifecycle to be constructed during node start-up. While the service-specific package usually takes care of Lifecycle creation and registration, for testing purposes, it is useful to be able to construct a Lifecycle on spot. LifecycleConstructor在节点启动的时候用来构造Lifecycle对象
type LifecycleConstructors ¶
type LifecycleConstructors map[string]LifecycleConstructor
LifecycleConstructors stores LifecycleConstructor functions to call during node start-up. 使用一个map封装多个服务的构造方法, string->LifecycleConstructor代表服务的名称->该服务的构造函数
type Node ¶
type Node interface { // Addr returns the node's address (e.g. an Enode URL) Addr() []byte // Client returns the RPC client which is created once the node is // up and running Client() (*rpc.Client, error) // ServeRPC serves RPC requests over the given connection ServeRPC(*websocket.Conn) error // Start starts the node with the given snapshots Start(snapshots map[string][]byte) error // Stop stops the node Stop() error // NodeInfo returns information about the node NodeInfo() *p2p.NodeInfo // Snapshots creates snapshots of the running services Snapshots() (map[string][]byte, error) }
Node represents a node in a simulation network which is created by a NodeAdapter, for example:
* SimNode - An in-memory node * ExecNode - A child process node * DockerNode - A Docker container node
Node代表在仿真网络中由NodeAdapter创建的节点对象 该接口由SimNode和ExecNode实现 SimNode 内存中的节点 ExecNode 使用子进程的节点
type NodeAdapter ¶
type NodeAdapter interface { // Name returns the name of the adapter for logging purposes Name() string // NewNode creates a new node with the given configuration NewNode(config *NodeConfig) (Node, error) }
NodeAdapter is used to create Nodes in a simulation network NodeAdapter用来在仿真网络中创建节点 有SimAdapter和ExecAdapter
type NodeConfig ¶
type NodeConfig struct { // ID is the node's ID which is used to identify the node in the // simulation network ID enode.ID // PrivateKey is the node's private key which is used by the devp2p // stack to encrypt communications // 必须指定私钥 PrivateKey *ecdsa.PrivateKey // Enable peer events for Msgs EnableMsgEvents bool // Name is a human friendly name for the node like "node01" Name string // Use an existing database instead of a temporary one if non-empty DataDir string // Lifecycles are the names of the service lifecycles which should be run when // starting the node (for SimNodes it should be the names of service lifecycles // contained in SimAdapter.lifecycles, for other nodes it should be // service lifecycles registered by calling the RegisterLifecycle function) // 必须指定至少一个服务名称 Lifecycles []string // Properties are the names of the properties this node should hold // within running services (e.g. "bootnode", "lightnode" or any custom values) // These values need to be checked and acted upon by node Services Properties []string // ExternalSigner specifies an external URI for a clef-type signer ExternalSigner string // ENR Record with entries to overwrite // 这里记录的信息优先级低,如果设置了Port会,记录中的端口也会更新 Record enr.Record // function to sanction or prevent suggesting a peer Reachable func(id enode.ID) bool Port uint16 // LogFile is the log file name of the p2p node at runtime. // // The default value is empty so that the default log writer // is the system standard output. LogFile string // LogVerbosity is the log verbosity of the p2p node at runtime. // // The default verbosity is INFO. LogVerbosity log.Lvl // contains filtered or unexported fields }
NodeConfig is the configuration used to start a node in a simulation network 使用适配器创建节点的配置,在NodeAdapter.NewNode函数中需要传入NodeConfig对象
func RandomNodeConfig ¶
func RandomNodeConfig() *NodeConfig
RandomNodeConfig returns node configuration with a randomly generated ID and PrivateKey 构造一个NodeConfig对象,随机生成节点的ID和私钥
func (*NodeConfig) MarshalJSON ¶
func (n *NodeConfig) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface by encoding the config fields as strings 将NodeConfig对象编码为json字符串
func (*NodeConfig) Node ¶
func (n *NodeConfig) Node() *enode.Node
Node returns the node descriptor represented by the config.
func (*NodeConfig) UnmarshalJSON ¶
func (n *NodeConfig) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface by decoding the json string values into the config fields 将json字符串转化为NodeConfig对象
type RPCDialer ¶
RPCDialer is used when initialising services which need to connect to other nodes in the network (for example a simulated Swarm node which needs to connect to a Geth node to resolve ENS names)
type ServiceContext ¶
type ServiceContext struct { RPCDialer Config *NodeConfig Snapshot []byte }
ServiceContext is a collection of options and methods which can be utilised when starting services
type SimAdapter ¶
type SimAdapter struct {
// contains filtered or unexported fields
}
SimAdapter is a NodeAdapter which creates in-memory simulation nodes and connects them using net.Pipe 用来创建使用net.Pipe进行通信的仿真节点 实现了p2p.NodeDialer,adapters.NodeAdapter,adapters.RPCDialer接口
func NewSimAdapter ¶
func NewSimAdapter(services LifecycleConstructors) *SimAdapter
NewSimAdapter creates a SimAdapter which is capable of running in-memory simulation nodes running any of the given services (the services to run on a particular node are passed to the NewNode function in the NodeConfig) the adapter uses a net.Pipe for in-memory simulated network connections 创建SimAdapter需要输入支持的服务,因为SimAdapter不使用RegisterLifecycles中注册的服务
func (*SimAdapter) Dial ¶
Dial implements the p2p.NodeDialer interface by connecting to the node using an in-memory net.Pipe 实现p2p.NodeDialer接口
func (*SimAdapter) DialRPC ¶
DialRPC implements the RPCDialer interface by creating an in-memory RPC client of the given node
func (*SimAdapter) GetNode ¶
func (s *SimAdapter) GetNode(id enode.ID) (*SimNode, bool)
GetNode returns the node with the given ID if it exists 查找SimAdapter.nodes[id]
func (*SimAdapter) Name ¶
func (s *SimAdapter) Name() string
Name returns the name of the adapter for logging purposes
func (*SimAdapter) NewNode ¶
func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error)
NewNode returns a new SimNode using the given config 输入内存节点的配置用来创建内存型节点 配置必须包含私钥和节点运行的服务,配置可以由RandomNodeConfig方法生成
type SimNode ¶
SimNode is an in-memory simulation node which connects to other nodes using net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that pipe 由SimAdapter.NewNode创建的节点就是SimNode对象 SimNode实现了adapters.Node接口,但是还额外实现了几个函数 Close,Node,Server,Service,ServiceMap,Services,SubscribeEvents
func (*SimNode) Client ¶
Client returns an rpc.Client which can be used to communicate with the underlying services (it is set once the node has started)
func (*SimNode) ServeRPC ¶
ServeRPC serves RPC requests over the given connection by creating an in-memory client to the node's RPC server.
func (*SimNode) ServiceMap ¶
ServiceMap returns a map by names of the underlying services
func (*SimNode) Snapshots ¶
Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method
func (*SimNode) Start ¶
Start registers the services and starts the underlying devp2p node 为节点注册服务,并启动节点
func (*SimNode) SubscribeEvents ¶
func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription
SubscribeEvents subscribes the given channel to peer events from the underlying p2p.Server
type SnapshotAPI ¶
type SnapshotAPI struct {
// contains filtered or unexported fields
}
SnapshotAPI provides an RPC method to create snapshots of services