Documentation ¶
Overview ¶
Package adapters implements simulation network adapters in several ways.
Index ¶
- Variables
- func ExternalIP() net.IP
- func RegisterServices(services Services)
- type CnAdapter
- func (s *CnAdapter) Dial(dest *discover.Node) (conn net.Conn, err error)
- func (s *CnAdapter) DialRPC(id discover.NodeID) (*rpc.Client, error)
- func (s *CnAdapter) GetNode(id discover.NodeID) (*CnNode, bool)
- func (s *CnAdapter) Name() string
- func (s *CnAdapter) NewNode(config *NodeConfig) (Node, error)
- type CnNode
- func (sn *CnNode) Addr() []byte
- func (sn *CnNode) Client() (*rpc.Client, error)
- func (sn *CnNode) DisconnectPeer(destID discover.NodeID)
- func (sn *CnNode) GetPeerCount() int
- func (sn *CnNode) Node() *discover.Node
- func (sn *CnNode) NodeInfo() *p2p.NodeInfo
- func (sn *CnNode) PeersInfo() []*p2p.PeerInfo
- func (sn *CnNode) ServeRPC(conn *websocket.Conn) error
- func (sn *CnNode) Server() p2p.Server
- func (sn *CnNode) Services() []node.Service
- func (sn *CnNode) Snapshots() (map[string][]byte, error)
- func (sn *CnNode) Start(snapshots map[string][]byte) error
- func (sn *CnNode) Stop() error
- func (sn *CnNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription
- type DockerAdapter
- type DockerNode
- type ExecAdapter
- type ExecNode
- func (n *ExecNode) Addr() []byte
- func (n *ExecNode) Client() (*rpc.Client, error)
- func (n *ExecNode) DisconnectPeer(destID discover.NodeID)
- func (n *ExecNode) GetPeerCount() int
- func (n *ExecNode) NodeInfo() *p2p.NodeInfo
- func (sn *ExecNode) PeersInfo() []*p2p.PeerInfo
- 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 Node
- type NodeAdapter
- type NodeConfig
- type RPCDialer
- type ServiceContext
- type ServiceFunc
- type Services
- type SimAdapter
- func (s *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error)
- func (s *SimAdapter) DialMulti(dest *discover.Node) (conn []net.Conn, err error)
- func (s *SimAdapter) DialRPC(id discover.NodeID) (*rpc.Client, error)
- func (s *SimAdapter) GetNode(id discover.NodeID) (*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 (n *SimNode) DisconnectPeer(destID discover.NodeID)
- func (sn *SimNode) GetPeerCount() int
- func (sn *SimNode) Node() *discover.Node
- func (sn *SimNode) NodeInfo() *p2p.NodeInfo
- func (sn *SimNode) PeersInfo() []*p2p.PeerInfo
- func (sn *SimNode) ServeRPC(conn *websocket.Conn) error
- func (sn *SimNode) Server() p2p.Server
- func (sn *SimNode) Services() []node.Service
- 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 ¶
var ErrLinuxOnly = errors.New("DockerAdapter can only be used on Linux as it uses the current binary (which must be a Linux binary)")
Functions ¶
func ExternalIP ¶
ExternalIP gets an external IP address so that Enode URL is usable
func RegisterServices ¶
func RegisterServices(services Services)
RegisterServices 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.
Types ¶
type CnAdapter ¶
type CnAdapter struct {
// contains filtered or unexported fields
}
CnAdapter is a NodeAdapter which creates in-memory simulation nodes and connects them using net.Pipe
func NewCnAdapter ¶
func NewCnAdapter(services map[string]ServiceFunc) *CnAdapter
NewCnAdapter creates a CnAdapter 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
func (*CnAdapter) Dial ¶
TODO : NOT USED Dial implements the p2p.NodeDialer interface by connecting to the node using an in-memory net.Pipe
func (*CnAdapter) DialRPC ¶
DialRPC implements the RPCDialer interface by creating an in-memory RPC client of the given node
type CnNode ¶
CnNode is an in-memory simulation node which connects to other nodes using net.Pipe (see CnAdapter.Dial), running devp2p protocols directly over that pipe
func (*CnNode) 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 (*CnNode) DisconnectPeer ¶
func (*CnNode) GetPeerCount ¶
func (*CnNode) ServeRPC ¶
ServeRPC serves RPC requests over the given connection by creating an in-memory client to the node's RPC server
func (*CnNode) Snapshots ¶
Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method
func (*CnNode) SubscribeEvents ¶
func (sn *CnNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription
SubscribeEvents subscribes the given channel to peer events from the underlying p2p.Server
type DockerAdapter ¶
type DockerAdapter struct {
ExecAdapter
}
DockerAdapter is a NodeAdapter which runs simulation nodes inside Docker containers.
A Docker image is built which contains the current binary at /bin/p2p-node which when executed runs the underlying service (see the description of the execP2PNode function for more details)
func NewDockerAdapter ¶
func NewDockerAdapter() (*DockerAdapter, error)
NewDockerAdapter builds the p2p-node Docker image containing the current binary and returns a DockerAdapter
func (*DockerAdapter) Name ¶
func (d *DockerAdapter) Name() string
Name returns the name of the adapter for logging purposes
func (*DockerAdapter) NewNode ¶
func (d *DockerAdapter) NewNode(config *NodeConfig) (Node, error)
NewNode returns a new DockerNode using the given config
type DockerNode ¶
type DockerNode struct {
ExecNode
}
DockerNode wraps an ExecNode but exec's the current binary in a docker container rather than locally
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.
An init hook is used so that the child process executes the node services (rather than whataver the main() function would normally do), see the execP2PNode function for more information.
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 discover.NodeID 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
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) DisconnectPeer ¶
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 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 // PeersInfo returns informations about the peers PeersInfo() []*p2p.PeerInfo // Snapshots creates snapshots of the running services Snapshots() (map[string][]byte, error) // GetPeerCount returns peer count of server in the node GetPeerCount() int // DisconnectPeer close connection to peer of destID DisconnectPeer(destID discover.NodeID) }
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
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
type NodeConfig ¶
type NodeConfig struct { // ID is the node's ID which is used to identify the node in the // simulation network ID discover.NodeID // 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 // Services are the names of the services which should be run when // starting the node (for SimNodes it should be the names of services // contained in SimAdapter.services, for other nodes it should be // services registered by calling the RegisterService function) Services []string // function to sanction or prevent suggesting a peer Reachable func(id discover.NodeID) bool Port uint16 }
NodeConfig is the configuration used to start a node in a simulation network
func RandomNodeConfig ¶
func RandomNodeConfig() *NodeConfig
RandomNodeConfig returns node configuration with a randomly generated ID and PrivateKey
func (*NodeConfig) MarshalJSON ¶
func (n *NodeConfig) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface by encoding the config fields as strings
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
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 the Kaia node to resolve ENS names)
type ServiceContext ¶
type ServiceContext struct { RPCDialer NodeContext *node.ServiceContext Config *NodeConfig Snapshot []byte }
ServiceContext is a collection of options and methods which can be utilised when starting services
type ServiceFunc ¶
type ServiceFunc func(ctx *ServiceContext) (node.Service, error)
ServiceFunc returns a node.Service which can be used to boot a devp2p node
type Services ¶
type Services map[string]ServiceFunc
Services is a collection of services which can be run in a simulation
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
func NewSimAdapter ¶
func NewSimAdapter(services map[string]ServiceFunc) *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
func NewTCPAdapter ¶
func NewTCPAdapter(services map[string]ServiceFunc) *SimAdapter
func (*SimAdapter) Dial ¶
Dial implements the p2p.NodeDialer interface by connecting to the node using an in-memory net.Pipe
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 discover.NodeID) (*SimNode, bool)
GetNode returns the node with the given ID if it exists
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
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
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) DisconnectPeer ¶
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) Snapshots ¶
Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method
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