Skip to content

Commit

Permalink
Caplin: Add switch to enable/disable static files generation (#12131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Sep 30, 2024
1 parent 4b9199b commit 60b68a6
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 27 deletions.
46 changes: 30 additions & 16 deletions cl/antiquary/antiquary.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,28 @@ const safetyMargin = 10_000 // We retire snapshots 10k blocks after the finalize

// Antiquary is where the snapshots go, aka old history, it is what keep track of the oldest records.
type Antiquary struct {
mainDB kv.RwDB // this is the main DB
blobStorage blob_storage.BlobStorage // this is the blob storage
dirs datadir.Dirs
downloader proto_downloader.DownloaderClient
logger log.Logger
sn *freezeblocks.CaplinSnapshots
snReader freezeblocks.BeaconSnapshotReader
snBuildSema *semaphore.Weighted // semaphore for building only one type (blocks, caplin, v3) at a time
ctx context.Context
backfilled *atomic.Bool
blobBackfilled *atomic.Bool
cfg *clparams.BeaconChainConfig
states, blocks, blobs bool
validatorsTable *state_accessors.StaticValidatorTable
genesisState *state.CachingBeaconState
mainDB kv.RwDB // this is the main DB
blobStorage blob_storage.BlobStorage // this is the blob storage
dirs datadir.Dirs
downloader proto_downloader.DownloaderClient
logger log.Logger
sn *freezeblocks.CaplinSnapshots
snReader freezeblocks.BeaconSnapshotReader
snBuildSema *semaphore.Weighted // semaphore for building only one type (blocks, caplin, v3) at a time
ctx context.Context
backfilled *atomic.Bool
blobBackfilled *atomic.Bool
cfg *clparams.BeaconChainConfig
states, blocks, blobs, snapgen bool

validatorsTable *state_accessors.StaticValidatorTable
genesisState *state.CachingBeaconState
// set to nil
currentState *state.CachingBeaconState
balances32 []byte
}

func NewAntiquary(ctx context.Context, blobStorage blob_storage.BlobStorage, genesisState *state.CachingBeaconState, validatorsTable *state_accessors.StaticValidatorTable, cfg *clparams.BeaconChainConfig, dirs datadir.Dirs, downloader proto_downloader.DownloaderClient, mainDB kv.RwDB, sn *freezeblocks.CaplinSnapshots, reader freezeblocks.BeaconSnapshotReader, logger log.Logger, states, blocks, blobs bool, snBuildSema *semaphore.Weighted) *Antiquary {
func NewAntiquary(ctx context.Context, blobStorage blob_storage.BlobStorage, genesisState *state.CachingBeaconState, validatorsTable *state_accessors.StaticValidatorTable, cfg *clparams.BeaconChainConfig, dirs datadir.Dirs, downloader proto_downloader.DownloaderClient, mainDB kv.RwDB, sn *freezeblocks.CaplinSnapshots, reader freezeblocks.BeaconSnapshotReader, logger log.Logger, states, blocks, blobs, snapgen bool, snBuildSema *semaphore.Weighted) *Antiquary {
backfilled := &atomic.Bool{}
blobBackfilled := &atomic.Bool{}
backfilled.Store(false)
Expand All @@ -87,6 +88,7 @@ func NewAntiquary(ctx context.Context, blobStorage blob_storage.BlobStorage, gen
genesisState: genesisState,
blocks: blocks,
blobs: blobs,
snapgen: snapgen,
}
}

Expand Down Expand Up @@ -278,6 +280,9 @@ const caplinSnapshotBuildSemaWeight int64 = 1

// Antiquate will antiquate a specific block range (aka. retire snapshots), this should be ran in the background.
func (a *Antiquary) antiquate(from, to uint64) error {
if !a.snapgen {
return nil
}
if a.snBuildSema != nil {
if !a.snBuildSema.TryAcquire(caplinSnapshotBuildSemaWeight) {
return nil
Expand Down Expand Up @@ -358,6 +363,15 @@ func (a *Antiquary) loopBlobs(ctx context.Context) {
}

func (a *Antiquary) antiquateBlobs() error {
if !a.snapgen {
return nil
}
if a.snBuildSema != nil {
if !a.snBuildSema.TryAcquire(caplinSnapshotBuildSemaWeight) {
return nil
}
defer a.snBuildSema.TryAcquire(caplinSnapshotBuildSemaWeight)
}
roTx, err := a.mainDB.BeginRo(a.ctx)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cl/antiquary/state_antiquary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postSt

ctx := context.Background()
vt := state_accessors.NewStaticValidatorTable()
a := NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, true, nil)
a := NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, true, false, nil)
require.NoError(t, a.IncrementBeaconState(ctx, blocks[len(blocks)-1].Block.Slot+33))
}

Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/handler/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func setupTestingHandler(t *testing.T, v clparams.StateVersion, logger log.Logge

ctx := context.Background()
vt := state_accessors.NewStaticValidatorTable()
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &bcfg, datadir.New("/tmp"), nil, db, nil, reader, logger, true, true, false, nil)
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &bcfg, datadir.New("/tmp"), nil, db, nil, reader, logger, true, true, false, false, nil)
require.NoError(t, a.IncrementBeaconState(ctx, blocks[len(blocks)-1].Block.Slot+33))
// historical states reader below
statesReader := historical_states_reader.NewHistoricalStatesReader(&bcfg, reader, vt, preState)
Expand Down
11 changes: 6 additions & 5 deletions cl/clparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ import (
var LatestStateFileName = "latest.ssz_snappy"

type CaplinConfig struct {
Backfilling bool
BlobBackfilling bool
BlobPruningDisabled bool
Archive bool
NetworkId NetworkType
Backfilling bool
BlobBackfilling bool
BlobPruningDisabled bool
Archive bool
SnapshotGenerationEnabled bool
NetworkId NetworkType
// DisableCheckpointSync is optional and is used to disable checkpoint sync used by default in the node
DisabledCheckpointSync bool
// CaplinMeVRelayUrl is optional and is used to connect to the external builder service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postSt

ctx := context.Background()
vt := state_accessors.NewStaticValidatorTable()
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, true, nil)
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, true, false, nil)
require.NoError(t, a.IncrementBeaconState(ctx, blocks[len(blocks)-1].Block.Slot+33))
// Now lets test it against the reader
tx, err := db.BeginRw(ctx)
Expand Down
2 changes: 1 addition & 1 deletion cl/sentinel/sentinel_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func loadChain(t *testing.T) (db kv.RwDB, blocks []*cltypes.SignedBeaconBlock, f

ctx := context.Background()
vt := state_accessors.NewStaticValidatorTable()
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, false, nil)
a := antiquary.NewAntiquary(ctx, nil, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, log.New(), true, true, false, false, nil)
require.NoError(t, a.IncrementBeaconState(ctx, blocks[len(blocks)-1].Block.Slot+33))
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/capcli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *Chain) Run(ctx *Context) error {
}

downloader := network.NewBackwardBeaconDownloader(ctx, beacon, nil, nil, db)
cfg := stages.StageHistoryReconstruction(downloader, antiquary.NewAntiquary(ctx, nil, nil, nil, nil, dirs, nil, nil, nil, nil, nil, false, false, false, nil), csn, db, nil, beaconConfig, true, false, true, bRoot, bs.Slot(), "/tmp", 300*time.Millisecond, nil, nil, blobStorage, log.Root())
cfg := stages.StageHistoryReconstruction(downloader, antiquary.NewAntiquary(ctx, nil, nil, nil, nil, dirs, nil, nil, nil, nil, nil, false, false, false, false, nil), csn, db, nil, beaconConfig, true, false, true, bRoot, bs.Slot(), "/tmp", 300*time.Millisecond, nil, nil, blobStorage, log.Root())
return stages.SpawnStageHistoryDownload(cfg, ctx, log.Root())
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/caplin/caplin1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func RunCaplinService(ctx context.Context, engine execution_client.ExecutionEngi
}
}

antiq := antiquary.NewAntiquary(ctx, blobStorage, genesisState, vTables, beaconConfig, dirs, snDownloader, indexDB, csn, rcsn, logger, states, backfilling, blobBackfilling, snBuildSema)
antiq := antiquary.NewAntiquary(ctx, blobStorage, genesisState, vTables, beaconConfig, dirs, snDownloader, indexDB, csn, rcsn, logger, states, backfilling, blobBackfilling, config.SnapshotGenerationEnabled, snBuildSema)
// Create the antiquary
go func() {
if err := antiq.Loop(); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,11 @@ var (
Usage: "enables archival node in caplin",
Value: false,
}
CaplinEnableSnapshotGeneration = cli.BoolFlag{
Name: "caplin.snapgen",
Usage: "enables snapshot generation in caplin",
Value: false,
}
BeaconApiAllowCredentialsFlag = cli.BoolFlag{
Name: "beacon.api.cors.allow-credentials",
Usage: "set the cors' allow credentials",
Expand Down Expand Up @@ -1713,6 +1718,7 @@ func setBeaconAPI(ctx *cli.Context, cfg *ethconfig.Config) error {
func setCaplin(ctx *cli.Context, cfg *ethconfig.Config) {
// Caplin's block's backfilling is enabled if any of the following flags are set
cfg.CaplinConfig.Backfilling = ctx.Bool(CaplinBackfillingFlag.Name) || ctx.Bool(CaplinArchiveFlag.Name) || ctx.Bool(CaplinBlobBackfillingFlag.Name)
cfg.CaplinConfig.SnapshotGenerationEnabled = ctx.Bool(CaplinEnableSnapshotGeneration.Name)
// More granularity here.
cfg.CaplinConfig.BlobBackfilling = ctx.Bool(CaplinBlobBackfillingFlag.Name)
cfg.CaplinConfig.BlobPruningDisabled = ctx.Bool(CaplinDisableBlobPruningFlag.Name)
Expand Down
1 change: 1 addition & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ var DefaultFlags = []cli.Flag{
&utils.CaplinDisableBlobPruningFlag,
&utils.CaplinDisableCheckpointSyncFlag,
&utils.CaplinArchiveFlag,
&utils.CaplinEnableSnapshotGeneration,
&utils.CaplinMevRelayUrl,
&utils.CaplinValidatorMonitorFlag,
&utils.CaplinCustomConfigFlag,
Expand Down

0 comments on commit 60b68a6

Please sign in to comment.