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