Skip to content

Commit

Permalink
Merge 5bf61b9 into d3225fe
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Mar 15, 2023
2 parents d3225fe + 5bf61b9 commit f68e00c
Show file tree
Hide file tree
Showing 38 changed files with 253 additions and 241 deletions.
38 changes: 29 additions & 9 deletions api/groups/nodeGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (
)

const (
pidQueryParam = "pid"
debugPath = "/debug"
heartbeatStatusPath = "/heartbeatstatus"
metricsPath = "/metrics"
p2pStatusPath = "/p2pstatus"
peerInfoPath = "/peerinfo"
statusPath = "/status"
epochStartDataForEpoch = "/epoch-start/:epoch"
bootstrapStatusPath = "/bootstrapstatus"
pidQueryParam = "pid"
debugPath = "/debug"
heartbeatStatusPath = "/heartbeatstatus"
metricsPath = "/metrics"
p2pStatusPath = "/p2pstatus"
peerInfoPath = "/peerinfo"
statusPath = "/status"
epochStartDataForEpoch = "/epoch-start/:epoch"
bootstrapStatusPath = "/bootstrapstatus"
connectedPeersRatingsPath = "/connected-peers-ratings"
)

// nodeFacadeHandler defines the methods to be implemented by a facade for node requests
Expand All @@ -35,6 +36,7 @@ type nodeFacadeHandler interface {
GetQueryHandler(name string) (debug.QueryHandler, error)
GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error)
GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatings() string
IsInterfaceNil() bool
}

Expand Down Expand Up @@ -102,6 +104,11 @@ func NewNodeGroup(facade nodeFacadeHandler) (*nodeGroup, error) {
Method: http.MethodGet,
Handler: ng.bootstrapMetrics,
},
{
Path: connectedPeersRatingsPath,
Method: http.MethodGet,
Handler: ng.connectedPeersRatings,
},
}
ng.endpoints = endpoints

Expand Down Expand Up @@ -318,6 +325,19 @@ func (ng *nodeGroup) bootstrapMetrics(c *gin.Context) {
)
}

// connectedPeersRatings returns the node's connected peers ratings
func (ng *nodeGroup) connectedPeersRatings(c *gin.Context) {
ratings := ng.getFacade().GetConnectedPeersRatings()
c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Data: gin.H{"ratings": ratings},
Error: "",
Code: shared.ReturnCodeSuccess,
},
)
}

func (ng *nodeGroup) getFacade() nodeFacadeHandler {
ng.mutFacade.RLock()
defer ng.mutFacade.RUnlock()
Expand Down
32 changes: 32 additions & 0 deletions api/groups/nodeGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,37 @@ func TestBootstrapStatusMetrics_ShouldWork(t *testing.T) {
assert.True(t, valuesFound)
}

func TestBootstrapGetConnectedPeersRatings_ShouldWork(t *testing.T) {
providedRatings := map[string]string{
"pid1": "100",
"pid2": "-50",
"pid3": "-5",
}
buff, _ := json.Marshal(providedRatings)
facade := mock.FacadeStub{
GetConnectedPeersRatingsCalled: func() string {
return string(buff)
},
}

nodeGroup, err := groups.NewNodeGroup(&facade)
require.NoError(t, err)

ws := startWebServer(nodeGroup, "node", getNodeRoutesConfig())

req, _ := http.NewRequest("GET", "/node/connected-peers-ratings", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)

response := &shared.GenericAPIResponse{}
loadResponse(resp.Body, response)
respMap, ok := response.Data.(map[string]interface{})
assert.True(t, ok)
ratings, ok := respMap["ratings"].(string)
assert.True(t, ok)
assert.Equal(t, string(buff), ratings)
}

func TestStatusMetrics_ShouldDisplayNonP2pMetrics(t *testing.T) {
statusMetricsProvider := statusHandler.NewStatusMetrics()
key := "test-details-key"
Expand Down Expand Up @@ -595,6 +626,7 @@ func getNodeRoutesConfig() config.ApiRoutesConfig {
{Name: "/peerinfo", Open: true},
{Name: "/epoch-start/:epoch", Open: true},
{Name: "/bootstrapstatus", Open: true},
{Name: "/connected-peers-ratings", Open: true},
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions api/mock/facadeStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type FacadeStub struct {
GetQueryHandlerCalled func(name string) (debug.QueryHandler, error)
GetValueForKeyCalled func(address string, key string, options api.AccountQueryOptions) (string, api.BlockInfo, error)
GetPeerInfoCalled func(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatingsCalled func() string
GetEpochStartDataAPICalled func(epoch uint32) (*common.EpochStartDataAPI, error)
GetThrottlerForEndpointCalled func(endpoint string) (core.Throttler, bool)
GetUsernameCalled func(address string, options api.AccountQueryOptions) (string, api.BlockInfo, error)
Expand Down Expand Up @@ -371,6 +372,11 @@ func (f *FacadeStub) GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error) {
return f.GetPeerInfoCalled(pid)
}

// GetConnectedPeersRatings -
func (f *FacadeStub) GetConnectedPeersRatings() string {
return f.GetConnectedPeersRatingsCalled()
}

// GetEpochStartDataAPI -
func (f *FacadeStub) GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error) {
return f.GetEpochStartDataAPICalled(epoch)
Expand Down
1 change: 1 addition & 0 deletions api/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type FacadeHandler interface {
GetQueryHandler(name string) (debug.QueryHandler, error)
GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error)
GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatings() string
GetProof(rootHash string, address string) (*common.GetProofResponse, error)
GetProofDataTrie(rootHash string, address string, key string) (*common.GetProofResponse, *common.GetProofResponse, error)
GetProofCurrentRootHash(address string) (*common.GetProofResponse, error)
Expand Down
5 changes: 4 additions & 1 deletion cmd/node/config/api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
{ Name = "/epoch-start/:epoch", Open = true },

# /node/bootstrapstatus will return all metrics available during bootstrap
{ Name = "/bootstrapstatus", Open = true }
{ Name = "/bootstrapstatus", Open = true },

# /node/connected-peers-ratings will return the peers' ratings
{ Name = "/connected-peers-ratings", Open = true }
]

[APIPackages.address]
Expand Down
3 changes: 0 additions & 3 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,6 @@
[PeersRatingConfig]
TopRatedCacheCapacity = 5000
BadRatedCacheCapacity = 5000
TimeWaitingForReconnectionInSec = 300 # 5 min
TimeBetweenMetricsUpdateInSec = 60 # 1 min
TimeBetweenCachersSweepInSec = 60 # 1 min

[PoolsCleanersConfig]
MaxRoundsToKeepUnprocessedMiniBlocks = 300 # max number of rounds unprocessed miniblocks are kept in pool
Expand Down
5 changes: 0 additions & 5 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package common
import (
"math"
"time"

"github.com/multiversx/mx-chain-go/p2p"
)

// PeerType represents the type of a peer
Expand Down Expand Up @@ -654,9 +652,6 @@ const MetricP2PUnknownPeers = "erd_p2p_unknown_shard_peers"
// MetricP2PNumConnectedPeersClassification is the metric for monitoring the number of connected peers split on the connection type
const MetricP2PNumConnectedPeersClassification = "erd_p2p_num_connected_peers_classification"

// MetricP2PPeersRating is the metric that outputs the peers ratings
const MetricP2PPeersRating = p2p.MetricP2PPeersRating

// MetricAreVMQueriesReady will hold the string representation of the boolean that indicated if the node is ready
// to process VM queries
const MetricAreVMQueriesReady = "erd_are_vm_queries_ready"
Expand Down
7 changes: 2 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,8 @@ type Config struct {

// PeersRatingConfig will hold settings related to peers rating
type PeersRatingConfig struct {
TopRatedCacheCapacity int
BadRatedCacheCapacity int
TimeWaitingForReconnectionInSec int64
TimeBetweenMetricsUpdateInSec int64
TimeBetweenCachersSweepInSec int64
TopRatedCacheCapacity int
BadRatedCacheCapacity int
}

// LogsConfig will hold settings related to the logging sub-system
Expand Down
2 changes: 0 additions & 2 deletions dataRetriever/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,9 @@ type PreferredPeersHolderHandler interface {

// PeersRatingHandler represent an entity able to handle peers ratings
type PeersRatingHandler interface {
AddPeers(pids []core.PeerID)
IncreaseRating(pid core.PeerID)
DecreaseRating(pid core.PeerID)
GetTopRatedPeersFromList(peers []core.PeerID, minNumOfPeersExpected int) []core.PeerID
Close() error
IsInterfaceNil() bool
}

Expand Down
9 changes: 0 additions & 9 deletions epochStart/bootstrap/disabled/disabledPeersRatingHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ func NewDisabledPeersRatingHandler() *disabledPeersRatingHandler {
return &disabledPeersRatingHandler{}
}

// AddPeers does nothing as it is disabled
func (dprs *disabledPeersRatingHandler) AddPeers(_ []core.PeerID) {
}

// IncreaseRating does nothing as it is disabled
func (dprs *disabledPeersRatingHandler) IncreaseRating(_ core.PeerID) {
}
Expand All @@ -27,11 +23,6 @@ func (dprs *disabledPeersRatingHandler) GetTopRatedPeersFromList(peers []core.Pe
return peers
}

// Close returns the nil as it is disabled
func (dprs *disabledPeersRatingHandler) Close() error {
return nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (dprs *disabledPeersRatingHandler) IsInterfaceNil() bool {
return dprs == nil
Expand Down
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,6 @@ var ErrNilGenesisNodesSetupHandler = errors.New("nil genesis nodes setup handler

// ErrNilPeersRatingHandler signals that a nil peers rating handler implementation has been provided
var ErrNilPeersRatingHandler = errors.New("nil peers rating handler")

// ErrNilPeersRatingMonitor signals that a nil peers rating monitor implementation has been provided
var ErrNilPeersRatingMonitor = errors.New("nil peers rating monitor")
5 changes: 5 additions & 0 deletions facade/initial/initialNodeFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ func (inf *initialNodeFacade) GetPeerInfo(_ string) ([]core.QueryP2PPeerInfo, er
return nil, errNodeStarting
}

// GetConnectedPeersRatings returns empty string
func (inf *initialNodeFacade) GetConnectedPeersRatings() string {
return ""
}

// GetEpochStartDataAPI returns nil and error
func (inf *initialNodeFacade) GetEpochStartDataAPI(_ uint32) (*common.EpochStartDataAPI, error) {
return nil, errNodeStarting
Expand Down
1 change: 1 addition & 0 deletions facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type NodeHandler interface {

GetQueryHandler(name string) (debug.QueryHandler, error)
GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatings() string

GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error)

Expand Down
10 changes: 10 additions & 0 deletions facade/mock/nodeStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type NodeStub struct {
GetQueryHandlerCalled func(name string) (debug.QueryHandler, error)
GetValueForKeyCalled func(address string, key string, options api.AccountQueryOptions) (string, api.BlockInfo, error)
GetPeerInfoCalled func(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatingsCalled func() string
GetEpochStartDataAPICalled func(epoch uint32) (*common.EpochStartDataAPI, error)
GetUsernameCalled func(address string, options api.AccountQueryOptions) (string, api.BlockInfo, error)
GetCodeHashCalled func(address string, options api.AccountQueryOptions) ([]byte, api.BlockInfo, error)
Expand Down Expand Up @@ -204,6 +205,15 @@ func (ns *NodeStub) GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error) {
return make([]core.QueryP2PPeerInfo, 0), nil
}

// GetConnectedPeersRatings -
func (ns *NodeStub) GetConnectedPeersRatings() string {
if ns.GetConnectedPeersRatingsCalled != nil {
return ns.GetConnectedPeersRatingsCalled()
}

return ""
}

// GetEpochStartDataAPI -
func (ns *NodeStub) GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error) {
if ns.GetEpochStartDataAPICalled != nil {
Expand Down
5 changes: 5 additions & 0 deletions facade/nodeFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ func (nf *nodeFacade) GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error) {
return nf.node.GetPeerInfo(pid)
}

// GetConnectedPeersRatings returns the connected peers ratings
func (nf *nodeFacade) GetConnectedPeersRatings() string {
return nf.node.GetConnectedPeersRatings()
}

// GetThrottlerForEndpoint returns the throttler for a given endpoint if found
func (nf *nodeFacade) GetThrottlerForEndpoint(endpoint string) (core.Throttler, bool) {
if !nf.wsAntifloodConfig.WebServerAntifloodEnabled {
Expand Down
1 change: 1 addition & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type NetworkComponentsHolder interface {
PeerHonestyHandler() PeerHonestyHandler
PreferredPeersHolderHandler() PreferredPeersHolderHandler
PeersRatingHandler() p2p.PeersRatingHandler
PeersRatingMonitor() p2p.PeersRatingMonitor
IsInterfaceNil() bool
}

Expand Down
6 changes: 6 additions & 0 deletions factory/mock/networkComponentsMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type NetworkComponentsMock struct {
PeerBlackList process.PeerBlackListCacher
PreferredPeersHolder factory.PreferredPeersHolderHandler
PeersRatingHandlerField p2p.PeersRatingHandler
PeersRatingMonitorField p2p.PeersRatingMonitor
}

// PubKeyCacher -
Expand Down Expand Up @@ -71,6 +72,11 @@ func (ncm *NetworkComponentsMock) PeersRatingHandler() p2p.PeersRatingHandler {
return ncm.PeersRatingHandlerField
}

// PeersRatingMonitor -
func (ncm *NetworkComponentsMock) PeersRatingMonitor() p2p.PeersRatingMonitor {
return ncm.PeersRatingMonitorField
}

// IsInterfaceNil -
func (ncm *NetworkComponentsMock) IsInterfaceNil() bool {
return ncm == nil
Expand Down
23 changes: 14 additions & 9 deletions factory/network/networkComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type networkComponents struct {
peerHonestyHandler consensus.PeerHonestyHandler
peersHolder factory.PreferredPeersHolderHandler
peersRatingHandler p2p.PeersRatingHandler
peersRatingMonitor p2p.PeersRatingMonitor
closeFunc context.CancelFunc
}

Expand Down Expand Up @@ -123,12 +124,8 @@ func (ncf *networkComponentsFactory) Create() (*networkComponents, error) {
return nil, err
}
argsPeersRatingHandler := p2pFactory.ArgPeersRatingHandler{
TopRatedCache: topRatedCache,
BadRatedCache: badRatedCache,
AppStatusHandler: ncf.statusHandler,
TimeWaitingForReconnection: time.Duration(peersRatingCfg.TimeWaitingForReconnectionInSec) * time.Second,
TimeBetweenMetricsUpdate: time.Duration(peersRatingCfg.TimeBetweenMetricsUpdateInSec) * time.Second,
TimeBetweenCachersSweep: time.Duration(peersRatingCfg.TimeBetweenCachersSweepInSec) * time.Second,
TopRatedCache: topRatedCache,
BadRatedCache: badRatedCache,
}
peersRatingHandler, err := p2pFactory.NewPeersRatingHandler(argsPeersRatingHandler)
if err != nil {
Expand Down Expand Up @@ -160,6 +157,16 @@ func (ncf *networkComponentsFactory) Create() (*networkComponents, error) {
}
}()

argsPeersRatingMonitor := p2pFactory.ArgPeersRatingMonitor{
TopRatedCache: topRatedCache,
BadRatedCache: badRatedCache,
ConnectionsProvider: netMessenger,
}
peersRatingMonitor, err := p2pFactory.NewPeersRatingMonitor(argsPeersRatingMonitor)
if err != nil {
return nil, err
}

var antiFloodComponents *antifloodFactory.AntiFloodComponents
antiFloodComponents, err = antifloodFactory.NewP2PAntiFloodComponents(ctx, ncf.mainConfig, ncf.statusHandler, netMessenger.ID())
if err != nil {
Expand Down Expand Up @@ -227,6 +234,7 @@ func (ncf *networkComponentsFactory) Create() (*networkComponents, error) {
peerHonestyHandler: peerHonestyHandler,
peersHolder: ph,
peersRatingHandler: peersRatingHandler,
peersRatingMonitor: peersRatingMonitor,
closeFunc: cancelFunc,
}, nil
}
Expand Down Expand Up @@ -261,9 +269,6 @@ func (nc *networkComponents) Close() error {
if !check.IfNil(nc.peerHonestyHandler) {
log.LogIfError(nc.peerHonestyHandler.Close())
}
if !check.IfNil(nc.peersRatingHandler) {
log.LogIfError(nc.peersRatingHandler.Close())
}

if nc.netMessenger != nil {
log.Debug("calling close on the network messenger instance...")
Expand Down

0 comments on commit f68e00c

Please sign in to comment.