2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
import (
|
2019-10-03 00:02:06 +00:00
|
|
|
"crypto/rand"
|
2019-10-17 10:18:40 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-01 09:09:48 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
2022-07-08 02:05:50 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
2019-07-01 09:09:48 +00:00
|
|
|
"go.uber.org/fx"
|
2022-06-14 15:00:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-07-01 09:09:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("p2pnode")
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
const (
|
2020-10-11 18:12:01 +00:00
|
|
|
KLibp2pHost = "libp2p-host"
|
|
|
|
KTLibp2pHost types.KeyType = KLibp2pHost
|
2020-06-29 19:41:43 +00:00
|
|
|
)
|
2019-10-03 00:02:06 +00:00
|
|
|
|
2019-07-01 09:09:48 +00:00
|
|
|
type Libp2pOpts struct {
|
|
|
|
fx.Out
|
|
|
|
|
|
|
|
Opts []libp2p.Option `group:"libp2p"`
|
|
|
|
}
|
|
|
|
|
2019-10-03 00:02:06 +00:00
|
|
|
func PrivKey(ks types.KeyStore) (crypto.PrivKey, error) {
|
2020-06-29 19:41:43 +00:00
|
|
|
k, err := ks.Get(KLibp2pHost)
|
2019-10-03 00:02:06 +00:00
|
|
|
if err == nil {
|
|
|
|
return crypto.UnmarshalPrivateKey(k.PrivateKey)
|
|
|
|
}
|
2019-10-17 10:18:40 +00:00
|
|
|
if !xerrors.Is(err, types.ErrKeyInfoNotFound) {
|
2019-10-03 00:02:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pk, err := genLibp2pKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-21 11:10:04 +00:00
|
|
|
kbytes, err := crypto.MarshalPrivateKey(pk)
|
2019-10-03 00:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
if err := ks.Put(KLibp2pHost, types.KeyInfo{
|
|
|
|
Type: KTLibp2pHost,
|
2019-10-03 00:02:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-01 09:09:48 +00:00
|
|
|
// Misc options
|
|
|
|
|
2019-12-17 16:09:43 +00:00
|
|
|
func ConnectionManager(low, high uint, grace time.Duration, protected []string) func() (opts Libp2pOpts, err error) {
|
2019-12-17 05:37:31 +00:00
|
|
|
return func() (Libp2pOpts, error) {
|
2021-12-14 13:30:29 +00:00
|
|
|
cm, err := connmgr.NewConnManager(int(low), int(high), connmgr.WithGracePeriod(grace))
|
|
|
|
if err != nil {
|
|
|
|
return Libp2pOpts{}, err
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:37:31 +00:00
|
|
|
for _, p := range protected {
|
2022-07-07 21:47:33 +00:00
|
|
|
pid, err := peer.Decode(p)
|
2019-12-17 05:37:31 +00:00
|
|
|
if err != nil {
|
2019-12-17 06:15:06 +00:00
|
|
|
return Libp2pOpts{}, xerrors.Errorf("failed to parse peer ID in protected peers array: %w", err)
|
2019-12-17 05:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm.Protect(pid, "config-prot")
|
|
|
|
}
|
2019-12-17 06:15:06 +00:00
|
|
|
|
2019-12-17 19:18:19 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-12-17 06:15:06 +00:00
|
|
|
return Libp2pOpts{
|
|
|
|
Opts: []libp2p.Option{libp2p.ConnectionManager(cm)},
|
|
|
|
}, nil
|
2019-07-01 09:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|