/
p2p.go
92 lines (78 loc) · 2.45 KB
/
p2p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package helpers
import (
astra_bls "github.com/astra-net/astra-network/crypto/bls"
nodeconfig "github.com/astra-net/astra-network/internal/configs/node"
"github.com/astra-net/astra-network/p2p"
"github.com/astra-net/bls/ffi/go/bls"
libp2p_crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/pkg/errors"
)
// Host - struct for representing a host (IP / Port)
type Host struct {
IP string
Port string
}
var (
// Hosts - host combinations
Hosts []Host
// Topics - p2p topics
Topics []string
// Bootnodes - p2p bootnodes
Bootnodes []string
)
func init() {
Hosts = []Host{
{IP: nodeconfig.DefaultLocalListenIP, Port: "9000"},
{IP: nodeconfig.DefaultLocalListenIP, Port: "9001"},
}
Topics = []string{
"astra/testnet/0.0.1/client/beacon",
"astra/testnet/0.0.1/node/beacon",
"astra/testnet/0.0.1/node/shard/1",
"astra/testnet/0.0.1/node/shard/2",
"astra/testnet/0.0.1/node/shard/3",
}
Bootnodes = []string{
"/ip4/54.86.126.90/tcp/9850/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv",
"/ip4/52.40.84.2/tcp/9850/p2p/QmbPVwrqWsTYXq1RxGWcxx9SWaTUCfoo1wA6wmdbduWe29",
}
}
// GenerateHost - test helper to generate a new host
func GenerateHost(address string, port string) (p2p.Host, *bls.PublicKey, error) {
nodePrivateKey, _, err := GeneratePrivateKey()
if err != nil {
return nil, nil, errors.New("failed to generate private key for node")
}
peer, err := GeneratePeer(address, port)
if err != nil {
return nil, nil, err
}
host, err := p2p.NewHost(p2p.HostConfig{
Self: &peer,
BLSKey: nodePrivateKey,
BootNodes: nil,
DataStoreFile: nil,
})
if err != nil {
return nil, nil, err
}
return host, peer.ConsensusPubKey, nil
}
// GeneratePeer - test helper to generate a new peer
func GeneratePeer(address string, port string) (p2p.Peer, error) {
peerPrivateKey := astra_bls.RandPrivateKey()
peerPublicKey := peerPrivateKey.GetPublicKey()
if peerPrivateKey == nil || peerPublicKey == nil {
return p2p.Peer{}, errors.New("failed to generate bls key for peer")
}
peer := p2p.Peer{IP: address, Port: port, ConsensusPubKey: peerPublicKey}
return peer, nil
}
// GeneratePrivateKey - test helper to generate a new private key to be used for p2p
func GeneratePrivateKey() (privateKey libp2p_crypto.PrivKey, publicKey libp2p_crypto.PubKey, err error) {
privateKey, publicKey, err = libp2p_crypto.GenerateKeyPair(libp2p_crypto.RSA, 2048)
if err != nil {
return nil, nil, err
}
return privateKey, publicKey, nil
}