github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/swarm/network/simulation/example_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-dubxcoin library.
     3  //
     4  // The go-dubxcoin 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-dubxcoin 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-dubxcoin 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/alexdevranger/node-1.8.27/log"
    26  	"github.com/alexdevranger/node-1.8.27/node"
    27  	"github.com/alexdevranger/node-1.8.27/p2p/simulations/adapters"
    28  	"github.com/alexdevranger/node-1.8.27/swarm/network"
    29  	"github.com/alexdevranger/node-1.8.27/swarm/network/simulation"
    30  )
    31  
    32  // Every node can have a Kademlia associated using the node bucket under
    33  // BucketKeyKademlia key. This allows to use WaitTillHealthy to block until
    34  // all nodes have the their Kademlias healthy.
    35  func ExampleSimulation_WaitTillHealthy() {
    36  
    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)
    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  
    78  // Watch all peer events in the simulation network, buy receiving from a channel.
    79  func ExampleSimulation_PeerEvents() {
    80  	sim := simulation.New(nil)
    81  	defer sim.Close()
    82  
    83  	events := sim.PeerEvents(context.Background(), sim.NodeIDs())
    84  
    85  	go func() {
    86  		for e := range events {
    87  			if e.Error != nil {
    88  				log.Error("peer event", "err", e.Error)
    89  				continue
    90  			}
    91  			log.Info("peer event", "node", e.NodeID, "peer", e.PeerID, "type", e.Event.Type)
    92  		}
    93  	}()
    94  }
    95  
    96  // Detect when a nodes drop a peer.
    97  func ExampleSimulation_PeerEvents_disconnections() {
    98  	sim := simulation.New(nil)
    99  	defer sim.Close()
   100  
   101  	disconnections := sim.PeerEvents(
   102  		context.Background(),
   103  		sim.NodeIDs(),
   104  		simulation.NewPeerEventsFilter().Drop(),
   105  	)
   106  
   107  	go func() {
   108  		for d := range disconnections {
   109  			if d.Error != nil {
   110  				log.Error("peer drop", "err", d.Error)
   111  				continue
   112  			}
   113  			log.Warn("peer drop", "node", d.NodeID, "peer", d.PeerID)
   114  		}
   115  	}()
   116  }
   117  
   118  // Watch multiple types of events or messages. In this case, they differ only
   119  // by MsgCode, but filters can be set for different types or protocols, too.
   120  func ExampleSimulation_PeerEvents_multipleFilters() {
   121  	sim := simulation.New(nil)
   122  	defer sim.Close()
   123  
   124  	msgs := sim.PeerEvents(
   125  		context.Background(),
   126  		sim.NodeIDs(),
   127  		// Watch when bzz messages 1 and 4 are received.
   128  		simulation.NewPeerEventsFilter().ReceivedMessages().Protocol("bzz").MsgCode(1),
   129  		simulation.NewPeerEventsFilter().ReceivedMessages().Protocol("bzz").MsgCode(4),
   130  	)
   131  
   132  	go func() {
   133  		for m := range msgs {
   134  			if m.Error != nil {
   135  				log.Error("bzz message", "err", m.Error)
   136  				continue
   137  			}
   138  			log.Info("bzz message", "node", m.NodeID, "peer", m.PeerID)
   139  		}
   140  	}()
   141  }