@blinklabs/dingo 0.6.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 (195) hide show
  1. package/.dockerignore +5 -0
  2. package/.github/CODEOWNERS +5 -0
  3. package/.github/assets/dingo-ate-my-blockchain.png +0 -0
  4. package/.github/assets/dingo-illustration.png +0 -0
  5. package/.github/assets/dingo-logo-with-text-horizontal.png +0 -0
  6. package/.github/assets/dingo-logo-with-text.png +0 -0
  7. package/.github/dependabot.yml +19 -0
  8. package/.github/dingo-20241210.png +0 -0
  9. package/.github/dingo.md +56 -0
  10. package/.github/workflows/ci-docker.yml +36 -0
  11. package/.github/workflows/conventional-commits.yml +17 -0
  12. package/.github/workflows/go-test.yml +29 -0
  13. package/.github/workflows/golangci-lint.yml +23 -0
  14. package/.github/workflows/publish.yml +207 -0
  15. package/.golangci.yml +71 -0
  16. package/Dockerfile +25 -0
  17. package/LICENSE +201 -0
  18. package/Makefile +53 -0
  19. package/README.md +150 -0
  20. package/blockfetch.go +144 -0
  21. package/chain/chain.go +504 -0
  22. package/chain/chain_test.go +468 -0
  23. package/chain/errors.go +80 -0
  24. package/chain/event.go +33 -0
  25. package/chain/iter.go +64 -0
  26. package/chainsync/chainsync.go +97 -0
  27. package/chainsync.go +223 -0
  28. package/cmd/dingo/load.go +52 -0
  29. package/cmd/dingo/main.go +118 -0
  30. package/cmd/dingo/serve.go +49 -0
  31. package/config/cardano/node.go +192 -0
  32. package/config/cardano/node_test.go +85 -0
  33. package/config/cardano/preview/README.md +4 -0
  34. package/config/cardano/preview/alonzo-genesis.json +196 -0
  35. package/config/cardano/preview/byron-genesis.json +117 -0
  36. package/config/cardano/preview/config.json +114 -0
  37. package/config/cardano/preview/conway-genesis.json +297 -0
  38. package/config/cardano/preview/shelley-genesis.json +68 -0
  39. package/config.go +245 -0
  40. package/connmanager/connection_manager.go +105 -0
  41. package/connmanager/connection_manager_test.go +185 -0
  42. package/connmanager/event.go +37 -0
  43. package/connmanager/listener.go +140 -0
  44. package/connmanager/outbound.go +93 -0
  45. package/connmanager/socket.go +55 -0
  46. package/connmanager/unix.go +78 -0
  47. package/custom-p2p-topology.json +24 -0
  48. package/custom-p2p-topology.json.backup +24 -0
  49. package/custom-p2p-topology.json.mainnet +37 -0
  50. package/database/account.go +138 -0
  51. package/database/block.go +362 -0
  52. package/database/certs.go +53 -0
  53. package/database/commit_timestamp.go +77 -0
  54. package/database/database.go +118 -0
  55. package/database/database_test.go +62 -0
  56. package/database/drep.go +27 -0
  57. package/database/epoch.go +121 -0
  58. package/database/immutable/chunk.go +182 -0
  59. package/database/immutable/immutable.go +350 -0
  60. package/database/immutable/immutable_test.go +59 -0
  61. package/database/immutable/primary.go +106 -0
  62. package/database/immutable/secondary.go +103 -0
  63. package/database/immutable/testdata/08893.chunk +0 -0
  64. package/database/immutable/testdata/08893.primary +0 -0
  65. package/database/immutable/testdata/08893.secondary +0 -0
  66. package/database/immutable/testdata/08894.chunk +0 -0
  67. package/database/immutable/testdata/08894.primary +0 -0
  68. package/database/immutable/testdata/08894.secondary +0 -0
  69. package/database/immutable/testdata/README.md +4 -0
  70. package/database/plugin/blob/badger/commit_timestamp.go +50 -0
  71. package/database/plugin/blob/badger/database.go +152 -0
  72. package/database/plugin/blob/badger/logger.go +63 -0
  73. package/database/plugin/blob/badger/metrics.go +98 -0
  74. package/database/plugin/blob/blob.go +19 -0
  75. package/database/plugin/blob/store.go +40 -0
  76. package/database/plugin/log.go +27 -0
  77. package/database/plugin/metadata/metadata.go +19 -0
  78. package/database/plugin/metadata/sqlite/account.go +224 -0
  79. package/database/plugin/metadata/sqlite/certs.go +58 -0
  80. package/database/plugin/metadata/sqlite/commit_timestamp.go +68 -0
  81. package/database/plugin/metadata/sqlite/database.go +218 -0
  82. package/database/plugin/metadata/sqlite/epoch.go +120 -0
  83. package/database/plugin/metadata/sqlite/models/account.go +81 -0
  84. package/database/plugin/metadata/sqlite/models/auth_committee_hot.go +26 -0
  85. package/database/plugin/metadata/sqlite/models/deregistration_drep.go +26 -0
  86. package/database/plugin/metadata/sqlite/models/drep.go +27 -0
  87. package/database/plugin/metadata/sqlite/models/epoch.go +31 -0
  88. package/database/plugin/metadata/sqlite/models/models.go +45 -0
  89. package/database/plugin/metadata/sqlite/models/pool.go +97 -0
  90. package/database/plugin/metadata/sqlite/models/pparam_update.go +27 -0
  91. package/database/plugin/metadata/sqlite/models/pparams.go +27 -0
  92. package/database/plugin/metadata/sqlite/models/registration_drep.go +28 -0
  93. package/database/plugin/metadata/sqlite/models/resign_committee_cold.go +27 -0
  94. package/database/plugin/metadata/sqlite/models/stake_registration_delegation.go +27 -0
  95. package/database/plugin/metadata/sqlite/models/stake_vote_delegation.go +27 -0
  96. package/database/plugin/metadata/sqlite/models/stake_vote_registration_delegation.go +27 -0
  97. package/database/plugin/metadata/sqlite/models/tip.go +26 -0
  98. package/database/plugin/metadata/sqlite/models/update_drep.go +27 -0
  99. package/database/plugin/metadata/sqlite/models/utxo.go +30 -0
  100. package/database/plugin/metadata/sqlite/models/vote_delegation.go +26 -0
  101. package/database/plugin/metadata/sqlite/models/vote_registration_delegation.go +26 -0
  102. package/database/plugin/metadata/sqlite/pool.go +240 -0
  103. package/database/plugin/metadata/sqlite/pparams.go +110 -0
  104. package/database/plugin/metadata/sqlite/tip.go +83 -0
  105. package/database/plugin/metadata/sqlite/utxo.go +292 -0
  106. package/database/plugin/metadata/store.go +168 -0
  107. package/database/plugin/option.go +190 -0
  108. package/database/plugin/plugin.go +20 -0
  109. package/database/plugin/register.go +118 -0
  110. package/database/pparams.go +145 -0
  111. package/database/tip.go +45 -0
  112. package/database/txn.go +147 -0
  113. package/database/types/types.go +74 -0
  114. package/database/types/types_test.go +83 -0
  115. package/database/utxo.go +263 -0
  116. package/dist/artifacts.json +1 -0
  117. package/dist/checksums.txt +22 -0
  118. package/dist/config.yaml +253 -0
  119. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz +0 -0
  120. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz.sbom.json +1 -0
  121. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz +0 -0
  122. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz.sbom.json +1 -0
  123. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz +0 -0
  124. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz.sbom.json +1 -0
  125. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk +0 -0
  126. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk.sbom.json +1 -0
  127. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb +0 -0
  128. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb.sbom.json +1 -0
  129. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm +0 -0
  130. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm.sbom.json +1 -0
  131. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk +0 -0
  132. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk.sbom.json +1 -0
  133. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb +0 -0
  134. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb.sbom.json +1 -0
  135. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm +0 -0
  136. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm.sbom.json +1 -0
  137. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz +0 -0
  138. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz.sbom.json +1 -0
  139. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz +0 -0
  140. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz.sbom.json +1 -0
  141. package/dist/dingo_darwin_amd64_v1/dingo +0 -0
  142. package/dist/dingo_darwin_arm64_v8.0/dingo +0 -0
  143. package/dist/dingo_linux_amd64_v1/dingo +0 -0
  144. package/dist/dingo_linux_arm64_v8.0/dingo +0 -0
  145. package/dist/homebrew/dingo.rb +51 -0
  146. package/dist/metadata.json +1 -0
  147. package/event/event.go +141 -0
  148. package/event/event_test.go +115 -0
  149. package/event/metrics.go +44 -0
  150. package/go.mod +98 -0
  151. package/go.sum +358 -0
  152. package/internal/config/config.go +145 -0
  153. package/internal/config/config_test.go +118 -0
  154. package/internal/node/load.go +149 -0
  155. package/internal/node/node.go +176 -0
  156. package/internal/version/version.go +33 -0
  157. package/ledger/certs.go +113 -0
  158. package/ledger/chainsync.go +578 -0
  159. package/ledger/eras/allegra.go +154 -0
  160. package/ledger/eras/alonzo.go +156 -0
  161. package/ledger/eras/babbage.go +154 -0
  162. package/ledger/eras/byron.go +42 -0
  163. package/ledger/eras/conway.go +158 -0
  164. package/ledger/eras/eras.go +44 -0
  165. package/ledger/eras/mary.go +154 -0
  166. package/ledger/eras/shelley.go +164 -0
  167. package/ledger/error.go +19 -0
  168. package/ledger/event.go +50 -0
  169. package/ledger/metrics.go +53 -0
  170. package/ledger/queries.go +260 -0
  171. package/ledger/slot.go +127 -0
  172. package/ledger/slot_test.go +147 -0
  173. package/ledger/state.go +726 -0
  174. package/ledger/view.go +73 -0
  175. package/localstatequery.go +50 -0
  176. package/localtxmonitor.go +44 -0
  177. package/localtxsubmission.go +52 -0
  178. package/mempool/consumer.go +98 -0
  179. package/mempool/mempool.go +322 -0
  180. package/node.go +320 -0
  181. package/package.json +33 -0
  182. package/peergov/event.go +27 -0
  183. package/peergov/peer.go +67 -0
  184. package/peergov/peergov.go +290 -0
  185. package/peersharing.go +70 -0
  186. package/preview-local-topology.json +23 -0
  187. package/topology/topology.go +69 -0
  188. package/topology/topology_test.go +179 -0
  189. package/tracing.go +65 -0
  190. package/txsubmission.go +233 -0
  191. package/utxorpc/query.go +311 -0
  192. package/utxorpc/submit.go +395 -0
  193. package/utxorpc/sync.go +276 -0
  194. package/utxorpc/utxorpc.go +166 -0
  195. package/utxorpc/watch.go +310 -0
@@ -0,0 +1,395 @@
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 utxorpc
16
+
17
+ import (
18
+ "bytes"
19
+ "context"
20
+ "encoding/hex"
21
+ "errors"
22
+ "fmt"
23
+
24
+ "connectrpc.com/connect"
25
+ "github.com/blinklabs-io/dingo/event"
26
+ "github.com/blinklabs-io/dingo/ledger"
27
+ gledger "github.com/blinklabs-io/gouroboros/ledger"
28
+ lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
29
+ cardano "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
30
+ submit "github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit"
31
+ "github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit/submitconnect"
32
+ )
33
+
34
+ // submitServiceServer implements the SubmitService API
35
+ type submitServiceServer struct {
36
+ submitconnect.UnimplementedSubmitServiceHandler
37
+ utxorpc *Utxorpc
38
+ }
39
+
40
+ // SubmitTx
41
+ func (s *submitServiceServer) SubmitTx(
42
+ ctx context.Context,
43
+ req *connect.Request[submit.SubmitTxRequest],
44
+ ) (*connect.Response[submit.SubmitTxResponse], error) {
45
+ txRawList := req.Msg.GetTx() // []*AnyChainTx
46
+
47
+ s.utxorpc.config.Logger.Info(
48
+ fmt.Sprintf(
49
+ "Got a SubmitTx request with %d transactions",
50
+ len(txRawList),
51
+ ),
52
+ )
53
+ resp := &submit.SubmitTxResponse{}
54
+
55
+ // Loop through the transactions and add each to the mempool
56
+ errorList := make([]error, len(txRawList))
57
+ hasError := false
58
+ for i, txi := range txRawList {
59
+ txRawBytes := txi.GetRaw() // raw bytes
60
+ txHash := lcommon.Blake2b256Hash(txRawBytes)
61
+ txType, err := gledger.DetermineTransactionType(txRawBytes)
62
+ placeholderRef := []byte{}
63
+ if err != nil {
64
+ resp.Ref = append(resp.Ref, placeholderRef)
65
+ errorList[i] = err
66
+ s.utxorpc.config.Logger.Error(
67
+ fmt.Sprintf(
68
+ "failed decoding tx %d: %v",
69
+ i,
70
+ err,
71
+ ),
72
+ )
73
+ hasError = true
74
+ continue
75
+ }
76
+ // Add transaction to mempool
77
+ err = s.utxorpc.config.Mempool.AddTransaction(txType, txRawBytes)
78
+ if err != nil {
79
+ resp.Ref = append(resp.Ref, placeholderRef)
80
+ errorList[i] = fmt.Errorf("%s", err.Error())
81
+ s.utxorpc.config.Logger.Error(
82
+ fmt.Sprintf(
83
+ "failed to add tx %s to mempool: %s",
84
+ txHash.String(),
85
+ err,
86
+ ),
87
+ )
88
+ hasError = true
89
+ continue
90
+ }
91
+ if err != nil {
92
+ resp.Ref = append(resp.Ref, placeholderRef)
93
+ errorList[i] = err
94
+ hasError = true
95
+ continue
96
+ }
97
+ resp.Ref = append(resp.Ref, txHash.Bytes())
98
+ }
99
+ if hasError {
100
+ return connect.NewResponse(resp), fmt.Errorf("%v", errorList)
101
+ }
102
+
103
+ return connect.NewResponse(resp), nil
104
+ }
105
+
106
+ // WaitForTx
107
+ func (s *submitServiceServer) WaitForTx(
108
+ ctx context.Context,
109
+ req *connect.Request[submit.WaitForTxRequest],
110
+ stream *connect.ServerStream[submit.WaitForTxResponse],
111
+ ) error {
112
+ ref := req.Msg.GetRef() // [][]byte
113
+
114
+ s.utxorpc.config.Logger.Info(
115
+ fmt.Sprintf(
116
+ "Received WaitForTx request with %d transactions",
117
+ len(ref),
118
+ ),
119
+ )
120
+ s.utxorpc.config.EventBus.SubscribeFunc(
121
+ ledger.BlockfetchEventType,
122
+ func(evt event.Event) {
123
+ e := evt.Data.(ledger.BlockfetchEvent)
124
+ for _, tx := range e.Block.Transactions() {
125
+ for _, r := range ref {
126
+ refHash := hex.EncodeToString(r)
127
+ // Compare our hashes
128
+ if refHash == tx.Hash().String() {
129
+ // Send confirmation response
130
+ err := stream.Send(&submit.WaitForTxResponse{
131
+ Ref: r,
132
+ Stage: submit.Stage_STAGE_CONFIRMED,
133
+ })
134
+ if err != nil {
135
+ if ctx.Err() != nil {
136
+ s.utxorpc.config.Logger.Warn(
137
+ "Client disconnected while sending response",
138
+ "error",
139
+ ctx.Err(),
140
+ )
141
+ return
142
+ }
143
+ s.utxorpc.config.Logger.Error(
144
+ "Error sending response to client",
145
+ "transaction_hash", tx.Hash(),
146
+ "error", err,
147
+ )
148
+ return
149
+ }
150
+ s.utxorpc.config.Logger.Debug(
151
+ "Confirmation response sent",
152
+ "transaction_hash", tx.Hash(),
153
+ )
154
+ return // Stop processing after confirming the transaction
155
+ }
156
+ }
157
+ }
158
+ },
159
+ )
160
+ return nil
161
+ }
162
+
163
+ // ReadMempool
164
+ func (s *submitServiceServer) ReadMempool(
165
+ ctx context.Context,
166
+ req *connect.Request[submit.ReadMempoolRequest],
167
+ ) (*connect.Response[submit.ReadMempoolResponse], error) {
168
+ s.utxorpc.config.Logger.Info("Got a ReadMempool request")
169
+ resp := &submit.ReadMempoolResponse{}
170
+
171
+ mempool := []*submit.TxInMempool{}
172
+ for _, tx := range s.utxorpc.config.Mempool.Transactions() {
173
+ record := &submit.TxInMempool{
174
+ NativeBytes: tx.Cbor,
175
+ Stage: submit.Stage_STAGE_MEMPOOL,
176
+ }
177
+ mempool = append(mempool, record)
178
+ }
179
+ resp.Items = mempool
180
+
181
+ return connect.NewResponse(resp), nil
182
+ }
183
+
184
+ // WatchMempool
185
+ func (s *submitServiceServer) WatchMempool(
186
+ ctx context.Context,
187
+ req *connect.Request[submit.WatchMempoolRequest],
188
+ stream *connect.ServerStream[submit.WatchMempoolResponse],
189
+ ) error {
190
+ predicate := req.Msg.GetPredicate() // Predicate
191
+ fieldMask := req.Msg.GetFieldMask()
192
+
193
+ s.utxorpc.config.Logger.Info(
194
+ fmt.Sprintf(
195
+ "Got a WatchMempool request with predicate %v and fieldMask %v",
196
+ predicate,
197
+ fieldMask,
198
+ ),
199
+ )
200
+
201
+ // Start our forever loop
202
+ for {
203
+ // Match against mempool transactions
204
+ for _, memTx := range s.utxorpc.config.Mempool.Transactions() {
205
+ txRawBytes := memTx.Cbor
206
+ txType, err := gledger.DetermineTransactionType(txRawBytes)
207
+ if err != nil {
208
+ return err
209
+ }
210
+ tx, err := gledger.NewTransactionFromCbor(txType, txRawBytes)
211
+ if err != nil {
212
+ return err
213
+ }
214
+ cTx := tx.Utxorpc() // *cardano.Tx
215
+ resp := &submit.WatchMempoolResponse{}
216
+ record := &submit.TxInMempool{
217
+ NativeBytes: txRawBytes,
218
+ Stage: submit.Stage_STAGE_MEMPOOL,
219
+ }
220
+ resp.Tx = record
221
+ if string(record.GetNativeBytes()) == cTx.String() {
222
+ if predicate == nil {
223
+ err := stream.Send(resp)
224
+ if err != nil {
225
+ return err
226
+ }
227
+ } else {
228
+ found := false
229
+ assetFound := false
230
+
231
+ // Check Predicate
232
+ addressPattern := predicate.GetMatch().GetCardano().GetHasAddress()
233
+ mintAssetPattern := predicate.GetMatch().GetCardano().GetMintsAsset()
234
+ moveAssetPattern := predicate.GetMatch().GetCardano().GetMovesAsset()
235
+
236
+ var addresses []gledger.Address
237
+ if addressPattern != nil {
238
+ // Handle Exact Address
239
+ exactAddressBytes := addressPattern.GetExactAddress()
240
+ if exactAddressBytes != nil {
241
+ var addr lcommon.Address
242
+ err := addr.UnmarshalCBOR(exactAddressBytes)
243
+ if err != nil {
244
+ return fmt.Errorf(
245
+ "failed to decode exact address: %w",
246
+ err,
247
+ )
248
+ }
249
+ addresses = append(addresses, addr)
250
+ }
251
+
252
+ // Handle Payment Part
253
+ paymentPart := addressPattern.GetPaymentPart()
254
+ if paymentPart != nil {
255
+ s.utxorpc.config.Logger.Info("PaymentPart is present, decoding...")
256
+ var paymentAddr lcommon.Address
257
+ err := paymentAddr.UnmarshalCBOR(paymentPart)
258
+ if err != nil {
259
+ return fmt.Errorf("failed to decode payment part: %w", err)
260
+ }
261
+ addresses = append(addresses, paymentAddr)
262
+ }
263
+
264
+ // Handle Delegation Part
265
+ delegationPart := addressPattern.GetDelegationPart()
266
+ if delegationPart != nil {
267
+ s.utxorpc.config.Logger.Info(
268
+ "DelegationPart is present, decoding...",
269
+ )
270
+ var delegationAddr lcommon.Address
271
+ err := delegationAddr.UnmarshalCBOR(delegationPart)
272
+ if err != nil {
273
+ return fmt.Errorf(
274
+ "failed to decode delegation part: %w",
275
+ err,
276
+ )
277
+ }
278
+ addresses = append(addresses, delegationAddr)
279
+ }
280
+ }
281
+
282
+ var assetPatterns []*cardano.AssetPattern
283
+ if mintAssetPattern != nil {
284
+ assetPatterns = append(assetPatterns, mintAssetPattern)
285
+ }
286
+ if moveAssetPattern != nil {
287
+ assetPatterns = append(assetPatterns, moveAssetPattern)
288
+ }
289
+
290
+ // Convert everything to utxos (gledger.TransactionOutput) for matching
291
+ var utxos []gledger.TransactionOutput
292
+ utxos = append(tx.Outputs(), tx.CollateralReturn())
293
+ var inputs []gledger.TransactionInput
294
+ inputs = append(tx.Inputs(), tx.ReferenceInputs()...)
295
+ inputs = append(inputs, tx.Collateral()...)
296
+ for _, input := range inputs {
297
+ utxo, err := s.utxorpc.config.LedgerState.UtxoByRef(
298
+ input.Id().Bytes(),
299
+ input.Index(),
300
+ )
301
+ if err != nil {
302
+ return fmt.Errorf(
303
+ "failed to look up input: %w",
304
+ err,
305
+ )
306
+ }
307
+ ret, err := utxo.Decode() // gledger.TransactionOutput
308
+ if err != nil {
309
+ return err
310
+ }
311
+ if ret == nil {
312
+ return errors.New("decode returned empty utxo")
313
+ }
314
+ utxos = append(utxos, ret)
315
+ }
316
+
317
+ // Check UTxOs for addresses
318
+ for _, address := range addresses {
319
+ if found {
320
+ break
321
+ }
322
+ if assetFound {
323
+ found = true
324
+ break
325
+ }
326
+ for _, utxo := range utxos {
327
+ if found {
328
+ break
329
+ }
330
+ if assetFound {
331
+ found = true
332
+ break
333
+ }
334
+ if utxo.Address().String() == address.String() {
335
+ if found {
336
+ break
337
+ }
338
+ if assetFound {
339
+ found = true
340
+ break
341
+ }
342
+ // We matched address, check assetPatterns
343
+ for _, assetPattern := range assetPatterns {
344
+ // Address found, no assetPattern
345
+ if assetPattern == nil {
346
+ found = true
347
+ break
348
+ }
349
+ // Filter on assetPattern
350
+ for _, policyId := range utxo.Assets().Policies() {
351
+ if assetFound {
352
+ found = true
353
+ break
354
+ }
355
+ if bytes.Equal(
356
+ policyId.Bytes(),
357
+ assetPattern.GetPolicyId(),
358
+ ) {
359
+ for _, asset := range utxo.Assets().Assets(
360
+ policyId,
361
+ ) {
362
+ if bytes.Equal(
363
+ asset,
364
+ assetPattern.GetAssetName(),
365
+ ) {
366
+ found = true
367
+ assetFound = true
368
+ break
369
+ }
370
+ }
371
+ }
372
+ }
373
+ }
374
+ if found {
375
+ break
376
+ }
377
+ // Asset not found; skip this UTxO
378
+ if !assetFound {
379
+ continue
380
+ }
381
+ found = true
382
+ }
383
+ }
384
+ }
385
+ if found {
386
+ err := stream.Send(resp)
387
+ if err != nil {
388
+ return err
389
+ }
390
+ }
391
+ }
392
+ }
393
+ }
394
+ }
395
+ }
@@ -0,0 +1,276 @@
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 utxorpc
16
+
17
+ import (
18
+ "context"
19
+ "errors"
20
+ "fmt"
21
+
22
+ "connectrpc.com/connect"
23
+ "github.com/blinklabs-io/gouroboros/ledger"
24
+ ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
25
+ sync "github.com/utxorpc/go-codegen/utxorpc/v1alpha/sync"
26
+ "github.com/utxorpc/go-codegen/utxorpc/v1alpha/sync/syncconnect"
27
+ )
28
+
29
+ // syncServiceServer implements the SyncService API
30
+ type syncServiceServer struct {
31
+ syncconnect.UnimplementedSyncServiceHandler
32
+ utxorpc *Utxorpc
33
+ }
34
+
35
+ // FetchBlock
36
+ func (s *syncServiceServer) FetchBlock(
37
+ ctx context.Context,
38
+ req *connect.Request[sync.FetchBlockRequest],
39
+ ) (*connect.Response[sync.FetchBlockResponse], error) {
40
+ ref := req.Msg.GetRef() // []*BlockRef
41
+ fieldMask := req.Msg.GetFieldMask()
42
+
43
+ s.utxorpc.config.Logger.Info(
44
+ fmt.Sprintf(
45
+ "Got a FetchBlock request with ref %v and fieldMask %v",
46
+ ref,
47
+ fieldMask,
48
+ ),
49
+ )
50
+ resp := &sync.FetchBlockResponse{}
51
+
52
+ // Get our points
53
+ var points []ocommon.Point
54
+ if len(ref) > 0 {
55
+ for _, blockRef := range ref {
56
+ blockIdx := blockRef.GetIndex()
57
+ blockHash := blockRef.GetHash()
58
+ slot := blockIdx
59
+ point := ocommon.NewPoint(slot, blockHash)
60
+ points = append(points, point)
61
+ }
62
+ } else {
63
+ point := s.utxorpc.config.LedgerState.Tip().Point
64
+ points = append(points, point)
65
+ }
66
+
67
+ for _, point := range points {
68
+ block, err := s.utxorpc.config.LedgerState.GetBlock(point)
69
+ if err != nil {
70
+ return nil, err
71
+ }
72
+ if block == nil {
73
+ return nil, errors.New("block returned nil")
74
+ }
75
+ var acb sync.AnyChainBlock
76
+ ret, err := block.Decode()
77
+ if err != nil {
78
+ return nil, err
79
+ }
80
+ acbc := sync.AnyChainBlock_Cardano{
81
+ Cardano: ret.Utxorpc(),
82
+ }
83
+ acb.Chain = &acbc
84
+ resp.Block = append(resp.Block, &acb)
85
+ }
86
+
87
+ return connect.NewResponse(resp), nil
88
+ }
89
+
90
+ // DumpHistory
91
+ func (s *syncServiceServer) DumpHistory(
92
+ ctx context.Context,
93
+ req *connect.Request[sync.DumpHistoryRequest],
94
+ ) (*connect.Response[sync.DumpHistoryResponse], error) {
95
+ startToken := req.Msg.GetStartToken() // *BlockRef
96
+ maxItems := req.Msg.GetMaxItems() // uint32
97
+ fieldMask := req.Msg.GetFieldMask()
98
+
99
+ s.utxorpc.config.Logger.Info(
100
+ fmt.Sprintf(
101
+ "Got a DumpHistory request with token %v and maxItems %d and fieldMask %v",
102
+ startToken,
103
+ maxItems,
104
+ fieldMask,
105
+ ),
106
+ )
107
+ resp := &sync.DumpHistoryResponse{}
108
+
109
+ // Get our points
110
+ var points []ocommon.Point
111
+ if maxItems > 0 {
112
+ tmpPoints, err := s.utxorpc.config.LedgerState.RecentChainPoints(
113
+ int(maxItems),
114
+ )
115
+ if err != nil {
116
+ return nil, err
117
+ }
118
+ points = tmpPoints
119
+ } else {
120
+ point := s.utxorpc.config.LedgerState.Tip().Point
121
+ points = append(points, point)
122
+ }
123
+ // TODO: make this work (#401)
124
+ // if startToken != nil {
125
+ // blockIdx := startToken.GetIndex()
126
+ // blockHash := startToken.GetHash()
127
+ // slot := uint64(blockIdx)
128
+ // point = ocommon.NewPoint(slot, blockHash)
129
+ // }
130
+
131
+ for _, point := range points {
132
+ block, err := s.utxorpc.config.LedgerState.GetBlock(point)
133
+ if err != nil {
134
+ return nil, err
135
+ }
136
+ if block == nil {
137
+ return nil, errors.New("block returned nil")
138
+ }
139
+ var acb sync.AnyChainBlock
140
+ ret, err := block.Decode()
141
+ if err != nil {
142
+ return nil, err
143
+ }
144
+ acbc := sync.AnyChainBlock_Cardano{
145
+ Cardano: ret.Utxorpc(),
146
+ }
147
+ acb.Chain = &acbc
148
+ resp.Block = append(resp.Block, &acb)
149
+ }
150
+
151
+ return connect.NewResponse(resp), nil
152
+ }
153
+
154
+ // FollowTip
155
+ func (s *syncServiceServer) FollowTip(
156
+ ctx context.Context,
157
+ req *connect.Request[sync.FollowTipRequest],
158
+ stream *connect.ServerStream[sync.FollowTipResponse],
159
+ ) error {
160
+ intersect := req.Msg.GetIntersect() // []*BlockRef
161
+
162
+ s.utxorpc.config.Logger.Info(
163
+ fmt.Sprintf(
164
+ "Got a FollowTip request with intersect %v",
165
+ intersect,
166
+ ),
167
+ )
168
+
169
+ // Get our points
170
+ var points []ocommon.Point
171
+ if len(intersect) > 0 {
172
+ for _, blockRef := range intersect {
173
+ blockIdx := blockRef.GetIndex()
174
+ blockHash := blockRef.GetHash()
175
+ slot := blockIdx
176
+ point := ocommon.NewPoint(slot, blockHash)
177
+ points = append(points, point)
178
+ }
179
+ } else {
180
+ point := s.utxorpc.config.LedgerState.Tip().Point
181
+ points = append(points, point)
182
+ }
183
+
184
+ // Get our starting point matching our chain
185
+ point, err := s.utxorpc.config.LedgerState.GetIntersectPoint(points)
186
+ if err != nil {
187
+ s.utxorpc.config.Logger.Error(
188
+ "failed to get points",
189
+ "error", err,
190
+ )
191
+ return err
192
+ }
193
+ if point == nil {
194
+ s.utxorpc.config.Logger.Error(
195
+ "nil point returned",
196
+ )
197
+ return errors.New("nil point returned")
198
+ }
199
+
200
+ // Create our chain iterator
201
+ chainIter, err := s.utxorpc.config.LedgerState.GetChainFromPoint(
202
+ *point,
203
+ false,
204
+ )
205
+ if err != nil {
206
+ s.utxorpc.config.Logger.Error(
207
+ "failed to get chain iterator",
208
+ "error", err,
209
+ )
210
+ return err
211
+ }
212
+
213
+ for {
214
+ // Check for available block
215
+ next, err := chainIter.Next(true)
216
+ if err != nil {
217
+ s.utxorpc.config.Logger.Error(
218
+ "failed to iterate chain",
219
+ "error", err,
220
+ )
221
+ return err
222
+ }
223
+ if next != nil {
224
+ // Send block response
225
+ blockBytes := next.Block.Cbor[:]
226
+ blockType, err := ledger.DetermineBlockType(blockBytes)
227
+ if err != nil {
228
+ s.utxorpc.config.Logger.Error(
229
+ "failed to get block type",
230
+ "error", err,
231
+ )
232
+ return err
233
+ }
234
+ block, err := ledger.NewBlockFromCbor(blockType, blockBytes)
235
+ if err != nil {
236
+ s.utxorpc.config.Logger.Error(
237
+ "failed to get block",
238
+ "error", err,
239
+ )
240
+ return err
241
+ }
242
+ var acb sync.AnyChainBlock
243
+ acbc := sync.AnyChainBlock_Cardano{
244
+ Cardano: block.Utxorpc(),
245
+ }
246
+ acb.Chain = &acbc
247
+ resp := &sync.FollowTipResponse{
248
+ Action: &sync.FollowTipResponse_Apply{
249
+ Apply: &acb,
250
+ },
251
+ }
252
+ err = stream.Send(resp)
253
+ if err != nil {
254
+ s.utxorpc.config.Logger.Error(
255
+ "failed to send message to client",
256
+ "error", err,
257
+ )
258
+ return err
259
+ }
260
+ }
261
+ }
262
+ }
263
+
264
+ // ReadTip
265
+ func (s *syncServiceServer) ReadTip(
266
+ ctx context.Context,
267
+ req *connect.Request[sync.ReadTipRequest],
268
+ ) (*connect.Response[sync.ReadTipResponse], error) {
269
+ s.utxorpc.config.Logger.Info("Got a ReadTip request")
270
+ resp := &sync.ReadTipResponse{}
271
+
272
+ point := s.utxorpc.config.LedgerState.Tip().Point
273
+ resp.Tip = &sync.BlockRef{Index: point.Slot, Hash: point.Hash}
274
+
275
+ return connect.NewResponse(resp), nil
276
+ }