Skip to content

Commit

Permalink
refactor(p2p): addressing hlib nits
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Aug 3, 2023
1 parent 882dd48 commit fa3c102
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var log = logging.Logger("header/p2p")
// chosen.
const minHeadResponses = 2

// numUntrustedHeadRequests is the number of head requests to be made to
// maxUntrustedHeadRequests is the number of head requests to be made to
// the network in order to determine the network head.
var numUntrustedHeadRequests = 4
var maxUntrustedHeadRequests = 4

// Exchange enables sending outbound HeaderRequests to the network as well as
// handling inbound HeaderRequests from the network.
Expand Down Expand Up @@ -120,26 +120,23 @@ func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.HeadOption) (H,
opt(&reqParams)
}

var (
zero H
peers = ex.trustedPeers()
)
peers := ex.trustedPeers()

// the TrustedHead field indicates whether the Exchange should use
// trusted peers for its Head request. If nil, trusted peers will
// be used. If non-nil, Exchange will ask several peers from its network for
// their Head and verify against the given trusted header.
useTrackedPeers := reqParams.TrustedHead != nil

if useTrackedPeers {
trackedPeers := ex.peerTracker.getPeers(numUntrustedHeadRequests)
trackedPeers := ex.peerTracker.getPeers(maxUntrustedHeadRequests)
if len(trackedPeers) > 0 {
peers = trackedPeers
log.Debugw("requesting head from tracked peers", "amount", len(peers))
}
}

var (
zero H
headerReq = &p2p_pb.HeaderRequest{
Data: &p2p_pb.HeaderRequest_Origin{Origin: uint64(0)},
Amount: 1,
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, error) {
if !isExpired(storeHead, s.Params.TrustingPeriod) {
return storeHead, nil
}
// otherwise, request head from a trusted peer via subjective initialization
// otherwise, request head from a trusted peer
log.Infow("stored head header expired", "height", storeHead.Height())
// single-flight protection
// ensure only one Head is requested at the time
Expand Down

0 comments on commit fa3c102

Please sign in to comment.