mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-11 05:20:05 +00:00
41185e2518
Basically, pruning is specified by the user, by default, 1 million (in the PR set to 100 for pruning purposes). the pruning for the database is stored inside the db
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package caplin1
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/datadir"
|
|
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
|
|
"github.com/ledgerwatch/erigon/cl/freezer"
|
|
"github.com/ledgerwatch/erigon/cl/persistence"
|
|
"github.com/ledgerwatch/erigon/cl/persistence/db_config"
|
|
"github.com/ledgerwatch/erigon/cl/persistence/sql_migrations"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/execution_client"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/network"
|
|
"github.com/ledgerwatch/erigon/cl/phase1/stages"
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/Giulio2002/bls"
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
|
|
"github.com/ledgerwatch/erigon/cl/clparams"
|
|
"github.com/ledgerwatch/erigon/cl/rpc"
|
|
"github.com/ledgerwatch/log/v3"
|
|
)
|
|
|
|
func RunCaplinPhase1(ctx context.Context, sentinel sentinel.SentinelClient,
|
|
beaconConfig *clparams.BeaconChainConfig, genesisConfig *clparams.GenesisConfig,
|
|
engine execution_client.ExecutionEngine, state *state.CachingBeaconState,
|
|
caplinFreezer freezer.Freezer, dirs datadir.Dirs) error {
|
|
ctx, cn := context.WithCancel(ctx)
|
|
defer cn()
|
|
|
|
database_config := db_config.DefaultDatabaseConfiguration
|
|
|
|
beaconRpc := rpc.NewBeaconRpcP2P(ctx, sentinel, beaconConfig, genesisConfig)
|
|
|
|
logger := log.New("app", "caplin")
|
|
|
|
if caplinFreezer != nil {
|
|
if err := freezer.PutObjectSSZIntoFreezer("beaconState", "caplin_core", 0, state, caplinFreezer); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
forkChoice, err := forkchoice.NewForkChoiceStore(state, engine, caplinFreezer, true)
|
|
if err != nil {
|
|
logger.Error("Could not create forkchoice", "err", err)
|
|
return err
|
|
}
|
|
bls.SetEnabledCaching(true)
|
|
state.ForEachValidator(func(v solid.Validator, idx, total int) bool {
|
|
pk := v.PublicKey()
|
|
if err := bls.LoadPublicKeyIntoCache(pk[:], false); err != nil {
|
|
panic(err)
|
|
}
|
|
return true
|
|
})
|
|
gossipManager := network.NewGossipReceiver(sentinel, forkChoice, beaconConfig, genesisConfig, caplinFreezer)
|
|
dataDirFs := afero.NewBasePathFs(afero.NewOsFs(), dirs.DataDir)
|
|
dataDirIndexer := path.Join(dirs.DataDir, "beacon_indicies")
|
|
db, err := sql.Open("sqlite3", dataDirIndexer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err := sql_migrations.ApplyMigrations(ctx, tx); err != nil {
|
|
return err
|
|
}
|
|
if err := db_config.WriteConfigurationIfNotExist(ctx, tx, database_config); err != nil {
|
|
return err
|
|
}
|
|
var haveDatabaseConfig db_config.DatabaseConfiguration
|
|
if haveDatabaseConfig, err = db_config.ReadConfiguration(ctx, tx); err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return err
|
|
}
|
|
logger.Info("Caplin Pruning", "pruning provided", database_config.PruneDepth, "effective pruning", haveDatabaseConfig.PruneDepth)
|
|
{ // start ticking forkChoice
|
|
go func() {
|
|
tickInterval := time.NewTicker(50 * time.Millisecond)
|
|
for {
|
|
select {
|
|
case <-tickInterval.C:
|
|
forkChoice.OnTick(uint64(time.Now().Unix()))
|
|
case <-ctx.Done():
|
|
db.Close() // close sql database here
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
{ // start the gossip manager
|
|
go gossipManager.Start(ctx)
|
|
logger.Info("Started Ethereum 2.0 Gossip Service")
|
|
}
|
|
|
|
{ // start logging peers
|
|
go func() {
|
|
logIntervalPeers := time.NewTicker(1 * time.Minute)
|
|
for {
|
|
select {
|
|
case <-logIntervalPeers.C:
|
|
if peerCount, err := beaconRpc.Peers(); err == nil {
|
|
logger.Info("P2P", "peers", peerCount)
|
|
}
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
beaconDB := persistence.NewbeaconChainDatabaseFilesystem(afero.NewBasePathFs(dataDirFs, dirs.DataDir), beaconConfig, db)
|
|
stageCfg := stages.ClStagesCfg(beaconRpc, genesisConfig, beaconConfig, state, engine, gossipManager, forkChoice, beaconDB, dirs, db, haveDatabaseConfig)
|
|
sync := stages.ConsensusClStages(ctx, stageCfg)
|
|
|
|
logger.Info("[caplin] starting clstages loop")
|
|
err = sync.StartWithStage(ctx, "WaitForPeers", logger, stageCfg)
|
|
logger.Info("[caplin] exiting clstages loop")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|