@blinklabs/dingo 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/.dingo/blob/000001.sst +0 -0
  2. package/.dingo/blob/000001.vlog +0 -0
  3. package/.dingo/blob/DISCARD +0 -0
  4. package/.dingo/blob/KEYREGISTRY +2 -0
  5. package/.dingo/blob/MANIFEST +0 -0
  6. package/.dingo/metadata.sqlite +0 -0
  7. package/.github/workflows/golangci-lint.yml +1 -1
  8. package/.github/workflows/publish.yml +1 -7
  9. package/README.md +2 -1
  10. package/{bin/dingo → dingo} +0 -0
  11. package/dingo.yaml.example +53 -0
  12. package/package.json +2 -2
  13. package/Dockerfile +0 -25
  14. package/Makefile +0 -53
  15. package/blockfetch.go +0 -144
  16. package/chain/chain.go +0 -504
  17. package/chain/chain_test.go +0 -468
  18. package/chain/errors.go +0 -80
  19. package/chain/event.go +0 -33
  20. package/chain/iter.go +0 -64
  21. package/chainsync/chainsync.go +0 -97
  22. package/chainsync.go +0 -223
  23. package/cmd/dingo/load.go +0 -52
  24. package/cmd/dingo/main.go +0 -118
  25. package/cmd/dingo/serve.go +0 -49
  26. package/config/cardano/node.go +0 -192
  27. package/config/cardano/node_test.go +0 -85
  28. package/config/cardano/preview/README.md +0 -4
  29. package/config/cardano/preview/alonzo-genesis.json +0 -196
  30. package/config/cardano/preview/byron-genesis.json +0 -117
  31. package/config/cardano/preview/config.json +0 -114
  32. package/config/cardano/preview/conway-genesis.json +0 -297
  33. package/config/cardano/preview/shelley-genesis.json +0 -68
  34. package/config.go +0 -245
  35. package/connmanager/connection_manager.go +0 -105
  36. package/connmanager/connection_manager_test.go +0 -185
  37. package/connmanager/event.go +0 -37
  38. package/connmanager/listener.go +0 -140
  39. package/connmanager/outbound.go +0 -93
  40. package/connmanager/socket.go +0 -55
  41. package/connmanager/unix.go +0 -78
  42. package/custom-p2p-topology.json +0 -24
  43. package/custom-p2p-topology.json.backup +0 -24
  44. package/custom-p2p-topology.json.mainnet +0 -37
  45. package/database/account.go +0 -180
  46. package/database/block.go +0 -362
  47. package/database/certs.go +0 -53
  48. package/database/commit_timestamp.go +0 -77
  49. package/database/database.go +0 -118
  50. package/database/database_test.go +0 -62
  51. package/database/drep.go +0 -47
  52. package/database/epoch.go +0 -121
  53. package/database/immutable/chunk.go +0 -182
  54. package/database/immutable/immutable.go +0 -350
  55. package/database/immutable/immutable_test.go +0 -59
  56. package/database/immutable/primary.go +0 -106
  57. package/database/immutable/secondary.go +0 -103
  58. package/database/immutable/testdata/08893.chunk +0 -0
  59. package/database/immutable/testdata/08893.primary +0 -0
  60. package/database/immutable/testdata/08893.secondary +0 -0
  61. package/database/immutable/testdata/08894.chunk +0 -0
  62. package/database/immutable/testdata/08894.primary +0 -0
  63. package/database/immutable/testdata/08894.secondary +0 -0
  64. package/database/immutable/testdata/README.md +0 -4
  65. package/database/plugin/blob/badger/commit_timestamp.go +0 -50
  66. package/database/plugin/blob/badger/database.go +0 -152
  67. package/database/plugin/blob/badger/logger.go +0 -63
  68. package/database/plugin/blob/badger/metrics.go +0 -98
  69. package/database/plugin/blob/blob.go +0 -19
  70. package/database/plugin/blob/store.go +0 -40
  71. package/database/plugin/log.go +0 -27
  72. package/database/plugin/metadata/metadata.go +0 -19
  73. package/database/plugin/metadata/sqlite/account.go +0 -313
  74. package/database/plugin/metadata/sqlite/certs.go +0 -58
  75. package/database/plugin/metadata/sqlite/commit_timestamp.go +0 -68
  76. package/database/plugin/metadata/sqlite/database.go +0 -218
  77. package/database/plugin/metadata/sqlite/drep.go +0 -140
  78. package/database/plugin/metadata/sqlite/epoch.go +0 -120
  79. package/database/plugin/metadata/sqlite/models/account.go +0 -118
  80. package/database/plugin/metadata/sqlite/models/auth_committee_hot.go +0 -26
  81. package/database/plugin/metadata/sqlite/models/drep.go +0 -52
  82. package/database/plugin/metadata/sqlite/models/epoch.go +0 -31
  83. package/database/plugin/metadata/sqlite/models/models.go +0 -46
  84. package/database/plugin/metadata/sqlite/models/pool.go +0 -97
  85. package/database/plugin/metadata/sqlite/models/pparam_update.go +0 -27
  86. package/database/plugin/metadata/sqlite/models/pparams.go +0 -27
  87. package/database/plugin/metadata/sqlite/models/resign_committee_cold.go +0 -27
  88. package/database/plugin/metadata/sqlite/models/stake_vote_delegation.go +0 -27
  89. package/database/plugin/metadata/sqlite/models/tip.go +0 -26
  90. package/database/plugin/metadata/sqlite/models/update_drep.go +0 -27
  91. package/database/plugin/metadata/sqlite/models/utxo.go +0 -30
  92. package/database/plugin/metadata/sqlite/models/vote_delegation.go +0 -26
  93. package/database/plugin/metadata/sqlite/models/vote_registration_delegation.go +0 -15
  94. package/database/plugin/metadata/sqlite/pool.go +0 -240
  95. package/database/plugin/metadata/sqlite/pparams.go +0 -110
  96. package/database/plugin/metadata/sqlite/tip.go +0 -83
  97. package/database/plugin/metadata/sqlite/utxo.go +0 -292
  98. package/database/plugin/metadata/store.go +0 -198
  99. package/database/plugin/option.go +0 -190
  100. package/database/plugin/plugin.go +0 -20
  101. package/database/plugin/register.go +0 -118
  102. package/database/pparams.go +0 -145
  103. package/database/tip.go +0 -45
  104. package/database/txn.go +0 -147
  105. package/database/types/types.go +0 -74
  106. package/database/types/types_test.go +0 -83
  107. package/database/utxo.go +0 -263
  108. package/dist/artifacts.json +0 -1
  109. package/dist/checksums.txt +0 -22
  110. package/dist/config.yaml +0 -253
  111. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz +0 -0
  112. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz.sbom.json +0 -1
  113. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz +0 -0
  114. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz.sbom.json +0 -1
  115. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz +0 -0
  116. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz.sbom.json +0 -1
  117. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk +0 -0
  118. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk.sbom.json +0 -1
  119. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb +0 -0
  120. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb.sbom.json +0 -1
  121. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm +0 -0
  122. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm.sbom.json +0 -1
  123. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk +0 -0
  124. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk.sbom.json +0 -1
  125. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb +0 -0
  126. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb.sbom.json +0 -1
  127. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm +0 -0
  128. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm.sbom.json +0 -1
  129. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz +0 -0
  130. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz.sbom.json +0 -1
  131. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz +0 -0
  132. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz.sbom.json +0 -1
  133. package/dist/dingo_darwin_amd64_v1/dingo +0 -0
  134. package/dist/dingo_darwin_arm64_v8.0/dingo +0 -0
  135. package/dist/dingo_linux_amd64_v1/dingo +0 -0
  136. package/dist/dingo_linux_arm64_v8.0/dingo +0 -0
  137. package/dist/homebrew/dingo.rb +0 -51
  138. package/dist/metadata.json +0 -1
  139. package/event/event.go +0 -141
  140. package/event/event_test.go +0 -115
  141. package/event/metrics.go +0 -44
  142. package/go.mod +0 -98
  143. package/go.sum +0 -358
  144. package/internal/config/config.go +0 -145
  145. package/internal/config/config_test.go +0 -118
  146. package/internal/node/load.go +0 -149
  147. package/internal/node/node.go +0 -176
  148. package/internal/version/version.go +0 -33
  149. package/ledger/certs.go +0 -164
  150. package/ledger/chainsync.go +0 -504
  151. package/ledger/delta.go +0 -99
  152. package/ledger/eras/allegra.go +0 -154
  153. package/ledger/eras/alonzo.go +0 -156
  154. package/ledger/eras/babbage.go +0 -154
  155. package/ledger/eras/byron.go +0 -42
  156. package/ledger/eras/conway.go +0 -166
  157. package/ledger/eras/eras.go +0 -44
  158. package/ledger/eras/mary.go +0 -154
  159. package/ledger/eras/shelley.go +0 -164
  160. package/ledger/error.go +0 -19
  161. package/ledger/event.go +0 -50
  162. package/ledger/metrics.go +0 -53
  163. package/ledger/queries.go +0 -258
  164. package/ledger/slot.go +0 -127
  165. package/ledger/slot_test.go +0 -147
  166. package/ledger/state.go +0 -821
  167. package/ledger/view.go +0 -73
  168. package/localstatequery.go +0 -50
  169. package/localtxmonitor.go +0 -44
  170. package/localtxsubmission.go +0 -52
  171. package/mempool/consumer.go +0 -98
  172. package/mempool/mempool.go +0 -322
  173. package/node.go +0 -320
  174. package/peergov/event.go +0 -27
  175. package/peergov/peer.go +0 -67
  176. package/peergov/peergov.go +0 -290
  177. package/peersharing.go +0 -70
  178. package/preview-local-topology.json +0 -23
  179. package/topology/topology.go +0 -69
  180. package/topology/topology_test.go +0 -179
  181. package/tracing.go +0 -65
  182. package/txsubmission.go +0 -233
  183. package/utxorpc/query.go +0 -311
  184. package/utxorpc/submit.go +0 -395
  185. package/utxorpc/sync.go +0 -276
  186. package/utxorpc/utxorpc.go +0 -166
  187. package/utxorpc/watch.go +0 -310
package/node.go DELETED
@@ -1,320 +0,0 @@
1
- // Copyright 2024 Blink Labs Software
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- package dingo
16
-
17
- import (
18
- "context"
19
- "errors"
20
- "fmt"
21
- "slices"
22
-
23
- "github.com/blinklabs-io/dingo/chainsync"
24
- "github.com/blinklabs-io/dingo/connmanager"
25
- "github.com/blinklabs-io/dingo/event"
26
- "github.com/blinklabs-io/dingo/ledger"
27
- "github.com/blinklabs-io/dingo/mempool"
28
- "github.com/blinklabs-io/dingo/peergov"
29
- "github.com/blinklabs-io/dingo/utxorpc"
30
- ouroboros "github.com/blinklabs-io/gouroboros"
31
- oblockfetch "github.com/blinklabs-io/gouroboros/protocol/blockfetch"
32
- ochainsync "github.com/blinklabs-io/gouroboros/protocol/chainsync"
33
- olocalstatequery "github.com/blinklabs-io/gouroboros/protocol/localstatequery"
34
- olocaltxmonitor "github.com/blinklabs-io/gouroboros/protocol/localtxmonitor"
35
- olocaltxsubmission "github.com/blinklabs-io/gouroboros/protocol/localtxsubmission"
36
- opeersharing "github.com/blinklabs-io/gouroboros/protocol/peersharing"
37
- otxsubmission "github.com/blinklabs-io/gouroboros/protocol/txsubmission"
38
- )
39
-
40
- type Node struct {
41
- config Config
42
- connManager *connmanager.ConnectionManager
43
- peerGov *peergov.PeerGovernor
44
- chainsyncState *chainsync.State
45
- eventBus *event.EventBus
46
- mempool *mempool.Mempool
47
- ledgerState *ledger.LedgerState
48
- utxorpc *utxorpc.Utxorpc
49
- shutdownFuncs []func(context.Context) error
50
- }
51
-
52
- func New(cfg Config) (*Node, error) {
53
- eventBus := event.NewEventBus(cfg.promRegistry)
54
- n := &Node{
55
- config: cfg,
56
- eventBus: eventBus,
57
- }
58
- if err := n.configPopulateNetworkMagic(); err != nil {
59
- return nil, fmt.Errorf("invalid configuration: %w", err)
60
- }
61
- if err := n.configValidate(); err != nil {
62
- return nil, fmt.Errorf("invalid configuration: %w", err)
63
- }
64
- return n, nil
65
- }
66
-
67
- func (n *Node) Run() error {
68
- // Configure tracing
69
- if n.config.tracing {
70
- if err := n.setupTracing(); err != nil {
71
- return err
72
- }
73
- }
74
- // Load state
75
- state, err := ledger.NewLedgerState(
76
- ledger.LedgerStateConfig{
77
- DataDir: n.config.dataDir,
78
- EventBus: n.eventBus,
79
- Logger: n.config.logger,
80
- CardanoNodeConfig: n.config.cardanoNodeConfig,
81
- PromRegistry: n.config.promRegistry,
82
- BlockfetchRequestRangeFunc: n.blockfetchClientRequestRange,
83
- },
84
- )
85
- if err != nil {
86
- return fmt.Errorf("failed to load state database: %w", err)
87
- }
88
- // Add shutdown cleanup for ledger/database
89
- n.shutdownFuncs = append(
90
- n.shutdownFuncs,
91
- func(_ context.Context) error {
92
- return state.Close()
93
- },
94
- )
95
- n.ledgerState = state
96
- // Initialize mempool
97
- n.mempool = mempool.NewMempool(
98
- n.config.logger,
99
- n.eventBus,
100
- n.config.promRegistry,
101
- n.ledgerState,
102
- )
103
- // Initialize chainsync state
104
- n.chainsyncState = chainsync.NewState(
105
- n.eventBus,
106
- n.ledgerState,
107
- )
108
- // Configure connection manager
109
- if err := n.configureConnManager(); err != nil {
110
- return err
111
- }
112
- // Configure peer governor
113
- n.peerGov = peergov.NewPeerGovernor(
114
- peergov.PeerGovernorConfig{
115
- Logger: n.config.logger,
116
- EventBus: n.eventBus,
117
- ConnManager: n.connManager,
118
- },
119
- )
120
- n.eventBus.SubscribeFunc(
121
- peergov.OutboundConnectionEventType,
122
- n.handleOutboundConnEvent,
123
- )
124
- if n.config.topologyConfig != nil {
125
- n.peerGov.LoadTopologyConfig(n.config.topologyConfig)
126
- }
127
- if err := n.peerGov.Start(); err != nil {
128
- return err
129
- }
130
- // Configure UTxO RPC
131
- n.utxorpc = utxorpc.NewUtxorpc(
132
- utxorpc.UtxorpcConfig{
133
- Logger: n.config.logger,
134
- EventBus: n.eventBus,
135
- LedgerState: n.ledgerState,
136
- Mempool: n.mempool,
137
- Port: n.config.utxorpcPort,
138
- },
139
- )
140
- if err := n.utxorpc.Start(); err != nil {
141
- return err
142
- }
143
-
144
- // Wait forever
145
- select {}
146
- }
147
-
148
- func (n *Node) Stop() error {
149
- return n.shutdown()
150
- }
151
-
152
- func (n *Node) shutdown() error {
153
- ctx := context.TODO()
154
- var err error
155
- // Shutdown ledger
156
- err = errors.Join(err, n.ledgerState.Close())
157
- // Call shutdown functions
158
- for _, fn := range n.shutdownFuncs {
159
- err = errors.Join(err, fn(ctx))
160
- }
161
- n.shutdownFuncs = nil
162
- return err
163
- }
164
-
165
- func (n *Node) configureConnManager() error {
166
- // Configure listeners
167
- tmpListeners := make([]ListenerConfig, len(n.config.listeners))
168
- for idx, l := range n.config.listeners {
169
- if l.UseNtC {
170
- // Node-to-client
171
- l.ConnectionOpts = append(
172
- l.ConnectionOpts,
173
- ouroboros.WithNetworkMagic(n.config.networkMagic),
174
- ouroboros.WithChainSyncConfig(
175
- ochainsync.NewConfig(
176
- n.chainsyncServerConnOpts()...,
177
- ),
178
- ),
179
- ouroboros.WithLocalStateQueryConfig(
180
- olocalstatequery.NewConfig(
181
- n.localstatequeryServerConnOpts()...,
182
- ),
183
- ),
184
- ouroboros.WithLocalTxMonitorConfig(
185
- olocaltxmonitor.NewConfig(
186
- n.localtxmonitorServerConnOpts()...,
187
- ),
188
- ),
189
- ouroboros.WithLocalTxSubmissionConfig(
190
- olocaltxsubmission.NewConfig(
191
- n.localtxsubmissionServerConnOpts()...,
192
- ),
193
- ),
194
- )
195
- } else {
196
- // Node-to-node config
197
- l.ConnectionOpts = append(
198
- l.ConnectionOpts,
199
- ouroboros.WithPeerSharing(n.config.peerSharing),
200
- ouroboros.WithNetworkMagic(n.config.networkMagic),
201
- ouroboros.WithPeerSharingConfig(
202
- opeersharing.NewConfig(
203
- n.peersharingServerConnOpts()...,
204
- ),
205
- ),
206
- ouroboros.WithTxSubmissionConfig(
207
- otxsubmission.NewConfig(
208
- n.txsubmissionServerConnOpts()...,
209
- ),
210
- ),
211
- ouroboros.WithChainSyncConfig(
212
- ochainsync.NewConfig(
213
- n.chainsyncServerConnOpts()...,
214
- ),
215
- ),
216
- ouroboros.WithBlockFetchConfig(
217
- oblockfetch.NewConfig(
218
- n.blockfetchServerConnOpts()...,
219
- ),
220
- ),
221
- )
222
- }
223
- tmpListeners[idx] = l
224
- }
225
- // Create connection manager
226
- n.connManager = connmanager.NewConnectionManager(
227
- connmanager.ConnectionManagerConfig{
228
- Logger: n.config.logger,
229
- EventBus: n.eventBus,
230
- Listeners: tmpListeners,
231
- OutboundSourcePort: n.config.outboundSourcePort,
232
- OutboundConnOpts: []ouroboros.ConnectionOptionFunc{
233
- ouroboros.WithNetworkMagic(n.config.networkMagic),
234
- ouroboros.WithNodeToNode(true),
235
- ouroboros.WithKeepAlive(true),
236
- ouroboros.WithFullDuplex(true),
237
- ouroboros.WithPeerSharing(n.config.peerSharing),
238
- ouroboros.WithPeerSharingConfig(
239
- opeersharing.NewConfig(
240
- slices.Concat(
241
- n.peersharingClientConnOpts(),
242
- n.peersharingServerConnOpts(),
243
- )...,
244
- ),
245
- ),
246
- ouroboros.WithTxSubmissionConfig(
247
- otxsubmission.NewConfig(
248
- slices.Concat(
249
- n.txsubmissionClientConnOpts(),
250
- n.txsubmissionServerConnOpts(),
251
- )...,
252
- ),
253
- ),
254
- ouroboros.WithChainSyncConfig(
255
- ochainsync.NewConfig(
256
- slices.Concat(
257
- n.chainsyncClientConnOpts(),
258
- n.chainsyncServerConnOpts(),
259
- )...,
260
- ),
261
- ),
262
- ouroboros.WithBlockFetchConfig(
263
- oblockfetch.NewConfig(
264
- slices.Concat(
265
- n.blockfetchClientConnOpts(),
266
- n.blockfetchServerConnOpts(),
267
- )...,
268
- ),
269
- ),
270
- },
271
- },
272
- )
273
- // Subscribe to connection closed events
274
- n.eventBus.SubscribeFunc(
275
- connmanager.ConnectionClosedEventType,
276
- n.handleConnClosedEvent,
277
- )
278
- // Start listeners
279
- if err := n.connManager.Start(); err != nil {
280
- return err
281
- }
282
- return nil
283
- }
284
-
285
- func (n *Node) handleConnClosedEvent(evt event.Event) {
286
- e := evt.Data.(connmanager.ConnectionClosedEvent)
287
- connId := e.ConnectionId
288
- // Remove any chainsync client state
289
- n.chainsyncState.RemoveClient(connId)
290
- // Remove mempool consumer
291
- n.mempool.RemoveConsumer(connId)
292
- // Release chainsync client
293
- n.chainsyncState.RemoveClientConnId(connId)
294
- }
295
-
296
- func (n *Node) handleOutboundConnEvent(evt event.Event) {
297
- e := evt.Data.(peergov.OutboundConnectionEvent)
298
- connId := e.ConnectionId
299
- // TODO: replace this with handling for multiple chainsync clients (#385)
300
- // Start chainsync client if we don't have another
301
- n.chainsyncState.Lock()
302
- defer n.chainsyncState.Unlock()
303
- chainsyncClientConnId := n.chainsyncState.GetClientConnId()
304
- if chainsyncClientConnId == nil {
305
- if err := n.chainsyncClientStart(connId); err != nil {
306
- n.config.logger.Error(
307
- "failed to start chainsync client",
308
- "error",
309
- err,
310
- )
311
- return
312
- }
313
- n.chainsyncState.SetClientConnId(connId)
314
- }
315
- // Start txsubmission client
316
- if err := n.txsubmissionClientStart(connId); err != nil {
317
- n.config.logger.Error("failed to start chainsync client", "error", err)
318
- return
319
- }
320
- }
package/peergov/event.go DELETED
@@ -1,27 +0,0 @@
1
- // Copyright 2024 Blink Labs Software
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- package peergov
16
-
17
- import (
18
- ouroboros "github.com/blinklabs-io/gouroboros"
19
- )
20
-
21
- const (
22
- OutboundConnectionEventType = "peergov.outbound-conn"
23
- )
24
-
25
- type OutboundConnectionEvent struct {
26
- ConnectionId ouroboros.ConnectionId
27
- }
package/peergov/peer.go DELETED
@@ -1,67 +0,0 @@
1
- // Copyright 2024 Blink Labs Software
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- package peergov
16
-
17
- import (
18
- "time"
19
-
20
- ouroboros "github.com/blinklabs-io/gouroboros"
21
- oprotocol "github.com/blinklabs-io/gouroboros/protocol"
22
- )
23
-
24
- type PeerSource uint16
25
-
26
- const (
27
- PeerSourceUnknown = 0
28
- PeerSourceTopologyLocalRoot = 1
29
- PeerSourceTopologyPublicRoot = 2
30
- PeerSourceTopologyBootstrapPeer = 3
31
- PeerSourceP2PLedger = 4
32
- PeerSourceP2PGossip = 5
33
- PeerSourceInboundConn = 6
34
- )
35
-
36
- type Peer struct {
37
- Address string
38
- Source PeerSource
39
- Connection *PeerConnection
40
- Sharable bool
41
- ReconnectCount int
42
- ReconnectDelay time.Duration
43
- }
44
-
45
- func (p *Peer) setConnection(conn *ouroboros.Connection, outbound bool) {
46
- connId := conn.Id()
47
- protoVersion, versionData := conn.ProtocolVersion()
48
- p.Connection = &PeerConnection{
49
- Id: connId,
50
- ProtocolVersion: uint(protoVersion),
51
- VersionData: versionData,
52
- }
53
- // Determine whether connection can be used as a client
54
- // This should be true for any outbound connections and any inbound
55
- // connections in full-duplex mode
56
- if outbound ||
57
- versionData.DiffusionMode() == oprotocol.DiffusionModeInitiatorAndResponder {
58
- p.Connection.IsClient = true
59
- }
60
- }
61
-
62
- type PeerConnection struct {
63
- Id ouroboros.ConnectionId
64
- ProtocolVersion uint
65
- VersionData oprotocol.VersionData
66
- IsClient bool
67
- }
@@ -1,290 +0,0 @@
1
- // Copyright 2025 Blink Labs Software
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- package peergov
16
-
17
- import (
18
- "fmt"
19
- "io"
20
- "log/slog"
21
- "net"
22
- "strconv"
23
- "sync"
24
- "time"
25
-
26
- "github.com/blinklabs-io/dingo/connmanager"
27
- "github.com/blinklabs-io/dingo/event"
28
- "github.com/blinklabs-io/dingo/topology"
29
- ouroboros "github.com/blinklabs-io/gouroboros"
30
- )
31
-
32
- const (
33
- initialReconnectDelay = 1 * time.Second
34
- maxReconnectDelay = 128 * time.Second
35
- reconnectBackoffFactor = 2
36
- )
37
-
38
- type PeerGovernor struct {
39
- mu sync.Mutex
40
- config PeerGovernorConfig
41
- peers []*Peer
42
- }
43
-
44
- type PeerGovernorConfig struct {
45
- Logger *slog.Logger
46
- EventBus *event.EventBus
47
- ConnManager *connmanager.ConnectionManager
48
- }
49
-
50
- func NewPeerGovernor(cfg PeerGovernorConfig) *PeerGovernor {
51
- if cfg.Logger == nil {
52
- cfg.Logger = slog.New(slog.NewJSONHandler(io.Discard, nil))
53
- }
54
- cfg.Logger = cfg.Logger.With("component", "peergov")
55
- return &PeerGovernor{
56
- config: cfg,
57
- }
58
- }
59
-
60
- func (p *PeerGovernor) Start() error {
61
- // Setup connmanager event listeners
62
- p.config.EventBus.SubscribeFunc(
63
- connmanager.InboundConnectionEventType,
64
- p.handleInboundConnectionEvent,
65
- )
66
- p.config.EventBus.SubscribeFunc(
67
- connmanager.ConnectionClosedEventType,
68
- p.handleConnectionClosedEvent,
69
- )
70
- // Start outbound connections
71
- p.startOutboundConnections()
72
- return nil
73
- }
74
-
75
- func (p *PeerGovernor) LoadTopologyConfig(
76
- topologyConfig *topology.TopologyConfig,
77
- ) {
78
- p.mu.Lock()
79
- defer p.mu.Unlock()
80
- // Remove peers originally sourced from the topology
81
- tmpPeers := []*Peer{}
82
- for _, tmpPeer := range p.peers {
83
- if tmpPeer.Source == PeerSourceTopologyBootstrapPeer ||
84
- tmpPeer.Source == PeerSourceTopologyLocalRoot ||
85
- tmpPeer.Source == PeerSourceTopologyPublicRoot {
86
- continue
87
- }
88
- tmpPeers = append(tmpPeers, tmpPeer)
89
- }
90
- p.peers = tmpPeers
91
- // Add topology bootstrap peers
92
- for _, bootstrapPeer := range topologyConfig.BootstrapPeers {
93
- tmpAddress := net.JoinHostPort(
94
- bootstrapPeer.Address,
95
- strconv.FormatUint(uint64(bootstrapPeer.Port), 10),
96
- )
97
- p.peers = append(
98
- p.peers,
99
- &Peer{
100
- Address: tmpAddress,
101
- Source: PeerSourceTopologyBootstrapPeer,
102
- },
103
- )
104
- }
105
- // Add topology local roots
106
- for _, localRoot := range topologyConfig.LocalRoots {
107
- for _, ap := range localRoot.AccessPoints {
108
- tmpAddress := net.JoinHostPort(
109
- ap.Address,
110
- strconv.FormatUint(uint64(ap.Port), 10),
111
- )
112
- tmpPeer := &Peer{
113
- Address: tmpAddress,
114
- Source: PeerSourceTopologyLocalRoot,
115
- Sharable: localRoot.Advertise,
116
- }
117
- for i, peer := range p.peers {
118
- // This peer already appears, remove it
119
- if peer.Address == tmpAddress {
120
- copy(p.peers[i:], p.peers[i+1:]) // shift left
121
- p.peers[len(p.peers)-1] = nil // clear last
122
- p.peers = p.peers[:len(p.peers)-1] // truncate
123
- }
124
- }
125
- p.peers = append(p.peers, tmpPeer)
126
- }
127
- }
128
- // Add topology public roots
129
- for _, publicRoot := range topologyConfig.PublicRoots {
130
- for _, ap := range publicRoot.AccessPoints {
131
- tmpAddress := net.JoinHostPort(
132
- ap.Address,
133
- strconv.FormatUint(uint64(ap.Port), 10),
134
- )
135
- tmpPeer := &Peer{
136
- Address: tmpAddress,
137
- Source: PeerSourceTopologyPublicRoot,
138
- Sharable: publicRoot.Advertise,
139
- }
140
- for i, peer := range p.peers {
141
- // This peer already appears, remove it
142
- if peer.Address == tmpAddress {
143
- copy(p.peers[i:], p.peers[i+1:]) // shift left
144
- p.peers[len(p.peers)-1] = nil // clear last
145
- p.peers = p.peers[:len(p.peers)-1] // truncate
146
- }
147
- }
148
- p.peers = append(p.peers, tmpPeer)
149
- }
150
- }
151
- }
152
-
153
- func (p *PeerGovernor) GetPeers() []Peer {
154
- p.mu.Lock()
155
- defer p.mu.Unlock()
156
- ret := make([]Peer, len(p.peers))
157
- for idx, peer := range p.peers {
158
- ret[idx] = *peer
159
- }
160
- return ret
161
- }
162
-
163
- func (p *PeerGovernor) peerIndexByAddress(address string) int {
164
- for idx, tmpPeer := range p.peers {
165
- if tmpPeer.Address == address {
166
- return idx
167
- }
168
- }
169
- return -1
170
- }
171
-
172
- func (p *PeerGovernor) peerIndexByConnId(connId ouroboros.ConnectionId) int {
173
- for idx, tmpPeer := range p.peers {
174
- if tmpPeer.Connection == nil {
175
- continue
176
- }
177
- if tmpPeer.Connection.Id == connId {
178
- return idx
179
- }
180
- }
181
- return -1
182
- }
183
-
184
- func (p *PeerGovernor) startOutboundConnections() {
185
- p.config.Logger.Debug(
186
- "starting connections",
187
- "role", "client",
188
- )
189
- for _, tmpPeer := range p.peers {
190
- go p.createOutboundConnection(tmpPeer)
191
- }
192
- }
193
-
194
- func (p *PeerGovernor) createOutboundConnection(peer *Peer) {
195
- for {
196
- conn, err := p.config.ConnManager.CreateOutboundConn(peer.Address)
197
- if err == nil {
198
- connId := conn.Id()
199
- peer.ReconnectCount = 0
200
- peer.setConnection(conn, true)
201
- // Generate event
202
- if p.config.EventBus != nil {
203
- p.config.EventBus.Publish(
204
- OutboundConnectionEventType,
205
- event.NewEvent(
206
- OutboundConnectionEventType,
207
- OutboundConnectionEvent{
208
- ConnectionId: connId,
209
- },
210
- ),
211
- )
212
- }
213
- return
214
- }
215
- p.config.Logger.Error(
216
- fmt.Sprintf(
217
- "outbound: failed to establish connection to %s: %s",
218
- peer.Address,
219
- err,
220
- ),
221
- )
222
- if peer.ReconnectDelay == 0 {
223
- peer.ReconnectDelay = initialReconnectDelay
224
- } else if peer.ReconnectDelay < maxReconnectDelay {
225
- peer.ReconnectDelay = peer.ReconnectDelay * reconnectBackoffFactor
226
- }
227
- peer.ReconnectCount += 1
228
- p.config.Logger.Info(
229
- fmt.Sprintf(
230
- "outbound: delaying %s (retry %d) before reconnecting to %s",
231
- peer.ReconnectDelay,
232
- peer.ReconnectCount,
233
- peer.Address,
234
- ),
235
- )
236
- time.Sleep(peer.ReconnectDelay)
237
- }
238
- }
239
-
240
- func (p *PeerGovernor) handleInboundConnectionEvent(evt event.Event) {
241
- p.mu.Lock()
242
- defer p.mu.Unlock()
243
- e := evt.Data.(connmanager.InboundConnectionEvent)
244
- var tmpPeer *Peer
245
- peerIdx := p.peerIndexByAddress(e.RemoteAddr.String())
246
- if peerIdx == -1 {
247
- tmpPeer = &Peer{
248
- Address: e.RemoteAddr.String(),
249
- Source: PeerSourceInboundConn,
250
- }
251
- // Add inbound peer
252
- p.peers = append(
253
- p.peers,
254
- tmpPeer,
255
- )
256
- } else {
257
- tmpPeer = p.peers[peerIdx]
258
- }
259
- conn := p.config.ConnManager.GetConnectionById(e.ConnectionId)
260
- tmpPeer.setConnection(conn, false)
261
- if tmpPeer.Connection != nil {
262
- tmpPeer.Sharable = tmpPeer.Connection.VersionData.PeerSharing()
263
- }
264
- }
265
-
266
- func (p *PeerGovernor) handleConnectionClosedEvent(evt event.Event) {
267
- p.mu.Lock()
268
- defer p.mu.Unlock()
269
- e := evt.Data.(connmanager.ConnectionClosedEvent)
270
- if e.Error != nil {
271
- p.config.Logger.Error(
272
- fmt.Sprintf(
273
- "unexpected connection failure: %s",
274
- e.Error,
275
- ),
276
- "connection_id", e.ConnectionId.String(),
277
- )
278
- } else {
279
- p.config.Logger.Info("connection closed",
280
- "connection_id", e.ConnectionId.String(),
281
- )
282
- }
283
- peerIdx := p.peerIndexByConnId(e.ConnectionId)
284
- if peerIdx != -1 {
285
- p.peers[peerIdx].Connection = nil
286
- if p.peers[peerIdx].Source != PeerSourceInboundConn {
287
- go p.createOutboundConnection(p.peers[peerIdx])
288
- }
289
- }
290
- }