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