-
Notifications
You must be signed in to change notification settings - Fork 2
/
outbound.go
283 lines (273 loc) · 7.02 KB
/
outbound.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package node
import (
"context"
"fmt"
"net"
"strconv"
"time"
ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/protocol/blockfetch"
"github.com/blinklabs-io/gouroboros/protocol/chainsync"
"github.com/blinklabs-io/gouroboros/protocol/peersharing"
"github.com/blinklabs-io/gouroboros/protocol/txsubmission"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
const (
initialReconnectDelay = 1 * time.Second
maxReconnectDelay = 128 * time.Second
reconnectBackoffFactor = 2
)
type outboundPeer struct {
Address string
ReconnectCount int
ReconnectDelay time.Duration
}
func (n *Node) startOutboundConnections() {
n.config.logger.Debug(
"starting connections",
"component", "network",
"role", "client",
)
var tmpHosts []string
for _, host := range n.config.topologyConfig.BootstrapPeers {
n.config.logger.Debug(
fmt.Sprintf(
"adding bootstrap peer topology host: %s:%d",
host.Address,
host.Port,
),
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
)
}
for _, localRoot := range n.config.topologyConfig.LocalRoots {
for _, host := range localRoot.AccessPoints {
n.config.logger.Debug(
fmt.Sprintf(
"adding localRoot topology host: %s:%d",
host.Address,
host.Port,
),
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
)
}
}
for _, publicRoot := range n.config.topologyConfig.PublicRoots {
for _, host := range publicRoot.AccessPoints {
n.config.logger.Debug(
fmt.Sprintf(
"adding publicRoot topology host: %s:%d",
host.Address,
host.Port,
),
"component", "network",
"role", "client",
)
tmpHosts = append(
tmpHosts,
net.JoinHostPort(host.Address, strconv.Itoa(int(host.Port))),
)
}
}
// Start outbound connections
for _, host := range tmpHosts {
tmpPeer := outboundPeer{Address: host}
go func(peer outboundPeer) {
if err := n.createOutboundConnection(peer); err != nil {
n.config.logger.Error(
fmt.Sprintf(
"outbound: failed to establish connection to %s: %s",
peer.Address,
err,
),
"component", "network",
)
go n.reconnectOutboundConnection(peer)
}
}(tmpPeer)
}
}
func (n *Node) createOutboundConnection(peer outboundPeer) error {
t := otel.Tracer("")
if t != nil {
_, span := t.Start(context.TODO(), "create outbound connection")
defer span.End()
span.SetAttributes(
attribute.String("peer.address", peer.Address),
)
}
var clientAddr net.Addr
dialer := net.Dialer{
Timeout: 10 * time.Second,
}
if n.config.outboundSourcePort > 0 {
// Setup connection to use our listening port as the source port
// This is required for peer sharing to be useful
clientAddr, _ = net.ResolveTCPAddr(
"tcp",
fmt.Sprintf(":%d", n.config.outboundSourcePort),
)
dialer.LocalAddr = clientAddr
dialer.Control = socketControl
}
n.config.logger.Debug(
fmt.Sprintf(
"establishing TCP connection to: %s",
peer.Address,
),
"component", "network",
"role", "client",
)
tmpConn, err := dialer.Dial("tcp", peer.Address)
if err != nil {
return err
}
// Build connection options
connOpts := []ouroboros.ConnectionOptionFunc{
ouroboros.WithConnection(tmpConn),
ouroboros.WithLogger(n.config.logger),
ouroboros.WithNetworkMagic(n.config.networkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
ouroboros.WithFullDuplex(true),
ouroboros.WithPeerSharing(n.config.peerSharing),
ouroboros.WithPeerSharingConfig(
peersharing.NewConfig(
mergeConnOpts(
n.peersharingClientConnOpts(),
n.peersharingServerConnOpts(),
)...,
),
),
ouroboros.WithTxSubmissionConfig(
txsubmission.NewConfig(
mergeConnOpts(
n.txsubmissionClientConnOpts(),
n.txsubmissionServerConnOpts(),
)...,
),
),
ouroboros.WithChainSyncConfig(
chainsync.NewConfig(
mergeConnOpts(
n.chainsyncClientConnOpts(),
n.chainsyncServerConnOpts(),
)...,
),
),
ouroboros.WithBlockFetchConfig(
blockfetch.NewConfig(
mergeConnOpts(
n.blockfetchClientConnOpts(),
n.blockfetchServerConnOpts(),
)...,
),
),
}
// Setup Ouroboros connection
n.config.logger.Debug(
fmt.Sprintf(
"establishing ouroboros protocol to %s",
peer.Address,
),
"component", "network",
"role", "client",
)
oConn, err := ouroboros.NewConnection(
connOpts...,
)
if err != nil {
return err
}
n.config.logger.Info(
fmt.Sprintf("connected ouroboros to %s", peer.Address),
"component", "network",
"role", "client",
)
n.config.logger.Debug(
fmt.Sprintf("peer address mapping: address: %s", peer.Address),
"component", "network",
"role", "client",
"connection_id", oConn.Id().String(),
)
peer.ReconnectCount = 0
peer.ReconnectDelay = 0
// Add to connection manager
n.connManager.AddConnection(oConn)
// Add to outbound connection tracking
n.outboundConnsMutex.Lock()
n.outboundConns[oConn.Id()] = peer
n.outboundConnsMutex.Unlock()
// TODO: replace this with handling for multiple chainsync clients
// Start chainsync client if we don't have another
n.chainsyncState.Lock()
defer n.chainsyncState.Unlock()
chainsyncClientConnId := n.chainsyncState.GetClientConnId()
if chainsyncClientConnId == nil {
if err := n.chainsyncClientStart(oConn.Id()); err != nil {
return err
}
n.chainsyncState.SetClientConnId(oConn.Id())
}
// Start txsubmission client
if err := n.txsubmissionClientStart(oConn.Id()); err != nil {
return err
}
return nil
}
func (n *Node) reconnectOutboundConnection(peer outboundPeer) {
for {
if peer.ReconnectDelay == 0 {
peer.ReconnectDelay = initialReconnectDelay
} else if peer.ReconnectDelay < maxReconnectDelay {
peer.ReconnectDelay = peer.ReconnectDelay * reconnectBackoffFactor
}
peer.ReconnectCount += 1
n.config.logger.Info(
fmt.Sprintf(
"outbound: delaying %s (retry %d) before reconnecting to %s",
peer.ReconnectDelay,
peer.ReconnectCount,
peer.Address,
),
"component", "network",
)
time.Sleep(peer.ReconnectDelay)
if err := n.createOutboundConnection(peer); err != nil {
n.config.logger.Error(
fmt.Sprintf(
"outbound: failed to establish connection to %s: %s",
peer.Address,
err,
),
"component", "network",
)
continue
}
peer.ReconnectCount = 0
return
}
}