github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/example_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package simulation_test
    26  
    27  import (
    28  	"context"
    29  	"fmt"
    30  	"sync"
    31  	"time"
    32  
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/node"
    35  	"github.com/ethereum/go-ethereum/p2p"
    36  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    37  	"github.com/ethereum/go-ethereum/swarm/network"
    38  	"github.com/ethereum/go-ethereum/swarm/network/simulation"
    39  )
    40  
    41  //
    42  //
    43  //
    44  func ExampleSimulation_WaitTillHealthy() {
    45  	sim := simulation.New(map[string]simulation.ServiceFunc{
    46  		"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    47  			addr := network.NewAddrFromNodeID(ctx.Config.ID)
    48  			hp := network.NewHiveParams()
    49  			hp.Discovery = false
    50  			config := &network.BzzConfig{
    51  				OverlayAddr:  addr.Over(),
    52  				UnderlayAddr: addr.Under(),
    53  				HiveParams:   hp,
    54  			}
    55  			kad := network.NewKademlia(addr.Over(), network.NewKadParams())
    56  //
    57  //
    58  			b.Store(simulation.BucketKeyKademlia, kad)
    59  			return network.NewBzz(config, kad, nil, nil, nil), nil, nil
    60  		},
    61  	})
    62  	defer sim.Close()
    63  
    64  	_, err := sim.AddNodesAndConnectRing(10)
    65  	if err != nil {
    66  //
    67  		panic(err)
    68  	}
    69  
    70  	ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
    71  	defer cancel()
    72  	ill, err := sim.WaitTillHealthy(ctx, 2)
    73  	if err != nil {
    74  //
    75  		for id, kad := range ill {
    76  			fmt.Println("Node", id)
    77  			fmt.Println(kad.String())
    78  		}
    79  //
    80  	}
    81  
    82  //
    83  }
    84  
    85  //
    86  func ExampleSimulation_PeerEvents() {
    87  	sim := simulation.New(nil)
    88  	defer sim.Close()
    89  
    90  	events := sim.PeerEvents(context.Background(), sim.NodeIDs())
    91  
    92  	go func() {
    93  		for e := range events {
    94  			if e.Error != nil {
    95  				log.Error("peer event", "err", e.Error)
    96  				continue
    97  			}
    98  			log.Info("peer event", "node", e.NodeID, "peer", e.Event.Peer, "msgcode", e.Event.MsgCode)
    99  		}
   100  	}()
   101  }
   102  
   103  //
   104  func ExampleSimulation_PeerEvents_disconnections() {
   105  	sim := simulation.New(nil)
   106  	defer sim.Close()
   107  
   108  	disconnections := sim.PeerEvents(
   109  		context.Background(),
   110  		sim.NodeIDs(),
   111  		simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeDrop),
   112  	)
   113  
   114  	go func() {
   115  		for d := range disconnections {
   116  			if d.Error != nil {
   117  				log.Error("peer drop", "err", d.Error)
   118  				continue
   119  			}
   120  			log.Warn("peer drop", "node", d.NodeID, "peer", d.Event.Peer)
   121  		}
   122  	}()
   123  }
   124  
   125  //
   126  //
   127  func ExampleSimulation_PeerEvents_multipleFilters() {
   128  	sim := simulation.New(nil)
   129  	defer sim.Close()
   130  
   131  	msgs := sim.PeerEvents(
   132  		context.Background(),
   133  		sim.NodeIDs(),
   134  //
   135  		simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeMsgRecv).Protocol("bzz").MsgCode(1),
   136  		simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeMsgRecv).Protocol("bzz").MsgCode(4),
   137  	)
   138  
   139  	go func() {
   140  		for m := range msgs {
   141  			if m.Error != nil {
   142  				log.Error("bzz message", "err", m.Error)
   143  				continue
   144  			}
   145  			log.Info("bzz message", "node", m.NodeID, "peer", m.Event.Peer)
   146  		}
   147  	}()
   148  }