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