github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/peer_scorer_test.go (about)

     1  package p2p_test
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     8  	peer "github.com/libp2p/go-libp2p/core/peer"
     9  	suite "github.com/stretchr/testify/suite"
    10  
    11  	log "github.com/ethereum/go-ethereum/log"
    12  
    13  	p2p "github.com/ethereum-optimism/optimism/op-node/p2p"
    14  	p2pMocks "github.com/ethereum-optimism/optimism/op-node/p2p/mocks"
    15  	"github.com/ethereum-optimism/optimism/op-node/p2p/store"
    16  	"github.com/ethereum-optimism/optimism/op-node/rollup"
    17  	"github.com/ethereum-optimism/optimism/op-service/testlog"
    18  )
    19  
    20  // PeerScorerTestSuite tests peer parameterization.
    21  type PeerScorerTestSuite struct {
    22  	suite.Suite
    23  
    24  	mockStore    *p2pMocks.Peerstore
    25  	mockMetricer *p2pMocks.ScoreMetrics
    26  	logger       log.Logger
    27  }
    28  
    29  // SetupTest sets up the test suite.
    30  func (testSuite *PeerScorerTestSuite) SetupTest() {
    31  	testSuite.mockStore = &p2pMocks.Peerstore{}
    32  	testSuite.mockMetricer = &p2pMocks.ScoreMetrics{}
    33  	testSuite.logger = testlog.Logger(testSuite.T(), log.LevelError)
    34  }
    35  
    36  // TestPeerScorer runs the PeerScorerTestSuite.
    37  func TestPeerScorer(t *testing.T) {
    38  	suite.Run(t, new(PeerScorerTestSuite))
    39  }
    40  
    41  // TestScorer_SnapshotHook tests running the snapshot hook on the peer scorer.
    42  func (testSuite *PeerScorerTestSuite) TestScorer_SnapshotHook() {
    43  	scorer := p2p.NewScorer(
    44  		&rollup.Config{L2ChainID: big.NewInt(123)},
    45  		testSuite.mockStore,
    46  		testSuite.mockMetricer,
    47  		&p2p.NoopApplicationScorer{},
    48  		testSuite.logger,
    49  	)
    50  	inspectFn := scorer.SnapshotHook()
    51  
    52  	scores := store.PeerScores{Gossip: store.GossipScores{Total: 3}}
    53  	// Expect updating the peer store
    54  	testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: float64(-100)}).Return(scores, nil).Once()
    55  
    56  	// The metricer should then be called with the peer score band map
    57  	testSuite.mockMetricer.On("SetPeerScores", []store.PeerScores{scores}).Return(nil).Once()
    58  
    59  	// Apply the snapshot
    60  	snapshotMap := map[peer.ID]*pubsub.PeerScoreSnapshot{
    61  		peer.ID("peer1"): {
    62  			Score: -100,
    63  		},
    64  	}
    65  	inspectFn(snapshotMap)
    66  
    67  	// Expect updating the peer store
    68  	testSuite.mockStore.On("SetScore", peer.ID("peer1"), &store.GossipScores{Total: 0}).Return(scores, nil).Once()
    69  
    70  	// The metricer should then be called with the peer score band map
    71  	testSuite.mockMetricer.On("SetPeerScores", []store.PeerScores{scores}).Return(nil).Once()
    72  
    73  	// Apply the snapshot
    74  	snapshotMap = map[peer.ID]*pubsub.PeerScoreSnapshot{
    75  		peer.ID("peer1"): {
    76  			Score: 0,
    77  		},
    78  	}
    79  	inspectFn(snapshotMap)
    80  }