From a9c1d25a355ef4c17a82dfde4a77c241e38a4995 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 24 Jun 2020 11:06:19 -0700 Subject: [PATCH] Best practice feedback - part 2 (#6389) * Feedback 10. Rename to highest epoch * Feedback 11. Rename to pidepoch * Feedback 39. Swap length * Typo * Merge refs/heads/master into best-practice-pt2 --- beacon-chain/p2p/encoder/ssz.go | 6 +++--- beacon-chain/p2p/peers/status.go | 12 ++++++------ beacon-chain/p2p/peers/status_test.go | 4 ++-- beacon-chain/sync/service.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/beacon-chain/p2p/encoder/ssz.go b/beacon-chain/p2p/encoder/ssz.go index 4bc1dce95..ecc25eea8 100644 --- a/beacon-chain/p2p/encoder/ssz.go +++ b/beacon-chain/p2p/encoder/ssz.go @@ -160,13 +160,13 @@ func (e SszNetworkEncoder) DecodeWithMaxLength(r io.Reader, to interface{}, maxS if err != nil { return err } + if msgLen > maxSize { + return fmt.Errorf("remaining bytes %d goes over the provided max limit of %d", msgLen, maxSize) + } if e.UseSnappyCompression { r = newBufferedReader(r) defer bufReaderPool.Put(r) } - if msgLen > maxSize { - return fmt.Errorf("remaining bytes %d goes over the provided max limit of %d", msgLen, maxSize) - } b := make([]byte, e.MaxLength(int(msgLen))) numOfBytes, err := r.Read(b) if err != nil { diff --git a/beacon-chain/p2p/peers/status.go b/beacon-chain/p2p/peers/status.go index 8d10d0e66..fb4189461 100644 --- a/beacon-chain/p2p/peers/status.go +++ b/beacon-chain/p2p/peers/status.go @@ -431,7 +431,7 @@ func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) ([]byte, connected := p.Connected() finalized := make(map[[32]byte]uint64) rootToEpoch := make(map[[32]byte]uint64) - pidEpochs := make(map[peer.ID]uint64) + pidEpoch := make(map[peer.ID]uint64) potentialPIDs := make([]peer.ID, 0, len(connected)) for _, pid := range connected { peerChainState, err := p.ChainState(pid) @@ -439,7 +439,7 @@ func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) ([]byte, root := bytesutil.ToBytes32(peerChainState.FinalizedRoot) finalized[root]++ rootToEpoch[root] = peerChainState.FinalizedEpoch - pidEpochs[pid] = peerChainState.FinalizedEpoch + pidEpoch[pid] = peerChainState.FinalizedEpoch potentialPIDs = append(potentialPIDs, pid) } } @@ -457,12 +457,12 @@ func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) ([]byte, // Sort PIDs by finalized epoch, in decreasing order. sort.Slice(potentialPIDs, func(i, j int) bool { - return pidEpochs[potentialPIDs[i]] > pidEpochs[potentialPIDs[j]] + return pidEpoch[potentialPIDs[i]] > pidEpoch[potentialPIDs[j]] }) // Trim potential peers to those on or after target epoch. for i, pid := range potentialPIDs { - if pidEpochs[pid] < targetEpoch { + if pidEpoch[pid] < targetEpoch { potentialPIDs = potentialPIDs[:i] break } @@ -484,8 +484,8 @@ func (p *Status) fetch(pid peer.ID) *peerStatus { return p.status[pid] } -// CurrentEpoch returns the highest reported epoch amongst peers. -func (p *Status) CurrentEpoch() uint64 { +// HighestEpoch returns the highest epoch reported epoch amongst peers. +func (p *Status) HighestEpoch() uint64 { p.lock.RLock() defer p.lock.RUnlock() var highestSlot uint64 diff --git a/beacon-chain/p2p/peers/status_test.go b/beacon-chain/p2p/peers/status_test.go index 9b81b147d..cab2f2fd5 100644 --- a/beacon-chain/p2p/peers/status_test.go +++ b/beacon-chain/p2p/peers/status_test.go @@ -671,8 +671,8 @@ func TestStatus_CurrentEpoch(t *testing.T) { HeadSlot: params.BeaconConfig().SlotsPerEpoch * 4, }) - if p.CurrentEpoch() != 5 { - t.Fatalf("Expected current epoch to be 5, got %d", p.CurrentEpoch()) + if p.HighestEpoch() != 5 { + t.Fatalf("Expected current epoch to be 5, got %d", p.HighestEpoch()) } } diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index a2527eed7..b4ed7095a 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -177,7 +177,7 @@ func (s *Service) Status() error { // If our head slot is on a previous epoch and our peers are reporting their head block are // in the most recent epoch, then we might be out of sync. if headEpoch := helpers.SlotToEpoch(s.chain.HeadSlot()); headEpoch+1 < helpers.SlotToEpoch(s.chain.CurrentSlot()) && - headEpoch+1 < s.p2p.Peers().CurrentEpoch() { + headEpoch+1 < s.p2p.Peers().HighestEpoch() { return errors.New("out of sync") } }