go-ethereum: github.com/axiomzen/go-ethereum/p2p/simulations/adapters Index | Files

package adapters

import "github.com/axiomzen/go-ethereum/p2p/simulations/adapters"

Index

Package Files

exec.go inproc.go types.go

func RegisterServices Uses

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.

type ExecAdapter Uses

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.

func NewExecAdapter Uses

func NewExecAdapter(baseDir string) *ExecAdapter

NewExecAdapter returns an ExecAdapter which stores node data in subdirectories of the given base directory

func (*ExecAdapter) Name Uses

func (e *ExecAdapter) Name() string

Name returns the name of the adapter for logging purposes

func (*ExecAdapter) NewNode Uses

func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error)

NewNode returns a new ExecNode using the given config

type ExecNode Uses

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

func (*ExecNode) Addr Uses

func (n *ExecNode) Addr() []byte

Addr returns the node's enode URL

func (*ExecNode) Client Uses

func (n *ExecNode) Client() (*rpc.Client, error)

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) NodeInfo Uses

func (n *ExecNode) NodeInfo() *p2p.NodeInfo

NodeInfo returns information about the node

func (*ExecNode) ServeRPC Uses

func (n *ExecNode) ServeRPC(clientConn net.Conn) error

ServeRPC serves RPC requests over the given connection by dialling the node's WebSocket address and joining the two connections

func (*ExecNode) Snapshots Uses

func (n *ExecNode) Snapshots() (map[string][]byte, error)

Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method

func (*ExecNode) Start Uses

func (n *ExecNode) Start(snapshots map[string][]byte) (err error)

Start exec's the node passing the ID and service as command line arguments and the node config encoded as JSON in an environment variable.

func (*ExecNode) Stop Uses

func (n *ExecNode) Stop() error

Stop stops the node by first sending SIGTERM and then SIGKILL if the node doesn't stop within 5s

type Node Uses

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(net.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

type NodeAdapter Uses

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 Uses

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

    // 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

    // ENR Record with entries to overwrite
    Record enr.Record

    // function to sanction or prevent suggesting a peer
    Reachable func(id enode.ID) bool

    Port uint16
    // contains filtered or unexported fields
}

NodeConfig is the configuration used to start a node in a simulation network

func RandomNodeConfig Uses

func RandomNodeConfig() *NodeConfig

RandomNodeConfig returns node configuration with a randomly generated ID and PrivateKey

func (*NodeConfig) MarshalJSON Uses

func (n *NodeConfig) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface by encoding the config fields as strings

func (*NodeConfig) Node Uses

func (n *NodeConfig) Node() *enode.Node

Node returns the node descriptor represented by the config.

func (*NodeConfig) UnmarshalJSON Uses

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 Uses

type RPCDialer interface {
    DialRPC(id enode.ID) (*rpc.Client, error)
}

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 Uses

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 Uses

type ServiceFunc func(ctx *ServiceContext) (node.Service, error)

ServiceFunc returns a node.Service which can be used to boot a devp2p node

type Services Uses

type Services map[string]ServiceFunc

Services is a collection of services which can be run in a simulation

type SimAdapter Uses

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 Uses

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 Uses

func NewTCPAdapter(services map[string]ServiceFunc) *SimAdapter

func (*SimAdapter) Dial Uses

func (s *SimAdapter) Dial(dest *enode.Node) (conn net.Conn, err error)

Dial implements the p2p.NodeDialer interface by connecting to the node using an in-memory net.Pipe

func (*SimAdapter) DialRPC Uses

func (s *SimAdapter) DialRPC(id enode.ID) (*rpc.Client, error)

DialRPC implements the RPCDialer interface by creating an in-memory RPC client of the given node

func (*SimAdapter) GetNode Uses

func (s *SimAdapter) GetNode(id enode.ID) (*SimNode, bool)

GetNode returns the node with the given ID if it exists

func (*SimAdapter) Name Uses

func (s *SimAdapter) Name() string

Name returns the name of the adapter for logging purposes

func (*SimAdapter) NewNode Uses

func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error)

NewNode returns a new SimNode using the given config

type SimNode Uses

type SimNode struct {
    ID enode.ID
    // contains filtered or unexported fields
}

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) Addr Uses

func (sn *SimNode) Addr() []byte

Addr returns the node's discovery address

func (*SimNode) Client Uses

func (sn *SimNode) Client() (*rpc.Client, error)

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) Close Uses

func (sn *SimNode) Close() error

Close closes the underlaying node.Node to release acquired resources.

func (*SimNode) Node Uses

func (sn *SimNode) Node() *enode.Node

Node returns a node descriptor representing the SimNode

func (*SimNode) NodeInfo Uses

func (sn *SimNode) NodeInfo() *p2p.NodeInfo

NodeInfo returns information about the node

func (*SimNode) ServeRPC Uses

func (sn *SimNode) ServeRPC(conn net.Conn) error

ServeRPC serves RPC requests over the given connection by creating an in-memory client to the node's RPC server

func (*SimNode) Server Uses

func (sn *SimNode) Server() *p2p.Server

Server returns the underlying p2p.Server

func (*SimNode) Service Uses

func (sn *SimNode) Service(name string) node.Service

Service returns a running service by name

func (*SimNode) ServiceMap Uses

func (sn *SimNode) ServiceMap() map[string]node.Service

ServiceMap returns a map by names of the underlying services

func (*SimNode) Services Uses

func (sn *SimNode) Services() []node.Service

Services returns a copy of the underlying services

func (*SimNode) Snapshots Uses

func (sn *SimNode) Snapshots() (map[string][]byte, error)

Snapshots creates snapshots of the services by calling the simulation_snapshot RPC method

func (*SimNode) Start Uses

func (sn *SimNode) Start(snapshots map[string][]byte) error

Start registers the services and starts the underlying devp2p node

func (*SimNode) Stop Uses

func (sn *SimNode) Stop() error

Stop closes the RPC client and stops the underlying devp2p node

func (*SimNode) SubscribeEvents Uses

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 Uses

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

SnapshotAPI provides an RPC method to create snapshots of services

func (SnapshotAPI) Snapshot Uses

func (api SnapshotAPI) Snapshot() (map[string][]byte, error)

Package adapters imports 31 packages (graph). Updated 2019-07-06. Refresh now. Tools for package owners.