@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,233 @@
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 dingo
16
+
17
+ import (
18
+ "encoding/hex"
19
+ "errors"
20
+ "fmt"
21
+ "math"
22
+
23
+ "github.com/blinklabs-io/dingo/mempool"
24
+ ouroboros "github.com/blinklabs-io/gouroboros"
25
+ "github.com/blinklabs-io/gouroboros/ledger"
26
+ "github.com/blinklabs-io/gouroboros/protocol/txsubmission"
27
+ )
28
+
29
+ const (
30
+ txsubmissionRequestTxIdsCount = 10 // Number of TxIds to request from peer at one time
31
+ )
32
+
33
+ func (n *Node) txsubmissionServerConnOpts() []txsubmission.TxSubmissionOptionFunc {
34
+ return []txsubmission.TxSubmissionOptionFunc{
35
+ txsubmission.WithInitFunc(n.txsubmissionServerInit),
36
+ }
37
+ }
38
+
39
+ func (n *Node) txsubmissionClientConnOpts() []txsubmission.TxSubmissionOptionFunc {
40
+ return []txsubmission.TxSubmissionOptionFunc{
41
+ txsubmission.WithRequestTxIdsFunc(n.txsubmissionClientRequestTxIds),
42
+ txsubmission.WithRequestTxsFunc(n.txsubmissionClientRequestTxs),
43
+ }
44
+ }
45
+
46
+ func (n *Node) txsubmissionClientStart(connId ouroboros.ConnectionId) error {
47
+ // Register mempool consumer
48
+ // We don't bother capturing the consumer because we can easily look it up later by connection ID
49
+ _ = n.mempool.AddConsumer(connId)
50
+ // Start TxSubmission loop
51
+ conn := n.connManager.GetConnectionById(connId)
52
+ if conn == nil {
53
+ return fmt.Errorf("failed to lookup connection ID: %s", connId.String())
54
+ }
55
+ conn.TxSubmission().Client.Init()
56
+ return nil
57
+ }
58
+
59
+ func (n *Node) txsubmissionServerInit(ctx txsubmission.CallbackContext) error {
60
+ // Start async loop to request transactions from the peer's mempool
61
+ go func() {
62
+ for {
63
+ // Request available TX IDs (era and TX hash) and sizes
64
+ // We make the request blocking to avoid looping on our side
65
+ txIds, err := ctx.Server.RequestTxIds(
66
+ true,
67
+ txsubmissionRequestTxIdsCount,
68
+ )
69
+ if err != nil {
70
+ n.config.logger.Error(
71
+ fmt.Sprintf(
72
+ "failed to get TxIds: %s",
73
+ err,
74
+ ),
75
+ "component", "network",
76
+ "protocol", "tx-submission",
77
+ "role", "server",
78
+ "connection_id", ctx.ConnectionId.String(),
79
+ )
80
+ return
81
+ }
82
+ if len(txIds) > 0 {
83
+ // Unwrap inner TxId from TxIdAndSize
84
+ var requestTxIds []txsubmission.TxId
85
+ for _, txId := range txIds {
86
+ requestTxIds = append(requestTxIds, txId.TxId)
87
+ }
88
+ // Request TX content for TxIds from above
89
+ txs, err := ctx.Server.RequestTxs(requestTxIds)
90
+ if err != nil {
91
+ n.config.logger.Error(
92
+ fmt.Sprintf(
93
+ "failed to get Txs: %s",
94
+ err,
95
+ ),
96
+ "component", "network",
97
+ "protocol", "tx-submission",
98
+ "role", "server",
99
+ "connection_id", ctx.ConnectionId.String(),
100
+ )
101
+ return
102
+ }
103
+ for _, txBody := range txs {
104
+ // Decode TX from CBOR
105
+ tx, err := ledger.NewTransactionFromCbor(
106
+ uint(txBody.EraId),
107
+ txBody.TxBody,
108
+ )
109
+ if err != nil {
110
+ n.config.logger.Error(
111
+ fmt.Sprintf(
112
+ "failed to parse transaction CBOR: %s",
113
+ err,
114
+ ),
115
+ "component", "network",
116
+ "protocol", "tx-submission",
117
+ "role", "server",
118
+ "connection_id", ctx.ConnectionId.String(),
119
+ )
120
+ return
121
+ }
122
+ n.config.logger.Debug(
123
+ "received tx",
124
+ "tx_hash", tx.Hash(),
125
+ "protocol", "tx-submission",
126
+ "role", "server",
127
+ "connection_id", ctx.ConnectionId.String(),
128
+ )
129
+ // Add transaction to mempool
130
+ err = n.mempool.AddTransaction(
131
+ uint(txBody.EraId),
132
+ txBody.TxBody,
133
+ )
134
+ if err != nil {
135
+ n.config.logger.Error(
136
+ fmt.Sprintf(
137
+ "failed to add tx %x to mempool: %s",
138
+ tx.Hash(),
139
+ err,
140
+ ),
141
+ "component", "network",
142
+ "protocol", "tx-submission",
143
+ "role", "server",
144
+ "connection_id", ctx.ConnectionId.String(),
145
+ )
146
+ return
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }()
152
+ return nil
153
+ }
154
+
155
+ func (n *Node) txsubmissionClientRequestTxIds(
156
+ ctx txsubmission.CallbackContext,
157
+ blocking bool,
158
+ ack uint16,
159
+ req uint16,
160
+ ) ([]txsubmission.TxIdAndSize, error) {
161
+ connId := ctx.ConnectionId
162
+ ret := []txsubmission.TxIdAndSize{}
163
+ consumer := n.mempool.Consumer(connId)
164
+ // Clear TX cache
165
+ if ack > 0 {
166
+ consumer.ClearCache()
167
+ }
168
+ // Get available TXs
169
+ var tmpTxs []*mempool.MempoolTransaction
170
+ for {
171
+ if blocking && len(tmpTxs) == 0 {
172
+ // Wait until we see a TX
173
+ tmpTx := consumer.NextTx(true)
174
+ if tmpTx == nil {
175
+ break
176
+ }
177
+ tmpTxs = append(tmpTxs, tmpTx)
178
+ } else {
179
+ // Return immediately if no TX is available
180
+ tmpTx := consumer.NextTx(false)
181
+ if tmpTx == nil {
182
+ break
183
+ }
184
+ tmpTxs = append(tmpTxs, tmpTx)
185
+ }
186
+ }
187
+ for _, tmpTx := range tmpTxs {
188
+ // Add to return value
189
+ txHashBytes, err := hex.DecodeString(tmpTx.Hash)
190
+ if err != nil {
191
+ return nil, err
192
+ }
193
+ txSize := len(tmpTx.Cbor)
194
+ if txSize > math.MaxUint32 {
195
+ return nil, errors.New("tx impossibly large")
196
+ }
197
+ ret = append(
198
+ ret,
199
+ txsubmission.TxIdAndSize{
200
+ TxId: txsubmission.TxId{
201
+ EraId: uint16(tmpTx.Type), // #nosec G115
202
+ TxId: [32]byte(txHashBytes),
203
+ },
204
+ Size: uint32(txSize),
205
+ },
206
+ )
207
+ }
208
+ return ret, nil
209
+ }
210
+
211
+ func (n *Node) txsubmissionClientRequestTxs(
212
+ ctx txsubmission.CallbackContext,
213
+ txIds []txsubmission.TxId,
214
+ ) ([]txsubmission.TxBody, error) {
215
+ connId := ctx.ConnectionId
216
+ ret := []txsubmission.TxBody{}
217
+ consumer := n.mempool.Consumer(connId)
218
+ for _, txId := range txIds {
219
+ txHash := hex.EncodeToString(txId.TxId[:])
220
+ tx := consumer.GetTxFromCache(txHash)
221
+ if tx != nil {
222
+ ret = append(
223
+ ret,
224
+ txsubmission.TxBody{
225
+ EraId: uint16(tx.Type), // #nosec G115
226
+ TxBody: tx.Cbor,
227
+ },
228
+ )
229
+ }
230
+ consumer.RemoveTxFromCache(txHash)
231
+ }
232
+ return ret, nil
233
+ }
@@ -0,0 +1,311 @@
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
+ "errors"
21
+ "fmt"
22
+
23
+ "connectrpc.com/connect"
24
+ "github.com/blinklabs-io/gouroboros/ledger"
25
+ query "github.com/utxorpc/go-codegen/utxorpc/v1alpha/query"
26
+ "github.com/utxorpc/go-codegen/utxorpc/v1alpha/query/queryconnect"
27
+ )
28
+
29
+ // queryServiceServer implements the QueryService API
30
+ type queryServiceServer struct {
31
+ queryconnect.UnimplementedQueryServiceHandler
32
+ utxorpc *Utxorpc
33
+ }
34
+
35
+ // ReadParams
36
+ func (s *queryServiceServer) ReadParams(
37
+ ctx context.Context,
38
+ req *connect.Request[query.ReadParamsRequest],
39
+ ) (*connect.Response[query.ReadParamsResponse], error) {
40
+ fieldMask := req.Msg.GetFieldMask()
41
+
42
+ s.utxorpc.config.Logger.Info(
43
+ fmt.Sprintf(
44
+ "Got a ReadParams request with fieldMask %v",
45
+ fieldMask,
46
+ ),
47
+ )
48
+ resp := &query.ReadParamsResponse{}
49
+
50
+ protoParams := s.utxorpc.config.LedgerState.GetCurrentPParams()
51
+ if protoParams == nil {
52
+ return nil, errors.New("current protocol parameters empty")
53
+ }
54
+
55
+ // Get chain point (slot and hash)
56
+ point := s.utxorpc.config.LedgerState.Tip().Point
57
+
58
+ // Set up response parameters
59
+ acpc := &query.AnyChainParams_Cardano{
60
+ Cardano: protoParams.Utxorpc(),
61
+ }
62
+ resp.LedgerTip = &query.ChainPoint{
63
+ Slot: point.Slot,
64
+ Hash: point.Hash,
65
+ }
66
+ resp.Values = &query.AnyChainParams{
67
+ Params: acpc,
68
+ }
69
+ return connect.NewResponse(resp), nil
70
+ }
71
+
72
+ // ReadUtxos
73
+ func (s *queryServiceServer) ReadUtxos(
74
+ ctx context.Context,
75
+ req *connect.Request[query.ReadUtxosRequest],
76
+ ) (*connect.Response[query.ReadUtxosResponse], error) {
77
+ keys := req.Msg.GetKeys() // []*TxoRef
78
+
79
+ s.utxorpc.config.Logger.Info(
80
+ fmt.Sprintf("Got a ReadUtxos request with keys %v", keys),
81
+ )
82
+ resp := &query.ReadUtxosResponse{}
83
+
84
+ // Get UTxOs from ledger
85
+ for _, txo := range keys {
86
+ utxo, err := s.utxorpc.config.LedgerState.UtxoByRef(
87
+ txo.GetHash(),
88
+ txo.GetIndex(),
89
+ )
90
+ if err != nil {
91
+ return nil, err
92
+ }
93
+ var aud query.AnyUtxoData
94
+ ret, err := utxo.Decode()
95
+ if err != nil {
96
+ return nil, err
97
+ }
98
+ if ret == nil {
99
+ return nil, errors.New("decode returned empty utxo")
100
+ }
101
+ audc := query.AnyUtxoData_Cardano{
102
+ Cardano: ret.Utxorpc(),
103
+ }
104
+ aud.NativeBytes = utxo.Cbor
105
+ aud.TxoRef = txo
106
+
107
+ if audc.Cardano.GetDatum() != nil {
108
+ // Check if Datum.Hash is all zeroes
109
+ isAllZeroes := true
110
+ for _, b := range audc.Cardano.GetDatum().GetHash() {
111
+ if b != 0 {
112
+ isAllZeroes = false
113
+ break
114
+ }
115
+ }
116
+ if isAllZeroes {
117
+ // No actual datum; set Datum to nil to omit it
118
+ audc.Cardano.Datum = nil
119
+ }
120
+ }
121
+ aud.ParsedState = &audc
122
+ resp.Items = append(resp.Items, &aud)
123
+ }
124
+
125
+ // Get chain point (slot and hash)
126
+ point := s.utxorpc.config.LedgerState.Tip().Point
127
+
128
+ // Set up response utxos
129
+ resp.LedgerTip = &query.ChainPoint{
130
+ Slot: point.Slot,
131
+ Hash: point.Hash,
132
+ }
133
+
134
+ return connect.NewResponse(resp), nil
135
+ }
136
+
137
+ // SearchUtxos
138
+ func (s *queryServiceServer) SearchUtxos(
139
+ ctx context.Context,
140
+ req *connect.Request[query.SearchUtxosRequest],
141
+ ) (*connect.Response[query.SearchUtxosResponse], error) {
142
+ predicate := req.Msg.GetPredicate() // *UtxoPredicate
143
+ startToken := req.Msg.GetStartToken() // string
144
+ maxItems := req.Msg.GetMaxItems() // int32
145
+ fieldMask := req.Msg.GetFieldMask()
146
+
147
+ s.utxorpc.config.Logger.Info(
148
+ fmt.Sprintf(
149
+ "Got a SearchUtxos request with predicate %v, startToken %s, maxItems %d, and fieldMask %v",
150
+ predicate,
151
+ startToken,
152
+ maxItems,
153
+ fieldMask,
154
+ ),
155
+ )
156
+ resp := &query.SearchUtxosResponse{}
157
+
158
+ // TODO: make this optional and create separate code paths
159
+ if predicate == nil {
160
+ return nil, fmt.Errorf("empty predicate: %v", predicate)
161
+ }
162
+
163
+ addressPattern := predicate.GetMatch().GetCardano().GetAddress()
164
+ assetPattern := predicate.GetMatch().GetCardano().GetAsset()
165
+
166
+ var addresses []ledger.Address
167
+ if addressPattern != nil {
168
+ // Handle Exact Address
169
+ exactAddressBytes := addressPattern.GetExactAddress()
170
+ if exactAddressBytes != nil {
171
+ var addr ledger.Address
172
+ err := addr.UnmarshalCBOR(exactAddressBytes)
173
+ if err != nil {
174
+ return nil, fmt.Errorf(
175
+ "failed to decode exact address: %w",
176
+ err,
177
+ )
178
+ }
179
+ addresses = append(addresses, addr)
180
+ }
181
+
182
+ // Handle Payment Part
183
+ paymentPart := addressPattern.GetPaymentPart()
184
+ if paymentPart != nil {
185
+ s.utxorpc.config.Logger.Info("PaymentPart is present, decoding...")
186
+ var paymentAddr ledger.Address
187
+ err := paymentAddr.UnmarshalCBOR(paymentPart)
188
+ if err != nil {
189
+ return nil, fmt.Errorf("failed to decode payment part: %w", err)
190
+ }
191
+ addresses = append(addresses, paymentAddr)
192
+ }
193
+
194
+ // Handle Delegation Part
195
+ delegationPart := addressPattern.GetDelegationPart()
196
+ if delegationPart != nil {
197
+ s.utxorpc.config.Logger.Info(
198
+ "DelegationPart is present, decoding...",
199
+ )
200
+ var delegationAddr ledger.Address
201
+ err := delegationAddr.UnmarshalCBOR(delegationPart)
202
+ if err != nil {
203
+ return nil, fmt.Errorf(
204
+ "failed to decode delegation part: %w",
205
+ err,
206
+ )
207
+ }
208
+ addresses = append(addresses, delegationAddr)
209
+ }
210
+ }
211
+
212
+ // Get UTxOs from ledger
213
+ for _, address := range addresses {
214
+ utxos, err := s.utxorpc.config.LedgerState.UtxosByAddress(address)
215
+ if err != nil {
216
+ return nil, err
217
+ }
218
+ for _, utxo := range utxos {
219
+ var aud query.AnyUtxoData
220
+ ret, err := utxo.Decode()
221
+ if err != nil {
222
+ return nil, err
223
+ }
224
+ if ret == nil {
225
+ return nil, errors.New("decode returned empty utxo")
226
+ }
227
+ audc := query.AnyUtxoData_Cardano{
228
+ Cardano: ret.Utxorpc(),
229
+ }
230
+ aud.NativeBytes = utxo.Cbor
231
+ aud.TxoRef = &query.TxoRef{
232
+ Hash: utxo.TxId,
233
+ Index: utxo.OutputIdx,
234
+ }
235
+ if audc.Cardano.GetDatum() != nil {
236
+ // Check if Datum.Hash is all zeroes
237
+ isAllZeroes := true
238
+ for _, b := range audc.Cardano.GetDatum().GetHash() {
239
+ if b != 0 {
240
+ isAllZeroes = false
241
+ break
242
+ }
243
+ }
244
+ if isAllZeroes {
245
+ // No actual datum; set Datum to nil to omit it
246
+ audc.Cardano.Datum = nil
247
+ }
248
+ }
249
+ aud.ParsedState = &audc
250
+
251
+ // If AssetPattern is specified, filter based on it
252
+ if assetPattern != nil {
253
+ assetFound := false
254
+ for _, multiasset := range audc.Cardano.GetAssets() {
255
+ if bytes.Equal(
256
+ multiasset.GetPolicyId(),
257
+ assetPattern.GetPolicyId(),
258
+ ) {
259
+ for _, asset := range multiasset.GetAssets() {
260
+ if bytes.Equal(
261
+ asset.GetName(),
262
+ assetPattern.GetAssetName(),
263
+ ) {
264
+ assetFound = true
265
+ break
266
+ }
267
+ }
268
+ }
269
+ if assetFound {
270
+ break
271
+ }
272
+ }
273
+
274
+ // Asset not found; skip this UTxO
275
+ if !assetFound {
276
+ continue
277
+ }
278
+ }
279
+ resp.Items = append(resp.Items, &aud)
280
+ }
281
+ }
282
+ // Get chain point (slot and hash)
283
+ point := s.utxorpc.config.LedgerState.Tip().Point
284
+
285
+ resp.LedgerTip = &query.ChainPoint{
286
+ Slot: point.Slot,
287
+ Hash: point.Hash,
288
+ }
289
+ return connect.NewResponse(resp), nil
290
+ }
291
+
292
+ // ReadData
293
+ func (s *queryServiceServer) ReadData(
294
+ ctx context.Context,
295
+ req *connect.Request[query.ReadDataRequest],
296
+ ) (*connect.Response[query.ReadDataResponse], error) {
297
+ keys := req.Msg.GetKeys() // [][]byte
298
+ fieldMask := req.Msg.GetFieldMask()
299
+
300
+ s.utxorpc.config.Logger.Info(
301
+ fmt.Sprintf(
302
+ "Got a ReadData request with keys %v and fieldMask %v",
303
+ keys,
304
+ fieldMask,
305
+ ),
306
+ )
307
+ resp := &query.ReadDataResponse{}
308
+
309
+ // TODO: do the thing once #317 is resolved
310
+ return connect.NewResponse(resp), nil
311
+ }