From ff7d5e5fc51ee1a5c2a8b098bed8678263a480e0 Mon Sep 17 00:00:00 2001 From: pk910 Date: Thu, 17 Aug 2023 04:10:08 +0200 Subject: [PATCH] fixed issues when synchronizing in non-healthy network --- indexer/indexer.go | 1 - indexer/writeDb.go | 16 ------------- rpc/beaconapi.go | 56 ++++++++++++++++++++++++---------------------- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/indexer/indexer.go b/indexer/indexer.go index 8ddd1e70..7e592b7b 100644 --- a/indexer/indexer.go +++ b/indexer/indexer.go @@ -691,7 +691,6 @@ func (indexer *Indexer) processEpoch(epoch uint64) { defer indexer.state.cacheMutex.RUnlock() logger.Infof("Process epoch %v", epoch) - // TODO: Process epoch aggregations and save to DB firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch lastSlot := firstSlot + utils.Config.Chain.Config.SlotsPerEpoch - 1 epochStats := indexer.state.epochStats[epoch] diff --git a/indexer/writeDb.go b/indexer/writeDb.go index e936c513..6427b9fb 100644 --- a/indexer/writeDb.go +++ b/indexer/writeDb.go @@ -105,24 +105,8 @@ func buildDbBlock(block *BlockInfo, epochStats *EpochStats) *dbtypes.Block { } func buildDbEpoch(epoch uint64, blockMap map[uint64][]*BlockInfo, epochStats *EpochStats, epochVotes *EpochVotes, blockFn func(block *BlockInfo)) *dbtypes.Epoch { - var firstBlock *BlockInfo firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch lastSlot := firstSlot + (utils.Config.Chain.Config.SlotsPerEpoch) - 1 -slotLoop: - for slot := firstSlot; slot <= lastSlot; slot++ { - if blockMap[slot] != nil { - blocks := blockMap[slot] - for bidx := 0; bidx < len(blocks); bidx++ { - if !blocks[bidx].Orphaned { - firstBlock = blocks[bidx] - break slotLoop - } - } - } - } - if firstBlock == nil { - return nil - } totalSyncAssigned := 0 totalSyncVoted := 0 diff --git a/rpc/beaconapi.go b/rpc/beaconapi.go index ca740d6a..5c9ae253 100644 --- a/rpc/beaconapi.go +++ b/rpc/beaconapi.go @@ -253,12 +253,6 @@ func (bc *BeaconClient) getEpochAssignments(epoch uint64) (*rpctypes.EpochAssign } depStateRoot := parsedHeader.Data.Header.Message.StateRoot - // Now use the state root to make a consistent committee query - var parsedCommittees rpctypes.StandardV1CommitteesResponse - err = bc.getJson(fmt.Sprintf("%s/eth/v1/beacon/states/%s/committees?epoch=%d", bc.endpoint, depStateRoot, epoch), &parsedCommittees) - if err != nil { - return nil, fmt.Errorf("error retrieving committees data: %w", err) - } assignments := &rpctypes.EpochAssignments{ DependendRoot: parsedProposerResponse.DependentRoot, DependendState: depStateRoot, @@ -271,18 +265,25 @@ func (bc *BeaconClient) getEpochAssignments(epoch uint64) (*rpctypes.EpochAssign assignments.ProposerAssignments[uint64(duty.Slot)] = uint64(duty.ValidatorIndex) } - // attester duties - for _, committee := range parsedCommittees.Data { - for i, valIndex := range committee.Validators { - valIndexU64, err := strconv.ParseUint(valIndex, 10, 64) - if err != nil { - return nil, fmt.Errorf("epoch %d committee %d index %d has bad validator index %q", epoch, committee.Index, i, valIndex) - } - k := fmt.Sprintf("%v-%v", uint64(committee.Slot), uint64(committee.Index)) - if assignments.AttestorAssignments[k] == nil { - assignments.AttestorAssignments[k] = make([]uint64, 0) + // Now use the state root to make a consistent committee query + var parsedCommittees rpctypes.StandardV1CommitteesResponse + err = bc.getJson(fmt.Sprintf("%s/eth/v1/beacon/states/%s/committees?epoch=%d", bc.endpoint, depStateRoot, epoch), &parsedCommittees) + if err != nil { + logger.Errorf("error retrieving committees data: %v", err) + } else { + // attester duties + for _, committee := range parsedCommittees.Data { + for i, valIndex := range committee.Validators { + valIndexU64, err := strconv.ParseUint(valIndex, 10, 64) + if err != nil { + return nil, fmt.Errorf("epoch %d committee %d index %d has bad validator index %q", epoch, committee.Index, i, valIndex) + } + k := fmt.Sprintf("%v-%v", uint64(committee.Slot), uint64(committee.Index)) + if assignments.AttestorAssignments[k] == nil { + assignments.AttestorAssignments[k] = make([]uint64, 0) + } + assignments.AttestorAssignments[k] = append(assignments.AttestorAssignments[k], valIndexU64) } - assignments.AttestorAssignments[k] = append(assignments.AttestorAssignments[k], valIndexU64) } } @@ -295,17 +296,18 @@ func (bc *BeaconClient) getEpochAssignments(epoch uint64) (*rpctypes.EpochAssign var parsedSyncCommittees rpctypes.StandardV1SyncCommitteesResponse err := bc.getJson(fmt.Sprintf("%s/eth/v1/beacon/states/%s/sync_committees?epoch=%d", bc.endpoint, syncCommitteeState, epoch), &parsedSyncCommittees) if err != nil { - return nil, fmt.Errorf("error retrieving sync_committees for epoch %v (state: %v): %w", epoch, syncCommitteeState, err) - } - assignments.SyncAssignments = make([]uint64, len(parsedSyncCommittees.Data.Validators)) - - // sync committee duties - for i, valIndexStr := range parsedSyncCommittees.Data.Validators { - valIndexU64, err := strconv.ParseUint(valIndexStr, 10, 64) - if err != nil { - return nil, fmt.Errorf("in sync_committee for epoch %d validator %d has bad validator index: %q", epoch, i, valIndexStr) + logger.Errorf("error retrieving sync_committees for epoch %v (state: %v): %v", epoch, syncCommitteeState, err) + } else { + assignments.SyncAssignments = make([]uint64, len(parsedSyncCommittees.Data.Validators)) + + // sync committee duties + for i, valIndexStr := range parsedSyncCommittees.Data.Validators { + valIndexU64, err := strconv.ParseUint(valIndexStr, 10, 64) + if err != nil { + return nil, fmt.Errorf("in sync_committee for epoch %d validator %d has bad validator index: %q", epoch, i, valIndexStr) + } + assignments.SyncAssignments[i] = valIndexU64 } - assignments.SyncAssignments[i] = valIndexU64 } }