github.com/xinfinOrg/xdposchain@v1.1.0/p2p/simulations/adapters/exec.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package adapters
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"crypto/ecdsa"
    23  	"encoding/json"
    24  	"errors"
    25  	"fmt"
    26  	"io"
    27  	"net"
    28  	"net/http"
    29  	"os"
    30  	"os/exec"
    31  	"os/signal"
    32  	"path/filepath"
    33  	"strings"
    34  	"sync"
    35  	"syscall"
    36  	"time"
    37  
    38  	"github.com/docker/docker/pkg/reexec"
    39  	"github.com/ethereum/go-ethereum/log"
    40  	"github.com/ethereum/go-ethereum/node"
    41  	"github.com/ethereum/go-ethereum/p2p"
    42  	"github.com/ethereum/go-ethereum/p2p/enode"
    43  	"github.com/ethereum/go-ethereum/rpc"
    44  	"golang.org/x/net/websocket"
    45  )
    46  
    47  func init() {
    48  	// Register a reexec function to start a simulation node when the current binary is
    49  	// executed as "p2p-node" (rather than whatever the main() function would normally do).
    50  	reexec.Register("p2p-node", execP2PNode)
    51  }
    52  
    53  // ExecAdapter is a NodeAdapter which runs simulation nodes by executing the current binary
    54  // as a child process.
    55  type ExecAdapter struct {
    56  	// BaseDir is the directory under which the data directories for each
    57  	// simulation node are created.
    58  	BaseDir string
    59  
    60  	nodes map[enode.ID]*ExecNode
    61  }
    62  
    63  // NewExecAdapter returns an ExecAdapter which stores node data in
    64  // subdirectories of the given base directory
    65  func NewExecAdapter(baseDir string) *ExecAdapter {
    66  	return &ExecAdapter{
    67  		BaseDir: baseDir,
    68  		nodes:   make(map[enode.ID]*ExecNode),
    69  	}
    70  }
    71  
    72  // Name returns the name of the adapter for logging purposes
    73  func (e *ExecAdapter) Name() string {
    74  	return "exec-adapter"
    75  }
    76  
    77  // NewNode returns a new ExecNode using the given config
    78  func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
    79  	if len(config.Services) == 0 {
    80  		return nil, errors.New("node must have at least one service")
    81  	}
    82  	for _, service := range config.Services {
    83  		if _, exists := serviceFuncs[service]; !exists {
    84  			return nil, fmt.Errorf("unknown node service %q", service)
    85  		}
    86  	}
    87  
    88  	// create the node directory using the first 12 characters of the ID
    89  	// as Unix socket paths cannot be longer than 256 characters
    90  	dir := filepath.Join(e.BaseDir, config.ID.String()[:12])
    91  	if err := os.Mkdir(dir, 0755); err != nil {
    92  		return nil, fmt.Errorf("error creating node directory: %s", err)
    93  	}
    94  
    95  	// generate the config
    96  	conf := &execNodeConfig{
    97  		Stack: node.DefaultConfig,
    98  		Node:  config,
    99  	}
   100  	conf.Stack.DataDir = filepath.Join(dir, "data")
   101  	conf.Stack.WSHost = "127.0.0.1"
   102  	conf.Stack.WSPort = 0
   103  	conf.Stack.WSOrigins = []string{"*"}
   104  	conf.Stack.WSExposeAll = true
   105  	conf.Stack.P2P.EnableMsgEvents = false
   106  	conf.Stack.P2P.NoDiscovery = true
   107  	conf.Stack.P2P.NAT = nil
   108  	conf.Stack.NoUSB = true
   109  
   110  	// listen on a localhost port, which we set when we
   111  	// initialise NodeConfig (usually a random port)
   112  	conf.Stack.P2P.ListenAddr = fmt.Sprintf(":%d", config.Port)
   113  
   114  	node := &ExecNode{
   115  		ID:      config.ID,
   116  		Dir:     dir,
   117  		Config:  conf,
   118  		adapter: e,
   119  	}
   120  	node.newCmd = node.execCommand
   121  	e.nodes[node.ID] = node
   122  	return node, nil
   123  }
   124  
   125  // ExecNode starts a simulation node by exec'ing the current binary and
   126  // running the configured services
   127  type ExecNode struct {
   128  	ID     enode.ID
   129  	Dir    string
   130  	Config *execNodeConfig
   131  	Cmd    *exec.Cmd
   132  	Info   *p2p.NodeInfo
   133  
   134  	adapter *ExecAdapter
   135  	client  *rpc.Client
   136  	wsAddr  string
   137  	newCmd  func() *exec.Cmd
   138  	key     *ecdsa.PrivateKey
   139  }
   140  
   141  // Addr returns the node's enode URL
   142  func (n *ExecNode) Addr() []byte {
   143  	if n.Info == nil {
   144  		return nil
   145  	}
   146  	return []byte(n.Info.Enode)
   147  }
   148  
   149  // Client returns an rpc.Client which can be used to communicate with the
   150  // underlying services (it is set once the node has started)
   151  func (n *ExecNode) Client() (*rpc.Client, error) {
   152  	return n.client, nil
   153  }
   154  
   155  // Start exec's the node passing the ID and service as command line arguments
   156  // and the node config encoded as JSON in an environment variable.
   157  func (n *ExecNode) Start(snapshots map[string][]byte) (err error) {
   158  	if n.Cmd != nil {
   159  		return errors.New("already started")
   160  	}
   161  	defer func() {
   162  		if err != nil {
   163  			n.Stop()
   164  		}
   165  	}()
   166  
   167  	// encode a copy of the config containing the snapshot
   168  	confCopy := *n.Config
   169  	confCopy.Snapshots = snapshots
   170  	confCopy.PeerAddrs = make(map[string]string)
   171  	for id, node := range n.adapter.nodes {
   172  		confCopy.PeerAddrs[id.String()] = node.wsAddr
   173  	}
   174  	confData, err := json.Marshal(confCopy)
   175  	if err != nil {
   176  		return fmt.Errorf("error generating node config: %s", err)
   177  	}
   178  
   179  	// start the one-shot server that waits for startup information
   180  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   181  	defer cancel()
   182  	statusURL, statusC := n.waitForStartupJSON(ctx)
   183  
   184  	// start the node
   185  	cmd := n.newCmd()
   186  	cmd.Stdout = os.Stdout
   187  	cmd.Stderr = os.Stderr
   188  	cmd.Env = append(os.Environ(),
   189  		envStatusURL+"="+statusURL,
   190  		envNodeConfig+"="+string(confData),
   191  	)
   192  	if err := cmd.Start(); err != nil {
   193  		return fmt.Errorf("error starting node: %s", err)
   194  	}
   195  	n.Cmd = cmd
   196  
   197  	// read the WebSocket address from the stderr logs
   198  	status := <-statusC
   199  	if status.Err != "" {
   200  		return errors.New(status.Err)
   201  	}
   202  	client, err := rpc.DialWebsocket(ctx, status.WSEndpoint, "http://localhost")
   203  	if err != nil {
   204  		return fmt.Errorf("can't connect to RPC server: %v", err)
   205  	}
   206  
   207  	// node ready :)
   208  	n.client = client
   209  	n.wsAddr = status.WSEndpoint
   210  	n.Info = status.NodeInfo
   211  	return nil
   212  }
   213  
   214  // waitForStartupJSON runs a one-shot HTTP server to receive a startup report.
   215  func (n *ExecNode) waitForStartupJSON(ctx context.Context) (string, chan nodeStartupJSON) {
   216  	var (
   217  		ch       = make(chan nodeStartupJSON, 1)
   218  		quitOnce sync.Once
   219  		srv      http.Server
   220  	)
   221  	l, err := net.Listen("tcp", "127.0.0.1:0")
   222  	if err != nil {
   223  		ch <- nodeStartupJSON{Err: err.Error()}
   224  		return "", ch
   225  	}
   226  	quit := func(status nodeStartupJSON) {
   227  		quitOnce.Do(func() {
   228  			l.Close()
   229  			ch <- status
   230  		})
   231  	}
   232  	srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   233  		var status nodeStartupJSON
   234  		if err := json.NewDecoder(r.Body).Decode(&status); err != nil {
   235  			status.Err = fmt.Sprintf("can't decode startup report: %v", err)
   236  		}
   237  		quit(status)
   238  	})
   239  	// Run the HTTP server, but don't wait forever and shut it down
   240  	// if the context is canceled.
   241  	go srv.Serve(l)
   242  	go func() {
   243  		<-ctx.Done()
   244  		quit(nodeStartupJSON{Err: "didn't get startup report"})
   245  	}()
   246  
   247  	url := "http://" + l.Addr().String()
   248  	return url, ch
   249  }
   250  
   251  // execCommand returns a command which runs the node locally by exec'ing
   252  // the current binary but setting argv[0] to "p2p-node" so that the child
   253  // runs execP2PNode
   254  func (n *ExecNode) execCommand() *exec.Cmd {
   255  	return &exec.Cmd{
   256  		Path: reexec.Self(),
   257  		Args: []string{"p2p-node", strings.Join(n.Config.Node.Services, ","), n.ID.String()},
   258  	}
   259  }
   260  
   261  // Stop stops the node by first sending SIGTERM and then SIGKILL if the node
   262  // doesn't stop within 5s
   263  func (n *ExecNode) Stop() error {
   264  	if n.Cmd == nil {
   265  		return nil
   266  	}
   267  	defer func() {
   268  		n.Cmd = nil
   269  	}()
   270  
   271  	if n.client != nil {
   272  		n.client.Close()
   273  		n.client = nil
   274  		n.wsAddr = ""
   275  		n.Info = nil
   276  	}
   277  
   278  	if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil {
   279  		return n.Cmd.Process.Kill()
   280  	}
   281  	waitErr := make(chan error)
   282  	go func() {
   283  		waitErr <- n.Cmd.Wait()
   284  	}()
   285  	select {
   286  	case err := <-waitErr:
   287  		return err
   288  	case <-time.After(5 * time.Second):
   289  		return n.Cmd.Process.Kill()
   290  	}
   291  }
   292  
   293  // NodeInfo returns information about the node
   294  func (n *ExecNode) NodeInfo() *p2p.NodeInfo {
   295  	info := &p2p.NodeInfo{
   296  		ID: n.ID.String(),
   297  	}
   298  	if n.client != nil {
   299  		n.client.Call(&info, "admin_nodeInfo")
   300  	}
   301  	return info
   302  }
   303  
   304  // ServeRPC serves RPC requests over the given connection by dialling the
   305  // node's WebSocket address and joining the two connections
   306  func (n *ExecNode) ServeRPC(clientConn net.Conn) error {
   307  	conn, err := websocket.Dial(n.wsAddr, "", "http://localhost")
   308  	if err != nil {
   309  		return err
   310  	}
   311  	var wg sync.WaitGroup
   312  	wg.Add(2)
   313  	join := func(src, dst net.Conn) {
   314  		defer wg.Done()
   315  		io.Copy(dst, src)
   316  		// close the write end of the destination connection
   317  		if cw, ok := dst.(interface {
   318  			CloseWrite() error
   319  		}); ok {
   320  			cw.CloseWrite()
   321  		} else {
   322  			dst.Close()
   323  		}
   324  	}
   325  	go join(conn, clientConn)
   326  	go join(clientConn, conn)
   327  	wg.Wait()
   328  	return nil
   329  }
   330  
   331  // Snapshots creates snapshots of the services by calling the
   332  // simulation_snapshot RPC method
   333  func (n *ExecNode) Snapshots() (map[string][]byte, error) {
   334  	if n.client == nil {
   335  		return nil, errors.New("RPC not started")
   336  	}
   337  	var snapshots map[string][]byte
   338  	return snapshots, n.client.Call(&snapshots, "simulation_snapshot")
   339  }
   340  
   341  // execNodeConfig is used to serialize the node configuration so it can be
   342  // passed to the child process as a JSON encoded environment variable
   343  type execNodeConfig struct {
   344  	Stack     node.Config       `json:"stack"`
   345  	Node      *NodeConfig       `json:"node"`
   346  	Snapshots map[string][]byte `json:"snapshots,omitempty"`
   347  	PeerAddrs map[string]string `json:"peer_addrs,omitempty"`
   348  }
   349  
   350  // execP2PNode starts a simulation node when the current binary is executed with
   351  // argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2]
   352  // and the node config from an environment variable.
   353  func execP2PNode() {
   354  	glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
   355  	glogger.Verbosity(log.LvlInfo)
   356  	log.Root().SetHandler(glogger)
   357  	statusURL := os.Getenv(envStatusURL)
   358  	if statusURL == "" {
   359  		log.Crit("missing " + envStatusURL)
   360  	}
   361  
   362  	// Start the node and gather startup report.
   363  	var status nodeStartupJSON
   364  	stack, stackErr := startExecNodeStack()
   365  	if stackErr != nil {
   366  		status.Err = stackErr.Error()
   367  	} else {
   368  		status.WSEndpoint = "ws://" + stack.WSEndpoint()
   369  		status.NodeInfo = stack.Server().NodeInfo()
   370  	}
   371  
   372  	// Send status to the host.
   373  	statusJSON, _ := json.Marshal(status)
   374  	if _, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)); err != nil {
   375  		log.Crit("Can't post startup info", "url", statusURL, "err", err)
   376  	}
   377  	if stackErr != nil {
   378  		os.Exit(1)
   379  	}
   380  
   381  	// Stop the stack if we get a SIGTERM signal.
   382  	go func() {
   383  		sigc := make(chan os.Signal, 1)
   384  		signal.Notify(sigc, syscall.SIGTERM)
   385  		defer signal.Stop(sigc)
   386  		<-sigc
   387  		log.Info("Received SIGTERM, shutting down...")
   388  		stack.Stop()
   389  	}()
   390  	stack.Wait() // Wait for the stack to exit.
   391  }
   392  
   393  func startExecNodeStack() (*node.Node, error) {
   394  	// read the services from argv
   395  	serviceNames := strings.Split(os.Args[1], ",")
   396  
   397  	// decode the config
   398  	confEnv := os.Getenv(envNodeConfig)
   399  	if confEnv == "" {
   400  		return nil, fmt.Errorf("missing " + envNodeConfig)
   401  	}
   402  	var conf execNodeConfig
   403  	if err := json.Unmarshal([]byte(confEnv), &conf); err != nil {
   404  		return nil, fmt.Errorf("error decoding %s: %v", envNodeConfig, err)
   405  	}
   406  	conf.Stack.P2P.PrivateKey = conf.Node.PrivateKey
   407  	conf.Stack.Logger = log.New("node.id", conf.Node.ID.String())
   408  
   409  	// initialize the devp2p stack
   410  	stack, err := node.New(&conf.Stack)
   411  	if err != nil {
   412  		return nil, fmt.Errorf("error creating node stack: %v", err)
   413  	}
   414  
   415  	// register the services, collecting them into a map so we can wrap
   416  	// them in a snapshot service
   417  	services := make(map[string]node.Service, len(serviceNames))
   418  	for _, name := range serviceNames {
   419  		serviceFunc, exists := serviceFuncs[name]
   420  		if !exists {
   421  			return nil, fmt.Errorf("unknown node service %q", err)
   422  		}
   423  		constructor := func(nodeCtx *node.ServiceContext) (node.Service, error) {
   424  			ctx := &ServiceContext{
   425  				RPCDialer:   &wsRPCDialer{addrs: conf.PeerAddrs},
   426  				NodeContext: nodeCtx,
   427  				Config:      conf.Node,
   428  			}
   429  			if conf.Snapshots != nil {
   430  				ctx.Snapshot = conf.Snapshots[name]
   431  			}
   432  			service, err := serviceFunc(ctx)
   433  			if err != nil {
   434  				return nil, err
   435  			}
   436  			services[name] = service
   437  			return service, nil
   438  		}
   439  		if err := stack.Register(constructor); err != nil {
   440  			return stack, fmt.Errorf("error registering service %q: %v", name, err)
   441  		}
   442  	}
   443  
   444  	// register the snapshot service
   445  	err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
   446  		return &snapshotService{services}, nil
   447  	})
   448  	if err != nil {
   449  		return stack, fmt.Errorf("error starting snapshot service: %v", err)
   450  	}
   451  
   452  	// start the stack
   453  	if err = stack.Start(); err != nil {
   454  		err = fmt.Errorf("error starting stack: %v", err)
   455  	}
   456  	return stack, err
   457  }
   458  
   459  const (
   460  	envStatusURL  = "_P2P_STATUS_URL"
   461  	envNodeConfig = "_P2P_NODE_CONFIG"
   462  )
   463  
   464  // nodeStartupJSON is sent to the simulation host after startup.
   465  type nodeStartupJSON struct {
   466  	Err        string
   467  	WSEndpoint string
   468  	NodeInfo   *p2p.NodeInfo
   469  }
   470  
   471  // snapshotService is a node.Service which wraps a list of services and
   472  // exposes an API to generate a snapshot of those services
   473  type snapshotService struct {
   474  	services map[string]node.Service
   475  }
   476  
   477  func (s *snapshotService) APIs() []rpc.API {
   478  	return []rpc.API{{
   479  		Namespace: "simulation",
   480  		Version:   "1.0",
   481  		Service:   SnapshotAPI{s.services},
   482  	}}
   483  }
   484  
   485  func (s *snapshotService) Protocols() []p2p.Protocol {
   486  	return nil
   487  }
   488  
   489  func (s *snapshotService) Start(*p2p.Server) error {
   490  	return nil
   491  }
   492  
   493  func (s *snapshotService) Stop() error {
   494  	return nil
   495  }
   496  
   497  // SnapshotAPI provides an RPC method to create snapshots of services
   498  type SnapshotAPI struct {
   499  	services map[string]node.Service
   500  }
   501  
   502  func (api SnapshotAPI) Snapshot() (map[string][]byte, error) {
   503  	snapshots := make(map[string][]byte)
   504  	for name, service := range api.services {
   505  		if s, ok := service.(interface {
   506  			Snapshot() ([]byte, error)
   507  		}); ok {
   508  			snap, err := s.Snapshot()
   509  			if err != nil {
   510  				return nil, err
   511  			}
   512  			snapshots[name] = snap
   513  		}
   514  	}
   515  	return snapshots, nil
   516  }
   517  
   518  type wsRPCDialer struct {
   519  	addrs map[string]string
   520  }
   521  
   522  // DialRPC implements the RPCDialer interface by creating a WebSocket RPC
   523  // client of the given node
   524  func (w *wsRPCDialer) DialRPC(id enode.ID) (*rpc.Client, error) {
   525  	addr, ok := w.addrs[id.String()]
   526  	if !ok {
   527  		return nil, fmt.Errorf("unknown node: %s", id)
   528  	}
   529  	return rpc.DialWebsocket(context.Background(), addr, "http://localhost")
   530  }