@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/ledger/queries.go DELETED
@@ -1,258 +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 ledger
16
-
17
- import (
18
- "errors"
19
- "fmt"
20
- "math/big"
21
-
22
- "github.com/blinklabs-io/dingo/database"
23
- "github.com/blinklabs-io/dingo/ledger/eras"
24
- "github.com/blinklabs-io/gouroboros/cbor"
25
- "github.com/blinklabs-io/gouroboros/ledger"
26
- olocalstatequery "github.com/blinklabs-io/gouroboros/protocol/localstatequery"
27
- )
28
-
29
- func (ls *LedgerState) Query(query any) (any, error) {
30
- switch q := query.(type) {
31
- case *olocalstatequery.BlockQuery:
32
- return ls.queryBlock(q)
33
- case *olocalstatequery.SystemStartQuery:
34
- return ls.querySystemStart()
35
- case *olocalstatequery.ChainBlockNoQuery:
36
- return ls.queryChainBlockNo()
37
- case *olocalstatequery.ChainPointQuery:
38
- return ls.queryChainPoint()
39
- default:
40
- return nil, fmt.Errorf("unsupported query type: %T", q)
41
- }
42
- }
43
-
44
- func (ls *LedgerState) queryBlock(
45
- query *olocalstatequery.BlockQuery,
46
- ) (any, error) {
47
- switch q := query.Query.(type) {
48
- case *olocalstatequery.HardForkQuery:
49
- return ls.queryHardFork(q)
50
- case *olocalstatequery.ShelleyQuery:
51
- return ls.queryShelley(q)
52
- default:
53
- return nil, fmt.Errorf("unsupported query type: %T", q)
54
- }
55
- }
56
-
57
- func (ls *LedgerState) querySystemStart() (any, error) {
58
- shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
59
- if shelleyGenesis == nil {
60
- return nil, errors.New(
61
- "unable to get shelley era genesis for system start",
62
- )
63
- }
64
- ret := olocalstatequery.SystemStartResult{
65
- Year: *big.NewInt(int64(shelleyGenesis.SystemStart.Year())),
66
- Day: shelleyGenesis.SystemStart.YearDay(),
67
- Picoseconds: *big.NewInt(int64(shelleyGenesis.SystemStart.Nanosecond() * 1000)),
68
- }
69
- return ret, nil
70
- }
71
-
72
- func (ls *LedgerState) queryChainBlockNo() (any, error) {
73
- ret := []any{
74
- 1, // TODO: figure out what this value is (#393)
75
- ls.currentTip.BlockNumber,
76
- }
77
- return ret, nil
78
- }
79
-
80
- func (ls *LedgerState) queryChainPoint() (any, error) {
81
- return ls.currentTip.Point, nil
82
- }
83
-
84
- func (ls *LedgerState) queryHardFork(
85
- query *olocalstatequery.HardForkQuery,
86
- ) (any, error) {
87
- switch q := query.Query.(type) {
88
- case *olocalstatequery.HardForkCurrentEraQuery:
89
- return ls.currentEra.Id, nil
90
- case *olocalstatequery.HardForkEraHistoryQuery:
91
- return ls.queryHardForkEraHistory()
92
- default:
93
- return nil, fmt.Errorf("unsupported query type: %T", q)
94
- }
95
- }
96
-
97
- func (ls *LedgerState) queryHardForkEraHistory() (any, error) {
98
- retData := []any{}
99
- timespan := big.NewInt(0)
100
- var epochs []database.Epoch
101
- var era eras.EraDesc
102
- var err error
103
- var tmpStart, tmpEnd []any
104
- var tmpEpoch database.Epoch
105
- var tmpEra, tmpParams []any
106
- var epochSlotLength, epochLength uint
107
- var idx int
108
- for _, era = range eras.Eras {
109
- epochSlotLength, epochLength, err = era.EpochLengthFunc(
110
- ls.config.CardanoNodeConfig,
111
- )
112
- if err != nil {
113
- return nil, err
114
- }
115
- epochs, err = ls.db.GetEpochsByEra(era.Id, nil)
116
- if err != nil {
117
- return nil, err
118
- }
119
- tmpStart = []any{0, 0, 0}
120
- tmpEnd = tmpStart
121
- tmpParams = []any{
122
- epochLength,
123
- epochSlotLength,
124
- []any{
125
- 0,
126
- 0,
127
- []any{0},
128
- },
129
- 0,
130
- }
131
- for idx, tmpEpoch = range epochs {
132
- // Update era start
133
- if idx == 0 {
134
- tmpStart = []any{
135
- new(big.Int).Set(timespan),
136
- tmpEpoch.StartSlot,
137
- tmpEpoch.EpochId,
138
- }
139
- }
140
- // Add epoch length in picoseconds to timespan
141
- timespan.Add(
142
- timespan,
143
- new(big.Int).SetUint64(
144
- uint64(
145
- tmpEpoch.SlotLength*tmpEpoch.LengthInSlots*1_000_000_000,
146
- ),
147
- ),
148
- )
149
- // Update era end
150
- if idx == len(epochs)-1 {
151
- tmpEnd = []any{
152
- new(big.Int).Set(timespan),
153
- tmpEpoch.StartSlot + uint64(tmpEpoch.LengthInSlots),
154
- tmpEpoch.EpochId + 1,
155
- }
156
- }
157
- }
158
- tmpEra = []any{
159
- tmpStart,
160
- tmpEnd,
161
- tmpParams,
162
- }
163
- retData = append(retData, tmpEra)
164
- }
165
- return cbor.IndefLengthList(retData), nil
166
- }
167
-
168
- func (ls *LedgerState) queryShelley(
169
- query *olocalstatequery.ShelleyQuery,
170
- ) (any, error) {
171
- switch q := query.Query.(type) {
172
- case *olocalstatequery.ShelleyEpochNoQuery:
173
- return []any{ls.currentEpoch.EpochId}, nil
174
- case *olocalstatequery.ShelleyCurrentProtocolParamsQuery:
175
- return []any{ls.currentPParams}, nil
176
- case *olocalstatequery.ShelleyGenesisConfigQuery:
177
- return ls.queryShelleyGenesisConfig()
178
- case *olocalstatequery.ShelleyUtxoByAddressQuery:
179
- return ls.queryShelleyUtxoByAddress(q.Addrs)
180
- case *olocalstatequery.ShelleyUtxoByTxinQuery:
181
- return ls.queryShelleyUtxoByTxIn(q.TxIns)
182
- // TODO (#394)
183
- /*
184
- case *olocalstatequery.ShelleyLedgerTipQuery:
185
- case *olocalstatequery.ShelleyNonMyopicMemberRewardsQuery:
186
- case *olocalstatequery.ShelleyProposedProtocolParamsUpdatesQuery:
187
- case *olocalstatequery.ShelleyStakeDistributionQuery:
188
- case *olocalstatequery.ShelleyUtxoWholeQuery:
189
- case *olocalstatequery.ShelleyDebugEpochStateQuery:
190
- case *olocalstatequery.ShelleyCborQuery:
191
- case *olocalstatequery.ShelleyFilteredDelegationAndRewardAccountsQuery:
192
- case *olocalstatequery.ShelleyDebugNewEpochStateQuery:
193
- case *olocalstatequery.ShelleyDebugChainDepStateQuery:
194
- case *olocalstatequery.ShelleyRewardProvenanceQuery:
195
- case *olocalstatequery.ShelleyStakePoolsQuery:
196
- case *olocalstatequery.ShelleyStakePoolParamsQuery:
197
- case *olocalstatequery.ShelleyRewardInfoPoolsQuery:
198
- case *olocalstatequery.ShelleyPoolStateQuery:
199
- case *olocalstatequery.ShelleyStakeSnapshotsQuery:
200
- case *olocalstatequery.ShelleyPoolDistrQuery:
201
- */
202
- default:
203
- return nil, fmt.Errorf("unsupported query type: %T", q)
204
- }
205
- }
206
-
207
- func (ls *LedgerState) queryShelleyGenesisConfig() (any, error) {
208
- shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
209
- return []any{shelleyGenesis}, nil
210
- }
211
-
212
- func (ls *LedgerState) queryShelleyUtxoByAddress(
213
- addrs []ledger.Address,
214
- ) (any, error) {
215
- ret := make(map[olocalstatequery.UtxoId]ledger.TransactionOutput)
216
- // TODO: support multiple addresses (#391)
217
- utxos, err := ls.db.UtxosByAddress(addrs[0], nil)
218
- if err != nil {
219
- return nil, err
220
- }
221
- for _, utxo := range utxos {
222
- txOut, err := utxo.Decode()
223
- if err != nil {
224
- return nil, err
225
- }
226
- utxoId := olocalstatequery.UtxoId{
227
- Hash: ledger.NewBlake2b256(utxo.TxId),
228
- Idx: int(utxo.OutputIdx),
229
- }
230
- ret[utxoId] = txOut
231
- }
232
- return []any{ret}, nil
233
- }
234
-
235
- func (ls *LedgerState) queryShelleyUtxoByTxIn(
236
- txIns []ledger.ShelleyTransactionInput,
237
- ) (any, error) {
238
- ret := make(map[olocalstatequery.UtxoId]ledger.TransactionOutput)
239
- // TODO: support multiple TxIns (#392)
240
- utxo, err := ls.db.UtxoByRef(
241
- txIns[0].Id().Bytes(),
242
- txIns[0].Index(),
243
- nil,
244
- )
245
- if err != nil {
246
- return nil, err
247
- }
248
- txOut, err := utxo.Decode()
249
- if err != nil {
250
- return nil, err
251
- }
252
- utxoId := olocalstatequery.UtxoId{
253
- Hash: ledger.NewBlake2b256(utxo.TxId),
254
- Idx: int(utxo.OutputIdx),
255
- }
256
- ret[utxoId] = txOut
257
- return []any{ret}, nil
258
- }
package/ledger/slot.go DELETED
@@ -1,127 +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 ledger
16
-
17
- import (
18
- "errors"
19
- "math"
20
- "time"
21
-
22
- "github.com/blinklabs-io/dingo/database"
23
- )
24
-
25
- // SlotToTime returns the current time for a given slot based on known epochs
26
- func (ls *LedgerState) SlotToTime(slot uint64) (time.Time, error) {
27
- if slot > math.MaxInt64 {
28
- return time.Time{}, errors.New("slot is larger than time.Duration")
29
- }
30
- shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
31
- if shelleyGenesis == nil {
32
- return time.Time{}, errors.New("could not get genesis config")
33
- }
34
- slotTime := shelleyGenesis.SystemStart
35
- // Special case for chain genesis
36
- if slot == 0 {
37
- return slotTime, nil
38
- }
39
- foundSlot := false
40
- for _, epoch := range ls.epochCache {
41
- if epoch.StartSlot > math.MaxInt64 ||
42
- epoch.LengthInSlots > math.MaxInt64 ||
43
- epoch.SlotLength > math.MaxInt64 {
44
- return time.Time{}, errors.New(
45
- "epoch slot values are larger than time.Duration",
46
- )
47
- }
48
- if slot < epoch.StartSlot+uint64(epoch.LengthInSlots) {
49
- slotTime = slotTime.Add(
50
- time.Duration(
51
- int64(slot)-int64(epoch.StartSlot),
52
- ) * (time.Duration(epoch.SlotLength) * time.Millisecond),
53
- )
54
- foundSlot = true
55
- break
56
- }
57
- slotTime = slotTime.Add(
58
- time.Duration(
59
- epoch.LengthInSlots,
60
- ) * (time.Duration(epoch.SlotLength) * time.Millisecond),
61
- )
62
- }
63
- if !foundSlot {
64
- return slotTime, errors.New("slot not found in known epochs")
65
- }
66
- return slotTime, nil
67
- }
68
-
69
- // TimeToSlot returns the slot number for a given time based on known epochs
70
- func (ls *LedgerState) TimeToSlot(t time.Time) (uint64, error) {
71
- shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
72
- if shelleyGenesis == nil {
73
- return 0, errors.New("could not get genesis config")
74
- }
75
- epochStartTime := shelleyGenesis.SystemStart
76
- // Special case for chain genesis
77
- if t.Equal(epochStartTime) {
78
- return 0, nil
79
- }
80
- var timeSlot uint64
81
- foundTime := false
82
- for _, epoch := range ls.epochCache {
83
- if epoch.LengthInSlots > math.MaxInt64 ||
84
- epoch.SlotLength > math.MaxInt64 {
85
- return 0, errors.New(
86
- "epoch slot values are larger than time.Duration",
87
- )
88
- }
89
- slotDuration := time.Duration(epoch.SlotLength) * time.Millisecond
90
- if slotDuration < 0 {
91
- return 0, errors.New("slot duration is negative")
92
- }
93
- epochEndTime := epochStartTime.Add(
94
- time.Duration(epoch.LengthInSlots) * slotDuration,
95
- )
96
- if (t.Equal(epochStartTime) || t.After(epochStartTime)) &&
97
- t.Before(epochEndTime) {
98
- // Figure out how far into the epoch the specified time is
99
- timeDiff := t.Sub(epochStartTime)
100
- // nolint:gosec
101
- // This will never overflow using 2 positive int64 values, but gosec seems determined
102
- // to complain about it
103
- timeSlot += uint64(timeDiff / slotDuration)
104
- foundTime = true
105
- break
106
- }
107
- epochStartTime = epochEndTime
108
- timeSlot += uint64(epoch.LengthInSlots)
109
- }
110
- if !foundTime {
111
- return timeSlot, errors.New("time not found in known epochs")
112
- }
113
- return timeSlot, nil
114
- }
115
-
116
- // SlotToEpoch returns a known epoch by slot number
117
- func (ls *LedgerState) SlotToEpoch(slot uint64) (database.Epoch, error) {
118
- for _, epoch := range ls.epochCache {
119
- if slot < epoch.StartSlot {
120
- continue
121
- }
122
- if slot < epoch.StartSlot+uint64(epoch.LengthInSlots) {
123
- return epoch, nil
124
- }
125
- }
126
- return database.Epoch{}, errors.New("slot not found in known epochs")
127
- }
@@ -1,147 +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 ledger
16
-
17
- import (
18
- "strings"
19
- "testing"
20
- "time"
21
-
22
- "github.com/blinklabs-io/dingo/config/cardano"
23
- "github.com/blinklabs-io/dingo/database"
24
- )
25
-
26
- func TestSlotCalc(t *testing.T) {
27
- testLedgerState := &LedgerState{
28
- epochCache: []database.Epoch{
29
- {
30
- EpochId: 0,
31
- StartSlot: 0,
32
- SlotLength: 1000,
33
- LengthInSlots: 86400,
34
- },
35
- {
36
- EpochId: 1,
37
- StartSlot: 86400,
38
- SlotLength: 1000,
39
- LengthInSlots: 86400,
40
- },
41
- {
42
- EpochId: 2,
43
- StartSlot: 172800,
44
- SlotLength: 1000,
45
- LengthInSlots: 86400,
46
- },
47
- {
48
- EpochId: 3,
49
- StartSlot: 259200,
50
- SlotLength: 1000,
51
- LengthInSlots: 86400,
52
- },
53
- {
54
- EpochId: 4,
55
- StartSlot: 345600,
56
- SlotLength: 1000,
57
- LengthInSlots: 86400,
58
- },
59
- {
60
- EpochId: 5,
61
- StartSlot: 432000,
62
- SlotLength: 1000,
63
- LengthInSlots: 86400,
64
- },
65
- },
66
- config: LedgerStateConfig{
67
- CardanoNodeConfig: &cardano.CardanoNodeConfig{},
68
- },
69
- }
70
- testShelleyGenesis := `{"systemStart": "2022-10-25T00:00:00Z"}`
71
- if err := testLedgerState.config.CardanoNodeConfig.LoadShelleyGenesisFromReader(strings.NewReader(testShelleyGenesis)); err != nil {
72
- t.Fatalf("unexpected error loading cardano node config: %s", err)
73
- }
74
- testDefs := []struct {
75
- slot uint64
76
- slotTime time.Time
77
- epoch uint64
78
- }{
79
- {
80
- slot: 0,
81
- slotTime: time.Date(2022, time.October, 25, 0, 0, 0, 0, time.UTC),
82
- epoch: 0,
83
- },
84
- {
85
- slot: 86399,
86
- slotTime: time.Date(
87
- 2022,
88
- time.October,
89
- 25,
90
- 23,
91
- 59,
92
- 59,
93
- 0,
94
- time.UTC,
95
- ),
96
- epoch: 0,
97
- },
98
- {
99
- slot: 86400,
100
- slotTime: time.Date(2022, time.October, 26, 0, 0, 0, 0, time.UTC),
101
- epoch: 1,
102
- },
103
- {
104
- slot: 432001,
105
- slotTime: time.Date(2022, time.October, 30, 0, 0, 1, 0, time.UTC),
106
- epoch: 5,
107
- },
108
- }
109
- for _, testDef := range testDefs {
110
- // Slot to time
111
- tmpSlotToTime, err := testLedgerState.SlotToTime(testDef.slot)
112
- if err != nil {
113
- t.Errorf("unexpected error converting slot to time: %s", err)
114
- }
115
- if !tmpSlotToTime.Equal(testDef.slotTime) {
116
- t.Errorf(
117
- "did not get expected time from slot: got %s, wanted %s",
118
- tmpSlotToTime,
119
- testDef.slotTime,
120
- )
121
- }
122
- // Time to slot
123
- tmpTimeToSlot, err := testLedgerState.TimeToSlot(testDef.slotTime)
124
- if err != nil {
125
- t.Errorf("unexpected error converting time to slot: %s", err)
126
- }
127
- if tmpTimeToSlot != testDef.slot {
128
- t.Errorf(
129
- "did not get expected slot from time: got %d, wanted %d",
130
- tmpTimeToSlot,
131
- testDef.slot,
132
- )
133
- }
134
- // Slot to epoch
135
- tmpSlotToEpoch, err := testLedgerState.SlotToEpoch(testDef.slot)
136
- if err != nil {
137
- t.Errorf("unexpected error getting epoch from slot: %s", err)
138
- }
139
- if tmpSlotToEpoch.EpochId != testDef.epoch {
140
- t.Errorf(
141
- "did not get expected epoch from slot: got %d, wanted %d",
142
- tmpSlotToEpoch.EpochId,
143
- testDef.epoch,
144
- )
145
- }
146
- }
147
- }