-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.go
312 lines (299 loc) · 8.4 KB
/
node.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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 dingo
import (
"context"
"errors"
"fmt"
"slices"
"github.com/blinklabs-io/dingo/chainsync"
"github.com/blinklabs-io/dingo/connmanager"
"github.com/blinklabs-io/dingo/event"
"github.com/blinklabs-io/dingo/mempool"
"github.com/blinklabs-io/dingo/peergov"
"github.com/blinklabs-io/dingo/state"
"github.com/blinklabs-io/dingo/utxorpc"
ouroboros "github.com/blinklabs-io/gouroboros"
oblockfetch "github.com/blinklabs-io/gouroboros/protocol/blockfetch"
ochainsync "github.com/blinklabs-io/gouroboros/protocol/chainsync"
olocalstatequery "github.com/blinklabs-io/gouroboros/protocol/localstatequery"
olocaltxmonitor "github.com/blinklabs-io/gouroboros/protocol/localtxmonitor"
olocaltxsubmission "github.com/blinklabs-io/gouroboros/protocol/localtxsubmission"
opeersharing "github.com/blinklabs-io/gouroboros/protocol/peersharing"
otxsubmission "github.com/blinklabs-io/gouroboros/protocol/txsubmission"
)
type Node struct {
config Config
connManager *connmanager.ConnectionManager
peerGov *peergov.PeerGovernor
chainsyncState *chainsync.State
eventBus *event.EventBus
mempool *mempool.Mempool
ledgerState *state.LedgerState
utxorpc *utxorpc.Utxorpc
shutdownFuncs []func(context.Context) error
}
func New(cfg Config) (*Node, error) {
eventBus := event.NewEventBus(cfg.promRegistry)
n := &Node{
config: cfg,
eventBus: eventBus,
mempool: mempool.NewMempool(
cfg.logger,
eventBus,
cfg.promRegistry,
),
}
if err := n.configPopulateNetworkMagic(); err != nil {
return nil, fmt.Errorf("invalid configuration: %s", err)
}
if err := n.configValidate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %s", err)
}
return n, nil
}
func (n *Node) Run() error {
// Configure tracing
if n.config.tracing {
if err := n.setupTracing(); err != nil {
return err
}
}
// Load state
state, err := state.NewLedgerState(
state.LedgerStateConfig{
DataDir: n.config.dataDir,
EventBus: n.eventBus,
Logger: n.config.logger,
CardanoNodeConfig: n.config.cardanoNodeConfig,
PromRegistry: n.config.promRegistry,
BlockfetchRequestRangeFunc: n.blockfetchClientRequestRange,
},
)
if err != nil {
return fmt.Errorf("failed to load state database: %w", err)
}
n.ledgerState = state
// Initialize chainsync state
n.chainsyncState = chainsync.NewState(
n.eventBus,
n.ledgerState,
)
// Configure connection manager
if err := n.configureConnManager(); err != nil {
return err
}
// Configure peer governor
n.peerGov = peergov.NewPeerGovernor(
peergov.PeerGovernorConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
ConnManager: n.connManager,
},
)
n.eventBus.SubscribeFunc(
peergov.OutboundConnectionEventType,
n.handleOutboundConnEvent,
)
if n.config.topologyConfig != nil {
n.peerGov.LoadTopologyConfig(n.config.topologyConfig)
}
if err := n.peerGov.Start(); err != nil {
return err
}
// Configure UTxO RPC
n.utxorpc = utxorpc.NewUtxorpc(
utxorpc.UtxorpcConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
LedgerState: n.ledgerState,
Mempool: n.mempool,
},
)
if err := n.utxorpc.Start(); err != nil {
return err
}
// Wait forever
select {}
}
func (n *Node) Stop() error {
// TODO: use a cancelable context and wait for it above to call shutdown
return n.shutdown()
}
func (n *Node) shutdown() error {
ctx := context.TODO()
var err error
// Shutdown ledger
err = errors.Join(err, n.ledgerState.Close())
// Call shutdown functions
for _, fn := range n.shutdownFuncs {
err = errors.Join(err, fn(ctx))
}
n.shutdownFuncs = nil
return err
}
func (n *Node) configureConnManager() error {
// Configure listeners
tmpListeners := make([]ListenerConfig, len(n.config.listeners))
for idx, l := range n.config.listeners {
if l.UseNtC {
// Node-to-client
l.ConnectionOpts = append(
l.ConnectionOpts,
ouroboros.WithNetworkMagic(n.config.networkMagic),
ouroboros.WithChainSyncConfig(
ochainsync.NewConfig(
n.chainsyncServerConnOpts()...,
),
),
ouroboros.WithLocalStateQueryConfig(
olocalstatequery.NewConfig(
n.localstatequeryServerConnOpts()...,
),
),
ouroboros.WithLocalTxMonitorConfig(
olocaltxmonitor.NewConfig(
n.localtxmonitorServerConnOpts()...,
),
),
ouroboros.WithLocalTxSubmissionConfig(
olocaltxsubmission.NewConfig(
n.localtxsubmissionServerConnOpts()...,
),
),
)
} else {
// Node-to-node config
l.ConnectionOpts = append(
l.ConnectionOpts,
ouroboros.WithPeerSharing(n.config.peerSharing),
ouroboros.WithNetworkMagic(n.config.networkMagic),
ouroboros.WithPeerSharingConfig(
opeersharing.NewConfig(
n.peersharingServerConnOpts()...,
),
),
ouroboros.WithTxSubmissionConfig(
otxsubmission.NewConfig(
n.txsubmissionServerConnOpts()...,
),
),
ouroboros.WithChainSyncConfig(
ochainsync.NewConfig(
n.chainsyncServerConnOpts()...,
),
),
ouroboros.WithBlockFetchConfig(
oblockfetch.NewConfig(
n.blockfetchServerConnOpts()...,
),
),
)
}
tmpListeners[idx] = l
}
// Create connection manager
n.connManager = connmanager.NewConnectionManager(
connmanager.ConnectionManagerConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
Listeners: tmpListeners,
OutboundSourcePort: n.config.outboundSourcePort,
OutboundConnOpts: []ouroboros.ConnectionOptionFunc{
ouroboros.WithNetworkMagic(n.config.networkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
ouroboros.WithFullDuplex(true),
ouroboros.WithPeerSharing(n.config.peerSharing),
ouroboros.WithPeerSharingConfig(
opeersharing.NewConfig(
slices.Concat(
n.peersharingClientConnOpts(),
n.peersharingServerConnOpts(),
)...,
),
),
ouroboros.WithTxSubmissionConfig(
otxsubmission.NewConfig(
slices.Concat(
n.txsubmissionClientConnOpts(),
n.txsubmissionServerConnOpts(),
)...,
),
),
ouroboros.WithChainSyncConfig(
ochainsync.NewConfig(
slices.Concat(
n.chainsyncClientConnOpts(),
n.chainsyncServerConnOpts(),
)...,
),
),
ouroboros.WithBlockFetchConfig(
oblockfetch.NewConfig(
slices.Concat(
n.blockfetchClientConnOpts(),
n.blockfetchServerConnOpts(),
)...,
),
),
},
},
)
// Subscribe to connection closed events
n.eventBus.SubscribeFunc(
connmanager.ConnectionClosedEventType,
n.handleConnClosedEvent,
)
// Start listeners
if err := n.connManager.Start(); err != nil {
return err
}
return nil
}
func (n *Node) handleConnClosedEvent(evt event.Event) {
e := evt.Data.(connmanager.ConnectionClosedEvent)
connId := e.ConnectionId
// Remove any chainsync client state
n.chainsyncState.RemoveClient(connId)
// Remove mempool consumer
n.mempool.RemoveConsumer(connId)
// Release chainsync client
n.chainsyncState.RemoveClientConnId(connId)
}
func (n *Node) handleOutboundConnEvent(evt event.Event) {
e := evt.Data.(peergov.OutboundConnectionEvent)
connId := e.ConnectionId
// 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(connId); err != nil {
n.config.logger.Error(
"failed to start chainsync client",
"error",
err,
)
return
}
n.chainsyncState.SetClientConnId(connId)
}
// Start txsubmission client
if err := n.txsubmissionClientStart(connId); err != nil {
n.config.logger.Error("failed to start chainsync client", "error", err)
return
}
}