@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
@@ -1,77 +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 database
16
-
17
- import (
18
- "fmt"
19
- )
20
-
21
- // CommitTimestampError contains the timestamps of the metadata and blob stores
22
- type CommitTimestampError struct {
23
- MetadataTimestamp int64
24
- BlobTimestamp int64
25
- }
26
-
27
- // Error returns the stringified error
28
- func (e CommitTimestampError) Error() string {
29
- return fmt.Sprintf(
30
- "commit timestamp mismatch: %d (metadata) != %d (blob)",
31
- e.MetadataTimestamp,
32
- e.BlobTimestamp,
33
- )
34
- }
35
-
36
- func (b *Database) checkCommitTimestamp() error {
37
- // Get value from metadata
38
- metadataTimestamp, metadataErr := b.Metadata().GetCommitTimestamp()
39
- if metadataErr != nil {
40
- return fmt.Errorf(
41
- "failed to get metadata timestamp from plugin: %w",
42
- metadataErr,
43
- )
44
- }
45
- // No timestamp in the database
46
- if metadataTimestamp <= 0 {
47
- return nil
48
- }
49
- // Get value from blob
50
- blobTimestamp, blobErr := b.Blob().GetCommitTimestamp()
51
- if blobErr != nil {
52
- return fmt.Errorf(
53
- "failed to get blob timestamp from plugin: %w",
54
- blobErr,
55
- )
56
- }
57
- // Compare values
58
- if blobTimestamp != metadataTimestamp {
59
- return CommitTimestampError{
60
- MetadataTimestamp: metadataTimestamp,
61
- BlobTimestamp: blobTimestamp,
62
- }
63
- }
64
- return nil
65
- }
66
-
67
- func (b *Database) updateCommitTimestamp(txn *Txn, timestamp int64) error {
68
- // Update metadata
69
- if err := b.Metadata().SetCommitTimestamp(txn.Metadata(), timestamp); err != nil {
70
- return err
71
- }
72
- // Update blob
73
- if err := b.Blob().SetCommitTimestamp(txn.Blob(), timestamp); err != nil {
74
- return err
75
- }
76
- return nil
77
- }
@@ -1,118 +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 database
16
-
17
- import (
18
- "errors"
19
- "io"
20
- "log/slog"
21
-
22
- "github.com/blinklabs-io/dingo/database/plugin/blob"
23
- "github.com/blinklabs-io/dingo/database/plugin/metadata"
24
- )
25
-
26
- // Database represents our data storage services
27
- type Database struct {
28
- logger *slog.Logger
29
- blob blob.BlobStore
30
- metadata metadata.MetadataStore
31
- dataDir string
32
- }
33
-
34
- // Blob returns the underling blob store instance
35
- func (d *Database) Blob() blob.BlobStore {
36
- return d.blob
37
- }
38
-
39
- // DataDir returns the path to the data directory used for storage
40
- func (d *Database) DataDir() string {
41
- return d.dataDir
42
- }
43
-
44
- // Logger returns the logger instance
45
- func (d *Database) Logger() *slog.Logger {
46
- return d.logger
47
- }
48
-
49
- // Metadata returns the underlying metadata store instance
50
- func (d *Database) Metadata() metadata.MetadataStore {
51
- return d.metadata
52
- }
53
-
54
- // Transaction starts a new database transaction and returns a handle to it
55
- func (d *Database) Transaction(readWrite bool) *Txn {
56
- return NewTxn(d, readWrite)
57
- }
58
-
59
- // BlobTxn starts a new blob-only database transaction and returns a handle to it
60
- func (d *Database) BlobTxn(readWrite bool) *Txn {
61
- return NewBlobOnlyTxn(d, readWrite)
62
- }
63
-
64
- // MetadataTxn starts a new metadata-only database transaction and returns a handle to it
65
- func (d *Database) MetadataTxn(readWrite bool) *Txn {
66
- return NewMetadataOnlyTxn(d, readWrite)
67
- }
68
-
69
- // Close cleans up the database connections
70
- func (d *Database) Close() error {
71
- var err error
72
- // Close metadata
73
- metadataErr := d.Metadata().Close()
74
- err = errors.Join(err, metadataErr)
75
- // Close blob
76
- blobErr := d.Blob().Close()
77
- err = errors.Join(err, blobErr)
78
- return err
79
- }
80
-
81
- func (d *Database) init() error {
82
- if d.logger == nil {
83
- // Create logger to throw away logs
84
- // We do this so we don't have to add guards around every log operation
85
- d.logger = slog.New(slog.NewJSONHandler(io.Discard, nil))
86
- }
87
- // Check commit timestamp
88
- if err := d.checkCommitTimestamp(); err != nil {
89
- return err
90
- }
91
- return nil
92
- }
93
-
94
- // New creates a new database instance with optional persistence using the provided data directory
95
- func New(
96
- logger *slog.Logger,
97
- dataDir string,
98
- ) (*Database, error) {
99
- metadataDb, err := metadata.New("sqlite", dataDir, logger)
100
- if err != nil {
101
- return nil, err
102
- }
103
- blobDb, err := blob.New("badger", dataDir, logger)
104
- if err != nil {
105
- return nil, err
106
- }
107
- db := &Database{
108
- logger: logger,
109
- blob: blobDb,
110
- metadata: metadataDb,
111
- dataDir: dataDir,
112
- }
113
- if err := db.init(); err != nil {
114
- // Database is available for recovery, so return it with error
115
- return db, err
116
- }
117
- return db, nil
118
- }
@@ -1,62 +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 database_test
16
-
17
- import (
18
- "testing"
19
- "time"
20
-
21
- "github.com/blinklabs-io/dingo/database"
22
- "gorm.io/gorm"
23
- )
24
-
25
- type TestTable struct {
26
- gorm.Model
27
- }
28
-
29
- // TestInMemorySqliteMultipleTransaction tests that our sqlite connection allows multiple
30
- // concurrent transactions when using in-memory mode. This requires special URI flags, and
31
- // this is mostly making sure that we don't lose them
32
- func TestInMemorySqliteMultipleTransaction(t *testing.T) {
33
- var db *database.Database
34
- doQuery := func(sleep time.Duration) error {
35
- txn := db.Metadata().Transaction()
36
- if result := txn.First(&TestTable{}); result.Error != nil {
37
- return result.Error
38
- }
39
- time.Sleep(sleep)
40
- if result := txn.Commit(); result.Error != nil {
41
- return result.Error
42
- }
43
- return nil
44
- }
45
- db, err := database.New(nil, "") // in-memory
46
- if err != nil {
47
- t.Fatalf("unexpected error: %s", err)
48
- }
49
- if err := db.Metadata().DB().AutoMigrate(&TestTable{}); err != nil {
50
- t.Fatalf("unexpected error: %s", err)
51
- }
52
- if result := db.Metadata().DB().Create(&TestTable{}); result.Error != nil {
53
- t.Fatalf("unexpected error: %s", result.Error)
54
- }
55
- // The linter calls us on the lack of error checking, but it's a goroutine...
56
- //nolint:errcheck
57
- go doQuery(5 * time.Second)
58
- time.Sleep(1 * time.Second)
59
- if err := doQuery(0); err != nil {
60
- t.Fatalf("unexpected error: %s", err)
61
- }
62
- }
package/database/drep.go DELETED
@@ -1,47 +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 database
16
-
17
- import (
18
- lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
19
- )
20
-
21
- // SetRegistrationDrep saves a registration drep certificate
22
- func (d *Database) SetRegistrationDrep(
23
- cert *lcommon.RegistrationDrepCertificate,
24
- slot, deposit uint64,
25
- txn *Txn,
26
- ) error {
27
- return d.metadata.SetRegistrationDrep(
28
- cert,
29
- slot,
30
- deposit,
31
- txn.Metadata(),
32
- )
33
- }
34
-
35
- // SetDeregistrationDrep saves a deregistration drep certificate
36
- func (d *Database) SetDeregistrationDrep(
37
- cert *lcommon.DeregistrationDrepCertificate,
38
- slot, deposit uint64,
39
- txn *Txn,
40
- ) error {
41
- return d.metadata.SetDeregistrationDrep(
42
- cert,
43
- slot,
44
- deposit,
45
- txn.Metadata(),
46
- )
47
- }
package/database/epoch.go DELETED
@@ -1,121 +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 database
16
-
17
- type Epoch struct {
18
- ID uint `gorm:"primarykey"`
19
- // NOTE: we would normally use this as the primary key, but GORM doesn't
20
- // like a primary key value of 0
21
- EpochId uint64 `gorm:"uniqueIndex"`
22
- StartSlot uint64
23
- Nonce []byte
24
- EraId uint
25
- SlotLength uint
26
- LengthInSlots uint
27
- }
28
-
29
- func (Epoch) TableName() string {
30
- return "epoch"
31
- }
32
-
33
- func (d *Database) GetEpochLatest(txn *Txn) (Epoch, error) {
34
- tmpEpoch := Epoch{}
35
- if txn == nil {
36
- latestEpoch, err := d.metadata.GetEpochLatest(nil)
37
- if err != nil {
38
- return tmpEpoch, err
39
- }
40
- tmpEpoch = Epoch(latestEpoch)
41
- } else {
42
- latestEpoch, err := d.metadata.GetEpochLatest(txn.Metadata())
43
- if err != nil {
44
- return tmpEpoch, err
45
- }
46
- tmpEpoch = Epoch(latestEpoch)
47
- }
48
- return tmpEpoch, nil
49
- }
50
-
51
- func (d *Database) GetEpochsByEra(eraId uint, txn *Txn) ([]Epoch, error) {
52
- tmpEpochs := []Epoch{}
53
- if txn == nil {
54
- epochs, err := d.metadata.GetEpochsByEra(eraId, nil)
55
- if err != nil {
56
- return tmpEpochs, err
57
- }
58
- for _, epoch := range epochs {
59
- tmpEpochs = append(tmpEpochs, Epoch(epoch))
60
- }
61
- } else {
62
- epochs, err := txn.db.metadata.GetEpochsByEra(eraId, txn.Metadata())
63
- if err != nil {
64
- return tmpEpochs, err
65
- }
66
- for _, epoch := range epochs {
67
- tmpEpochs = append(tmpEpochs, Epoch(epoch))
68
- }
69
- }
70
- return tmpEpochs, nil
71
- }
72
-
73
- func (d *Database) GetEpochs(txn *Txn) ([]Epoch, error) {
74
- tmpEpochs := []Epoch{}
75
- if txn == nil {
76
- epochs, err := d.metadata.GetEpochs(nil)
77
- if err != nil {
78
- return tmpEpochs, err
79
- }
80
- for _, epoch := range epochs {
81
- tmpEpochs = append(tmpEpochs, Epoch(epoch))
82
- }
83
- } else {
84
- epochs, err := txn.db.metadata.GetEpochs(txn.Metadata())
85
- if err != nil {
86
- return tmpEpochs, err
87
- }
88
- for _, epoch := range epochs {
89
- tmpEpochs = append(tmpEpochs, Epoch(epoch))
90
- }
91
- }
92
- return tmpEpochs, nil
93
- }
94
-
95
- func (d *Database) SetEpoch(
96
- slot, epoch uint64,
97
- nonce []byte,
98
- era, slotLength, lengthInSlots uint,
99
- txn *Txn,
100
- ) error {
101
- if txn == nil {
102
- err := d.metadata.SetEpoch(
103
- slot,
104
- epoch,
105
- nonce,
106
- era,
107
- slotLength,
108
- lengthInSlots,
109
- nil,
110
- )
111
- if err != nil {
112
- return err
113
- }
114
- } else {
115
- err := d.metadata.SetEpoch(slot, epoch, nonce, era, slotLength, lengthInSlots, txn.Metadata())
116
- if err != nil {
117
- return err
118
- }
119
- }
120
- return nil
121
- }
@@ -1,182 +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 immutable
16
-
17
- import (
18
- "errors"
19
- "fmt"
20
- "math"
21
- "os"
22
-
23
- "github.com/blinklabs-io/gouroboros/cbor"
24
- )
25
-
26
- const (
27
- chunkFileExtension = ".chunk"
28
- )
29
-
30
- type chunk struct {
31
- file *os.File
32
- fileSize int64
33
- secondary *secondaryIndex
34
- currentEntry *secondaryIndexEntry
35
- nextEntry *secondaryIndexEntry
36
- }
37
-
38
- func newChunk() *chunk {
39
- return &chunk{}
40
- }
41
-
42
- func (c *chunk) Open(path string, secondary *secondaryIndex) error {
43
- f, err := os.Open(path)
44
- if err != nil {
45
- return err
46
- }
47
- c.file = f
48
- c.secondary = secondary
49
- if stat, err := f.Stat(); err != nil {
50
- return err
51
- } else {
52
- c.fileSize = stat.Size()
53
- }
54
- currentEntry, err := secondary.Next()
55
- if err != nil {
56
- return err
57
- }
58
- c.currentEntry = currentEntry
59
- nextEntry, err := secondary.Next()
60
- if err != nil {
61
- return err
62
- }
63
- c.nextEntry = nextEntry
64
- return nil
65
- }
66
-
67
- func (c *chunk) Close() error {
68
- if err := c.secondary.Close(); err != nil {
69
- return err
70
- }
71
- return c.file.Close()
72
- }
73
-
74
- func (c *chunk) Next() (*Block, error) {
75
- if c.currentEntry == nil {
76
- return nil, nil
77
- }
78
- if c.nextEntry == nil {
79
- if c.currentEntry.BlockOffset > math.MaxInt64 {
80
- return nil, errors.New("current block offset integer overflow")
81
- }
82
- // This triggers even though we check it above
83
- // #nosec G115
84
- currOffset := int64(c.currentEntry.BlockOffset)
85
-
86
- // We've reached the last entry in the chunk, so we calculate
87
- // block size based on the size of the file
88
- blockSize := c.fileSize - currOffset
89
- blockData := make([]byte, blockSize)
90
- // Seek to offset
91
- if _, err := c.file.Seek(currOffset, 0); err != nil {
92
- return nil, err
93
- }
94
- n, err := c.file.Read(blockData)
95
- if err != nil {
96
- return nil, err
97
- }
98
- if int64(n) < blockSize {
99
- return nil, fmt.Errorf(
100
- "did not read expected amount of block data: expected %d, got %d",
101
- blockSize,
102
- n,
103
- )
104
- }
105
- blkType, blkBytes, err := c.unwrapBlock(blockData)
106
- if err != nil {
107
- return nil, err
108
- }
109
- ret := &Block{
110
- Type: blkType,
111
- Slot: c.currentEntry.BlockOrEbb,
112
- Hash: c.currentEntry.HeaderHash[:],
113
- IsEbb: c.currentEntry.IsEbb,
114
- Cbor: blkBytes[:],
115
- }
116
- c.currentEntry = nil
117
- c.nextEntry = nil
118
- return ret, nil
119
- } else {
120
- // Calculate block size based on the offsets for the current and next entries
121
- if c.currentEntry.BlockOffset > math.MaxInt64 {
122
- return nil, errors.New("current block offset integer overflow")
123
- }
124
- // This triggers even though we check it above
125
- // #nosec G115
126
- currOffset := int64(c.currentEntry.BlockOffset)
127
-
128
- if c.nextEntry.BlockOffset > math.MaxInt64 {
129
- return nil, errors.New("next block offset integer overflow")
130
- }
131
- // This triggers even though we check it above
132
- // #nosec G115
133
- nextOffset := int64(c.nextEntry.BlockOffset)
134
-
135
- blockSize := nextOffset - currOffset
136
- blockData := make([]byte, blockSize)
137
- // Seek to offset
138
- if _, err := c.file.Seek(currOffset, 0); err != nil {
139
- return nil, err
140
- }
141
- n, err := c.file.Read(blockData)
142
- if err != nil {
143
- return nil, err
144
- }
145
- if int64(n) < blockSize {
146
- return nil, fmt.Errorf(
147
- "did not read expected amount of block data: expected %d, got %d",
148
- blockSize,
149
- n,
150
- )
151
- }
152
- blkType, blkBytes, err := c.unwrapBlock(blockData)
153
- if err != nil {
154
- return nil, err
155
- }
156
- ret := &Block{
157
- Type: blkType,
158
- Slot: c.currentEntry.BlockOrEbb,
159
- Hash: c.currentEntry.HeaderHash[:],
160
- Cbor: blkBytes[:],
161
- }
162
- c.currentEntry = c.nextEntry
163
- nextEntry, err := c.secondary.Next()
164
- if err != nil {
165
- return nil, err
166
- }
167
- c.nextEntry = nextEntry
168
- return ret, nil
169
- }
170
- }
171
-
172
- func (c *chunk) unwrapBlock(data []byte) (uint, []byte, error) {
173
- tmpData := struct {
174
- cbor.StructAsArray
175
- BlockType uint
176
- BlockCbor cbor.RawMessage
177
- }{}
178
- if _, err := cbor.Decode(data, &tmpData); err != nil {
179
- return 0, nil, err
180
- }
181
- return tmpData.BlockType, []byte(tmpData.BlockCbor), nil
182
- }