adapters

package
v0.0.0-...-d766351 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2023 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

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.

Types

type ExecAdapter

type ExecAdapter struct {
	// BaseDir is the directory under which the data directories for each
	// simulation nokta 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

func NewExecAdapter(baseDir string) *ExecAdapter

NewExecAdapter returns an ExecAdapter which stores nokta 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) (Nokta, 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 nokta by exec'ing the current binary and running the configured services

func (*ExecNode) Addr

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

Addr returns the nokta's enode URL

func (*ExecNode) Client

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 nokta has started)

func (*ExecNode) NodeInfo

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

NodeInfo returns information about the nokta

func (*ExecNode) ServeRPC

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

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

func (*ExecNode) Snapshots

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

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

func (*ExecNode) Start

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

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

func (*ExecNode) Stop

func (n *ExecNode) Stop() error

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

type LifecycleConstructor

type LifecycleConstructor func(ctx *ServiceContext, stack *nokta.Nokta) (nokta.Lifecycle, error)

LifecycleConstructor allows a Lifecycle to be constructed during nokta 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.

type LifecycleConstructors

type LifecycleConstructors map[string]LifecycleConstructor

LifecycleConstructors stores LifecycleConstructor functions to call during nokta start-up.

type NodeAdapter

type NodeAdapter interface {
	// Name returns the name of the adapter for logging purposes
	Name() string

	// NewNode creates a new nokta with the given configuration
	NewNode(config *NodeConfig) (Nokta, error)
}

NodeAdapter is used to create Nodes in a simulation network

type NodeConfig

type NodeConfig struct {
	// ID is the nokta's ID which is used to identify the nokta in the
	// simulation network
	ID enode.ID

	// PrivateKey is the nokta'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 nokta 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 nokta (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 nokta should hold
	// within running services (e.g. "bootnode", "lightnode" or any custom values)
	// These values need to be checked and acted upon by nokta Services
	Properties []string

	// ExternalSigner specifies an external URI for a clef-type imzalayan
	ExternalSigner 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

	// LogFile is the log file name of the p2p nokta 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 nokta at runtime.
	//
	// The default verbosity is INFO.
	LogVerbosity log.Lvl
	// contains filtered or unexported fields
}

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

func RandomNodeConfig

func RandomNodeConfig() *NodeConfig

RandomNodeConfig returns nokta 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) Nokta

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

Nokta returns the nokta 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

type Nokta

type Nokta interface {
	// Addr returns the nokta's address (e.g. an Enode URL)
	Addr() []byte

	// Client returns the RPC client which is created once the nokta is
	// up and running
	Client() (*rpc.Client, error)

	// ServeRPC serves RPC requests over the given connection
	ServeRPC(*websocket.Conn) error

	// Start starts the nokta with the given snapshots
	Start(snapshots map[string][]byte) error

	// Stop stops the nokta
	Stop() error

	// NodeInfo returns information about the nokta
	NodeInfo() *p2p.NodeInfo

	// Snapshots creates snapshots of the running services
	Snapshots() (map[string][]byte, error)
}

Nokta represents a nokta in a simulation network which is created by a NodeAdapter, for example:

  • SimNode, an in-memory nokta in the same process
  • ExecNode, a child process nokta
  • DockerNode, a nokta running in a Docker container

type RPCDialer

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 Birlik nokta which needs to connect to a Haydi nokta 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

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 nokta are passed to the NewNode function in the NodeConfig) the adapter uses a net.Pipe for in-memory simulated network connections

func (*SimAdapter) Dial

func (s *SimAdapter) Dial(ctx context.Context, dest *enode.Nokta) (conn net.Conn, err error)

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

func (*SimAdapter) DialRPC

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 nokta

func (*SimAdapter) GetNode

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

GetNode returns the nokta 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) (Nokta, error)

NewNode returns a new SimNode using the given config

type SimNode

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

SimNode is an in-memory simulation nokta which connects to other nodes using net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that pipe

func (*SimNode) Addr

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

Addr returns the nokta's discovery address

func (*SimNode) Client

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 nokta has started)

func (*SimNode) Close

func (sn *SimNode) Close() error

Close closes the underlaying nokta.Nokta to release acquired resources.

func (*SimNode) NodeInfo

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

NodeInfo returns information about the nokta

func (*SimNode) Nokta

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

Nokta returns a nokta descriptor representing the SimNode

func (*SimNode) ServeRPC

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

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

func (*SimNode) Server

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

Server returns the underlying p2p.Server

func (*SimNode) Service

func (sn *SimNode) Service(name string) nokta.Lifecycle

Service returns a running service by name

func (*SimNode) ServiceMap

func (sn *SimNode) ServiceMap() map[string]nokta.Lifecycle

ServiceMap returns a map by names of the underlying services

func (*SimNode) Services

func (sn *SimNode) Services() []nokta.Lifecycle

Services returns a copy of the underlying services

func (*SimNode) Snapshots

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

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

func (*SimNode) Start

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

Start registers the services and starts the underlying devp2p nokta

func (*SimNode) Stop

func (sn *SimNode) Stop() error

Stop closes the RPC client and stops the underlying devp2p nokta

func (*SimNode) SubscribeEvents

func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) etkinlik.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

func (SnapshotAPI) Snapshot

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL