Skip to content

Commit

Permalink
Merge pull request #5345 from multiversx/feat/multiple_p2p_messengers
Browse files Browse the repository at this point in the history
Feat/multiple p2p messengers
  • Loading branch information
sstanculeanu authored Jul 21, 2023
2 parents b001a85 + 33486c2 commit c24eeef
Show file tree
Hide file tree
Showing 195 changed files with 4,819 additions and 2,535 deletions.
16 changes: 14 additions & 2 deletions api/groups/nodeGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type nodeFacadeHandler interface {
GetQueryHandler(name string) (debug.QueryHandler, error)
GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error)
GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatings() string
GetConnectedPeersRatingsOnMainNetwork() (string, error)
GetManagedKeysCount() int
GetManagedKeys() []string
GetEligibleManagedKeys() ([]string, error)
Expand Down Expand Up @@ -355,7 +355,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()
ratings, err := ng.getFacade().GetConnectedPeersRatingsOnMainNetwork()
if err != nil {
c.JSON(
http.StatusInternalServerError,
shared.GenericAPIResponse{
Data: nil,
Error: err.Error(),
Code: shared.ReturnCodeInternalError,
},
)
return
}

c.JSON(
http.StatusOK,
shared.GenericAPIResponse{
Expand Down
56 changes: 31 additions & 25 deletions api/groups/nodeGroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,35 +280,41 @@ 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)
},
}
func TestNodeGroup_GetConnectedPeersRatings(t *testing.T) {
t.Parallel()

nodeGroup, err := groups.NewNodeGroup(&facade)
require.NoError(t, err)
t.Run("should work", func(t *testing.T) {
t.Parallel()

ws := startWebServer(nodeGroup, "node", getNodeRoutesConfig())
providedRatings := map[string]string{
"pid1": "100",
"pid2": "-50",
"pid3": "-5",
}
buff, _ := json.Marshal(providedRatings)
facade := mock.FacadeStub{
GetConnectedPeersRatingsOnMainNetworkCalled: func() (string, error) {
return string(buff), nil
},
}

req, _ := http.NewRequest("GET", "/node/connected-peers-ratings", nil)
resp := httptest.NewRecorder()
ws.ServeHTTP(resp, req)
nodeGroup, err := groups.NewNodeGroup(&facade)
require.NoError(t, err)

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)
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) {
Expand Down
8 changes: 4 additions & 4 deletions api/mock/facadeStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type FacadeStub struct {
GetValueForKeyCalled func(address string, key string, options api.AccountQueryOptions) (string, api.BlockInfo, error)
GetGuardianDataCalled func(address string, options api.AccountQueryOptions) (api.GuardianData, api.BlockInfo, error)
GetPeerInfoCalled func(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatingsCalled func() string
GetConnectedPeersRatingsOnMainNetworkCalled func() (string, error)
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 @@ -388,9 +388,9 @@ func (f *FacadeStub) GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error) {
return f.GetPeerInfoCalled(pid)
}

// GetConnectedPeersRatings -
func (f *FacadeStub) GetConnectedPeersRatings() string {
return f.GetConnectedPeersRatingsCalled()
// GetConnectedPeersRatingsOnMainNetwork -
func (f *FacadeStub) GetConnectedPeersRatingsOnMainNetwork() (string, error) {
return f.GetConnectedPeersRatingsOnMainNetworkCalled()
}

// GetEpochStartDataAPI -
Expand Down
2 changes: 1 addition & 1 deletion api/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type FacadeHandler interface {
GetQueryHandler(name string) (debug.QueryHandler, error)
GetEpochStartDataAPI(epoch uint32) (*common.EpochStartDataAPI, error)
GetPeerInfo(pid string) ([]core.QueryP2PPeerInfo, error)
GetConnectedPeersRatings() string
GetConnectedPeersRatingsOnMainNetwork() (string, error)
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
2 changes: 2 additions & 0 deletions cmd/node/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ GLOBAL OPTIONS:
--config-preferences [path] The [path] for the preferences configuration file. This TOML file contains preferences configurations, such as the node display name or the shard to start in when starting as observer (default: "./config/prefs.toml")
--config-external [path] The [path] for the external configuration file. This TOML file contains external configurations such as ElasticSearch's URL and login information (default: "./config/external.toml")
--p2p-config [path] The [path] for the p2p configuration file. This TOML file contains peer-to-peer configurations such as port, target peer count or KadDHT settings (default: "./config/p2p.toml")
--full-archive-p2p-config [path] The [path] for the p2p configuration file for the full archive network. This TOML file contains peer-to-peer configurations such as port, target peer count or KadDHT settings (default: "./config/fullArchiveP2P.toml")
--epoch-config [path] The [path] for the epoch configuration file. This TOML file contains activation epochs configurations (default: "./config/enableEpochs.toml")
--round-config [path] The [path] for the round configuration file. This TOML file contains activation round configurations (default: "./config/enableRounds.toml")
--gas-costs-config [path] The [path] for the gas costs configuration directory. (default: "./config/gasSchedules")
--sk-index value The index in the PEM file of the private key to be used by the node. (default: 0)
--validator-key-pem-file filepath The filepath for the PEM file which contains the secret keys for the validator key. (default: "./config/validatorKey.pem")
--all-validator-keys-pem-file filepath The filepath for the PEM file which contains all the secret keys managed by the current node. (default: "./config/allValidatorsKeys.pem")
--port [p2p port] The [p2p port] number on which the application will start. Can use single values such as `0, 10230, 15670` or range of ports such as `5000-10000` (default: "0")
--full-archive-port [p2p port] The [p2p port] number on which the application will start the second network when running in full archive mode. Can use single values such as `0, 10230, 15670` or range of ports such as `5000-10000` (default: "0")
--profile-mode Boolean option for enabling the profiling mode. If set, the /debug/pprof routes will be available on the node for profiling the application.
--use-health-service Boolean option for enabling the health service.
--storage-cleanup Boolean option for starting the node with clean storage. If set, the Node will empty its storage before starting, otherwise it will start from the last state stored on disk..
Expand Down
72 changes: 72 additions & 0 deletions cmd/node/config/fullArchiveP2P.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# FullArchiveP2P config file

# NodeConfig holds the P2P settings
[Node]
# Port is the port that will be opened by the node on all interfaces so other peers can connect to it
# If the port = 0, the node will search for a free port on the machine and use it
Port = "37373-38383"

# ThresholdMinConnectedPeers represents the minimum number of connections a node should have before it can start
# the sync and consensus mechanisms
ThresholdMinConnectedPeers = 3

# MinNumPeersToWaitForOnBootstrap is the minimum number of peers to wait on bootstrap or the node will wait the default
# time which is now set to ~20 seconds (the const defined in the common package named TimeToWaitForP2PBootstrap)
MinNumPeersToWaitForOnBootstrap = 10

# P2P peer discovery section

# The following sections correspond to the way new peers will be discovered
# If all config types are disabled then the peer will run in single mode (will not try to find other peers)
# If more than one peer discovery mechanism is enabled, the application will output an error and will not start

[KadDhtPeerDiscovery]
# Enabled: true/false to enable/disable this discovery mechanism
Enabled = true

# Type represents the kad-dht glue code implementation.
# "legacy" will define the first implementation.
# "optimized" represents the new variant able to connect to multiple seeders at once. This implementation also has
# a built-in timer that will try to automatically reconnect to the seeders (in case the seeders recover after a
# premature shutdown)
Type = "optimized"

# RefreshIntervalInSec represents the time in seconds between querying for new peers
RefreshIntervalInSec = 10

# ProtocolID represents the protocol that this node will advertize to other peers
# To connect to other nodes, those nodes should have the same ProtocolID string
ProtocolID = "/erd/kad/1.0.0"

# InitialPeerList represents the list of strings of some known nodes that will bootstrap this node
# The address will be in a self-describing addressing format.
# More can be found here: https://github.com/libp2p/specs/blob/master/3-requirements.md#34-transport-agnostic
# Example:
# /ip6/fe80::8823:6dff:fee7:f172/tcp/4001/p2p/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
# /ip4/162.246.145.218/udp/4001/utp/ipfs/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
#
# If the initial peers list is left empty, the node will not try to connect to other peers during initial bootstrap
# phase but will accept connections and will do the network discovery if another peer connects to it
InitialPeerList = ["/ip4/127.0.0.1/tcp/9999/p2p/16Uiu2HAkw5SNNtSvH1zJiQ6Gc3WoGNSxiyNueRKe6fuAuh57G3Bk"]

# kademlia's routing table bucket size
BucketSize = 100

# RoutingTableRefreshIntervalInSec defines how many seconds should pass between 2 kad routing table auto refresh calls
RoutingTableRefreshIntervalInSec = 300

[Sharding]
# The targeted number of peer connections
TargetPeerCount = 36
MaxIntraShardValidators = 7
MaxCrossShardValidators = 15
MaxIntraShardObservers = 2
MaxCrossShardObservers = 3
MaxSeeders = 2

# available options:
# `ListsSharder` will split the peers based on the shard membership (intra, cross or unknown)
# `OneListSharder` will do just the connection triming (upto TargetPeerCount value) not taking into account
# the shard membership of the connected peers
# `NilListSharder` will disable conection trimming (sharder is off)
Type = "ListsSharder"
70 changes: 33 additions & 37 deletions cmd/node/config/p2p.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#P2P config file
# P2P config file

#NodeConfig holds the P2P settings
# NodeConfig holds the P2P settings
[Node]
#Port is the port that will be opened by the node on all interfaces so other peers can connect to it
#If the port = 0, the node will search for a free port on the machine and use it
# Port is the port that will be opened by the node on all interfaces so other peers can connect to it
# If the port = 0, the node will search for a free port on the machine and use it
Port = "37373-38383"

#ThresholdMinConnectedPeers represents the minimum number of connections a node should have before it can start
#the sync and consensus mechanisms
# ThresholdMinConnectedPeers represents the minimum number of connections a node should have before it can start
# the sync and consensus mechanisms
ThresholdMinConnectedPeers = 3

# MinNumPeersToWaitForOnBootstrap is the minimum number of peers to wait on bootstrap or the node will wait the default
Expand All @@ -16,43 +16,43 @@

# P2P peer discovery section

#The following sections correspond to the way new peers will be discovered
#If all config types are disabled then the peer will run in single mode (will not try to find other peers)
#If more than one peer discovery mechanism is enabled, the application will output an error and will not start
# The following sections correspond to the way new peers will be discovered
# If all config types are disabled then the peer will run in single mode (will not try to find other peers)
# If more than one peer discovery mechanism is enabled, the application will output an error and will not start

[KadDhtPeerDiscovery]
#Enabled: true/false to enable/disable this discovery mechanism
# Enabled: true/false to enable/disable this discovery mechanism
Enabled = true

#Type represents the kad-dht glue code implementation.
#"legacy" will define the first implementation.
#"optimized" represents the new variant able to connect to multiple seeders at once. This implementation also has
#a built-in timer that will try to automatically reconnect to the seeders (in case the seeders recover after a
#premature shutdown)
# Type represents the kad-dht glue code implementation.
# "legacy" will define the first implementation.
# "optimized" represents the new variant able to connect to multiple seeders at once. This implementation also has
# a built-in timer that will try to automatically reconnect to the seeders (in case the seeders recover after a
# premature shutdown)
Type = "optimized"

#RefreshIntervalInSec represents the time in seconds between querying for new peers
# RefreshIntervalInSec represents the time in seconds between querying for new peers
RefreshIntervalInSec = 10

#ProtocolID represents the protocol that this node will advertize to other peers
#To connect to other nodes, those nodes should have the same ProtocolID string
# ProtocolID represents the protocol that this node will advertize to other peers
# To connect to other nodes, those nodes should have the same ProtocolID string
ProtocolID = "/erd/kad/1.0.0"

#InitialPeerList represents the list of strings of some known nodes that will bootstrap this node
#The address will be in a self-describing addressing format.
#More can be found here: https://github.com/libp2p/specs/blob/master/3-requirements.md#34-transport-agnostic
#Example:
# /ip6/fe80::8823:6dff:fee7:f172/tcp/4001/p2p/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
# /ip4/162.246.145.218/udp/4001/utp/ipfs/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
# InitialPeerList represents the list of strings of some known nodes that will bootstrap this node
# The address will be in a self-describing addressing format.
# More can be found here: https://github.com/libp2p/specs/blob/master/3-requirements.md#34-transport-agnostic
# Example:
# /ip6/fe80::8823:6dff:fee7:f172/tcp/4001/p2p/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
# /ip4/162.246.145.218/udp/4001/utp/ipfs/QmYJyUMAcXEw1b5bFfbBbzYu5wyyjLMRHXGUkCXpag74Fu
#
#If the initial peers list is left empty, the node will not try to connect to other peers during initial bootstrap
#phase but will accept connections and will do the network discovery if another peer connects to it
# If the initial peers list is left empty, the node will not try to connect to other peers during initial bootstrap
# phase but will accept connections and will do the network discovery if another peer connects to it
InitialPeerList = ["/ip4/127.0.0.1/tcp/9999/p2p/16Uiu2HAkw5SNNtSvH1zJiQ6Gc3WoGNSxiyNueRKe6fuAuh57G3Bk"]

#kademlia's routing table bucket size
# kademlia's routing table bucket size
BucketSize = 100

#RoutingTableRefreshIntervalInSec defines how many seconds should pass between 2 kad routing table auto refresh calls
# RoutingTableRefreshIntervalInSec defines how many seconds should pass between 2 kad routing table auto refresh calls
RoutingTableRefreshIntervalInSec = 300

[Sharding]
Expand All @@ -64,13 +64,9 @@
MaxCrossShardObservers = 3
MaxSeeders = 2

#available options:
# `ListsSharder` will split the peers based on the shard membership (intra, cross or unknown)
# `OneListSharder` will do just the connection triming (upto TargetPeerCount value) not taking into account
# the shard membership of the connected peers
# `NilListSharder` will disable conection trimming (sharder is off)
# available options:
# `ListsSharder` will split the peers based on the shard membership (intra, cross or unknown)
# `OneListSharder` will do just the connection triming (upto TargetPeerCount value) not taking into account
# the shard membership of the connected peers
# `NilListSharder` will disable conection trimming (sharder is off)
Type = "ListsSharder"

[AdditionalConnections]
#this value will be added to the target peer count automatically when the node will be in full archive mode
MaxFullHistoryObservers = 10
Loading

0 comments on commit c24eeef

Please sign in to comment.