exec.go 14 KB

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