example_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_test
  17. import (
  18. "context"
  19. "github.com/ethereum/go-ethereum/log"
  20. "github.com/ethereum/go-ethereum/swarm/network/simulation"
  21. )
  22. // Every node can have a Kademlia associated using the node bucket under
  23. // BucketKeyKademlia key. This allows to use WaitTillHealthy to block until
  24. // all nodes have the their Kademlias healthy.
  25. func ExampleSimulation_WaitTillHealthy() {
  26. log.Error("temporarily disabled as simulations.WaitTillHealthy cannot be trusted")
  27. /* Commented out to avoid go vet errors/warnings
  28. sim := simulation.New(map[string]simulation.ServiceFunc{
  29. "bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
  30. addr := network.NewAddr(ctx.Config.Node())
  31. hp := network.NewHiveParams()
  32. hp.Discovery = false
  33. config := &network.BzzConfig{
  34. OverlayAddr: addr.Over(),
  35. UnderlayAddr: addr.Under(),
  36. HiveParams: hp,
  37. }
  38. kad := network.NewKademlia(addr.Over(), network.NewKadParams())
  39. // store kademlia in node's bucket under BucketKeyKademlia
  40. // so that it can be found by WaitTillHealthy method.
  41. b.Store(simulation.BucketKeyKademlia, kad)
  42. return network.NewBzz(config, kad, nil, nil, nil), nil, nil
  43. },
  44. })
  45. defer sim.Close()
  46. _, err := sim.AddNodesAndConnectRing(10)
  47. if err != nil {
  48. // handle error properly...
  49. panic(err)
  50. }
  51. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  52. defer cancel()
  53. ill, err := sim.WaitTillHealthy(ctx, 2)
  54. if err != nil {
  55. // inspect the latest detected not healthy kademlias
  56. for id, kad := range ill {
  57. fmt.Println("Node", id)
  58. fmt.Println(kad.String())
  59. }
  60. // handle error...
  61. }
  62. // continue with the test
  63. */
  64. }
  65. // Watch all peer events in the simulation network, buy receiving from a channel.
  66. func ExampleSimulation_PeerEvents() {
  67. sim := simulation.New(nil)
  68. defer sim.Close()
  69. events := sim.PeerEvents(context.Background(), sim.NodeIDs())
  70. go func() {
  71. for e := range events {
  72. if e.Error != nil {
  73. log.Error("peer event", "err", e.Error)
  74. continue
  75. }
  76. log.Info("peer event", "node", e.NodeID, "peer", e.PeerID, "type", e.Event.Type)
  77. }
  78. }()
  79. }
  80. // Detect when a nodes drop a peer.
  81. func ExampleSimulation_PeerEvents_disconnections() {
  82. sim := simulation.New(nil)
  83. defer sim.Close()
  84. disconnections := sim.PeerEvents(
  85. context.Background(),
  86. sim.NodeIDs(),
  87. simulation.NewPeerEventsFilter().Drop(),
  88. )
  89. go func() {
  90. for d := range disconnections {
  91. if d.Error != nil {
  92. log.Error("peer drop", "err", d.Error)
  93. continue
  94. }
  95. log.Warn("peer drop", "node", d.NodeID, "peer", d.PeerID)
  96. }
  97. }()
  98. }
  99. // Watch multiple types of events or messages. In this case, they differ only
  100. // by MsgCode, but filters can be set for different types or protocols, too.
  101. func ExampleSimulation_PeerEvents_multipleFilters() {
  102. sim := simulation.New(nil)
  103. defer sim.Close()
  104. msgs := sim.PeerEvents(
  105. context.Background(),
  106. sim.NodeIDs(),
  107. // Watch when bzz messages 1 and 4 are received.
  108. simulation.NewPeerEventsFilter().ReceivedMessages().Protocol("bzz").MsgCode(1),
  109. simulation.NewPeerEventsFilter().ReceivedMessages().Protocol("bzz").MsgCode(4),
  110. )
  111. go func() {
  112. for m := range msgs {
  113. if m.Error != nil {
  114. log.Error("bzz message", "err", m.Error)
  115. continue
  116. }
  117. log.Info("bzz message", "node", m.NodeID, "peer", m.PeerID)
  118. }
  119. }()
  120. }