Skip to content

Commit

Permalink
chore: apply suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan <ryan@celestia.org>
  • Loading branch information
vgonkivs and distractedm1nd committed Dec 19, 2022
1 parent 3b043f9 commit 9a5e2df
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 7 additions & 2 deletions header/p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func TestExchange_RequestFullRangeHeaders(t *testing.T) {
servers[index], err = NewExchangeServer(hosts[index], store, protocolSuffix)
require.NoError(t, err)
servers[index].Start(context.Background()) //nolint:errcheck
exchange.peerTracker.peerLk.Lock()
exchange.peerTracker.trackedPeers[hosts[index].ID()] = &peerStat{peerID: hosts[index].ID()}
exchange.peerTracker.peerLk.Unlock()
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
Expand Down Expand Up @@ -140,13 +142,15 @@ func TestExchange_RequestHeadersFromAnotherPeer(t *testing.T) {
t.Cleanup(func() {
serverSideEx.Stop(context.Background()) //nolint:errcheck
})
// add higher score to peer that does not have the requesting range.
exchg.peerTracker.trackedPeers[hosts[1].ID()] = &peerStat{peerID: hosts[1].ID(), peerScore: 100}
exchg.peerTracker.peerLk.Lock()
exchg.peerTracker.trackedPeers[hosts[2].ID()] = &peerStat{peerID: hosts[2].ID(), peerScore: 20}
exchg.peerTracker.peerLk.Unlock()
_, err = exchg.GetRangeByHeight(context.Background(), 5, 3)
require.NoError(t, err)
exchg.peerTracker.peerLk.Lock()
// ensure that peerScore for the second peer is changed
newPeerScore := exchg.peerTracker.trackedPeers[hosts[2].ID()].peerScore
exchg.peerTracker.peerLk.Unlock()
require.NotEqual(t, 20, newPeerScore)
}

Expand Down Expand Up @@ -312,6 +316,7 @@ func createP2PExAndServer(t *testing.T, host, tpeer libhost.Host) (*Exchange, *m
require.NoError(t, err)
ex, err := NewExchange(host, []peer.ID{tpeer.ID()}, "private", connGater)
require.NoError(t, err)
ex.peerTracker.trackedPeers[tpeer.ID()] = &peerStat{peerID: tpeer.ID(), peerScore: 100}
require.NoError(t, ex.Start(context.Background()))

t.Cleanup(func() {
Expand Down
4 changes: 2 additions & 2 deletions header/p2p/peer_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func (p *peerTracker) stop() {
}
}

// blockPeer blocks peer on the networking level and removes it from the local cache.
// blockPeer blocks a peer on the networking level and removes it from the local cache.
func (p *peerTracker) blockPeer(pID peer.ID) {
// add peer tho the blacklist, so we can't connect to it in the future.
// add peer to the blacklist, so we can't connect to it in the future.
err := p.connGater.BlockPeer(pID)
if err != nil {
log.Errorw("header/p2p: blocking peer failed", "err", err, "pID", pID)
Expand Down
5 changes: 5 additions & 0 deletions header/p2p/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type session struct {
// peerTracker contains discovered peers with records that describes their activity.
peerTracker *peerTracker

// `from` is set when additional validation for range is needed.
// Otherwise, it will be nil.
from *header.ExtendedHeader

ctx context.Context
Expand Down Expand Up @@ -191,10 +193,12 @@ func (s *session) processResponse(responses []*p2p_pb.ExtendedHeaderResponse) ([

// validate checks that the received range of headers is valid against the provided header.
func (s *session) validate(headers []*header.ExtendedHeader) error {
// if `from` is empty, then additional validation for the header`s range is not needed.
if s.from == nil {
return nil
}

// verify that the first header in range is valid against the trusted header.
err := s.from.VerifyNonAdjacent(headers[0])
if err != nil {
return nil
Expand All @@ -205,6 +209,7 @@ func (s *session) validate(headers []*header.ExtendedHeader) error {
}

trusted := headers[0]
// verify that the whole range is valid.
for i := 1; i < len(headers); i++ {
err = trusted.VerifyAdjacent(headers[i])
if err != nil {
Expand Down

0 comments on commit 9a5e2df

Please sign in to comment.