forked from protolambda/rumor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
from_config.go
55 lines (45 loc) · 1004 Bytes
/
from_config.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
package custom
import (
"context"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
)
type starter interface {
Start()
}
type NodeOpts struct {
PeerKey crypto.PrivKey
ConnectionGater connmgr.ConnectionGater
}
type Config struct {
NodeOpts
HostOpts
NetworkOpts
TransportOpts
}
// NewNode constructs a new libp2p Host from the Config.
//
// This function consumes the config. Do not reuse it (really!).
func NewNode(ctx context.Context, cfg *Config) (host.Host, error) {
swrm, err := NewNetwork(ctx, &cfg.NodeOpts, &cfg.NetworkOpts)
if err != nil {
return nil, err
}
var h host.Host
h, err = NewHost(ctx, swrm, &cfg.HostOpts)
if err != nil {
swrm.Close()
return nil, err
}
err = AddTransports(h, swrm, &cfg.NodeOpts, &cfg.TransportOpts)
if err != nil {
h.Close()
return nil, err
}
// start the host background tasks
if st, ok := h.(starter); ok {
st.Start()
}
return h, nil
}