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