network.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 simulations
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "math/rand"
  24. "sync"
  25. "time"
  26. "github.com/ethereum/go-ethereum/event"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/p2p"
  29. "github.com/ethereum/go-ethereum/p2p/enode"
  30. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  31. )
  32. var DialBanTimeout = 200 * time.Millisecond
  33. // NetworkConfig defines configuration options for starting a Network
  34. type NetworkConfig struct {
  35. ID string `json:"id"`
  36. DefaultService string `json:"default_service,omitempty"`
  37. }
  38. // Network models a p2p simulation network which consists of a collection of
  39. // simulated nodes and the connections which exist between them.
  40. //
  41. // The Network has a single NodeAdapter which is responsible for actually
  42. // starting nodes and connecting them together.
  43. //
  44. // The Network emits events when nodes are started and stopped, when they are
  45. // connected and disconnected, and also when messages are sent between nodes.
  46. type Network struct {
  47. NetworkConfig
  48. Nodes []*Node `json:"nodes"`
  49. nodeMap map[enode.ID]int
  50. // Maps a node property string to node indexes of all nodes that hold this property
  51. propertyMap map[string][]int
  52. Conns []*Conn `json:"conns"`
  53. connMap map[string]int
  54. nodeAdapter adapters.NodeAdapter
  55. events event.Feed
  56. lock sync.RWMutex
  57. quitc chan struct{}
  58. }
  59. // NewNetwork returns a Network which uses the given NodeAdapter and NetworkConfig
  60. func NewNetwork(nodeAdapter adapters.NodeAdapter, conf *NetworkConfig) *Network {
  61. return &Network{
  62. NetworkConfig: *conf,
  63. nodeAdapter: nodeAdapter,
  64. nodeMap: make(map[enode.ID]int),
  65. propertyMap: make(map[string][]int),
  66. connMap: make(map[string]int),
  67. quitc: make(chan struct{}),
  68. }
  69. }
  70. // Events returns the output event feed of the Network.
  71. func (net *Network) Events() *event.Feed {
  72. return &net.events
  73. }
  74. // NewNodeWithConfig adds a new node to the network with the given config,
  75. // returning an error if a node with the same ID or name already exists
  76. func (net *Network) NewNodeWithConfig(conf *adapters.NodeConfig) (*Node, error) {
  77. net.lock.Lock()
  78. defer net.lock.Unlock()
  79. if conf.Reachable == nil {
  80. conf.Reachable = func(otherID enode.ID) bool {
  81. _, err := net.InitConn(conf.ID, otherID)
  82. if err != nil && bytes.Compare(conf.ID.Bytes(), otherID.Bytes()) < 0 {
  83. return false
  84. }
  85. return true
  86. }
  87. }
  88. // check the node doesn't already exist
  89. if node := net.getNode(conf.ID); node != nil {
  90. return nil, fmt.Errorf("node with ID %q already exists", conf.ID)
  91. }
  92. if node := net.getNodeByName(conf.Name); node != nil {
  93. return nil, fmt.Errorf("node with name %q already exists", conf.Name)
  94. }
  95. // if no services are configured, use the default service
  96. if len(conf.Lifecycles) == 0 {
  97. conf.Lifecycles = []string{net.DefaultService}
  98. }
  99. // use the NodeAdapter to create the node
  100. adapterNode, err := net.nodeAdapter.NewNode(conf)
  101. if err != nil {
  102. return nil, err
  103. }
  104. node := newNode(adapterNode, conf, false)
  105. log.Trace("Node created", "id", conf.ID)
  106. nodeIndex := len(net.Nodes)
  107. net.nodeMap[conf.ID] = nodeIndex
  108. net.Nodes = append(net.Nodes, node)
  109. // Register any node properties with the network-level propertyMap
  110. for _, property := range conf.Properties {
  111. net.propertyMap[property] = append(net.propertyMap[property], nodeIndex)
  112. }
  113. // emit a "control" event
  114. net.events.Send(ControlEvent(node))
  115. return node, nil
  116. }
  117. // Config returns the network configuration
  118. func (net *Network) Config() *NetworkConfig {
  119. return &net.NetworkConfig
  120. }
  121. // StartAll starts all nodes in the network
  122. func (net *Network) StartAll() error {
  123. for _, node := range net.Nodes {
  124. if node.Up() {
  125. continue
  126. }
  127. if err := net.Start(node.ID()); err != nil {
  128. return err
  129. }
  130. }
  131. return nil
  132. }
  133. // StopAll stops all nodes in the network
  134. func (net *Network) StopAll() error {
  135. for _, node := range net.Nodes {
  136. if !node.Up() {
  137. continue
  138. }
  139. if err := net.Stop(node.ID()); err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. // Start starts the node with the given ID
  146. func (net *Network) Start(id enode.ID) error {
  147. return net.startWithSnapshots(id, nil)
  148. }
  149. // startWithSnapshots starts the node with the given ID using the give
  150. // snapshots
  151. func (net *Network) startWithSnapshots(id enode.ID, snapshots map[string][]byte) error {
  152. net.lock.Lock()
  153. defer net.lock.Unlock()
  154. node := net.getNode(id)
  155. if node == nil {
  156. return fmt.Errorf("node %v does not exist", id)
  157. }
  158. if node.Up() {
  159. return fmt.Errorf("node %v already up", id)
  160. }
  161. log.Trace("Starting node", "id", id, "adapter", net.nodeAdapter.Name())
  162. if err := node.Start(snapshots); err != nil {
  163. log.Warn("Node startup failed", "id", id, "err", err)
  164. return err
  165. }
  166. node.SetUp(true)
  167. log.Info("Started node", "id", id)
  168. ev := NewEvent(node)
  169. net.events.Send(ev)
  170. // subscribe to peer events
  171. client, err := node.Client()
  172. if err != nil {
  173. return fmt.Errorf("error getting rpc client for node %v: %s", id, err)
  174. }
  175. events := make(chan *p2p.PeerEvent)
  176. sub, err := client.Subscribe(context.Background(), "admin", events, "peerEvents")
  177. if err != nil {
  178. return fmt.Errorf("error getting peer events for node %v: %s", id, err)
  179. }
  180. go net.watchPeerEvents(id, events, sub)
  181. return nil
  182. }
  183. // watchPeerEvents reads peer events from the given channel and emits
  184. // corresponding network events
  185. func (net *Network) watchPeerEvents(id enode.ID, events chan *p2p.PeerEvent, sub event.Subscription) {
  186. defer func() {
  187. sub.Unsubscribe()
  188. // assume the node is now down
  189. net.lock.Lock()
  190. defer net.lock.Unlock()
  191. node := net.getNode(id)
  192. if node == nil {
  193. return
  194. }
  195. node.SetUp(false)
  196. ev := NewEvent(node)
  197. net.events.Send(ev)
  198. }()
  199. for {
  200. select {
  201. case event, ok := <-events:
  202. if !ok {
  203. return
  204. }
  205. peer := event.Peer
  206. switch event.Type {
  207. case p2p.PeerEventTypeAdd:
  208. net.DidConnect(id, peer)
  209. case p2p.PeerEventTypeDrop:
  210. net.DidDisconnect(id, peer)
  211. case p2p.PeerEventTypeMsgSend:
  212. net.DidSend(id, peer, event.Protocol, *event.MsgCode)
  213. case p2p.PeerEventTypeMsgRecv:
  214. net.DidReceive(peer, id, event.Protocol, *event.MsgCode)
  215. }
  216. case err := <-sub.Err():
  217. if err != nil {
  218. log.Error("Error in peer event subscription", "id", id, "err", err)
  219. }
  220. return
  221. }
  222. }
  223. }
  224. // Stop stops the node with the given ID
  225. func (net *Network) Stop(id enode.ID) error {
  226. // IMPORTANT: node.Stop() must NOT be called under net.lock as
  227. // node.Reachable() closure has a reference to the network and
  228. // calls net.InitConn() what also locks the network. => DEADLOCK
  229. // That holds until the following ticket is not resolved:
  230. var err error
  231. node, err := func() (*Node, error) {
  232. net.lock.Lock()
  233. defer net.lock.Unlock()
  234. node := net.getNode(id)
  235. if node == nil {
  236. return nil, fmt.Errorf("node %v does not exist", id)
  237. }
  238. if !node.Up() {
  239. return nil, fmt.Errorf("node %v already down", id)
  240. }
  241. node.SetUp(false)
  242. return node, nil
  243. }()
  244. if err != nil {
  245. return err
  246. }
  247. err = node.Stop() // must be called without net.lock
  248. net.lock.Lock()
  249. defer net.lock.Unlock()
  250. if err != nil {
  251. node.SetUp(true)
  252. return err
  253. }
  254. log.Info("Stopped node", "id", id, "err", err)
  255. ev := ControlEvent(node)
  256. net.events.Send(ev)
  257. return nil
  258. }
  259. // Connect connects two nodes together by calling the "admin_addPeer" RPC
  260. // method on the "one" node so that it connects to the "other" node
  261. func (net *Network) Connect(oneID, otherID enode.ID) error {
  262. net.lock.Lock()
  263. defer net.lock.Unlock()
  264. return net.connect(oneID, otherID)
  265. }
  266. func (net *Network) connect(oneID, otherID enode.ID) error {
  267. log.Debug("Connecting nodes with addPeer", "id", oneID, "other", otherID)
  268. conn, err := net.initConn(oneID, otherID)
  269. if err != nil {
  270. return err
  271. }
  272. client, err := conn.one.Client()
  273. if err != nil {
  274. return err
  275. }
  276. net.events.Send(ControlEvent(conn))
  277. return client.Call(nil, "admin_addPeer", string(conn.other.Addr()))
  278. }
  279. // Disconnect disconnects two nodes by calling the "admin_removePeer" RPC
  280. // method on the "one" node so that it disconnects from the "other" node
  281. func (net *Network) Disconnect(oneID, otherID enode.ID) error {
  282. conn := net.GetConn(oneID, otherID)
  283. if conn == nil {
  284. return fmt.Errorf("connection between %v and %v does not exist", oneID, otherID)
  285. }
  286. if !conn.Up {
  287. return fmt.Errorf("%v and %v already disconnected", oneID, otherID)
  288. }
  289. client, err := conn.one.Client()
  290. if err != nil {
  291. return err
  292. }
  293. net.events.Send(ControlEvent(conn))
  294. return client.Call(nil, "admin_removePeer", string(conn.other.Addr()))
  295. }
  296. // DidConnect tracks the fact that the "one" node connected to the "other" node
  297. func (net *Network) DidConnect(one, other enode.ID) error {
  298. net.lock.Lock()
  299. defer net.lock.Unlock()
  300. conn, err := net.getOrCreateConn(one, other)
  301. if err != nil {
  302. return fmt.Errorf("connection between %v and %v does not exist", one, other)
  303. }
  304. if conn.Up {
  305. return fmt.Errorf("%v and %v already connected", one, other)
  306. }
  307. conn.Up = true
  308. net.events.Send(NewEvent(conn))
  309. return nil
  310. }
  311. // DidDisconnect tracks the fact that the "one" node disconnected from the
  312. // "other" node
  313. func (net *Network) DidDisconnect(one, other enode.ID) error {
  314. net.lock.Lock()
  315. defer net.lock.Unlock()
  316. conn := net.getConn(one, other)
  317. if conn == nil {
  318. return fmt.Errorf("connection between %v and %v does not exist", one, other)
  319. }
  320. if !conn.Up {
  321. return fmt.Errorf("%v and %v already disconnected", one, other)
  322. }
  323. conn.Up = false
  324. conn.initiated = time.Now().Add(-DialBanTimeout)
  325. net.events.Send(NewEvent(conn))
  326. return nil
  327. }
  328. // DidSend tracks the fact that "sender" sent a message to "receiver"
  329. func (net *Network) DidSend(sender, receiver enode.ID, proto string, code uint64) error {
  330. msg := &Msg{
  331. One: sender,
  332. Other: receiver,
  333. Protocol: proto,
  334. Code: code,
  335. Received: false,
  336. }
  337. net.events.Send(NewEvent(msg))
  338. return nil
  339. }
  340. // DidReceive tracks the fact that "receiver" received a message from "sender"
  341. func (net *Network) DidReceive(sender, receiver enode.ID, proto string, code uint64) error {
  342. msg := &Msg{
  343. One: sender,
  344. Other: receiver,
  345. Protocol: proto,
  346. Code: code,
  347. Received: true,
  348. }
  349. net.events.Send(NewEvent(msg))
  350. return nil
  351. }
  352. // GetNode gets the node with the given ID, returning nil if the node does not
  353. // exist
  354. func (net *Network) GetNode(id enode.ID) *Node {
  355. net.lock.RLock()
  356. defer net.lock.RUnlock()
  357. return net.getNode(id)
  358. }
  359. func (net *Network) getNode(id enode.ID) *Node {
  360. i, found := net.nodeMap[id]
  361. if !found {
  362. return nil
  363. }
  364. return net.Nodes[i]
  365. }
  366. // GetNodeByName gets the node with the given name, returning nil if the node does
  367. // not exist
  368. func (net *Network) GetNodeByName(name string) *Node {
  369. net.lock.RLock()
  370. defer net.lock.RUnlock()
  371. return net.getNodeByName(name)
  372. }
  373. func (net *Network) getNodeByName(name string) *Node {
  374. for _, node := range net.Nodes {
  375. if node.Config.Name == name {
  376. return node
  377. }
  378. }
  379. return nil
  380. }
  381. // GetNodeIDs returns the IDs of all existing nodes
  382. // Nodes can optionally be excluded by specifying their enode.ID.
  383. func (net *Network) GetNodeIDs(excludeIDs ...enode.ID) []enode.ID {
  384. net.lock.RLock()
  385. defer net.lock.RUnlock()
  386. return net.getNodeIDs(excludeIDs)
  387. }
  388. func (net *Network) getNodeIDs(excludeIDs []enode.ID) []enode.ID {
  389. // Get all current nodeIDs
  390. nodeIDs := make([]enode.ID, 0, len(net.nodeMap))
  391. for id := range net.nodeMap {
  392. nodeIDs = append(nodeIDs, id)
  393. }
  394. if len(excludeIDs) > 0 {
  395. // Return the difference of nodeIDs and excludeIDs
  396. return filterIDs(nodeIDs, excludeIDs)
  397. }
  398. return nodeIDs
  399. }
  400. // GetNodes returns the existing nodes.
  401. // Nodes can optionally be excluded by specifying their enode.ID.
  402. func (net *Network) GetNodes(excludeIDs ...enode.ID) []*Node {
  403. net.lock.RLock()
  404. defer net.lock.RUnlock()
  405. return net.getNodes(excludeIDs)
  406. }
  407. func (net *Network) getNodes(excludeIDs []enode.ID) []*Node {
  408. if len(excludeIDs) > 0 {
  409. nodeIDs := net.getNodeIDs(excludeIDs)
  410. return net.getNodesByID(nodeIDs)
  411. }
  412. return net.Nodes
  413. }
  414. // GetNodesByID returns existing nodes with the given enode.IDs.
  415. // If a node doesn't exist with a given enode.ID, it is ignored.
  416. func (net *Network) GetNodesByID(nodeIDs []enode.ID) []*Node {
  417. net.lock.RLock()
  418. defer net.lock.RUnlock()
  419. return net.getNodesByID(nodeIDs)
  420. }
  421. func (net *Network) getNodesByID(nodeIDs []enode.ID) []*Node {
  422. nodes := make([]*Node, 0, len(nodeIDs))
  423. for _, id := range nodeIDs {
  424. node := net.getNode(id)
  425. if node != nil {
  426. nodes = append(nodes, node)
  427. }
  428. }
  429. return nodes
  430. }
  431. // GetNodesByProperty returns existing nodes that have the given property string registered in their NodeConfig
  432. func (net *Network) GetNodesByProperty(property string) []*Node {
  433. net.lock.RLock()
  434. defer net.lock.RUnlock()
  435. return net.getNodesByProperty(property)
  436. }
  437. func (net *Network) getNodesByProperty(property string) []*Node {
  438. nodes := make([]*Node, 0, len(net.propertyMap[property]))
  439. for _, nodeIndex := range net.propertyMap[property] {
  440. nodes = append(nodes, net.Nodes[nodeIndex])
  441. }
  442. return nodes
  443. }
  444. // GetNodeIDsByProperty returns existing node's enode IDs that have the given property string registered in the NodeConfig
  445. func (net *Network) GetNodeIDsByProperty(property string) []enode.ID {
  446. net.lock.RLock()
  447. defer net.lock.RUnlock()
  448. return net.getNodeIDsByProperty(property)
  449. }
  450. func (net *Network) getNodeIDsByProperty(property string) []enode.ID {
  451. nodeIDs := make([]enode.ID, 0, len(net.propertyMap[property]))
  452. for _, nodeIndex := range net.propertyMap[property] {
  453. node := net.Nodes[nodeIndex]
  454. nodeIDs = append(nodeIDs, node.ID())
  455. }
  456. return nodeIDs
  457. }
  458. // GetRandomUpNode returns a random node on the network, which is running.
  459. func (net *Network) GetRandomUpNode(excludeIDs ...enode.ID) *Node {
  460. net.lock.RLock()
  461. defer net.lock.RUnlock()
  462. return net.getRandomUpNode(excludeIDs...)
  463. }
  464. // GetRandomUpNode returns a random node on the network, which is running.
  465. func (net *Network) getRandomUpNode(excludeIDs ...enode.ID) *Node {
  466. return net.getRandomNode(net.getUpNodeIDs(), excludeIDs)
  467. }
  468. func (net *Network) getUpNodeIDs() (ids []enode.ID) {
  469. for _, node := range net.Nodes {
  470. if node.Up() {
  471. ids = append(ids, node.ID())
  472. }
  473. }
  474. return ids
  475. }
  476. // GetRandomDownNode returns a random node on the network, which is stopped.
  477. func (net *Network) GetRandomDownNode(excludeIDs ...enode.ID) *Node {
  478. net.lock.RLock()
  479. defer net.lock.RUnlock()
  480. return net.getRandomNode(net.getDownNodeIDs(), excludeIDs)
  481. }
  482. func (net *Network) getDownNodeIDs() (ids []enode.ID) {
  483. for _, node := range net.Nodes {
  484. if !node.Up() {
  485. ids = append(ids, node.ID())
  486. }
  487. }
  488. return ids
  489. }
  490. // GetRandomNode returns a random node on the network, regardless of whether it is running or not
  491. func (net *Network) GetRandomNode(excludeIDs ...enode.ID) *Node {
  492. net.lock.RLock()
  493. defer net.lock.RUnlock()
  494. return net.getRandomNode(net.getNodeIDs(nil), excludeIDs) // no need to exclude twice
  495. }
  496. func (net *Network) getRandomNode(ids []enode.ID, excludeIDs []enode.ID) *Node {
  497. filtered := filterIDs(ids, excludeIDs)
  498. l := len(filtered)
  499. if l == 0 {
  500. return nil
  501. }
  502. return net.getNode(filtered[rand.Intn(l)])
  503. }
  504. func filterIDs(ids []enode.ID, excludeIDs []enode.ID) []enode.ID {
  505. exclude := make(map[enode.ID]bool)
  506. for _, id := range excludeIDs {
  507. exclude[id] = true
  508. }
  509. var filtered []enode.ID
  510. for _, id := range ids {
  511. if _, found := exclude[id]; !found {
  512. filtered = append(filtered, id)
  513. }
  514. }
  515. return filtered
  516. }
  517. // GetConn returns the connection which exists between "one" and "other"
  518. // regardless of which node initiated the connection
  519. func (net *Network) GetConn(oneID, otherID enode.ID) *Conn {
  520. net.lock.RLock()
  521. defer net.lock.RUnlock()
  522. return net.getConn(oneID, otherID)
  523. }
  524. // GetOrCreateConn is like GetConn but creates the connection if it doesn't
  525. // already exist
  526. func (net *Network) GetOrCreateConn(oneID, otherID enode.ID) (*Conn, error) {
  527. net.lock.Lock()
  528. defer net.lock.Unlock()
  529. return net.getOrCreateConn(oneID, otherID)
  530. }
  531. func (net *Network) getOrCreateConn(oneID, otherID enode.ID) (*Conn, error) {
  532. if conn := net.getConn(oneID, otherID); conn != nil {
  533. return conn, nil
  534. }
  535. one := net.getNode(oneID)
  536. if one == nil {
  537. return nil, fmt.Errorf("node %v does not exist", oneID)
  538. }
  539. other := net.getNode(otherID)
  540. if other == nil {
  541. return nil, fmt.Errorf("node %v does not exist", otherID)
  542. }
  543. conn := &Conn{
  544. One: oneID,
  545. Other: otherID,
  546. one: one,
  547. other: other,
  548. }
  549. label := ConnLabel(oneID, otherID)
  550. net.connMap[label] = len(net.Conns)
  551. net.Conns = append(net.Conns, conn)
  552. return conn, nil
  553. }
  554. func (net *Network) getConn(oneID, otherID enode.ID) *Conn {
  555. label := ConnLabel(oneID, otherID)
  556. i, found := net.connMap[label]
  557. if !found {
  558. return nil
  559. }
  560. return net.Conns[i]
  561. }
  562. // InitConn(one, other) retrieves the connection model for the connection between
  563. // peers one and other, or creates a new one if it does not exist
  564. // the order of nodes does not matter, i.e., Conn(i,j) == Conn(j, i)
  565. // it checks if the connection is already up, and if the nodes are running
  566. // NOTE:
  567. // it also checks whether there has been recent attempt to connect the peers
  568. // this is cheating as the simulation is used as an oracle and know about
  569. // remote peers attempt to connect to a node which will then not initiate the connection
  570. func (net *Network) InitConn(oneID, otherID enode.ID) (*Conn, error) {
  571. net.lock.Lock()
  572. defer net.lock.Unlock()
  573. return net.initConn(oneID, otherID)
  574. }
  575. func (net *Network) initConn(oneID, otherID enode.ID) (*Conn, error) {
  576. if oneID == otherID {
  577. return nil, fmt.Errorf("refusing to connect to self %v", oneID)
  578. }
  579. conn, err := net.getOrCreateConn(oneID, otherID)
  580. if err != nil {
  581. return nil, err
  582. }
  583. if conn.Up {
  584. return nil, fmt.Errorf("%v and %v already connected", oneID, otherID)
  585. }
  586. if time.Since(conn.initiated) < DialBanTimeout {
  587. return nil, fmt.Errorf("connection between %v and %v recently attempted", oneID, otherID)
  588. }
  589. err = conn.nodesUp()
  590. if err != nil {
  591. log.Trace("Nodes not up", "err", err)
  592. return nil, fmt.Errorf("nodes not up: %v", err)
  593. }
  594. log.Debug("Connection initiated", "id", oneID, "other", otherID)
  595. conn.initiated = time.Now()
  596. return conn, nil
  597. }
  598. // Shutdown stops all nodes in the network and closes the quit channel
  599. func (net *Network) Shutdown() {
  600. for _, node := range net.Nodes {
  601. log.Debug("Stopping node", "id", node.ID())
  602. if err := node.Stop(); err != nil {
  603. log.Warn("Can't stop node", "id", node.ID(), "err", err)
  604. }
  605. }
  606. close(net.quitc)
  607. }
  608. // Reset resets all network properties:
  609. // empties the nodes and the connection list
  610. func (net *Network) Reset() {
  611. net.lock.Lock()
  612. defer net.lock.Unlock()
  613. //re-initialize the maps
  614. net.connMap = make(map[string]int)
  615. net.nodeMap = make(map[enode.ID]int)
  616. net.propertyMap = make(map[string][]int)
  617. net.Nodes = nil
  618. net.Conns = nil
  619. }
  620. // Node is a wrapper around adapters.Node which is used to track the status
  621. // of a node in the network
  622. type Node struct {
  623. adapters.Node `json:"-"`
  624. // Config if the config used to created the node
  625. Config *adapters.NodeConfig `json:"config"`
  626. // up tracks whether or not the node is running
  627. up bool
  628. upMu *sync.RWMutex
  629. }
  630. func newNode(an adapters.Node, ac *adapters.NodeConfig, up bool) *Node {
  631. return &Node{Node: an, Config: ac, up: up, upMu: new(sync.RWMutex)}
  632. }
  633. func (n *Node) copy() *Node {
  634. configCpy := *n.Config
  635. return newNode(n.Node, &configCpy, n.Up())
  636. }
  637. // Up returns whether the node is currently up (online)
  638. func (n *Node) Up() bool {
  639. n.upMu.RLock()
  640. defer n.upMu.RUnlock()
  641. return n.up
  642. }
  643. // SetUp sets the up (online) status of the nodes with the given value
  644. func (n *Node) SetUp(up bool) {
  645. n.upMu.Lock()
  646. defer n.upMu.Unlock()
  647. n.up = up
  648. }
  649. // ID returns the ID of the node
  650. func (n *Node) ID() enode.ID {
  651. return n.Config.ID
  652. }
  653. // String returns a log-friendly string
  654. func (n *Node) String() string {
  655. return fmt.Sprintf("Node %v", n.ID().TerminalString())
  656. }
  657. // NodeInfo returns information about the node
  658. func (n *Node) NodeInfo() *p2p.NodeInfo {
  659. // avoid a panic if the node is not started yet
  660. if n.Node == nil {
  661. return nil
  662. }
  663. info := n.Node.NodeInfo()
  664. info.Name = n.Config.Name
  665. return info
  666. }
  667. // MarshalJSON implements the json.Marshaler interface so that the encoded
  668. // JSON includes the NodeInfo
  669. func (n *Node) MarshalJSON() ([]byte, error) {
  670. return json.Marshal(struct {
  671. Info *p2p.NodeInfo `json:"info,omitempty"`
  672. Config *adapters.NodeConfig `json:"config,omitempty"`
  673. Up bool `json:"up"`
  674. }{
  675. Info: n.NodeInfo(),
  676. Config: n.Config,
  677. Up: n.Up(),
  678. })
  679. }
  680. // UnmarshalJSON implements json.Unmarshaler interface so that we don't lose Node.up
  681. // status. IMPORTANT: The implementation is incomplete; we lose p2p.NodeInfo.
  682. func (n *Node) UnmarshalJSON(raw []byte) error {
  683. // TODO: How should we turn back NodeInfo into n.Node?
  684. // Ticket: https://github.com/ethersphere/go-ethereum/issues/1177
  685. var node struct {
  686. Config *adapters.NodeConfig `json:"config,omitempty"`
  687. Up bool `json:"up"`
  688. }
  689. if err := json.Unmarshal(raw, &node); err != nil {
  690. return err
  691. }
  692. *n = *newNode(nil, node.Config, node.Up)
  693. return nil
  694. }
  695. // Conn represents a connection between two nodes in the network
  696. type Conn struct {
  697. // One is the node which initiated the connection
  698. One enode.ID `json:"one"`
  699. // Other is the node which the connection was made to
  700. Other enode.ID `json:"other"`
  701. // Up tracks whether or not the connection is active
  702. Up bool `json:"up"`
  703. // Registers when the connection was grabbed to dial
  704. initiated time.Time
  705. one *Node
  706. other *Node
  707. }
  708. // nodesUp returns whether both nodes are currently up
  709. func (c *Conn) nodesUp() error {
  710. if !c.one.Up() {
  711. return fmt.Errorf("one %v is not up", c.One)
  712. }
  713. if !c.other.Up() {
  714. return fmt.Errorf("other %v is not up", c.Other)
  715. }
  716. return nil
  717. }
  718. // String returns a log-friendly string
  719. func (c *Conn) String() string {
  720. return fmt.Sprintf("Conn %v->%v", c.One.TerminalString(), c.Other.TerminalString())
  721. }
  722. // Msg represents a p2p message sent between two nodes in the network
  723. type Msg struct {
  724. One enode.ID `json:"one"`
  725. Other enode.ID `json:"other"`
  726. Protocol string `json:"protocol"`
  727. Code uint64 `json:"code"`
  728. Received bool `json:"received"`
  729. }
  730. // String returns a log-friendly string
  731. func (m *Msg) String() string {
  732. return fmt.Sprintf("Msg(%d) %v->%v", m.Code, m.One.TerminalString(), m.Other.TerminalString())
  733. }
  734. // ConnLabel generates a deterministic string which represents a connection
  735. // between two nodes, used to compare if two connections are between the same
  736. // nodes
  737. func ConnLabel(source, target enode.ID) string {
  738. var first, second enode.ID
  739. if bytes.Compare(source.Bytes(), target.Bytes()) > 0 {
  740. first = target
  741. second = source
  742. } else {
  743. first = source
  744. second = target
  745. }
  746. return fmt.Sprintf("%v-%v", first, second)
  747. }
  748. // Snapshot represents the state of a network at a single point in time and can
  749. // be used to restore the state of a network
  750. type Snapshot struct {
  751. Nodes []NodeSnapshot `json:"nodes,omitempty"`
  752. Conns []Conn `json:"conns,omitempty"`
  753. }
  754. // NodeSnapshot represents the state of a node in the network
  755. type NodeSnapshot struct {
  756. Node Node `json:"node,omitempty"`
  757. // Snapshots is arbitrary data gathered from calling node.Snapshots()
  758. Snapshots map[string][]byte `json:"snapshots,omitempty"`
  759. }
  760. // Snapshot creates a network snapshot
  761. func (net *Network) Snapshot() (*Snapshot, error) {
  762. return net.snapshot(nil, nil)
  763. }
  764. func (net *Network) SnapshotWithServices(addServices []string, removeServices []string) (*Snapshot, error) {
  765. return net.snapshot(addServices, removeServices)
  766. }
  767. func (net *Network) snapshot(addServices []string, removeServices []string) (*Snapshot, error) {
  768. net.lock.Lock()
  769. defer net.lock.Unlock()
  770. snap := &Snapshot{
  771. Nodes: make([]NodeSnapshot, len(net.Nodes)),
  772. }
  773. for i, node := range net.Nodes {
  774. snap.Nodes[i] = NodeSnapshot{Node: *node.copy()}
  775. if !node.Up() {
  776. continue
  777. }
  778. snapshots, err := node.Snapshots()
  779. if err != nil {
  780. return nil, err
  781. }
  782. snap.Nodes[i].Snapshots = snapshots
  783. for _, addSvc := range addServices {
  784. haveSvc := false
  785. for _, svc := range snap.Nodes[i].Node.Config.Lifecycles {
  786. if svc == addSvc {
  787. haveSvc = true
  788. break
  789. }
  790. }
  791. if !haveSvc {
  792. snap.Nodes[i].Node.Config.Lifecycles = append(snap.Nodes[i].Node.Config.Lifecycles, addSvc)
  793. }
  794. }
  795. if len(removeServices) > 0 {
  796. var cleanedServices []string
  797. for _, svc := range snap.Nodes[i].Node.Config.Lifecycles {
  798. haveSvc := false
  799. for _, rmSvc := range removeServices {
  800. if rmSvc == svc {
  801. haveSvc = true
  802. break
  803. }
  804. }
  805. if !haveSvc {
  806. cleanedServices = append(cleanedServices, svc)
  807. }
  808. }
  809. snap.Nodes[i].Node.Config.Lifecycles = cleanedServices
  810. }
  811. }
  812. for _, conn := range net.Conns {
  813. if conn.Up {
  814. snap.Conns = append(snap.Conns, *conn)
  815. }
  816. }
  817. return snap, nil
  818. }
  819. // longrunning tests may need a longer timeout
  820. var snapshotLoadTimeout = 900 * time.Second
  821. // Load loads a network snapshot
  822. func (net *Network) Load(snap *Snapshot) error {
  823. // Start nodes.
  824. for _, n := range snap.Nodes {
  825. if _, err := net.NewNodeWithConfig(n.Node.Config); err != nil {
  826. return err
  827. }
  828. if !n.Node.Up() {
  829. continue
  830. }
  831. if err := net.startWithSnapshots(n.Node.Config.ID, n.Snapshots); err != nil {
  832. return err
  833. }
  834. }
  835. // Prepare connection events counter.
  836. allConnected := make(chan struct{}) // closed when all connections are established
  837. done := make(chan struct{}) // ensures that the event loop goroutine is terminated
  838. defer close(done)
  839. // Subscribe to event channel.
  840. // It needs to be done outside of the event loop goroutine (created below)
  841. // to ensure that the event channel is blocking before connect calls are made.
  842. events := make(chan *Event)
  843. sub := net.Events().Subscribe(events)
  844. defer sub.Unsubscribe()
  845. go func() {
  846. // Expected number of connections.
  847. total := len(snap.Conns)
  848. // Set of all established connections from the snapshot, not other connections.
  849. // Key array element 0 is the connection One field value, and element 1 connection Other field.
  850. connections := make(map[[2]enode.ID]struct{}, total)
  851. for {
  852. select {
  853. case e := <-events:
  854. // Ignore control events as they do not represent
  855. // connect or disconnect (Up) state change.
  856. if e.Control {
  857. continue
  858. }
  859. // Detect only connection events.
  860. if e.Type != EventTypeConn {
  861. continue
  862. }
  863. connection := [2]enode.ID{e.Conn.One, e.Conn.Other}
  864. // Nodes are still not connected or have been disconnected.
  865. if !e.Conn.Up {
  866. // Delete the connection from the set of established connections.
  867. // This will prevent false positive in case disconnections happen.
  868. delete(connections, connection)
  869. log.Warn("load snapshot: unexpected disconnection", "one", e.Conn.One, "other", e.Conn.Other)
  870. continue
  871. }
  872. // Check that the connection is from the snapshot.
  873. for _, conn := range snap.Conns {
  874. if conn.One == e.Conn.One && conn.Other == e.Conn.Other {
  875. // Add the connection to the set of established connections.
  876. connections[connection] = struct{}{}
  877. if len(connections) == total {
  878. // Signal that all nodes are connected.
  879. close(allConnected)
  880. return
  881. }
  882. break
  883. }
  884. }
  885. case <-done:
  886. // Load function returned, terminate this goroutine.
  887. return
  888. }
  889. }
  890. }()
  891. // Start connecting.
  892. for _, conn := range snap.Conns {
  893. if !net.GetNode(conn.One).Up() || !net.GetNode(conn.Other).Up() {
  894. //in this case, at least one of the nodes of a connection is not up,
  895. //so it would result in the snapshot `Load` to fail
  896. continue
  897. }
  898. if err := net.Connect(conn.One, conn.Other); err != nil {
  899. return err
  900. }
  901. }
  902. select {
  903. // Wait until all connections from the snapshot are established.
  904. case <-allConnected:
  905. // Make sure that we do not wait forever.
  906. case <-time.After(snapshotLoadTimeout):
  907. return errors.New("snapshot connections not established")
  908. }
  909. return nil
  910. }
  911. // Subscribe reads control events from a channel and executes them
  912. func (net *Network) Subscribe(events chan *Event) {
  913. for {
  914. select {
  915. case event, ok := <-events:
  916. if !ok {
  917. return
  918. }
  919. if event.Control {
  920. net.executeControlEvent(event)
  921. }
  922. case <-net.quitc:
  923. return
  924. }
  925. }
  926. }
  927. func (net *Network) executeControlEvent(event *Event) {
  928. log.Trace("Executing control event", "type", event.Type, "event", event)
  929. switch event.Type {
  930. case EventTypeNode:
  931. if err := net.executeNodeEvent(event); err != nil {
  932. log.Error("Error executing node event", "event", event, "err", err)
  933. }
  934. case EventTypeConn:
  935. if err := net.executeConnEvent(event); err != nil {
  936. log.Error("Error executing conn event", "event", event, "err", err)
  937. }
  938. case EventTypeMsg:
  939. log.Warn("Ignoring control msg event")
  940. }
  941. }
  942. func (net *Network) executeNodeEvent(e *Event) error {
  943. if !e.Node.Up() {
  944. return net.Stop(e.Node.ID())
  945. }
  946. if _, err := net.NewNodeWithConfig(e.Node.Config); err != nil {
  947. return err
  948. }
  949. return net.Start(e.Node.ID())
  950. }
  951. func (net *Network) executeConnEvent(e *Event) error {
  952. if e.Conn.Up {
  953. return net.Connect(e.Conn.One, e.Conn.Other)
  954. }
  955. return net.Disconnect(e.Conn.One, e.Conn.Other)
  956. }