// Copyright 2016 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 . package adapters import ( "errors" "fmt" "math" "net" "sync" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/rpc" ) // SimAdapter is a NodeAdapter which creates in-memory nodes and connects them // using an in-memory p2p.MsgReadWriter pipe type SimAdapter struct { mtx sync.RWMutex nodes map[discover.NodeID]*SimNode services map[string]ServiceFunc } // NewSimAdapter creates a SimAdapter which is capable of running in-memory // nodes running any of the given services (the service to run on a particular // node is passed to the NewNode function in the NodeConfig) func NewSimAdapter(services map[string]ServiceFunc) *SimAdapter { return &SimAdapter{ nodes: make(map[discover.NodeID]*SimNode), services: services, } } // Name returns the name of the adapter for logging purpoeses 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) } } node := &SimNode{ ID: id, config: config, adapter: s, running: make(map[string]node.Service), } s.nodes[id] = node return node, nil } 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) } pipe1, pipe2 := net.Pipe() go srv.SetupConn(pipe1, 0, nil) return pipe2, nil } func (s *SimAdapter) DialRPC(id discover.NodeID) (*rpc.Client, error) { simNode, ok := s.GetNode(id) if !ok { return nil, fmt.Errorf("unknown node: %s", id) } simNode.lock.RLock() node := simNode.node simNode.lock.RUnlock() if node == nil { return nil, errors.New("node not started") } handler, err := 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 node which connects to other SimNodes using an // in-memory p2p.MsgReadWriter pipe, running an underlying service protocol // directly over that pipe. // // It implements the p2p.Server interface so it can be used transparently // by the underlying service. type SimNode struct { lock sync.RWMutex ID discover.NodeID config *NodeConfig adapter *SimAdapter node *node.Node running map[string]node.Service client *rpc.Client } // Addr returns the node's discovery address func (self *SimNode) Addr() []byte { return []byte(self.Node().String()) } // Node returns a discover.Node representing the SimNode func (self *SimNode) Node() *discover.Node { return discover.NewNode(self.ID, net.IP{127, 0, 0, 1}, 30303, 30303) } // Client returns an rpc.Client which can be used to communicate with the // underlying service (it is set once the node has started) func (self *SimNode) Client() (*rpc.Client, error) { self.lock.RLock() defer self.lock.RUnlock() if self.client == nil { return nil, errors.New("node not started") } return self.client, nil } // ServeRPC serves RPC requests over the given connection by creating an // in-process client to the node's RPC server func (self *SimNode) ServeRPC(conn net.Conn) error { self.lock.RLock() node := self.node self.lock.RUnlock() if node == nil { return errors.New("node not started") } handler, err := node.RPCHandler() 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 func (self *SimNode) Snapshots() (map[string][]byte, error) { self.lock.Lock() services := self.running self.lock.Unlock() 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 starts the RPC handler and the underlying service func (self *SimNode) Start(snapshots map[string][]byte) error { self.lock.Lock() defer self.lock.Unlock() if self.node != nil { return errors.New("node already started") } newService := func(name string) func(ctx *node.ServiceContext) (node.Service, error) { return func(nodeCtx *node.ServiceContext) (node.Service, error) { ctx := &ServiceContext{ RPCDialer: self.adapter, NodeContext: nodeCtx, Config: self.config, } if snapshots != nil { ctx.Snapshot = snapshots[name] } serviceFunc := self.adapter.services[name] service, err := serviceFunc(ctx) if err != nil { return nil, err } self.running[name] = service return service, nil } } node, err := node.New(&node.Config{ P2P: p2p.Config{ PrivateKey: self.config.PrivateKey, MaxPeers: math.MaxInt32, NoDiscovery: true, Dialer: self.adapter, EnableMsgEvents: false, }, NoUSB: true, }) if err != nil { return err } for _, name := range self.config.Services { if err := node.Register(newService(name)); err != nil { return err } } if err := node.Start(); err != nil { return err } // create an in-process RPC client handler, err := node.RPCHandler() if err != nil { return err } self.client = rpc.DialInProc(handler) self.node = node return nil } func (self *SimNode) Stop() error { self.lock.Lock() defer self.lock.Unlock() if self.node == nil { return nil } if err := self.node.Stop(); err != nil { return err } self.node = nil return nil } // Services returns the underlying services func (self *SimNode) Services() []node.Service { self.lock.Lock() defer self.lock.Unlock() services := make([]node.Service, 0, len(self.running)) for _, service := range self.running { services = append(services, service) } return services } func (self *SimNode) Server() *p2p.Server { self.lock.Lock() defer self.lock.Unlock() if self.node == nil { return nil } return self.node.Server() } func (self *SimNode) SubscribeEvents(ch chan *p2p.PeerEvent) event.Subscription { srv := self.Server() if srv == nil { panic("node not running") } return srv.SubscribeEvents(ch) } func (self *SimNode) NodeInfo() *p2p.NodeInfo { server := self.Server() if server == nil { return &p2p.NodeInfo{ ID: self.ID.String(), Enode: self.Node().String(), } } return server.NodeInfo() }