-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
libp2p.go
114 lines (93 loc) · 2.49 KB
/
libp2p.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package lp2p
import (
"crypto/rand"
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
connmgr "github.com/libp2p/go-libp2p-connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
)
var log = logging.Logger("p2pnode")
const (
KLibp2pHost = "libp2p-host"
KTLibp2pHost types.KeyType = KLibp2pHost
)
type Libp2pOpts struct {
fx.Out
Opts []libp2p.Option `group:"libp2p"`
}
func PrivKey(ks types.KeyStore) (crypto.PrivKey, error) {
k, err := ks.Get(KLibp2pHost)
if err == nil {
return crypto.UnmarshalPrivateKey(k.PrivateKey)
}
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
return nil, err
}
pk, err := genLibp2pKey()
if err != nil {
return nil, err
}
kbytes, err := crypto.MarshalPrivateKey(pk)
if err != nil {
return nil, err
}
if err := ks.Put(KLibp2pHost, types.KeyInfo{
Type: KTLibp2pHost,
PrivateKey: kbytes,
}); err != nil {
return nil, err
}
return pk, nil
}
func genLibp2pKey() (crypto.PrivKey, error) {
pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return nil, err
}
return pk, nil
}
// Misc options
func ConnectionManager(low, high uint, grace time.Duration, protected []string) func() (opts Libp2pOpts, err error) {
return func() (Libp2pOpts, error) {
cm, err := connmgr.NewConnManager(int(low), int(high), connmgr.WithGracePeriod(grace))
if err != nil {
return Libp2pOpts{}, err
}
for _, p := range protected {
pid, err := peer.IDFromString(p)
if err != nil {
return Libp2pOpts{}, xerrors.Errorf("failed to parse peer ID in protected peers array: %w", err)
}
cm.Protect(pid, "config-prot")
}
infos, err := build.BuiltinBootstrap()
if err != nil {
return Libp2pOpts{}, xerrors.Errorf("failed to get bootstrap peers: %w", err)
}
for _, inf := range infos {
cm.Protect(inf.ID, "bootstrap")
}
return Libp2pOpts{
Opts: []libp2p.Option{libp2p.ConnectionManager(cm)},
}, nil
}
}
func PstoreAddSelfKeys(id peer.ID, sk crypto.PrivKey, ps peerstore.Peerstore) error {
if err := ps.AddPubKey(id, sk.GetPublic()); err != nil {
return err
}
return ps.AddPrivKey(id, sk)
}
func simpleOpt(opt libp2p.Option) func() (opts Libp2pOpts, err error) {
return func() (opts Libp2pOpts, err error) {
opts.Opts = append(opts.Opts, opt)
return
}
}