github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/bootstrap_test.go (about)

     1  package dht
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	p2pty "github.com/turingchain2020/turingchain/system/p2p/dht/types"
     9  	blankhost "github.com/libp2p/go-libp2p-blankhost"
    10  	"github.com/libp2p/go-libp2p-core/host"
    11  	"github.com/libp2p/go-libp2p-core/peer"
    12  	swarmt "github.com/libp2p/go-libp2p-swarm/testing"
    13  )
    14  
    15  func getNetHosts(ctx context.Context, n int, t *testing.T) []host.Host {
    16  	var out []host.Host
    17  
    18  	for i := 0; i < n; i++ {
    19  		netw := swarmt.GenSwarm(t, ctx)
    20  		h := blankhost.NewBlankHost(netw)
    21  		out = append(out, h)
    22  	}
    23  
    24  	return out
    25  }
    26  
    27  func Test_initInnerPeers(t *testing.T) {
    28  	ctx, cancel := context.WithCancel(context.Background())
    29  	defer cancel()
    30  
    31  	hosts := getNetHosts(ctx, 8, t)
    32  
    33  	h0 := hosts[0].Peerstore().PeerInfo(hosts[0].ID())
    34  	h1 := hosts[1].Peerstore().PeerInfo(hosts[1].ID())
    35  	h2 := hosts[2].Peerstore().PeerInfo(hosts[2].ID())
    36  	h3 := hosts[3].Peerstore().PeerInfo(hosts[3].ID())
    37  	h4 := hosts[4].Peerstore().PeerInfo(hosts[4].ID())
    38  	h7 := hosts[7].Peerstore().PeerInfo(hosts[7].ID())
    39  	h0str := h0.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h0.ID.Pretty())
    40  	h1str := h1.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h1.ID.Pretty())
    41  	h2str := h2.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h2.ID.Pretty())
    42  	h3str := h3.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h3.ID.Pretty())
    43  	h4str := h4.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h4.ID.Pretty())
    44  	h7str := h7.Addrs[0].String() + fmt.Sprintf("/p2p/%v", h7.ID.Pretty())
    45  	subcfg := &p2pty.P2PSubConfig{}
    46  	subcfg.BootStraps = []string{h1str, h2str, h3str, h4str}
    47  	subcfg.Seeds = []string{h0str, h7str}
    48  	subcfg.RelayEnable = true
    49  	subcfg.RelayNodeAddr = []string{h0str}
    50  	peerinfo := []peer.AddrInfo{{ID: h7.ID, Addrs: h7.Addrs}}
    51  	initInnerPeers(hosts[5], peerinfo, subcfg)
    52  	hosts[5].Close()
    53  
    54  }