@blinklabs/dingo 0.7.0 → 0.8.1

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 (192) hide show
  1. package/README.md +2 -1
  2. package/{bin/dingo → dingo} +0 -0
  3. package/dingo.yaml.example +53 -0
  4. package/package.json +2 -2
  5. package/.github/CODEOWNERS +0 -5
  6. package/.github/assets/dingo-ate-my-blockchain.png +0 -0
  7. package/.github/assets/dingo-illustration.png +0 -0
  8. package/.github/assets/dingo-logo-with-text-horizontal.png +0 -0
  9. package/.github/assets/dingo-logo-with-text.png +0 -0
  10. package/.github/dependabot.yml +0 -19
  11. package/.github/dingo-20241210.png +0 -0
  12. package/.github/dingo.md +0 -56
  13. package/.github/workflows/ci-docker.yml +0 -36
  14. package/.github/workflows/conventional-commits.yml +0 -17
  15. package/.github/workflows/go-test.yml +0 -29
  16. package/.github/workflows/golangci-lint.yml +0 -23
  17. package/.github/workflows/publish.yml +0 -207
  18. package/Dockerfile +0 -25
  19. package/Makefile +0 -53
  20. package/blockfetch.go +0 -144
  21. package/chain/chain.go +0 -504
  22. package/chain/chain_test.go +0 -468
  23. package/chain/errors.go +0 -80
  24. package/chain/event.go +0 -33
  25. package/chain/iter.go +0 -64
  26. package/chainsync/chainsync.go +0 -97
  27. package/chainsync.go +0 -223
  28. package/cmd/dingo/load.go +0 -52
  29. package/cmd/dingo/main.go +0 -118
  30. package/cmd/dingo/serve.go +0 -49
  31. package/config/cardano/node.go +0 -192
  32. package/config/cardano/node_test.go +0 -85
  33. package/config/cardano/preview/README.md +0 -4
  34. package/config/cardano/preview/alonzo-genesis.json +0 -196
  35. package/config/cardano/preview/byron-genesis.json +0 -117
  36. package/config/cardano/preview/config.json +0 -114
  37. package/config/cardano/preview/conway-genesis.json +0 -297
  38. package/config/cardano/preview/shelley-genesis.json +0 -68
  39. package/config.go +0 -245
  40. package/connmanager/connection_manager.go +0 -105
  41. package/connmanager/connection_manager_test.go +0 -185
  42. package/connmanager/event.go +0 -37
  43. package/connmanager/listener.go +0 -140
  44. package/connmanager/outbound.go +0 -93
  45. package/connmanager/socket.go +0 -55
  46. package/connmanager/unix.go +0 -78
  47. package/custom-p2p-topology.json +0 -24
  48. package/custom-p2p-topology.json.backup +0 -24
  49. package/custom-p2p-topology.json.mainnet +0 -37
  50. package/database/account.go +0 -180
  51. package/database/block.go +0 -362
  52. package/database/certs.go +0 -53
  53. package/database/commit_timestamp.go +0 -77
  54. package/database/database.go +0 -118
  55. package/database/database_test.go +0 -62
  56. package/database/drep.go +0 -47
  57. package/database/epoch.go +0 -121
  58. package/database/immutable/chunk.go +0 -182
  59. package/database/immutable/immutable.go +0 -350
  60. package/database/immutable/immutable_test.go +0 -59
  61. package/database/immutable/primary.go +0 -106
  62. package/database/immutable/secondary.go +0 -103
  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 +0 -4
  70. package/database/plugin/blob/badger/commit_timestamp.go +0 -50
  71. package/database/plugin/blob/badger/database.go +0 -152
  72. package/database/plugin/blob/badger/logger.go +0 -63
  73. package/database/plugin/blob/badger/metrics.go +0 -98
  74. package/database/plugin/blob/blob.go +0 -19
  75. package/database/plugin/blob/store.go +0 -40
  76. package/database/plugin/log.go +0 -27
  77. package/database/plugin/metadata/metadata.go +0 -19
  78. package/database/plugin/metadata/sqlite/account.go +0 -313
  79. package/database/plugin/metadata/sqlite/certs.go +0 -58
  80. package/database/plugin/metadata/sqlite/commit_timestamp.go +0 -68
  81. package/database/plugin/metadata/sqlite/database.go +0 -218
  82. package/database/plugin/metadata/sqlite/drep.go +0 -140
  83. package/database/plugin/metadata/sqlite/epoch.go +0 -120
  84. package/database/plugin/metadata/sqlite/models/account.go +0 -118
  85. package/database/plugin/metadata/sqlite/models/auth_committee_hot.go +0 -26
  86. package/database/plugin/metadata/sqlite/models/drep.go +0 -52
  87. package/database/plugin/metadata/sqlite/models/epoch.go +0 -31
  88. package/database/plugin/metadata/sqlite/models/models.go +0 -46
  89. package/database/plugin/metadata/sqlite/models/pool.go +0 -97
  90. package/database/plugin/metadata/sqlite/models/pparam_update.go +0 -27
  91. package/database/plugin/metadata/sqlite/models/pparams.go +0 -27
  92. package/database/plugin/metadata/sqlite/models/resign_committee_cold.go +0 -27
  93. package/database/plugin/metadata/sqlite/models/stake_vote_delegation.go +0 -27
  94. package/database/plugin/metadata/sqlite/models/tip.go +0 -26
  95. package/database/plugin/metadata/sqlite/models/update_drep.go +0 -27
  96. package/database/plugin/metadata/sqlite/models/utxo.go +0 -30
  97. package/database/plugin/metadata/sqlite/models/vote_delegation.go +0 -26
  98. package/database/plugin/metadata/sqlite/models/vote_registration_delegation.go +0 -15
  99. package/database/plugin/metadata/sqlite/pool.go +0 -240
  100. package/database/plugin/metadata/sqlite/pparams.go +0 -110
  101. package/database/plugin/metadata/sqlite/tip.go +0 -83
  102. package/database/plugin/metadata/sqlite/utxo.go +0 -292
  103. package/database/plugin/metadata/store.go +0 -198
  104. package/database/plugin/option.go +0 -190
  105. package/database/plugin/plugin.go +0 -20
  106. package/database/plugin/register.go +0 -118
  107. package/database/pparams.go +0 -145
  108. package/database/tip.go +0 -45
  109. package/database/txn.go +0 -147
  110. package/database/types/types.go +0 -74
  111. package/database/types/types_test.go +0 -83
  112. package/database/utxo.go +0 -263
  113. package/dist/artifacts.json +0 -1
  114. package/dist/checksums.txt +0 -22
  115. package/dist/config.yaml +0 -253
  116. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz +0 -0
  117. package/dist/dingo-0.5.0-SNAPSHOT-d9431e4.tar.gz.sbom.json +0 -1
  118. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz +0 -0
  119. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_arm64.tar.gz.sbom.json +0 -1
  120. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz +0 -0
  121. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_darwin_x86_64.tar.gz.sbom.json +0 -1
  122. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk +0 -0
  123. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.apk.sbom.json +0 -1
  124. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb +0 -0
  125. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.deb.sbom.json +0 -1
  126. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm +0 -0
  127. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_amd64.rpm.sbom.json +0 -1
  128. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk +0 -0
  129. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.apk.sbom.json +0 -1
  130. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb +0 -0
  131. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.deb.sbom.json +0 -1
  132. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm +0 -0
  133. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.rpm.sbom.json +0 -1
  134. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz +0 -0
  135. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_arm64.tar.gz.sbom.json +0 -1
  136. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz +0 -0
  137. package/dist/dingo_0.5.0-SNAPSHOT-d9431e4_linux_x86_64.tar.gz.sbom.json +0 -1
  138. package/dist/dingo_darwin_amd64_v1/dingo +0 -0
  139. package/dist/dingo_darwin_arm64_v8.0/dingo +0 -0
  140. package/dist/dingo_linux_amd64_v1/dingo +0 -0
  141. package/dist/dingo_linux_arm64_v8.0/dingo +0 -0
  142. package/dist/homebrew/dingo.rb +0 -51
  143. package/dist/metadata.json +0 -1
  144. package/event/event.go +0 -141
  145. package/event/event_test.go +0 -115
  146. package/event/metrics.go +0 -44
  147. package/go.mod +0 -98
  148. package/go.sum +0 -358
  149. package/internal/config/config.go +0 -145
  150. package/internal/config/config_test.go +0 -118
  151. package/internal/node/load.go +0 -149
  152. package/internal/node/node.go +0 -176
  153. package/internal/version/version.go +0 -33
  154. package/ledger/certs.go +0 -164
  155. package/ledger/chainsync.go +0 -504
  156. package/ledger/delta.go +0 -99
  157. package/ledger/eras/allegra.go +0 -154
  158. package/ledger/eras/alonzo.go +0 -156
  159. package/ledger/eras/babbage.go +0 -154
  160. package/ledger/eras/byron.go +0 -42
  161. package/ledger/eras/conway.go +0 -166
  162. package/ledger/eras/eras.go +0 -44
  163. package/ledger/eras/mary.go +0 -154
  164. package/ledger/eras/shelley.go +0 -164
  165. package/ledger/error.go +0 -19
  166. package/ledger/event.go +0 -50
  167. package/ledger/metrics.go +0 -53
  168. package/ledger/queries.go +0 -258
  169. package/ledger/slot.go +0 -127
  170. package/ledger/slot_test.go +0 -147
  171. package/ledger/state.go +0 -821
  172. package/ledger/view.go +0 -73
  173. package/localstatequery.go +0 -50
  174. package/localtxmonitor.go +0 -44
  175. package/localtxsubmission.go +0 -52
  176. package/mempool/consumer.go +0 -98
  177. package/mempool/mempool.go +0 -322
  178. package/node.go +0 -320
  179. package/peergov/event.go +0 -27
  180. package/peergov/peer.go +0 -67
  181. package/peergov/peergov.go +0 -290
  182. package/peersharing.go +0 -70
  183. package/preview-local-topology.json +0 -23
  184. package/topology/topology.go +0 -69
  185. package/topology/topology_test.go +0 -179
  186. package/tracing.go +0 -65
  187. package/txsubmission.go +0 -233
  188. package/utxorpc/query.go +0 -311
  189. package/utxorpc/submit.go +0 -395
  190. package/utxorpc/sync.go +0 -276
  191. package/utxorpc/utxorpc.go +0 -166
  192. package/utxorpc/watch.go +0 -310
package/utxorpc/query.go DELETED
@@ -1,311 +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 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
- }
package/utxorpc/submit.go DELETED
@@ -1,395 +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 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
- }