Skip to content

Commit

Permalink
no error returned
Browse files Browse the repository at this point in the history
  • Loading branch information
rauljordan committed Jun 26, 2020
1 parent 78465e2 commit ad92b65
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions beacon-chain/p2p/peers/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
ma "github.com/multiformats/go-multiaddr"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-bitfield"

"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/roughtime"
)

Expand Down Expand Up @@ -427,33 +427,29 @@ func (p *Status) Decay() {
// Ideally, all peers would be reporting the same finalized epoch but some may be behind due to their own latency, or because of
// their finalized epoch at the time we queried them.
// Returns the best finalized root, epoch number, and list of peers that are at or beyond that epoch.
func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) ([]byte, uint64, []peer.ID) {
func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) (uint64, []peer.ID, error) {
connected := p.Connected()
finalized := make(map[[32]byte]uint64)
rootToEpoch := make(map[[32]byte]uint64)
finalizedEpochVotes := make(map[uint64]uint64)
pidEpoch := make(map[peer.ID]uint64)
potentialPIDs := make([]peer.ID, 0, len(connected))
for _, pid := range connected {
peerChainState, err := p.ChainState(pid)
if err == nil && peerChainState != nil && peerChainState.FinalizedEpoch >= ourFinalizedEpoch {
root := bytesutil.ToBytes32(peerChainState.FinalizedRoot)
finalized[root]++
rootToEpoch[root] = peerChainState.FinalizedEpoch
finalizedEpochVotes[peerChainState.FinalizedEpoch]++
pidEpoch[pid] = peerChainState.FinalizedEpoch
potentialPIDs = append(potentialPIDs, pid)
}
}

// Select the target epoch, which is the epoch most peers agree upon.
var targetRoot [32]byte
var targetEpoch uint64
var mostVotes uint64
for root, count := range finalized {
for epoch, count := range finalizedEpochVotes {
if count > mostVotes {
mostVotes = count
targetRoot = root
targetEpoch = epoch
}
}
targetEpoch := rootToEpoch[targetRoot]

// Sort PIDs by finalized epoch, in decreasing order.
sort.Slice(potentialPIDs, func(i, j int) bool {
Expand All @@ -473,7 +469,7 @@ func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) ([]byte,
potentialPIDs = potentialPIDs[:maxPeers]
}

return targetRoot[:], targetEpoch, potentialPIDs
return targetEpoch, potentialPIDs, nil
}

// fetch is a helper function that fetches a peer status, possibly creating it.
Expand Down

0 comments on commit ad92b65

Please sign in to comment.