Skip to content

Commit 1558fc7

Browse files
authored
Refactored types to force runtime registrations to be type dependent (#10147)
This resolves #10135 All enums are constrained by their owning type which forces package includsion and hence type registration. Added tests for each type to check the construction cycle.
1 parent 01e4969 commit 1558fc7

File tree

24 files changed

+354
-242
lines changed

24 files changed

+354
-242
lines changed

cmd/silkworm_api/snapshot_idx.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
1111
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
1212
"github.com/ledgerwatch/erigon/cmd/hack/tool/fromdb"
13+
coresnaptype "github.com/ledgerwatch/erigon/core/snaptype"
1314
"github.com/ledgerwatch/erigon/turbo/debug"
1415
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"
1516
"github.com/ledgerwatch/log/v3"
@@ -91,7 +92,7 @@ func buildIndex(cliCtx *cli.Context, dataDir string, snapshotPaths []string, min
9192
}
9293

9394
switch segment.Type.Enum() {
94-
case snaptype.Enums.Headers, snaptype.Enums.Bodies, snaptype.Enums.Transactions:
95+
case coresnaptype.Enums.Headers, coresnaptype.Enums.Bodies, coresnaptype.Enums.Transactions:
9596
g.Go(func() error {
9697
jobProgress := &background.Progress{}
9798
ps.Add(jobProgress)

cmd/snapshots/cmp/cmp.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ func cmp(cliCtx *cli.Context) error {
260260
})
261261
} else {
262262
for _, snapType := range snapTypes {
263-
if snapType.Enum() == snaptype.Enums.Headers {
263+
if snapType.Enum() == coresnaptype.Enums.Headers {
264264
funcs = append(funcs, func(ctx context.Context) (time.Duration, time.Duration, time.Duration, error) {
265265
return c.compareHeaders(ctx, h1ents, h2ents, headerWorkers, logger)
266266
})
267267
}
268268

269-
if snapType.Enum() == snaptype.Enums.Bodies {
269+
if snapType.Enum() == coresnaptype.Enums.Bodies {
270270
funcs = append(funcs, func(ctx context.Context) (time.Duration, time.Duration, time.Duration, error) {
271271
return c.compareBodies(ctx, b1ents, b2ents, bodyWorkers, logger)
272272
})
@@ -325,11 +325,11 @@ func splitEntries(files []fs.DirEntry, version snaptype.Version, firstBlock, las
325325
(firstBlock == 0 || snapInfo.From() >= firstBlock) &&
326326
(lastBlock == 0 || snapInfo.From() < lastBlock) {
327327

328-
if snapInfo.Type().Enum() == snaptype.Enums.Headers {
328+
if snapInfo.Type().Enum() == coresnaptype.Enums.Headers {
329329
hents = append(hents, ent)
330330
}
331331

332-
if snapInfo.Type().Enum() == snaptype.Enums.Bodies {
332+
if snapInfo.Type().Enum() == coresnaptype.Enums.Bodies {
333333
found := false
334334

335335
for _, bent := range bents {
@@ -345,7 +345,7 @@ func splitEntries(files []fs.DirEntry, version snaptype.Version, firstBlock, las
345345
}
346346
}
347347

348-
if snapInfo.Type().Enum() == snaptype.Enums.Transactions {
348+
if snapInfo.Type().Enum() == coresnaptype.Enums.Transactions {
349349
found := false
350350

351351
for _, bent := range bents {

core/snaptype/block_types.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,40 @@ func init() {
3535
snapcfg.RegisterKnownTypes(networkname.ChiadoChainName, ethereumTypes)
3636
}
3737

38+
var Enums = struct {
39+
snaptype.Enums
40+
Headers,
41+
Bodies,
42+
Transactions snaptype.Enum
43+
}{
44+
Enums: snaptype.Enums{},
45+
Headers: snaptype.MinCoreEnum,
46+
Bodies: snaptype.MinCoreEnum + 1,
47+
Transactions: snaptype.MinCoreEnum + 2,
48+
}
49+
50+
var Indexes = struct {
51+
HeaderHash,
52+
BodyHash,
53+
TxnHash,
54+
TxnHash2BlockNum snaptype.Index
55+
}{
56+
HeaderHash: snaptype.Index{Name: "headers"},
57+
BodyHash: snaptype.Index{Name: "bodies"},
58+
TxnHash: snaptype.Index{Name: "transactions"},
59+
TxnHash2BlockNum: snaptype.Index{Name: "transactions-to-block", Offset: 1},
60+
}
61+
3862
var (
3963
Headers = snaptype.RegisterType(
40-
snaptype.Enums.Headers,
64+
Enums.Headers,
65+
"headers",
4166
snaptype.Versions{
4267
Current: 1, //2,
4368
MinSupported: 1,
4469
},
4570
nil,
46-
[]snaptype.Index{snaptype.Indexes.HeaderHash},
71+
[]snaptype.Index{Indexes.HeaderHash},
4772
snaptype.IndexBuilderFunc(
4873
func(ctx context.Context, info snaptype.FileInfo, salt uint32, _ *chain.Config, tmpDir string, p *background.Progress, lvl log.Lvl, logger log.Logger) (err error) {
4974
hasher := crypto.NewKeccakState()
@@ -70,13 +95,14 @@ var (
7095
)
7196

7297
Bodies = snaptype.RegisterType(
73-
snaptype.Enums.Bodies,
98+
Enums.Bodies,
99+
"bodies",
74100
snaptype.Versions{
75101
Current: 1, //2,
76102
MinSupported: 1,
77103
},
78104
nil,
79-
[]snaptype.Index{snaptype.Indexes.BodyHash},
105+
[]snaptype.Index{Indexes.BodyHash},
80106
snaptype.IndexBuilderFunc(
81107
func(ctx context.Context, info snaptype.FileInfo, salt uint32, _ *chain.Config, tmpDir string, p *background.Progress, lvl log.Lvl, logger log.Logger) (err error) {
82108
num := make([]byte, 8)
@@ -98,13 +124,14 @@ var (
98124
)
99125

100126
Transactions = snaptype.RegisterType(
101-
snaptype.Enums.Transactions,
127+
Enums.Transactions,
128+
"transactions",
102129
snaptype.Versions{
103130
Current: 1, //2,
104131
MinSupported: 1,
105132
},
106133
nil,
107-
[]snaptype.Index{snaptype.Indexes.TxnHash, snaptype.Indexes.TxnHash2BlockNum},
134+
[]snaptype.Index{Indexes.TxnHash, Indexes.TxnHash2BlockNum},
108135
snaptype.IndexBuilderFunc(
109136
func(ctx context.Context, sn snaptype.FileInfo, salt uint32, chainConfig *chain.Config, tmpDir string, p *background.Progress, lvl log.Lvl, logger log.Logger) (err error) {
110137
defer func() {
@@ -162,7 +189,7 @@ var (
162189
BucketSize: 2000,
163190
LeafSize: 8,
164191
TmpDir: tmpDir,
165-
IndexFile: filepath.Join(sn.Dir(), sn.Type.IdxFileName(sn.Version, sn.From, sn.To, snaptype.Indexes.TxnHash2BlockNum)),
192+
IndexFile: filepath.Join(sn.Dir(), sn.Type.IdxFileName(sn.Version, sn.From, sn.To, Indexes.TxnHash2BlockNum)),
166193
BaseDataID: firstBlockNum,
167194
}, logger)
168195
if err != nil {

core/snaptype/block_types_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package snaptype_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ledgerwatch/erigon/core/snaptype"
7+
)
8+
9+
func TestEnumeration(t *testing.T) {
10+
11+
if snaptype.Headers.Enum() != snaptype.Enums.Headers {
12+
t.Fatal("enum mismatch", snaptype.Headers, snaptype.Headers.Enum(), snaptype.Enums.Headers)
13+
}
14+
15+
if snaptype.Bodies.Enum() != snaptype.Enums.Bodies {
16+
t.Fatal("enum mismatch", snaptype.Bodies, snaptype.Bodies.Enum(), snaptype.Enums.Bodies)
17+
}
18+
19+
if snaptype.Transactions.Enum() != snaptype.Enums.Transactions {
20+
t.Fatal("enum mismatch", snaptype.Transactions, snaptype.Transactions.Enum(), snaptype.Enums.Transactions)
21+
}
22+
23+
}
24+
25+
func TestNames(t *testing.T) {
26+
27+
if snaptype.Headers.Name() != snaptype.Enums.Headers.String() {
28+
t.Fatal("name mismatch", snaptype.Headers, snaptype.Headers.Name(), snaptype.Enums.Headers.String())
29+
}
30+
31+
if snaptype.Bodies.Name() != snaptype.Enums.Bodies.String() {
32+
t.Fatal("name mismatch", snaptype.Bodies, snaptype.Bodies.Name(), snaptype.Enums.Bodies.String())
33+
}
34+
35+
if snaptype.Transactions.Name() != snaptype.Enums.Transactions.String() {
36+
t.Fatal("name mismatch", snaptype.Transactions, snaptype.Transactions.Name(), snaptype.Enums.Transactions.String())
37+
}
38+
}

erigon-lib/chain/snapcfg/util.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (p Preverified) Typed(types []snaptype.Type) Preverified {
8585
include := false
8686

8787
for _, typ := range types {
88-
if typeName == typ.String() {
88+
if typeName == typ.Name() {
8989
preferredVersion = typ.Versions().Current
9090
minVersion = typ.Versions().MinSupported
9191
include = true
@@ -285,15 +285,15 @@ func (c Cfg) Seedable(info snaptype.FileInfo) bool {
285285
}
286286

287287
func (c Cfg) MergeLimit(t snaptype.Enum, fromBlock uint64) uint64 {
288-
hasType := t == snaptype.Enums.Headers
288+
hasType := t == snaptype.MinCoreEnum
289289

290290
for _, p := range c.Preverified {
291291
info, _, ok := snaptype.ParseFileName("", p.Name)
292292
if !ok {
293293
continue
294294
}
295295

296-
if info.Ext != ".seg" || (t != snaptype.Enums.Unknown && t != info.Type.Enum()) {
296+
if info.Ext != ".seg" || (t != snaptype.Unknown && t != info.Type.Enum()) {
297297
continue
298298
}
299299

@@ -322,7 +322,7 @@ func (c Cfg) MergeLimit(t snaptype.Enum, fromBlock uint64) uint64 {
322322
return snaptype.Erigon2MergeLimit
323323
}
324324

325-
return c.MergeLimit(snaptype.Enums.Headers, fromBlock)
325+
return c.MergeLimit(snaptype.MinCoreEnum, fromBlock)
326326
}
327327

328328
var knownPreverified = map[string]Preverified{
@@ -359,7 +359,7 @@ func MaxSeedableSegment(chain string, dir string) uint64 {
359359

360360
if list, err := snaptype.Segments(dir); err == nil {
361361
for _, info := range list {
362-
if Seedable(chain, info) && info.Type.Enum() == snaptype.Enums.Headers && info.To > max {
362+
if Seedable(chain, info) && info.Type.Enum() == snaptype.MinCoreEnum && info.To > max {
363363
max = info.To
364364
}
365365
}

erigon-lib/downloader/snaptype/caplin_types.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package snaptype
22

33
var (
44
BeaconBlocks = snapType{
5-
enum: Enums.BeaconBlocks,
5+
enum: CaplinEnums.BeaconBlocks,
6+
name: "beaconblocks",
67
versions: Versions{
78
Current: 1,
89
MinSupported: 1,
910
},
10-
indexes: []Index{Indexes.BeaconBlockSlot},
11+
indexes: []Index{CaplinIndexes.BeaconBlockSlot},
1112
}
1213
BlobSidecars = snapType{
13-
enum: Enums.BlobSidecars,
14+
enum: CaplinEnums.BlobSidecars,
15+
name: "blobsidecars",
1416
versions: Versions{
1517
Current: 1,
1618
MinSupported: 1,
1719
},
18-
indexes: []Index{Indexes.BlobSidecarSlot},
20+
indexes: []Index{CaplinIndexes.BlobSidecarSlot},
1921
}
2022

2123
CaplinSnapshotTypes = []Type{BeaconBlocks, BlobSidecars}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package snaptype_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
7+
)
8+
9+
func TestEnumeration(t *testing.T) {
10+
11+
if snaptype.BlobSidecars.Enum() != snaptype.CaplinEnums.BlobSidecars {
12+
t.Fatal("enum mismatch", snaptype.BlobSidecars, snaptype.BlobSidecars.Enum(), snaptype.CaplinEnums.BlobSidecars)
13+
}
14+
15+
if snaptype.BeaconBlocks.Enum() != snaptype.CaplinEnums.BeaconBlocks {
16+
t.Fatal("enum mismatch", snaptype.BeaconBlocks, snaptype.BeaconBlocks.Enum(), snaptype.CaplinEnums.BeaconBlocks)
17+
}
18+
}
19+
20+
func TestNames(t *testing.T) {
21+
22+
if snaptype.BeaconBlocks.Name() != snaptype.CaplinEnums.BeaconBlocks.String() {
23+
t.Fatal("name mismatch", snaptype.BeaconBlocks, snaptype.BeaconBlocks.Name(), snaptype.CaplinEnums.BeaconBlocks.String())
24+
}
25+
26+
if snaptype.BlobSidecars.Name() != snaptype.CaplinEnums.BlobSidecars.String() {
27+
t.Fatal("name mismatch", snaptype.BlobSidecars, snaptype.BlobSidecars.Name(), snaptype.CaplinEnums.BlobSidecars.String())
28+
}
29+
30+
}

erigon-lib/downloader/snaptype/files.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func FilterExt(in []FileInfo, expectExt string) (out []FileInfo) {
6060
}
6161

6262
slices.SortFunc(out, func(a, b FileInfo) int {
63-
if cmp := strings.Compare(a.Type.String(), b.Type.String()); cmp != 0 {
63+
if cmp := strings.Compare(a.Type.Name(), b.Type.Name()); cmp != 0 {
6464
return cmp
6565
}
6666

@@ -234,7 +234,8 @@ func (f FileInfo) CompareTo(o FileInfo) int {
234234
return res
235235
}
236236

237-
return strings.Compare(f.Type.String(), o.Type.String())
237+
// this is a lexical comparison (don't use enum)
238+
return strings.Compare(f.Type.Name(), o.Type.Name())
238239
}
239240

240241
func (f FileInfo) As(t Type) FileInfo {

0 commit comments

Comments
 (0)