Skip to content

Commit

Permalink
fixed issues when synchronizing in non-healthy network
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Aug 17, 2023
1 parent f6e5a75 commit ff7d5e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
1 change: 0 additions & 1 deletion indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 0 additions & 16 deletions indexer/writeDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 29 additions & 27 deletions rpc/beaconapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand All @@ -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
}
}

Expand Down

0 comments on commit ff7d5e5

Please sign in to comment.