Repository navigation
Expand file tree
/
Copy pathnode_components.go
More file actions
261 lines (248 loc) · 10.4 KB
/
Copy pathnode_components.go
File metadata and controls
261 lines (248 loc) · 10.4 KB
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
// Copyright 2026 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"
"time"
"github.com/blinklabs-io/dingo/config/cardano"
"github.com/blinklabs-io/dingo/connmanager"
"github.com/blinklabs-io/dingo/database"
"github.com/blinklabs-io/dingo/database/models"
"github.com/blinklabs-io/dingo/event"
"github.com/blinklabs-io/dingo/internal/dblifecycle"
"github.com/blinklabs-io/dingo/internal/historyexpiry"
"github.com/blinklabs-io/dingo/internal/node/ledgerpeers"
"github.com/blinklabs-io/dingo/internal/offchainmetadata"
"github.com/blinklabs-io/dingo/ledger/snapshot"
midnightserver "github.com/blinklabs-io/dingo/midnight/server"
"github.com/blinklabs-io/dingo/peergov"
"github.com/blinklabs-io/dingo/plugin"
ouroboros "github.com/blinklabs-io/gouroboros"
)
func (n *Node) databaseConfig() *database.Config {
return &database.Config{
DataDir: n.config.dataDir,
Logger: n.config.logger,
PromRegistry: n.config.promRegistry,
StorageMode: string(n.config.storageMode),
Network: n.config.network,
NetworkMagic: n.config.networkMagic,
StartEra: string(n.config.startEra),
StrictUtxoValidation: n.config.strictUtxoValidation,
BlobPlugin: n.config.pluginSelections[plugin.CapabilityStorageBlob].
Provider,
MetadataPlugin: n.config.pluginSelections[plugin.CapabilityStorageMetadata].
Provider,
CacheConfig: database.CborCacheConfig{
BlockLRUEntries: n.config.cacheBlockLRUEntries,
HotUtxoEntries: n.config.cacheHotUtxoEntries,
HotTxEntries: n.config.cacheHotTxEntries,
HotTxMaxBytes: n.config.cacheHotTxMaxBytes,
},
AlonzoLovelacePerUtxoWord: cardano.AlonzoLovelacePerUtxoWord(
n.config.cardanoNodeConfig, "", n.config.network,
),
}
}
func (n *Node) newConnectionManager() *connmanager.ConnectionManager {
return connmanager.NewConnectionManager(
connmanager.ConnectionManagerConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
ListenersProvider: func() []connmanager.ListenerConfig {
return n.ouroboros().
ConfigureListeners(n.ctx, n.config.listeners)
},
OutboundSourcePort: n.config.outboundSourcePort,
OutboundConnOptsProvider: func() []ouroboros.ConnectionOptionFunc {
return n.ouroboros().OutboundConnOpts(n.ctx)
},
PromRegistry: n.config.promRegistry,
MaxConnectionsPerIP: n.config.maxConnectionsPerIP,
MaxInboundConns: n.config.maxInboundConns,
MaxNtCConns: n.config.maxNtCConns,
MaxNtCConnectionsPerIP: n.config.maxNtCConnectionsPerIP,
MaxTrustedLocalNtCConns: n.config.maxTrustedLocalNtCConns,
ConnClosedOwnerFunc: n.handleConnManagerClosedOwner,
},
)
}
func (n *Node) newPeerGovernor() *peergov.PeerGovernor {
ledgerPeerProvider := ledgerpeers.NewProvider(n.poolRelayProvider)
var useLedgerAfterSlot int64 = -1
if n.config.topologyConfig != nil {
useLedgerAfterSlot = n.config.topologyConfig.UseLedgerAfterSlot
}
peerGovConfig := peergov.PeerGovernorConfig{
Logger: n.config.logger,
EventBus: n.eventBus,
ConnManager: n.connManager,
DisableOutbound: n.config.isDevMode(),
PromRegistry: n.config.promRegistry,
PeerRequestFunc: func(peer *peergov.Peer) []string {
return n.ouroboros().RequestPeersFromPeer(peer)
},
LedgerPeerProvider: ledgerPeerProvider,
UseLedgerAfterSlot: useLedgerAfterSlot,
LedgerPeerTarget: n.config.ledgerPeerTarget,
ActivePeersTopologyQuota: n.config.activePeersTopologyQuota,
ActivePeersGossipQuota: n.config.activePeersGossipQuota,
ActivePeersLedgerQuota: n.config.activePeersLedgerQuota,
InboundWarmTarget: n.config.inboundWarmTarget,
InboundHotQuota: n.config.inboundHotQuota,
InboundMinTenure: n.config.inboundMinTenure,
InboundHotScoreThreshold: n.config.inboundHotScoreThreshold,
InboundPruneAfter: n.config.inboundPruneAfter,
InboundDuplexOnlyForHot: n.config.inboundDuplexOnlyForHot,
InboundCooldown: n.config.inboundCooldown,
MinHotPeers: n.config.minHotPeers,
ReconcileInterval: n.config.reconcileInterval,
InactivityTimeout: n.config.inactivityTimeout,
SyncProgressProvider: n.ledgerState,
BootstrapPromotionMinDiversityGroups: n.config.bootstrapPromotionMinDiversityGroups,
}
applyPeerTargets(n.config, &peerGovConfig)
return peergov.NewPeerGovernor(peerGovConfig)
}
func (n *Node) newHistoryExpiryPruner() *historyexpiry.Pruner {
prunerFreq := n.config.historyExpiry.Frequency
if prunerFreq <= 0 {
prunerFreq = time.Hour
}
return historyexpiry.NewPruner(historyexpiry.PrunerConfig{
LedgerState: n.ledgerState,
DB: n.db,
Logger: n.config.logger,
Frequency: prunerFreq,
})
}
func (n *Node) newDatabaseLifecycleManager() *dblifecycle.Manager {
return dblifecycle.NewManager(
n.db,
n.eventBus,
n.config.databaseLifecycle,
n.config.pluginSelections[plugin.CapabilityStorageBlob].Provider,
n.config.pluginSelections[plugin.CapabilityStorageMetadata].Provider,
n.destinationRegistry,
n.config.logger,
)
}
func (n *Node) midnightServerConfig(ctx context.Context) midnightserver.Config {
return midnightserver.Config{
Logger: n.config.logger,
Metadata: n.db.Metadata(),
BlockNumberByHash: func(hash []byte) (uint64, bool, error) {
block, err := database.BlockByHash(ctx, n.db, hash)
if err != nil {
if errors.Is(err, models.ErrBlockNotFound) {
return 0, false, nil
}
// The Midnight server names the request step.
return 0, false, err
}
return block.Number, true, nil
},
Host: n.config.midnight.Host,
Port: n.config.midnight.Port,
TLSCertFilePath: n.config.tlsCertFilePath,
TLSKeyFilePath: n.config.tlsKeyFilePath,
ReflectionEnabled: n.config.midnight.ReflectionEnabled,
ShutdownTimeout: n.config.shutdownTimeout,
Database: midnightserver.NewDatabase(n.db),
SlotTimer: n.ledgerState,
PromRegistry: n.config.promRegistry,
}
}
func (n *Node) offchainMetadataConfig() offchainmetadata.Config {
return offchainmetadata.Config{
Logger: n.config.logger,
Store: n.db.Metadata(),
HTTPClient: n.config.offchainMetadata.HTTPClient,
Interval: n.config.offchainMetadata.Interval,
RequestTimeout: n.config.offchainMetadata.RequestTimeout,
UserAgent: n.config.offchainMetadata.UserAgent,
IPFSGatewayURL: n.config.offchainMetadata.IPFSGatewayURL,
BatchSize: n.config.offchainMetadata.BatchSize,
MaxBytes: n.config.offchainMetadata.MaxBytes,
AllowPrivateAddresses: n.config.offchainMetadata.AllowPrivateAddresses,
}
}
func (n *Node) configureSnapshotManager() error {
n.snapshotMgr = snapshot.NewManager(
n.db,
n.eventBus,
n.config.logger,
)
// Mirror the CIP-0163 reward-account inactivity gate into snapshot capture
// so it matches the ledger config that drives account expiry stamping.
if err := n.snapshotMgr.SetDelegatorInactivity(
n.config.delegatorInactivityEnabled,
n.config.delegatorInactivity,
); err != nil {
return fmt.Errorf("configuring snapshot manager: %w", err)
}
n.snapshotMgr.SetPromRegistry(n.config.promRegistry)
// When the Koios parity observer is enabled, retain reward_account_output
// without bound in CORE storage mode too: the observer only
// validates a closed epoch after fetching and comparing against Koios over
// the network, which can fall arbitrarily far behind chain progression
// during a from-genesis or catch-up sync, well past the fixed 4-epoch
// window cleanupOldSnapshots otherwise prunes reward_account_output to.
// Configure this before capture or startup locks the settings.
n.snapshotMgr.SetRewardAccountOutputRetentionUnbounded(
n.config.koiosParity.Enabled,
)
// Prune pool snapshots through the deferred-header retention guard, so a
// snapshot a queued/deferred header still needs for leader validation is
// never pruned out from under it and misread as pool absence, and the
// floor selection is atomic with deferred-header admission.
// Set before Start; the pin is released automatically as headers resolve.
n.snapshotMgr.SetPoolSnapshotRetentionGuard(
n.ledgerState.PrunePoolSnapshotsWithRetentionFloor,
)
// Wire the authoritative epoch-boundary capture before block sync begins so
// each epoch rollover stages its mark snapshot atomically at the SNAP point.
// Set before CaptureGenesisSnapshot/sync; a nil hook (never set) would leave
// only the event-driven fallback capture.
// The stake read runs at the SNAP point (after MIR and before POOLREAP/
// enactment) and the row write at the end of the rollover, both inside the
// same transaction.
n.ledgerState.SetEpochBoundarySnapshotStakeHook(
func(txn *database.Txn, evt event.EpochTransitionEvent) error {
return n.snapshotMgr.ComputeEpochBoundarySnapshot(n.ctx, txn, evt)
},
)
n.ledgerState.SetEpochBoundarySnapshotHook(
func(txn *database.Txn, evt event.EpochTransitionEvent) error {
return n.snapshotMgr.CaptureEpochBoundarySnapshot(n.ctx, txn, evt)
},
)
wireDeferredRewardStakeInputs(n.ledgerState, n.snapshotMgr)
// Wire governance's same-boundary SPO stake read: RATIFY
// tallies mark[NewEpoch] -- this same boundary's own mark snapshot -- but
// that row is not durably written until the hook above runs, later in
// the same rollover. Without this, governance would silently see zero
// SPO stake for every SPO-gated action at every boundary.
n.ledgerState.SetCurrentBoundarySPOStakeHook(
func(
txn *database.Txn,
evt event.EpochTransitionEvent,
) ([]*models.PoolStakeSnapshot, error) {
return n.snapshotMgr.CurrentBoundarySPOStakeRows(n.ctx, txn, evt)
},
)
return nil
}