From 6dafd30d17ced01aa9cb2d7d6983534787595de4 Mon Sep 17 00:00:00 2001 From: pk910 Date: Fri, 18 Aug 2023 19:29:44 +0200 Subject: [PATCH] fix fetching genesis epoch 0 duties / validators with some clients --- indexer/indexer.go | 8 +++++++- indexer/synchronizer.go | 9 ++++++++- rpc/beaconapi.go | 19 ++++++++++++++++--- rpctypes/beaconapi.go | 1 + 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/indexer/indexer.go b/indexer/indexer.go index 94d9aa6a..99da4597 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -646,7 +646,13 @@ func (indexer *Indexer) loadEpochValidators(epoch uint64, epochStats *EpochStats logger.Infof("Epoch %v head, loading validator set (state: %v)", epoch, epochStats.Assignments.DependendState) // load epoch stats - epochValidators, err := indexer.rpcClient.GetStateValidators(epochStats.Assignments.DependendState) + var epochValidators *rpctypes.StandardV1StateValidatorsResponse + var err error + if epochStats.Assignments.DependendIsGenesis { + epochValidators, err = indexer.rpcClient.GetGenesisValidators() + } else { + epochValidators, err = indexer.rpcClient.GetStateValidators(epochStats.Assignments.DependendState) + } if err != nil { logger.Errorf("Error fetching epoch %v validators: %v", epoch, err) } else { diff --git a/indexer/synchronizer.go b/indexer/synchronizer.go index 6f2e2a1b..095ebaf9 100644 --- a/indexer/synchronizer.go +++ b/indexer/synchronizer.go @@ -9,6 +9,7 @@ import ( "github.com/pk910/light-beaconchain-explorer/db" "github.com/pk910/light-beaconchain-explorer/dbtypes" + "github.com/pk910/light-beaconchain-explorer/rpctypes" "github.com/pk910/light-beaconchain-explorer/utils" ) @@ -189,7 +190,13 @@ func (sync *synchronizerState) syncEpoch(syncEpoch uint64) bool { }, } if epochAssignments != nil { - epochValidators, err := sync.indexer.rpcClient.GetStateValidators(epochAssignments.DependendState) + var epochValidators *rpctypes.StandardV1StateValidatorsResponse + var err error + if epochAssignments.DependendIsGenesis { + epochValidators, err = sync.indexer.rpcClient.GetGenesisValidators() + } else { + epochValidators, err = sync.indexer.rpcClient.GetStateValidators(epochAssignments.DependendState) + } if err != nil { logger.Errorf("Error fetching epoch %v validators (state: %v): %v", syncEpoch, epochAssignments.DependendState, err) } else { diff --git a/rpc/beaconapi.go b/rpc/beaconapi.go index c69d54b3..8716123f 100644 --- a/rpc/beaconapi.go +++ b/rpc/beaconapi.go @@ -272,11 +272,15 @@ func (bc *BeaconClient) getEpochAssignments(epoch uint64) (*rpctypes.EpochAssign if err != nil { return nil, err } - depStateRoot := parsedHeader.Data.Header.Message.StateRoot + var depStateRoot string = parsedHeader.Data.Header.Message.StateRoot.String() + if epoch == 0 { + depStateRoot = "genesis" + } assignments := &rpctypes.EpochAssignments{ DependendRoot: parsedProposerResponse.DependentRoot, - DependendState: depStateRoot, + DependendState: parsedHeader.Data.Header.Message.StateRoot, + DependendIsGenesis: epoch == 0, ProposerAssignments: make(map[uint64]uint64), AttestorAssignments: make(map[string][]uint64), } @@ -310,7 +314,7 @@ func (bc *BeaconClient) getEpochAssignments(epoch uint64) (*rpctypes.EpochAssign if epoch >= utils.Config.Chain.Config.AltairForkEpoch { syncCommitteeState := fmt.Sprintf("%s", depStateRoot) - if epoch == utils.Config.Chain.Config.AltairForkEpoch { + if epoch > 0 && epoch == utils.Config.Chain.Config.AltairForkEpoch { syncCommitteeState = fmt.Sprintf("%d", utils.Config.Chain.Config.AltairForkEpoch*utils.Config.Chain.Config.SlotsPerEpoch) } @@ -344,6 +348,15 @@ func (bc *BeaconClient) GetStateValidators(stateroot []byte) (*rpctypes.Standard return &parsedResponse, nil } +func (bc *BeaconClient) GetGenesisValidators() (*rpctypes.StandardV1StateValidatorsResponse, error) { + var parsedResponse rpctypes.StandardV1StateValidatorsResponse + err := bc.getJson(fmt.Sprintf("%s/eth/v1/beacon/states/genesis/validators", bc.endpoint), &parsedResponse) + if err != nil { + return nil, fmt.Errorf("error retrieving state validators: %v", err) + } + return &parsedResponse, nil +} + func (bc *BeaconClient) GetBlobSidecarsByBlockroot(blockroot []byte) (*rpctypes.StandardV1BlobSidecarsResponse, error) { resp, err := bc.get(fmt.Sprintf("%s/eth/v1/beacon/blob_sidecars/0x%x", bc.endpoint, blockroot)) if err != nil { diff --git a/rpctypes/beaconapi.go b/rpctypes/beaconapi.go index aaa8b8e7..c5bf13d1 100644 --- a/rpctypes/beaconapi.go +++ b/rpctypes/beaconapi.go @@ -73,6 +73,7 @@ type StandardV1SyncCommitteesResponse struct { type EpochAssignments struct { DependendRoot BytesHexStr `json:"dep_root"` DependendState BytesHexStr `json:"dep_state"` + DependendIsGenesis bool `json:"dep_genesis"` ProposerAssignments map[uint64]uint64 `json:"prop"` AttestorAssignments map[string][]uint64 `json:"att"` SyncAssignments []uint64 `json:"sync"`