node_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright 2018 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 simulation
  17. import (
  18. "context"
  19. "fmt"
  20. "sync"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/log"
  24. "github.com/ethereum/go-ethereum/node"
  25. "github.com/ethereum/go-ethereum/p2p/enode"
  26. "github.com/ethereum/go-ethereum/p2p/simulations"
  27. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  28. "github.com/ethereum/go-ethereum/swarm/network"
  29. )
  30. func TestUpDownNodeIDs(t *testing.T) {
  31. sim := New(noopServiceFuncMap)
  32. defer sim.Close()
  33. ids, err := sim.AddNodes(10)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. gotIDs := sim.NodeIDs()
  38. if !equalNodeIDs(ids, gotIDs) {
  39. t.Error("returned nodes are not equal to added ones")
  40. }
  41. stoppedIDs, err := sim.StopRandomNodes(3)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. gotIDs = sim.UpNodeIDs()
  46. for _, id := range gotIDs {
  47. if !sim.Net.GetNode(id).Up() {
  48. t.Errorf("node %s should not be down", id)
  49. }
  50. }
  51. if !equalNodeIDs(ids, append(gotIDs, stoppedIDs...)) {
  52. t.Error("returned nodes are not equal to added ones")
  53. }
  54. gotIDs = sim.DownNodeIDs()
  55. for _, id := range gotIDs {
  56. if sim.Net.GetNode(id).Up() {
  57. t.Errorf("node %s should not be up", id)
  58. }
  59. }
  60. if !equalNodeIDs(stoppedIDs, gotIDs) {
  61. t.Error("returned nodes are not equal to the stopped ones")
  62. }
  63. }
  64. func equalNodeIDs(one, other []enode.ID) bool {
  65. if len(one) != len(other) {
  66. return false
  67. }
  68. var count int
  69. for _, a := range one {
  70. var found bool
  71. for _, b := range other {
  72. if a == b {
  73. found = true
  74. break
  75. }
  76. }
  77. if found {
  78. count++
  79. } else {
  80. return false
  81. }
  82. }
  83. return count == len(one)
  84. }
  85. func TestAddNode(t *testing.T) {
  86. sim := New(noopServiceFuncMap)
  87. defer sim.Close()
  88. id, err := sim.AddNode()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. n := sim.Net.GetNode(id)
  93. if n == nil {
  94. t.Fatal("node not found")
  95. }
  96. if !n.Up() {
  97. t.Error("node not started")
  98. }
  99. }
  100. func TestAddNodeWithMsgEvents(t *testing.T) {
  101. sim := New(noopServiceFuncMap)
  102. defer sim.Close()
  103. id, err := sim.AddNode(AddNodeWithMsgEvents(true))
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if !sim.Net.GetNode(id).Config.EnableMsgEvents {
  108. t.Error("EnableMsgEvents is false")
  109. }
  110. id, err = sim.AddNode(AddNodeWithMsgEvents(false))
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. if sim.Net.GetNode(id).Config.EnableMsgEvents {
  115. t.Error("EnableMsgEvents is true")
  116. }
  117. }
  118. func TestAddNodeWithService(t *testing.T) {
  119. sim := New(map[string]ServiceFunc{
  120. "noop1": noopServiceFunc,
  121. "noop2": noopServiceFunc,
  122. })
  123. defer sim.Close()
  124. id, err := sim.AddNode(AddNodeWithService("noop1"))
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. n := sim.Net.GetNode(id).Node.(*adapters.SimNode)
  129. if n.Service("noop1") == nil {
  130. t.Error("service noop1 not found on node")
  131. }
  132. if n.Service("noop2") != nil {
  133. t.Error("service noop2 should not be found on node")
  134. }
  135. }
  136. func TestAddNodeMultipleServices(t *testing.T) {
  137. sim := New(map[string]ServiceFunc{
  138. "noop1": noopServiceFunc,
  139. "noop2": noopService2Func,
  140. })
  141. defer sim.Close()
  142. id, err := sim.AddNode()
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. n := sim.Net.GetNode(id).Node.(*adapters.SimNode)
  147. if n.Service("noop1") == nil {
  148. t.Error("service noop1 not found on node")
  149. }
  150. if n.Service("noop2") == nil {
  151. t.Error("service noop2 not found on node")
  152. }
  153. }
  154. func TestAddNodeDuplicateServiceError(t *testing.T) {
  155. sim := New(map[string]ServiceFunc{
  156. "noop1": noopServiceFunc,
  157. "noop2": noopServiceFunc,
  158. })
  159. defer sim.Close()
  160. wantErr := "duplicate service: *simulation.noopService"
  161. _, err := sim.AddNode()
  162. if err.Error() != wantErr {
  163. t.Errorf("got error %q, want %q", err, wantErr)
  164. }
  165. }
  166. func TestAddNodes(t *testing.T) {
  167. sim := New(noopServiceFuncMap)
  168. defer sim.Close()
  169. nodesCount := 12
  170. ids, err := sim.AddNodes(nodesCount)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. count := len(ids)
  175. if count != nodesCount {
  176. t.Errorf("expected %v nodes, got %v", nodesCount, count)
  177. }
  178. count = len(sim.Net.GetNodes())
  179. if count != nodesCount {
  180. t.Errorf("expected %v nodes, got %v", nodesCount, count)
  181. }
  182. }
  183. func TestAddNodesAndConnectFull(t *testing.T) {
  184. sim := New(noopServiceFuncMap)
  185. defer sim.Close()
  186. n := 12
  187. ids, err := sim.AddNodesAndConnectFull(n)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. simulations.VerifyFull(t, sim.Net, ids)
  192. }
  193. func TestAddNodesAndConnectChain(t *testing.T) {
  194. sim := New(noopServiceFuncMap)
  195. defer sim.Close()
  196. _, err := sim.AddNodesAndConnectChain(12)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. // add another set of nodes to test
  201. // if two chains are connected
  202. _, err = sim.AddNodesAndConnectChain(7)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. simulations.VerifyChain(t, sim.Net, sim.UpNodeIDs())
  207. }
  208. func TestAddNodesAndConnectRing(t *testing.T) {
  209. sim := New(noopServiceFuncMap)
  210. defer sim.Close()
  211. ids, err := sim.AddNodesAndConnectRing(12)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. simulations.VerifyRing(t, sim.Net, ids)
  216. }
  217. func TestAddNodesAndConnectStar(t *testing.T) {
  218. sim := New(noopServiceFuncMap)
  219. defer sim.Close()
  220. ids, err := sim.AddNodesAndConnectStar(12)
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. simulations.VerifyStar(t, sim.Net, ids, 0)
  225. }
  226. //To test that uploading a snapshot works
  227. func TestUploadSnapshot(t *testing.T) {
  228. log.Debug("Creating simulation")
  229. s := New(map[string]ServiceFunc{
  230. "bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
  231. addr := network.NewAddr(ctx.Config.Node())
  232. hp := network.NewHiveParams()
  233. hp.Discovery = false
  234. config := &network.BzzConfig{
  235. OverlayAddr: addr.Over(),
  236. UnderlayAddr: addr.Under(),
  237. HiveParams: hp,
  238. }
  239. kad := network.NewKademlia(addr.Over(), network.NewKadParams())
  240. return network.NewBzz(config, kad, nil, nil, nil), nil, nil
  241. },
  242. })
  243. defer s.Close()
  244. nodeCount := 16
  245. log.Debug("Uploading snapshot")
  246. err := s.UploadSnapshot(fmt.Sprintf("../stream/testing/snapshot_%d.json", nodeCount))
  247. if err != nil {
  248. t.Fatalf("Error uploading snapshot to simulation network: %v", err)
  249. }
  250. ctx := context.Background()
  251. log.Debug("Starting simulation...")
  252. s.Run(ctx, func(ctx context.Context, sim *Simulation) error {
  253. log.Debug("Checking")
  254. nodes := sim.UpNodeIDs()
  255. if len(nodes) != nodeCount {
  256. t.Fatal("Simulation network node number doesn't match snapshot node number")
  257. }
  258. return nil
  259. })
  260. log.Debug("Done.")
  261. }
  262. func TestStartStopNode(t *testing.T) {
  263. sim := New(noopServiceFuncMap)
  264. defer sim.Close()
  265. id, err := sim.AddNode()
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. n := sim.Net.GetNode(id)
  270. if n == nil {
  271. t.Fatal("node not found")
  272. }
  273. if !n.Up() {
  274. t.Error("node not started")
  275. }
  276. err = sim.StopNode(id)
  277. if err != nil {
  278. t.Fatal(err)
  279. }
  280. if n.Up() {
  281. t.Error("node not stopped")
  282. }
  283. waitForPeerEventPropagation()
  284. err = sim.StartNode(id)
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. if !n.Up() {
  289. t.Error("node not started")
  290. }
  291. }
  292. func TestStartStopRandomNode(t *testing.T) {
  293. sim := New(noopServiceFuncMap)
  294. defer sim.Close()
  295. _, err := sim.AddNodes(3)
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. id, err := sim.StopRandomNode()
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. n := sim.Net.GetNode(id)
  304. if n == nil {
  305. t.Fatal("node not found")
  306. }
  307. if n.Up() {
  308. t.Error("node not stopped")
  309. }
  310. id2, err := sim.StopRandomNode()
  311. if err != nil {
  312. t.Fatal(err)
  313. }
  314. waitForPeerEventPropagation()
  315. idStarted, err := sim.StartRandomNode()
  316. if err != nil {
  317. t.Fatal(err)
  318. }
  319. if idStarted != id && idStarted != id2 {
  320. t.Error("unexpected started node ID")
  321. }
  322. }
  323. func TestStartStopRandomNodes(t *testing.T) {
  324. sim := New(noopServiceFuncMap)
  325. defer sim.Close()
  326. _, err := sim.AddNodes(10)
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. ids, err := sim.StopRandomNodes(3)
  331. if err != nil {
  332. t.Fatal(err)
  333. }
  334. for _, id := range ids {
  335. n := sim.Net.GetNode(id)
  336. if n == nil {
  337. t.Fatal("node not found")
  338. }
  339. if n.Up() {
  340. t.Error("node not stopped")
  341. }
  342. }
  343. waitForPeerEventPropagation()
  344. ids, err = sim.StartRandomNodes(2)
  345. if err != nil {
  346. t.Fatal(err)
  347. }
  348. for _, id := range ids {
  349. n := sim.Net.GetNode(id)
  350. if n == nil {
  351. t.Fatal("node not found")
  352. }
  353. if !n.Up() {
  354. t.Error("node not started")
  355. }
  356. }
  357. }
  358. func waitForPeerEventPropagation() {
  359. // Sleep here to ensure that Network.watchPeerEvents defer function
  360. // has set the `node.Up() = false` before we start the node again.
  361. //
  362. // The same node is stopped and started again, and upon start
  363. // watchPeerEvents is started in a goroutine. If the node is stopped
  364. // and then very quickly started, that goroutine may be scheduled later
  365. // then start and force `node.Up() = false` in its defer function.
  366. // This will make this test unreliable.
  367. time.Sleep(1 * time.Second)
  368. }