Skip to content

Commit

Permalink
fix fetching genesis epoch 0 duties / validators with some clients
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Aug 18, 2023
1 parent 04fa321 commit 6dafd30
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion indexer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions rpc/beaconapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions rpctypes/beaconapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 6dafd30

Please sign in to comment.