-
Notifications
You must be signed in to change notification settings - Fork 1
/
p2p.go
69 lines (61 loc) · 2.14 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
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package p2pgo
import (
"context"
"strings"
"github.com/CESSProject/p2p-go/config"
"github.com/CESSProject/p2p-go/core"
)
// Config describes a set of settings for a p2p peer.
type Config = config.Config
// Option is a p2p config option that can be given to the p2p constructor.
type Option = config.Option
// New constructs a new libp2p node with the given options, falling back on
// reasonable defaults. The defaults are:
//
// - If no listening port is provided, the host listens on the default
// port: 4001
//
// - If no bootstrap nodes is provided, the host will run as a server
//
// - If no protocol version is provided, The host uses the default
// protocol version: /kldr/1.0
//
// - If no DHT protocol version is provided, The host uses the default
// DHT protocol version: /kldr/kad/1.0
//
// To stop/shutdown the returned p2p node, the user needs to cancel the passed
// context and call `Close` on the returned Host.
func New(ctx context.Context, opts ...Option) (*core.PeerNode, error) {
return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...)
}
// NewWithoutDefaults constructs a new libp2p node with the given options but
// *without* falling back on reasonable defaults.
//
// Warning: This function should not be considered a stable interface. We may
// choose to add required services at any time and, by using this function, you
// opt-out of any defaults we may provide.
func NewWithoutDefaults(ctx context.Context, opts ...Option) (*core.PeerNode, error) {
var cfg Config
if err := cfg.Apply(opts...); err != nil {
return nil, err
}
if cfg.ProtocolPrefix == "" {
if len(cfg.BootPeers) > 0 {
if strings.Contains(cfg.BootPeers[0], "test") {
cfg.ProtocolPrefix = config.TestnetProtocolPrefix
} else if strings.Contains(cfg.BootPeers[0], "main") {
cfg.ProtocolPrefix = config.MainnetProtocolPrefix
} else {
cfg.ProtocolPrefix = config.DevnetProtocolPrefix
}
} else {
cfg.ProtocolPrefix = config.DevnetProtocolPrefix
}
}
return core.NewPeerNode(ctx, &cfg)
}