2017-10-17 09:58:04 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-02-27 10:39:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-10-17 09:58:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2018-08-07 13:31:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
|
2017-10-17 09:58:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SimAdapter is a NodeAdapter which creates in-memory simulation nodes and
|
2018-08-07 13:31:06 +00:00
|
|
|
// connects them using net.Pipe
|
2017-10-17 09:58:04 +00:00
|
|
|
type SimAdapter struct {
|
2018-08-07 13:31:06 +00:00
|
|
|
pipe func() (net.Conn, net.Conn, error)
|
2017-10-17 09:58:04 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
nodes map[discover.NodeID]*SimNode
|
|
|
|
services map[string]ServiceFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-08-07 13:31:06 +00:00
|
|
|
// the adapter uses a net.Pipe for in-memory simulated network connections
|
2017-10-17 09:58:04 +00:00
|
|
|
func NewSimAdapter(services map[string]ServiceFunc) *SimAdapter {
|
|
|
|
return &SimAdapter{
|
2018-08-07 13:31:06 +00:00
|
|
|
pipe: pipes.NetPipe,
|
|
|
|
nodes: make(map[discover.NodeID]*SimNode),
|
|
|
|
services: services,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTCPAdapter(services map[string]ServiceFunc) *SimAdapter {
|
|
|
|
return &SimAdapter{
|
|
|
|
pipe: pipes.TCPPipe,
|
2017-10-17 09:58:04 +00:00
|
|
|
nodes: make(map[discover.NodeID]*SimNode),
|
|
|
|
services: services,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the adapter for logging purposes
|
|
|
|
func (s *SimAdapter) Name() string {
|
|
|
|
return "sim-adapter"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNode returns a new SimNode using the given config
|
|
|
|
func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
// check a node with the ID doesn't already exist
|
|
|
|
id := config.ID
|
|
|
|
if _, exists := s.nodes[id]; exists {
|
|
|
|
return nil, fmt.Errorf("node already exists: %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the services are valid
|
|
|
|
if len(config.Services) == 0 {
|
|
|
|
return nil, errors.New("node must have at least one service")
|
|
|
|
}
|
|
|
|
for _, service := range config.Services {
|
|
|
|
if _, exists := s.services[service]; !exists {
|
|
|
|
return nil, fmt.Errorf("unknown node service %q", service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := node.New(&node.Config{
|
|
|
|
P2P: p2p.Config{
|
|
|
|
PrivateKey: config.PrivateKey,
|
|
|
|
MaxPeers: math.MaxInt32,
|
|
|
|
NoDiscovery: true,
|
|
|
|
Dialer: s,
|
2018-08-07 13:31:06 +00:00
|
|
|
EnableMsgEvents: config.EnableMsgEvents,
|
2017-10-17 09:58:04 +00:00
|
|
|
},
|
2018-02-27 10:39:30 +00:00
|
|
|
NoUSB: true,
|
|
|
|
Logger: log.New("node.id", id.String()),
|
2017-10-17 09:58:04 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
simNode := &SimNode{
|
|
|
|
ID: id,
|
|
|
|
config: config,
|
|
|
|
node: n,
|
|
|
|
adapter: s,
|
|
|
|
running: make(map[string]node.Service),
|
|
|
|
}
|
|
|
|
s.nodes[id] = simNode
|
|
|
|
return simNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial implements the p2p.NodeDialer interface by connecting to the node using
|
2018-08-07 13:31:06 +00:00
|
|
|
// an in-memory net.Pipe
|
2017-10-17 09:58:04 +00:00
|
|
|
func (s *SimAdapter) Dial(dest *discover.Node) (conn net.Conn, err error) {
|
|
|
|
node, ok := s.GetNode(dest.ID)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown node: %s", dest.ID)
|
|
|
|
}
|
|
|
|
srv := node.Server()
|
|
|
|
if srv == nil {
|
|
|
|
return nil, fmt.Errorf("node not running: %s", dest.ID)
|
|
|
|
}
|
2018-08-07 13:31:06 +00:00
|
|
|
// SimAdapter.pipe is net.Pipe (NewSimAdapter)
|
|
|
|
pipe1, pipe2, err := s.pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// this is simulated 'listening'
|
|
|
|
// asynchronously call the dialed destintion node's p2p server
|
|
|
|
// to set up connection on the 'listening' side
|
2017-10-17 09:58:04 +00:00
|
|
|
go srv.SetupConn(pipe1, 0, nil)
|
|
|
|
return pipe2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialRPC implements the RPCDialer interface by creating an in-memory RPC
|
|
|
|
// client of the given node
|
|
|
|
func (s *SimAdapter) DialRPC(id discover.NodeID) (*rpc.Client, error) {
|
|
|
|
node, ok := s.GetNode(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown node: %s", id)
|
|
|
|
}
|
|
|
|
handler, err := node.node.RPCHandler()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rpc.DialInProc(handler), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode returns the node with the given ID if it exists
|
|
|
|
func (s *SimAdapter) GetNode(id discover.NodeID) (*SimNode, bool) {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
node, ok := s.nodes[id]
|
|
|
|
return node, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimNode is an in-memory simulation node which connects to other nodes using
|
2018-08-07 13:31:06 +00:00
|
|
|
// net.Pipe (see SimAdapter.Dial), running devp2p protocols directly over that
|
|
|
|
// pipe
|
2017-10-17 09:58:04 +00:00
|
|
|
type SimNode struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
ID discover.NodeID
|
|
|
|
config *NodeConfig
|
|
|
|
adapter *SimAdapter
|
|
|
|
node *node.Node
|
|
|
|
running map[string]node.Service
|
|
|
|
client *rpc.Client
|
|
|
|
registerOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the node's discovery address
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Addr() []byte {
|
|
|
|
return []byte(sn.Node().String())
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns a discover.Node representing the SimNode
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Node() *discover.Node {
|
|
|
|
return discover.NewNode(sn.ID, net.IP{127, 0, 0, 1}, 30303, 30303)
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns an rpc.Client which can be used to communicate with the
|
|
|
|
// underlying services (it is set once the node has started)
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Client() (*rpc.Client, error) {
|
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
if sn.client == nil {
|
2017-10-17 09:58:04 +00:00
|
|
|
return nil, errors.New("node not started")
|
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
return sn.client, nil
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeRPC serves RPC requests over the given connection by creating an
|
|
|
|
// in-memory client to the node's RPC server
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) ServeRPC(conn net.Conn) error {
|
|
|
|
handler, err := sn.node.RPCHandler()
|
2017-10-17 09:58:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
handler.ServeCodec(rpc.NewJSONCodec(conn), rpc.OptionMethodInvocation|rpc.OptionSubscriptions)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshots creates snapshots of the services by calling the
|
|
|
|
// simulation_snapshot RPC method
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Snapshots() (map[string][]byte, error) {
|
|
|
|
sn.lock.RLock()
|
|
|
|
services := make(map[string]node.Service, len(sn.running))
|
|
|
|
for name, service := range sn.running {
|
2017-10-17 09:58:04 +00:00
|
|
|
services[name] = service
|
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
sn.lock.RUnlock()
|
2017-10-17 09:58:04 +00:00
|
|
|
if len(services) == 0 {
|
|
|
|
return nil, errors.New("no running services")
|
|
|
|
}
|
|
|
|
snapshots := make(map[string][]byte)
|
|
|
|
for name, service := range services {
|
|
|
|
if s, ok := service.(interface {
|
|
|
|
Snapshot() ([]byte, error)
|
|
|
|
}); ok {
|
|
|
|
snap, err := s.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
snapshots[name] = snap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return snapshots, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start registers the services and starts the underlying devp2p node
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Start(snapshots map[string][]byte) error {
|
2017-10-17 09:58:04 +00:00
|
|
|
newService := func(name string) func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
return func(nodeCtx *node.ServiceContext) (node.Service, error) {
|
|
|
|
ctx := &ServiceContext{
|
2018-06-20 14:51:55 +00:00
|
|
|
RPCDialer: sn.adapter,
|
2017-10-17 09:58:04 +00:00
|
|
|
NodeContext: nodeCtx,
|
2018-06-20 14:51:55 +00:00
|
|
|
Config: sn.config,
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
if snapshots != nil {
|
|
|
|
ctx.Snapshot = snapshots[name]
|
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
serviceFunc := sn.adapter.services[name]
|
2017-10-17 09:58:04 +00:00
|
|
|
service, err := serviceFunc(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
sn.running[name] = service
|
2017-10-17 09:58:04 +00:00
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we only register the services once in the case of the node
|
|
|
|
// being stopped and then started again
|
|
|
|
var regErr error
|
2018-06-20 14:51:55 +00:00
|
|
|
sn.registerOnce.Do(func() {
|
|
|
|
for _, name := range sn.config.Services {
|
|
|
|
if err := sn.node.Register(newService(name)); err != nil {
|
2017-10-17 09:58:04 +00:00
|
|
|
regErr = err
|
2018-08-07 13:31:06 +00:00
|
|
|
break
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if regErr != nil {
|
|
|
|
return regErr
|
|
|
|
}
|
|
|
|
|
2018-06-20 14:51:55 +00:00
|
|
|
if err := sn.node.Start(); err != nil {
|
2017-10-17 09:58:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an in-process RPC client
|
2018-06-20 14:51:55 +00:00
|
|
|
handler, err := sn.node.RPCHandler()
|
2017-10-17 09:58:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-20 14:51:55 +00:00
|
|
|
sn.lock.Lock()
|
|
|
|
sn.client = rpc.DialInProc(handler)
|
|
|
|
sn.lock.Unlock()
|
2017-10-17 09:58:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop closes the RPC client and stops the underlying devp2p node
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Stop() error {
|
|
|
|
sn.lock.Lock()
|
|
|
|
if sn.client != nil {
|
|
|
|
sn.client.Close()
|
|
|
|
sn.client = nil
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
2018-06-20 14:51:55 +00:00
|
|
|
sn.lock.Unlock()
|
|
|
|
return sn.node.Stop()
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 13:31:06 +00:00
|
|
|
// Service returns a running service by name
|
|
|
|
func (sn *SimNode) Service(name string) node.Service {
|
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
return sn.running[name]
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:58:04 +00:00
|
|
|
// Services returns a copy of the underlying services
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Services() []node.Service {
|
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
services := make([]node.Service, 0, len(sn.running))
|
|
|
|
for _, service := range sn.running {
|
2017-10-17 09:58:04 +00:00
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2018-08-07 13:31:06 +00:00
|
|
|
// ServiceMap returns a map by names of the underlying services
|
|
|
|
func (sn *SimNode) ServiceMap() map[string]node.Service {
|
|
|
|
sn.lock.RLock()
|
|
|
|
defer sn.lock.RUnlock()
|
|
|
|
services := make(map[string]node.Service, len(sn.running))
|
|
|
|
for name, service := range sn.running {
|
|
|
|
services[name] = service
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:58:04 +00:00
|
|
|
// Server returns the underlying p2p.Server
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) Server() *p2p.Server {
|
|
|
|
return sn.node.Server()
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeEvents subscribes the given channel to peer events from the
|
|
|
|
// underlying p2p.Server
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription {
|
|
|
|
srv := sn.Server()
|
2017-10-17 09:58:04 +00:00
|
|
|
if srv == nil {
|
|
|
|
panic("node not running")
|
|
|
|
}
|
|
|
|
return srv.SubscribeEvents(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeInfo returns information about the node
|
2018-06-20 14:51:55 +00:00
|
|
|
func (sn *SimNode) NodeInfo() *p2p.NodeInfo {
|
|
|
|
server := sn.Server()
|
2017-10-17 09:58:04 +00:00
|
|
|
if server == nil {
|
|
|
|
return &p2p.NodeInfo{
|
2018-06-20 14:51:55 +00:00
|
|
|
ID: sn.ID.String(),
|
|
|
|
Enode: sn.Node().String(),
|
2017-10-17 09:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return server.NodeInfo()
|
|
|
|
}
|
2018-08-07 13:31:06 +00:00
|
|
|
|
|
|
|
func setSocketBuffer(conn net.Conn, socketReadBuffer int, socketWriteBuffer int) error {
|
|
|
|
if v, ok := conn.(*net.UnixConn); ok {
|
|
|
|
err := v.SetReadBuffer(socketReadBuffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = v.SetWriteBuffer(socketWriteBuffer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|