-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
p2p_v1_config.go
141 lines (125 loc) · 4.83 KB
/
p2p_v1_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package config
import (
"fmt"
"net"
"time"
"github.com/pkg/errors"
ocrnetworking "github.com/smartcontractkit/libocr/networking"
"github.com/smartcontractkit/chainlink/core/config/envvar"
"github.com/smartcontractkit/chainlink/core/config/parse"
)
// P2PV1Networking is a subset of global config relevant to p2p v1 networking.
type P2PV1Networking interface {
P2PAnnounceIP() net.IP
P2PAnnouncePort() uint16
P2PBootstrapPeers() ([]string, error)
P2PDHTAnnouncementCounterUserPrefix() uint32
P2PListenIP() net.IP
P2PListenPort() uint16
P2PListenPortRaw() string
P2PNewStreamTimeout() time.Duration
P2PBootstrapCheckInterval() time.Duration
P2PDHTLookupInterval() int
P2PPeerstoreWriteInterval() time.Duration
}
func (c *generalConfig) P2PPeerstoreWriteInterval() time.Duration {
return c.getWithFallback("P2PPeerstoreWriteInterval", parse.Duration).(time.Duration)
}
func (c *generalConfig) P2PBootstrapPeers() ([]string, error) {
if c.viper.IsSet(envvar.Name("P2PBootstrapPeers")) {
bps := c.viper.GetStringSlice(envvar.Name("P2PBootstrapPeers"))
if bps != nil {
return bps, nil
}
return nil, errors.Wrap(ErrEnvUnset, "P2P_BOOTSTRAP_PEERS env var is not set")
}
return []string{}, nil
}
// P2PListenIP is the ip that libp2p willl bind to and listen on
func (c *generalConfig) P2PListenIP() net.IP {
return c.getWithFallback("P2PListenIP", parse.IP).(net.IP)
}
// P2PListenPort is the port that libp2p will bind to and listen on
func (c *generalConfig) P2PListenPort() uint16 {
if c.viper.IsSet(envvar.Name("P2PListenPort")) {
return uint16(c.viper.GetUint32(envvar.Name("P2PListenPort")))
}
switch c.P2PNetworkingStack() {
case ocrnetworking.NetworkingStackV1, ocrnetworking.NetworkingStackV1V2:
return c.randomP2PListenPort()
default:
return 0
}
}
func (c *generalConfig) randomP2PListenPort() uint16 {
// Fast path in case it was already set
c.randomP2PPortMtx.RLock()
if c.randomP2PPort > 0 {
c.randomP2PPortMtx.RUnlock()
return c.randomP2PPort
}
c.randomP2PPortMtx.RUnlock()
// Path for initial set
c.randomP2PPortMtx.Lock()
defer c.randomP2PPortMtx.Unlock()
if c.randomP2PPort > 0 {
return c.randomP2PPort
}
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
panic(fmt.Errorf("unexpected ResolveTCPAddr error generating random port: %w", err))
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(fmt.Errorf("unexpected ListenTCP error generating random port: %w", err))
}
defer l.Close()
c.randomP2PPort = uint16(l.Addr().(*net.TCPAddr).Port)
c.lggr.Warnw(fmt.Sprintf("P2P_LISTEN_PORT was not set, listening on random port %d. A new random port will be generated on every boot, for stability it is recommended to set P2P_LISTEN_PORT to a fixed value in your environment", c.randomP2PPort), "p2pPort", c.randomP2PPort)
return c.randomP2PPort
}
// P2PListenPortRaw returns the raw string value of P2P_LISTEN_PORT
func (c *generalConfig) P2PListenPortRaw() string {
return c.viper.GetString(envvar.Name("P2PListenPort"))
}
// P2PAnnounceIP is an optional override. If specified it will force the p2p
// layer to announce this IP as the externally reachable one to the DHT
// If this is set, P2PAnnouncePort MUST also be set.
func (c *generalConfig) P2PAnnounceIP() net.IP {
str := c.viper.GetString(envvar.Name("P2PAnnounceIP"))
return net.ParseIP(str)
}
// P2PAnnouncePort is an optional override. If specified it will force the p2p
// layer to announce this port as the externally reachable one to the DHT.
// If this is set, P2PAnnounceIP MUST also be set.
func (c *generalConfig) P2PAnnouncePort() uint16 {
return uint16(c.viper.GetUint32(envvar.Name("P2PAnnouncePort")))
}
// P2PDHTAnnouncementCounterUserPrefix can be used to restore the node's
// ability to announce its IP/port on the P2P network after a database
// rollback. Make sure to only increase this value, and *never* decrease it.
// Don't use this variable unless you really know what you're doing, since you
// could semi-permanently exclude your node from the P2P network by
// misconfiguring it.
func (c *generalConfig) P2PDHTAnnouncementCounterUserPrefix() uint32 {
return c.viper.GetUint32(envvar.Name("P2PDHTAnnouncementCounterUserPrefix"))
}
// FIXME: Add comments to all of these
func (c *generalConfig) P2PBootstrapCheckInterval() time.Duration {
if c.ocrBootstrapCheckInterval() != 0 {
return c.ocrBootstrapCheckInterval()
}
return c.getWithFallback("P2PBootstrapCheckInterval", parse.Duration).(time.Duration)
}
func (c *generalConfig) P2PDHTLookupInterval() int {
if c.ocrDHTLookupInterval() != 0 {
return c.ocrDHTLookupInterval()
}
return int(getEnvWithFallback(c, envvar.NewUint16("P2PDHTLookupInterval")))
}
func (c *generalConfig) P2PNewStreamTimeout() time.Duration {
if c.ocrNewStreamTimeout() != 0 {
return c.ocrNewStreamTimeout()
}
return c.getWithFallback("P2PNewStreamTimeout", parse.Duration).(time.Duration)
}