github.com/koko1123/flow-go-1@v0.29.6/network/p2p/p2pnode/libp2pUtils_test.go (about)

     1  package p2pnode_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  	"github.com/multiformats/go-multiaddr"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/stretchr/testify/suite"
    12  
    13  	"github.com/koko1123/flow-go-1/network/p2p/utils"
    14  
    15  	"github.com/onflow/flow-go/crypto"
    16  
    17  	"github.com/koko1123/flow-go-1/model/flow"
    18  	"github.com/koko1123/flow-go-1/network/p2p/keyutils"
    19  	"github.com/koko1123/flow-go-1/utils/unittest"
    20  )
    21  
    22  type LibP2PUtilsTestSuite struct {
    23  	suite.Suite
    24  }
    25  
    26  func TestLibP2PUtilsTestSuite(t *testing.T) {
    27  	suite.Run(t, new(LibP2PUtilsTestSuite))
    28  }
    29  
    30  // TestPeerInfoFromID tests that PeerInfoFromID converts a flow.Identity to peer.AddrInfo correctly
    31  func (ts *LibP2PUtilsTestSuite) TestPeerInfoFromID() {
    32  	ids, exceptedPeerInfos := idsAndPeerInfos(ts.T())
    33  	for i, id := range ids {
    34  		actualAddrInfo, err := utils.PeerAddressInfo(*id)
    35  		assert.NoError(ts.T(), err)
    36  		assert.Equal(ts.T(), exceptedPeerInfos[i].String(), actualAddrInfo.String())
    37  	}
    38  }
    39  
    40  func idsAndPeerInfos(t *testing.T) (flow.IdentityList, []peer.AddrInfo) {
    41  	ips := []string{"1.1.1.1", "2.2.2.2"}
    42  	port := "3569"
    43  
    44  	ids := make(flow.IdentityList, len(ips))
    45  	peerInfos := make([]peer.AddrInfo, len(ips))
    46  
    47  	keyOpt := func(id *flow.Identity) {
    48  		key, err := generateFlowNetworkingKey(id.NodeID)
    49  		require.NoError(t, err)
    50  		id.NetworkPubKey = key.PublicKey()
    51  	}
    52  
    53  	for i, ip := range ips {
    54  		// create a flow Identity
    55  		id := unittest.IdentityFixture(keyOpt)
    56  		id.Address = fmt.Sprintf("%s:%s", ip, port)
    57  		ids[i] = id
    58  
    59  		// create a libp2p p2p.PeerAddressInfo
    60  		libp2pKey, err := keyutils.LibP2PPublicKeyFromFlow(id.NetworkPubKey)
    61  		assert.NoError(t, err)
    62  		peerID, err := peer.IDFromPublicKey(libp2pKey)
    63  		assert.NoError(t, err)
    64  		addrInfo := peer.AddrInfo{
    65  			ID:    peerID,
    66  			Addrs: []multiaddr.Multiaddr{multiaddr.StringCast(fmt.Sprintf("/ip4/%s/tcp/%s", ip, port))},
    67  		}
    68  		peerInfos[i] = addrInfo
    69  	}
    70  
    71  	return ids, peerInfos
    72  }
    73  
    74  func generateFlowNetworkingKey(s flow.Identifier) (crypto.PrivateKey, error) {
    75  	seed := make([]byte, crypto.KeyGenSeedMinLenECDSASecp256k1)
    76  	copy(seed, s[:])
    77  	return crypto.GeneratePrivateKey(crypto.ECDSASecp256k1, seed)
    78  }
    79  
    80  func BenchmarkPeerInfoFromID(b *testing.B) {
    81  	b.StopTimer()
    82  	id := unittest.IdentityFixture()
    83  	key, _ := generateFlowNetworkingKey(id.NodeID)
    84  	id.NetworkPubKey = key.PublicKey()
    85  	id.Address = "1.1.1.1:3569"
    86  	b.StartTimer()
    87  	for n := 0; n < b.N; n++ {
    88  		_, _ = utils.PeerAddressInfo(*id)
    89  	}
    90  }