mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2025-01-15 02:30:05 +08:00
07fbce0b46
Avoids circular module dependency.
31 lines
515 B
Go
31 lines
515 B
Go
package test
|
|
|
|
import (
|
|
"io"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
|
)
|
|
|
|
func RandPeerID() (peer.ID, error) {
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
buf := make([]byte, 16)
|
|
if _, err := io.ReadFull(r, buf); err != nil {
|
|
return "", err
|
|
}
|
|
h, _ := mh.Sum(buf, mh.SHA2_256, -1)
|
|
return peer.ID(h), nil
|
|
}
|
|
|
|
func RandPeerIDFatal(t testing.TB) peer.ID {
|
|
p, err := RandPeerID()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|