Skip to content

Commit 36828fb

Browse files
authored
Added tool for processing for all mainnet beacon blocks (#7095)
1 parent e4b2072 commit 36828fb

File tree

14 files changed

+134
-26
lines changed

14 files changed

+134
-26
lines changed

cl/clparams/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const (
4343
MaxDialTimeout = 2 * time.Second
4444
VersionLength int = 4
4545
MaxChunkSize uint64 = 1 << 20 // 1 MiB
46-
ReqTimeout time.Duration = 1 * time.Second
47-
RespTimeout time.Duration = 1 * time.Second
46+
ReqTimeout time.Duration = 10 * time.Second
47+
RespTimeout time.Duration = 15 * time.Second
4848
)
4949

5050
var (
28.4 MB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package initial_state
2+
3+
import (
4+
_ "embed"
5+
"fmt"
6+
7+
"github.com/ledgerwatch/erigon/cl/clparams"
8+
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state"
9+
)
10+
11+
//go:embed mainnet.state.ssz
12+
var mainnetStateSSZ []byte
13+
14+
//go:embed sepolia.state.ssz
15+
var sepoliaStateSSZ []byte
16+
17+
//go:embed goerli.state.ssz
18+
var goerliStateSSZ []byte
19+
20+
// Return genesis state
21+
func GetGenesisState(network clparams.NetworkType) (*state.BeaconState, error) {
22+
_, _, config := clparams.GetConfigsByNetwork(network)
23+
returnState := state.New(config)
24+
25+
switch network {
26+
case clparams.MainnetNetwork:
27+
if err := returnState.DecodeSSZWithVersion(mainnetStateSSZ, int(clparams.Phase0Version)); err != nil {
28+
return nil, err
29+
}
30+
case clparams.GoerliNetwork:
31+
if err := returnState.DecodeSSZWithVersion(goerliStateSSZ, int(clparams.Phase0Version)); err != nil {
32+
return nil, err
33+
}
34+
case clparams.SepoliaNetwork:
35+
if err := returnState.DecodeSSZWithVersion(sepoliaStateSSZ, int(clparams.Phase0Version)); err != nil {
36+
return nil, err
37+
}
38+
default:
39+
return nil, fmt.Errorf("unsupported network for genesis fetching")
40+
}
41+
return returnState, nil
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package initial_state_test
2+
3+
import (
4+
"testing"
5+
6+
libcommon "github.com/ledgerwatch/erigon-lib/common"
7+
"github.com/ledgerwatch/erigon/cl/clparams"
8+
"github.com/ledgerwatch/erigon/cl/clparams/initial_state"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestMainnet(t *testing.T) {
13+
state, err := initial_state.GetGenesisState(clparams.MainnetNetwork)
14+
assert.NoError(t, err)
15+
root, err := state.HashSSZ()
16+
assert.NoError(t, err)
17+
assert.Equal(t, libcommon.Hash(root), libcommon.HexToHash("7e76880eb67bbdc86250aa578958e9d0675e64e714337855204fb5abaaf82c2b"))
18+
}
19+
20+
func TestGoerli(t *testing.T) {
21+
state, err := initial_state.GetGenesisState(clparams.GoerliNetwork)
22+
assert.NoError(t, err)
23+
root, err := state.HashSSZ()
24+
assert.NoError(t, err)
25+
assert.Equal(t, libcommon.Hash(root), libcommon.HexToHash("895390e92edc03df7096e9f51e51896e8dbe6e7e838180dadbfd869fdd77a659"))
26+
}
27+
28+
func TestSepolia(t *testing.T) {
29+
state, err := initial_state.GetGenesisState(clparams.SepoliaNetwork)
30+
assert.NoError(t, err)
31+
root, err := state.HashSSZ()
32+
assert.NoError(t, err)
33+
assert.Equal(t, libcommon.Hash(root), libcommon.HexToHash("fb9afe32150fa39f4b346be2519a67e2a4f5efcd50a1dc192c3f6b3d013d2798"))
34+
}
5.15 MB
Binary file not shown.
2.76 MB
Binary file not shown.

cmd/erigon-cl/main.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ledgerwatch/erigon-lib/kv"
1111
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
1212
"github.com/ledgerwatch/erigon/cl/clparams"
13+
"github.com/ledgerwatch/erigon/cl/clparams/initial_state"
1314
"github.com/ledgerwatch/erigon/cl/cltypes"
1415
"github.com/ledgerwatch/erigon/cl/fork"
1516
"github.com/ledgerwatch/erigon/cl/rpc"
@@ -20,6 +21,7 @@ import (
2021
"github.com/ledgerwatch/erigon/cmd/erigon-cl/network"
2122
"github.com/ledgerwatch/erigon/cmd/erigon-cl/stages"
2223
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel/cli"
24+
2325
"github.com/ledgerwatch/erigon/cmd/sentinel/cli/flags"
2426
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel"
2527
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/handshake"
@@ -58,12 +60,8 @@ func runConsensusLayerNode(cliCtx *cli.Context) error {
5860
log.Error("Could load beacon data configuration", "err", err)
5961
return err
6062
}
61-
// Fetch the checkpoint state.
62-
cpState, err := getCheckpointState(ctx, db, cfg.BeaconCfg, cfg.GenesisCfg, cfg.CheckpointUri)
63-
if err != nil {
64-
log.Error("Could not get checkpoint", "err", err)
65-
return err
66-
}
63+
64+
tmpdir := "/tmp"
6765
var executionClient *execution_client.ExecutionClient
6866
if cfg.ELEnabled {
6967
executionClient, err = execution_client.NewExecutionClient(ctx, "127.0.0.1:8989")
@@ -73,8 +71,24 @@ func runConsensusLayerNode(cliCtx *cli.Context) error {
7371
}
7472
}
7573

74+
if cfg.TransitionChain {
75+
state, err := initial_state.GetGenesisState(cfg.NetworkType)
76+
if err != nil {
77+
return err
78+
}
79+
// Execute from genesis to whatever we have.
80+
return stages.SpawnStageBeaconState(stages.StageBeaconState(db, cfg.BeaconCfg, state, nil, true, executionClient), nil, ctx)
81+
}
82+
83+
fmt.Println(cfg.CheckpointUri)
84+
// Fetch the checkpoint state.
85+
cpState, err := getCheckpointState(ctx, db, cfg.BeaconCfg, cfg.GenesisCfg, cfg.CheckpointUri)
86+
if err != nil {
87+
log.Error("Could not get checkpoint", "err", err)
88+
return err
89+
}
90+
7691
log.Info("Starting sync from checkpoint.")
77-
tmpdir := "/tmp"
7892
// Start the sentinel service
7993
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
8094
log.Info("[Sentinel] running sentinel with configuration", "cfg", cfg)

cmd/erigon-cl/stages/stages.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func ConsensusStages(ctx context.Context, historyReconstruction StageHistoryReco
4040
ID: stages.BeaconState,
4141
Description: "Execute Consensus Layer transition",
4242
Forward: func(firstCycle bool, badBlockUnwind bool, s *stagedsync.StageState, u stagedsync.Unwinder, tx kv.RwTx, quiet bool) error {
43-
return SpawnStageBeaconState(beaconState, s, tx, ctx)
43+
return SpawnStageBeaconState(beaconState, tx, ctx)
4444
},
4545
Unwind: func(firstCycle bool, u *stagedsync.UnwindState, s *stagedsync.StageState, tx kv.RwTx) error {
4646
return nil
@@ -77,7 +77,7 @@ func NewConsensusStagedSync(ctx context.Context,
7777
ctx,
7878
StageHistoryReconstruction(db, backwardDownloader, genesisCfg, beaconCfg, beaconDBCfg, state, tmpdir, executionClient),
7979
StageBeaconsBlock(db, forwardDownloader, genesisCfg, beaconCfg, state, executionClient),
80-
StageBeaconState(db, genesisCfg, beaconCfg, state, triggerExecution, clearEth1Data, executionClient),
80+
StageBeaconState(db, beaconCfg, state, triggerExecution, clearEth1Data, executionClient),
8181
),
8282
ConsensusUnwindOrder,
8383
ConsensusPruneOrder,

cmd/erigon-cl/stages/stages_beacon_state.go

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

33
import (
44
"context"
5-
"fmt"
65

76
libcommon "github.com/ledgerwatch/erigon-lib/common"
87
"github.com/ledgerwatch/erigon-lib/kv"
@@ -14,7 +13,6 @@ import (
1413
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state"
1514
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition"
1615
"github.com/ledgerwatch/erigon/cmd/erigon-cl/execution_client"
17-
"github.com/ledgerwatch/erigon/eth/stagedsync"
1816
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
1917
"github.com/ledgerwatch/log/v3"
2018
)
@@ -24,19 +22,17 @@ type triggerExecutionFunc func(*cltypes.SignedBeaconBlock) error
2422

2523
type StageBeaconStateCfg struct {
2624
db kv.RwDB
27-
genesisCfg *clparams.GenesisConfig
2825
beaconCfg *clparams.BeaconChainConfig
2926
state *state.BeaconState
3027
clearEth1Data bool // Whether we want to discard eth1 data.
3128
triggerExecution triggerExecutionFunc
3229
executionClient *execution_client.ExecutionClient
3330
}
3431

35-
func StageBeaconState(db kv.RwDB, genesisCfg *clparams.GenesisConfig,
32+
func StageBeaconState(db kv.RwDB,
3633
beaconCfg *clparams.BeaconChainConfig, state *state.BeaconState, triggerExecution triggerExecutionFunc, clearEth1Data bool, executionClient *execution_client.ExecutionClient) StageBeaconStateCfg {
3734
return StageBeaconStateCfg{
3835
db: db,
39-
genesisCfg: genesisCfg,
4036
beaconCfg: beaconCfg,
4137
state: state,
4238
clearEth1Data: clearEth1Data,
@@ -46,7 +42,7 @@ func StageBeaconState(db kv.RwDB, genesisCfg *clparams.GenesisConfig,
4642
}
4743

4844
// SpawnStageBeaconForward spawn the beacon forward stage
49-
func SpawnStageBeaconState(cfg StageBeaconStateCfg, s *stagedsync.StageState, tx kv.RwTx, ctx context.Context) error {
45+
func SpawnStageBeaconState(cfg StageBeaconStateCfg, tx kv.RwTx, ctx context.Context) error {
5046
useExternalTx := tx != nil
5147
var err error
5248
if !useExternalTx {
@@ -83,7 +79,7 @@ func SpawnStageBeaconState(cfg StageBeaconStateCfg, s *stagedsync.StageState, tx
8379
return err
8480
}
8581
// validate fully only in current epoch.
86-
fullValidate := utils.GetCurrentEpoch(cfg.genesisCfg.GenesisTime, cfg.beaconCfg.SecondsPerSlot, cfg.beaconCfg.SlotsPerEpoch) == cfg.state.Epoch()
82+
fullValidate := utils.GetCurrentEpoch(cfg.state.GenesisTime(), cfg.beaconCfg.SecondsPerSlot, cfg.beaconCfg.SlotsPerEpoch) == cfg.state.Epoch()
8783
if err := transition.TransitionState(cfg.state, block, fullValidate); err != nil {
8884
log.Info("Found epoch, so stopping now...", "count", slot-(fromSlot+1), "slot", slot)
8985
return err
@@ -129,7 +125,7 @@ func SpawnStageBeaconState(cfg StageBeaconStateCfg, s *stagedsync.StageState, tx
129125
}
130126
}
131127

132-
log.Info(fmt.Sprintf("[%s] Finished transitioning state", s.LogPrefix()), "from", fromSlot, "to", endSlot)
128+
log.Info("[BeaconState] Finished transitioning state", "from", fromSlot, "to", endSlot)
133129
if !useExternalTx {
134130
if err = tx.Commit(); err != nil {
135131
return err

cmd/erigon-el-mock/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package main
22

33
import (
4+
"flag"
45
"net"
56

67
"github.com/c2h5oh/datasize"
78
"github.com/ledgerwatch/erigon-lib/gointerfaces/execution"
9+
"github.com/ledgerwatch/erigon-lib/kv"
10+
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
811
"github.com/ledgerwatch/erigon-lib/kv/memdb"
912
"google.golang.org/grpc"
1013

1114
"github.com/ledgerwatch/log/v3"
1215
)
1316

1417
func main() {
18+
19+
datadir := flag.String("datadir", "", "non in-memory db for EL simulation")
20+
flag.Parse()
1521
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StderrHandler))
1622
lis, err := net.Listen("tcp", "127.0.0.1:8989")
1723
if err != nil {
@@ -20,7 +26,17 @@ func main() {
2026
maxReceiveSize := 500 * datasize.MB
2127

2228
s := grpc.NewServer(grpc.MaxRecvMsgSize(int(maxReceiveSize)))
23-
execution.RegisterExecutionServer(s, NewEth1Execution(memdb.New("" /* tmpDir */)))
29+
var db kv.RwDB
30+
if *datadir == "" {
31+
db = memdb.New("")
32+
} else {
33+
db, err = mdbx.Open(*datadir, log.Root(), false)
34+
if err != nil {
35+
log.Error("Could not open database", "err", err)
36+
return
37+
}
38+
}
39+
execution.RegisterExecutionServer(s, NewEth1Execution(db))
2440
log.Info("Serving mock Execution layer.")
2541
if err := s.Serve(lis); err != nil {
2642
log.Error("failed to serve", "err", err)

0 commit comments

Comments
 (0)