Skip to content

Commit

Permalink
Refactored types to force runtime registrations to be type dependent (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
mh0lt authored Apr 30, 2024
1 parent 01e4969 commit 1558fc7
Show file tree
Hide file tree
Showing 24 changed files with 354 additions and 242 deletions.
3 changes: 2 additions & 1 deletion cmd/silkworm_api/snapshot_idx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/erigon/cmd/hack/tool/fromdb"
coresnaptype "github.com/ledgerwatch/erigon/core/snaptype"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"
"github.com/ledgerwatch/log/v3"
Expand Down Expand Up @@ -91,7 +92,7 @@ func buildIndex(cliCtx *cli.Context, dataDir string, snapshotPaths []string, min
}

switch segment.Type.Enum() {
case snaptype.Enums.Headers, snaptype.Enums.Bodies, snaptype.Enums.Transactions:
case coresnaptype.Enums.Headers, coresnaptype.Enums.Bodies, coresnaptype.Enums.Transactions:
g.Go(func() error {
jobProgress := &background.Progress{}
ps.Add(jobProgress)
Expand Down
10 changes: 5 additions & 5 deletions cmd/snapshots/cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ func cmp(cliCtx *cli.Context) error {
})
} else {
for _, snapType := range snapTypes {
if snapType.Enum() == snaptype.Enums.Headers {
if snapType.Enum() == coresnaptype.Enums.Headers {
funcs = append(funcs, func(ctx context.Context) (time.Duration, time.Duration, time.Duration, error) {
return c.compareHeaders(ctx, h1ents, h2ents, headerWorkers, logger)
})
}

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

if snapInfo.Type().Enum() == snaptype.Enums.Headers {
if snapInfo.Type().Enum() == coresnaptype.Enums.Headers {
hents = append(hents, ent)
}

if snapInfo.Type().Enum() == snaptype.Enums.Bodies {
if snapInfo.Type().Enum() == coresnaptype.Enums.Bodies {
found := false

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

if snapInfo.Type().Enum() == snaptype.Enums.Transactions {
if snapInfo.Type().Enum() == coresnaptype.Enums.Transactions {
found := false

for _, bent := range bents {
Expand Down
41 changes: 34 additions & 7 deletions core/snaptype/block_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,40 @@ func init() {
snapcfg.RegisterKnownTypes(networkname.ChiadoChainName, ethereumTypes)
}

var Enums = struct {
snaptype.Enums
Headers,
Bodies,
Transactions snaptype.Enum
}{
Enums: snaptype.Enums{},
Headers: snaptype.MinCoreEnum,
Bodies: snaptype.MinCoreEnum + 1,
Transactions: snaptype.MinCoreEnum + 2,
}

var Indexes = struct {
HeaderHash,
BodyHash,
TxnHash,
TxnHash2BlockNum snaptype.Index
}{
HeaderHash: snaptype.Index{Name: "headers"},
BodyHash: snaptype.Index{Name: "bodies"},
TxnHash: snaptype.Index{Name: "transactions"},
TxnHash2BlockNum: snaptype.Index{Name: "transactions-to-block", Offset: 1},
}

var (
Headers = snaptype.RegisterType(
snaptype.Enums.Headers,
Enums.Headers,
"headers",
snaptype.Versions{
Current: 1, //2,
MinSupported: 1,
},
nil,
[]snaptype.Index{snaptype.Indexes.HeaderHash},
[]snaptype.Index{Indexes.HeaderHash},
snaptype.IndexBuilderFunc(
func(ctx context.Context, info snaptype.FileInfo, salt uint32, _ *chain.Config, tmpDir string, p *background.Progress, lvl log.Lvl, logger log.Logger) (err error) {
hasher := crypto.NewKeccakState()
Expand All @@ -70,13 +95,14 @@ var (
)

Bodies = snaptype.RegisterType(
snaptype.Enums.Bodies,
Enums.Bodies,
"bodies",
snaptype.Versions{
Current: 1, //2,
MinSupported: 1,
},
nil,
[]snaptype.Index{snaptype.Indexes.BodyHash},
[]snaptype.Index{Indexes.BodyHash},
snaptype.IndexBuilderFunc(
func(ctx context.Context, info snaptype.FileInfo, salt uint32, _ *chain.Config, tmpDir string, p *background.Progress, lvl log.Lvl, logger log.Logger) (err error) {
num := make([]byte, 8)
Expand All @@ -98,13 +124,14 @@ var (
)

Transactions = snaptype.RegisterType(
snaptype.Enums.Transactions,
Enums.Transactions,
"transactions",
snaptype.Versions{
Current: 1, //2,
MinSupported: 1,
},
nil,
[]snaptype.Index{snaptype.Indexes.TxnHash, snaptype.Indexes.TxnHash2BlockNum},
[]snaptype.Index{Indexes.TxnHash, Indexes.TxnHash2BlockNum},
snaptype.IndexBuilderFunc(
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) {
defer func() {
Expand Down Expand Up @@ -162,7 +189,7 @@ var (
BucketSize: 2000,
LeafSize: 8,
TmpDir: tmpDir,
IndexFile: filepath.Join(sn.Dir(), sn.Type.IdxFileName(sn.Version, sn.From, sn.To, snaptype.Indexes.TxnHash2BlockNum)),
IndexFile: filepath.Join(sn.Dir(), sn.Type.IdxFileName(sn.Version, sn.From, sn.To, Indexes.TxnHash2BlockNum)),
BaseDataID: firstBlockNum,
}, logger)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions core/snaptype/block_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package snaptype_test

import (
"testing"

"github.com/ledgerwatch/erigon/core/snaptype"
)

func TestEnumeration(t *testing.T) {

if snaptype.Headers.Enum() != snaptype.Enums.Headers {
t.Fatal("enum mismatch", snaptype.Headers, snaptype.Headers.Enum(), snaptype.Enums.Headers)
}

if snaptype.Bodies.Enum() != snaptype.Enums.Bodies {
t.Fatal("enum mismatch", snaptype.Bodies, snaptype.Bodies.Enum(), snaptype.Enums.Bodies)
}

if snaptype.Transactions.Enum() != snaptype.Enums.Transactions {
t.Fatal("enum mismatch", snaptype.Transactions, snaptype.Transactions.Enum(), snaptype.Enums.Transactions)
}

}

func TestNames(t *testing.T) {

if snaptype.Headers.Name() != snaptype.Enums.Headers.String() {
t.Fatal("name mismatch", snaptype.Headers, snaptype.Headers.Name(), snaptype.Enums.Headers.String())
}

if snaptype.Bodies.Name() != snaptype.Enums.Bodies.String() {
t.Fatal("name mismatch", snaptype.Bodies, snaptype.Bodies.Name(), snaptype.Enums.Bodies.String())
}

if snaptype.Transactions.Name() != snaptype.Enums.Transactions.String() {
t.Fatal("name mismatch", snaptype.Transactions, snaptype.Transactions.Name(), snaptype.Enums.Transactions.String())
}
}
10 changes: 5 additions & 5 deletions erigon-lib/chain/snapcfg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (p Preverified) Typed(types []snaptype.Type) Preverified {
include := false

for _, typ := range types {
if typeName == typ.String() {
if typeName == typ.Name() {
preferredVersion = typ.Versions().Current
minVersion = typ.Versions().MinSupported
include = true
Expand Down Expand Up @@ -285,15 +285,15 @@ func (c Cfg) Seedable(info snaptype.FileInfo) bool {
}

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

for _, p := range c.Preverified {
info, _, ok := snaptype.ParseFileName("", p.Name)
if !ok {
continue
}

if info.Ext != ".seg" || (t != snaptype.Enums.Unknown && t != info.Type.Enum()) {
if info.Ext != ".seg" || (t != snaptype.Unknown && t != info.Type.Enum()) {
continue
}

Expand Down Expand Up @@ -322,7 +322,7 @@ func (c Cfg) MergeLimit(t snaptype.Enum, fromBlock uint64) uint64 {
return snaptype.Erigon2MergeLimit
}

return c.MergeLimit(snaptype.Enums.Headers, fromBlock)
return c.MergeLimit(snaptype.MinCoreEnum, fromBlock)
}

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

if list, err := snaptype.Segments(dir); err == nil {
for _, info := range list {
if Seedable(chain, info) && info.Type.Enum() == snaptype.Enums.Headers && info.To > max {
if Seedable(chain, info) && info.Type.Enum() == snaptype.MinCoreEnum && info.To > max {
max = info.To
}
}
Expand Down
10 changes: 6 additions & 4 deletions erigon-lib/downloader/snaptype/caplin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package snaptype

var (
BeaconBlocks = snapType{
enum: Enums.BeaconBlocks,
enum: CaplinEnums.BeaconBlocks,
name: "beaconblocks",
versions: Versions{
Current: 1,
MinSupported: 1,
},
indexes: []Index{Indexes.BeaconBlockSlot},
indexes: []Index{CaplinIndexes.BeaconBlockSlot},
}
BlobSidecars = snapType{
enum: Enums.BlobSidecars,
enum: CaplinEnums.BlobSidecars,
name: "blobsidecars",
versions: Versions{
Current: 1,
MinSupported: 1,
},
indexes: []Index{Indexes.BlobSidecarSlot},
indexes: []Index{CaplinIndexes.BlobSidecarSlot},
}

CaplinSnapshotTypes = []Type{BeaconBlocks, BlobSidecars}
Expand Down
30 changes: 30 additions & 0 deletions erigon-lib/downloader/snaptype/caplin_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package snaptype_test

import (
"testing"

"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
)

func TestEnumeration(t *testing.T) {

if snaptype.BlobSidecars.Enum() != snaptype.CaplinEnums.BlobSidecars {
t.Fatal("enum mismatch", snaptype.BlobSidecars, snaptype.BlobSidecars.Enum(), snaptype.CaplinEnums.BlobSidecars)
}

if snaptype.BeaconBlocks.Enum() != snaptype.CaplinEnums.BeaconBlocks {
t.Fatal("enum mismatch", snaptype.BeaconBlocks, snaptype.BeaconBlocks.Enum(), snaptype.CaplinEnums.BeaconBlocks)
}
}

func TestNames(t *testing.T) {

if snaptype.BeaconBlocks.Name() != snaptype.CaplinEnums.BeaconBlocks.String() {
t.Fatal("name mismatch", snaptype.BeaconBlocks, snaptype.BeaconBlocks.Name(), snaptype.CaplinEnums.BeaconBlocks.String())
}

if snaptype.BlobSidecars.Name() != snaptype.CaplinEnums.BlobSidecars.String() {
t.Fatal("name mismatch", snaptype.BlobSidecars, snaptype.BlobSidecars.Name(), snaptype.CaplinEnums.BlobSidecars.String())
}

}
5 changes: 3 additions & 2 deletions erigon-lib/downloader/snaptype/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func FilterExt(in []FileInfo, expectExt string) (out []FileInfo) {
}

slices.SortFunc(out, func(a, b FileInfo) int {
if cmp := strings.Compare(a.Type.String(), b.Type.String()); cmp != 0 {
if cmp := strings.Compare(a.Type.Name(), b.Type.Name()); cmp != 0 {
return cmp
}

Expand Down Expand Up @@ -234,7 +234,8 @@ func (f FileInfo) CompareTo(o FileInfo) int {
return res
}

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

func (f FileInfo) As(t Type) FileInfo {
Expand Down
Loading

0 comments on commit 1558fc7

Please sign in to comment.