Skip to content

Commit

Permalink
refactor(network): refactor peer manager and redefine the min cons (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored and themantre committed May 5, 2024
1 parent 438a137 commit 87de4b5
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 189 deletions.
28 changes: 14 additions & 14 deletions config/bootstrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"name": "JohnVNBnode",
"email": "[email protected]",
"website": "docs.vnbnode.com",
"address": "/ip4/62.171.130.196/tcp/21888/p2p/12D3KooWHvTVCcGqqYRnAexgE1VGqXiGAZd9vuLGs4YgGtHmRucN"
"address": "/ip4/62.171.130.196/tcp/21888/p2p/12D3KooWSkwB8AiWz3VGyG5cgC3ry5dmCw3TthQNc1jvRNe9EEKH"
},
{
"name": "NodeSync_Top",
Expand Down Expand Up @@ -93,13 +93,13 @@
"name": "Validator247",
"email": "[email protected]",
"website": "https://validator247.com/",
"address": "/ip4/65.21.180.80/tcp/21888/p2p/12D3KooWJWKXjrLg3GeA8x98Zc9Q7iUawPyZFcKmy4PWJNzu1u21"
"address": "/ip4/65.21.180.80/tcp/21888/p2p/12D3KooWANqay6T1xCrK3imfAEr3GpiGzxT6SWCEsRapo8tJWAZN"
},
{
"name": "BearValidator",
"email": "[email protected]",
"website": "https://validator.wiki/",
"address": "/ip4/37.27.26.40/tcp/21888/p2p/12D3KooWGtBFHXraHUTBPYfPhC8LKRYN2YcUafgVaLhPR53h41jg"
"address": "/ip4/37.27.26.40/tcp/21888/p2p/12D3KooWPehhWgfdZG6x9zE7QdQxuAcfbQPAyBPjcXLfVYfU9FAd"
},
{
"name": "MoneyLow-VNBnode",
Expand All @@ -117,7 +117,7 @@
"name": "CherryValidator",
"email": "[email protected]",
"website": "https://cherryvalidator.us",
"address": "/ip4/65.21.155.128/tcp/21888/p2p/12D3KooWHrEWfv52wHhN9tK11RLGeMDyWCerw3Z7eMAr4KPXFAu5"
"address": "/ip4/65.21.155.128/tcp/21888/p2p/12D3KooWPykSnzWpEasRijCgQ99vxAxWKL32zY24MmvtaXsHapSf"
},
{
"name": "TeoViTeoVi",
Expand All @@ -135,13 +135,13 @@
"name": "tiantsi",
"email": "[email protected]",
"website": "n/a",
"address": "/ip4/43.135.166.197/tcp/21888/p2p/12D3KooWQkDzRDb7MNrAKStt2NTbHxY1hui2fnu1ABKJGWjz4JQa"
"address": "/ip4/43.135.166.197/tcp/21888/p2p/12D3KooWEqyjMNzFe92HFZMZQd3YZ6zvztuPaC3a4wAUvPf4moyP"
},
{
"name": "vinhhathacha",
"email": "[email protected]",
"website": "https://github.com/vinhhathacha",
"address": "/ip4/46.250.233.5/tcp/21888/p2p/12D3KooWDQGDBUtYF2fagCAGVphrQNB2fpg1LidaJ9GFXhoKcrGS"
"address": "/ip4/46.250.233.5/tcp/21888/p2p/12D3KooWH1kam9g1QFWmtWMLzP2MDY4ptpLTMPU8wSq3Q4k3rvnn"
},
{
"name": "CodeBlockLabs",
Expand Down Expand Up @@ -183,7 +183,7 @@
"name": "Toncatuyvu",
"email": "[email protected]",
"website": "n/a",
"address": "/ip4/109.123.240.5/tcp/21888/p2p/12D3KooWJM6tRpWbVxuoDiLg1bDPoCZMX17gqRNDjPEEA7Vj2Ddz"
"address": "/ip4/109.123.240.5/tcp/21888/p2p/12D3KooWCT92yaS4JgrHHzeRPsJxaLzRdSEknGEN4HxQFfscoY8k"
},
{
"name": "TPES",
Expand All @@ -201,7 +201,7 @@
"name": "Wanderersg2014",
"email": "[email protected]",
"website": "n/a",
"address": "/ip4/109.123.253.88/tcp/21888/p2p/12D3KooWPFUshoRYHcw281vmuKv8kPoXF2GZUW6incQ7wU1jgzQa"
"address": "/ip4/109.123.253.88/tcp/21888/p2p/12D3KooWFUJhDjYcAXgEnW1pTLRDWnH9CLgBPcezgU7pCpxMngt8"
},
{
"name": "node-life",
Expand Down Expand Up @@ -249,13 +249,13 @@
"name": "duyenhtm",
"email": "[email protected]",
"website": "",
"address": "/ip4/85.190.246.251/tcp/21888/p2p/12D3KooWFEm9ZNxthmR3bJKe5RCS9rjw2wLL1FYRAh251KNvpnQM"
"address": "/ip4/85.190.246.251/tcp/21888/p2p/12D3KooWMTPKpr6EHMmZ2NSXkup4NaqwKnYmScHEsN3HpcqYgBZL"
},
{
"name": "minhtoanbui135",
"email": "[email protected]",
"website": "n/a",
"address": "/ip4/45.134.226.48/tcp/21888/p2p/12D3KooWP9dFS5QdSCZaPYVrcpXf5YbNyon5RchLohMVYUxZJZgZ"
"address": "/ip4/45.134.226.48/tcp/21888/p2p/12D3KooWBSP8AdiHkkvPunpbhfZE96YTY6om4hw35Xpe3bJFBUjh"
},
{
"name": "NodeOps",
Expand All @@ -273,7 +273,7 @@
"name": "5t0q6pr",
"email": "[email protected]",
"website": "n/a",
"address": "/ip4/167.86.116.61/tcp/21888/p2p/12D3KooWAijtinK9cepHGQF7vYGHQ9jLrYhfoTMdJzK9xeKuxChK"
"address": "/ip4/167.86.116.61/tcp/21888/p2p/12D3KooWAmc69XaFh6T76BQQaFmSakYFgDyvzDLfVBgTzqmNtyXM"
},
{
"name": "00PactucValidator32",
Expand Down Expand Up @@ -303,7 +303,7 @@
"name": "Thomas Jr",
"email": "[email protected]",
"website": "catsmile.tech",
"address": "/ip4/194.163.189.255/tcp/21888/p2p/12D3KooWF54PkAE34dRkC3WaiAgpL7HMnRFi1nW8iPnSH8nKLtZ9"
"address": "/ip4/194.163.189.255/tcp/21888/p2p/12D3KooWExbN79BrgDWjdLUd8TZaP2LvCDN8zJkMuPhRHkjKDcJS"
},
{
"name": "Winnode",
Expand Down Expand Up @@ -357,7 +357,7 @@
"name": "hainguyen0094_22439",
"email": "[email protected]",
"website": "",
"address": "/ip4/94.72.120.50/tcp/21888/p2p/12D3KooWQ6rsLNG1RpkkeLQj1HeqthYEuouDN8b7PnX4cMboYe6k"
"address": "/ip4/94.72.120.50/tcp/21888/p2p/12D3KooWCJX9aLnzmEFtEasvbbaFqHLTboFVFptZxeFnbnSxpb2m"
},
{
"name": "anpjwq",
Expand All @@ -369,6 +369,6 @@
"name": "Dezh Technologies",
"email": "[email protected]",
"website": "https://dezh.tech",
"address": "/dns/pactus-bootstrap1.dezh.tech/tcp/21888/p2p/12D3KooWL8yQcyzk1kU31q76KTSVRB8SUtoK8vJrsp8oFbKvWH4N"
"address": "/dns/pactus-bootstrap1.dezh.tech/tcp/21888/p2p/12D3KooWK1z7QAskVrQd98r98UMSPwTLr4out8B9NkQTrCdZPCZx"
}
]
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func DefaultConfigLocalnet() *Config {
conf.Network.EnableNATService = false
conf.Network.EnableUPnP = false
conf.Network.BootstrapAddrStrings = []string{}
conf.Network.MaxConns = 0
conf.Network.MaxConns = 16
conf.Network.NetworkName = "pactus-localnet"
conf.Network.DefaultPort = 0
conf.Network.ForcePrivateNetwork = true
Expand Down
12 changes: 8 additions & 4 deletions execution/executor/bond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func TestExecuteBondTx(t *testing.T) {
senderBalance := senderAcc.Balance()
pub, _ := td.RandBLSKeyPair()
receiverAddr := pub.ValidatorAddress()
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, td.sandbox.TestParams.MaximumStake)
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake,
td.sandbox.TestParams.MaximumStake-10e9)
lockTime := td.sandbox.CurrentHeight()

t.Run("Should fail, invalid sender", func(t *testing.T) {
Expand Down Expand Up @@ -114,7 +115,8 @@ func TestBondInsideCommittee(t *testing.T) {
exe1 := NewBondExecutor(true)
exe2 := NewBondExecutor(false)
senderAddr, _ := td.sandbox.TestStore.RandomTestAcc()
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, td.sandbox.TestParams.MaximumStake)
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake,
td.sandbox.TestParams.MaximumStake-10e9)
lockTime := td.sandbox.CurrentHeight()

pub := td.sandbox.Committee().Proposer(0).PublicKey()
Expand All @@ -135,7 +137,8 @@ func TestBondJoiningCommittee(t *testing.T) {
exe2 := NewBondExecutor(false)
senderAddr, _ := td.sandbox.TestStore.RandomTestAcc()
pub, _ := td.RandBLSKeyPair()
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, td.sandbox.TestParams.MaximumStake)
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake,
td.sandbox.TestParams.MaximumStake-10e9)
lockTime := td.sandbox.CurrentHeight()

val := td.sandbox.MakeNewValidator(pub)
Expand Down Expand Up @@ -178,7 +181,8 @@ func TestPowerDeltaBond(t *testing.T) {
senderAddr, _ := td.sandbox.TestStore.RandomTestAcc()
pub, _ := td.RandBLSKeyPair()
receiverAddr := pub.ValidatorAddress()
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake, td.sandbox.TestParams.MaximumStake)
amt, fee := td.randomAmountAndFee(td.sandbox.TestParams.MinimumStake,
td.sandbox.TestParams.MaximumStake-10e9)
lockTime := td.sandbox.CurrentHeight()
trx := tx.NewBondTx(lockTime, senderAddr,
receiverAddr, pub, amt, fee, "ok")
Expand Down
13 changes: 7 additions & 6 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (conf *Config) BasicCheck() error {
Reason: "both the relay and relay service cannot be active at the same time",
}
}
if conf.MaxConns < 16 {
return ConfigError{
Reason: "maximum connection should be greater than 16",
}
}

return validateAddrInfo(conf.BootstrapAddrStrings...)
}
Expand Down Expand Up @@ -138,10 +143,6 @@ func (conf *Config) CheckIsBootstrapper(pid lp2pcore.PeerID) {
}
}

func (conf *Config) ScaledMaxConns() int {
return util.LogScale(conf.MaxConns)
}

func (conf *Config) ScaledMinConns() int {
return conf.ScaledMaxConns() / 4
func (conf *Config) MinConns() int {
return (conf.MaxConns / 4) - 2
}
29 changes: 16 additions & 13 deletions network/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func TestConfigBasicCheck(t *testing.T) {
c.BootstrapAddrStrings = []string{"/ip4/127.0.0.1/"}
},
},
{
name: "Low MaxConns - Expect Error",
expectError: ConfigError{
Reason: "maximum connection should be greater than 16",
},
updateFn: func(c *Config) {
c.MaxConns = 8
},
},
{
name: "Valid Public Address - No Error",
expectError: nil,
Expand Down Expand Up @@ -134,28 +143,22 @@ func TestIsBootstrapper(t *testing.T) {
assert.True(t, conf.IsBootstrapper)
}

func TestScaledConns(t *testing.T) {
func TestMinConns(t *testing.T) {
tests := []struct {
config Config
expectedMax int
expectedMin int
}{
{Config{MaxConns: 1}, 1, 0},
{Config{MaxConns: 8}, 8, 2},
{Config{MaxConns: 30}, 32, 8},
{Config{MaxConns: 1000}, 1024, 256},
{Config{MaxConns: 16}, 2},
{Config{MaxConns: 30}, 5},
{Config{MaxConns: 128}, 30},
}

for _, test := range tests {
resultMax := test.config.ScaledMaxConns()
resultMin := test.config.ScaledMinConns()
if resultMax != test.expectedMax ||
resultMin != test.expectedMin {
resultMin := test.config.MinConns()
if resultMin != test.expectedMin {
t.Errorf("For MaxConns %d, "+
"NormedMaxConns() returned %d (expected %d), "+
"NormedMinConns() returned %d (expected %d)",
"MinConns() returned %d (expected %d)",
test.config.MaxConns,
resultMax, test.expectedMax,
resultMin, test.expectedMin)
}
}
Expand Down
14 changes: 7 additions & 7 deletions network/gater.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func NewConnectionGater(conf *Config, log *logger.SubLogger) (*ConnectionGater,
filters = SubnetsToFilters(privateSubnets, multiaddr.ActionDeny)
}

acceptLimit := conf.ScaledMaxConns()
dialLimit := conf.ScaledMaxConns() / 4
acceptLimit := conf.MaxConns
dialLimit := conf.MaxConns / 4
log.Info("connection gater created", "listen", acceptLimit, "dial", dialLimit)

return &ConnectionGater{
Expand Down Expand Up @@ -70,7 +70,7 @@ func (g *ConnectionGater) InterceptPeerDial(pid lp2ppeer.ID) bool {
defer g.lk.RUnlock()

if g.onDialLimit() {
g.logger.Info("InterceptPeerDial rejected: many connections",
g.logger.Debug("InterceptPeerDial rejected: many connections",
"pid", pid, "outbound", g.peerMgr.NumOutbound())

return false
Expand All @@ -84,15 +84,15 @@ func (g *ConnectionGater) InterceptAddrDial(pid lp2ppeer.ID, ma multiaddr.Multia
defer g.lk.RUnlock()

if g.onDialLimit() {
g.logger.Info("InterceptAddrDial rejected: many connections",
g.logger.Debug("InterceptAddrDial rejected: many connections",
"pid", pid, "ma", ma.String(), "outbound", g.peerMgr.NumOutbound())

return false
}

deny := g.filters.AddrBlocked(ma)
if deny {
g.logger.Info("InterceptAddrDial rejected", "pid", pid, "ma", ma.String())
g.logger.Debug("InterceptAddrDial rejected", "pid", pid, "ma", ma.String())

return false
}
Expand All @@ -105,15 +105,15 @@ func (g *ConnectionGater) InterceptAccept(cma lp2pnetwork.ConnMultiaddrs) bool {
defer g.lk.RUnlock()

if g.onAcceptLimit() {
g.logger.Info("InterceptAccept rejected: many connections",
g.logger.Debug("InterceptAccept rejected: many connections",
"inbound", g.peerMgr.NumInbound())

return false
}

deny := g.filters.AddrBlocked(cma.RemoteMultiaddr())
if deny {
g.logger.Info("InterceptAccept rejected")
g.logger.Debug("InterceptAccept rejected")

return false
}
Expand Down
15 changes: 7 additions & 8 deletions network/gater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func TestMaxConnection(t *testing.T) {
ts := testsuite.NewTestSuite(t)
conf := testConfig()
conf.MaxConns = 4
assert.Equal(t, conf.ScaledMaxConns(), 4)
net := makeTestNetwork(t, conf, nil)

maPrivate := multiaddr.StringCast("/ip4/127.0.0.1/tcp/1234")
Expand All @@ -72,27 +71,27 @@ func TestMaxConnection(t *testing.T) {
cmaPublic := &mockConnMultiaddrs{remote: maPublic}
pid := ts.RandPeerID()

net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirOutbound)
net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirOutbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)

assert.True(t, net.connGater.InterceptPeerDial(pid))
assert.True(t, net.connGater.InterceptAddrDial(pid, maPrivate))
assert.True(t, net.connGater.InterceptAddrDial(pid, maPublic))
assert.True(t, net.connGater.InterceptAccept(cmaPrivate))
assert.True(t, net.connGater.InterceptAccept(cmaPublic))

net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirOutbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirOutbound)

assert.False(t, net.connGater.InterceptPeerDial(pid))
assert.False(t, net.connGater.InterceptAddrDial(pid, maPrivate))
assert.False(t, net.connGater.InterceptAddrDial(pid, maPublic))
assert.True(t, net.connGater.InterceptAccept(cmaPrivate))
assert.True(t, net.connGater.InterceptAccept(cmaPublic))

net.peerMgr.AddPeer(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)
net.peerMgr.PeerConnected(ts.RandPeerID(), aMultiAddr, lp2pnetwork.DirInbound)

assert.False(t, net.connGater.InterceptPeerDial(pid))
assert.False(t, net.connGater.InterceptAddrDial(pid, maPrivate))
Expand Down
5 changes: 2 additions & 3 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ func newNetwork(conf *Config, log *logger.SubLogger, opts []lp2p.Option) (*netwo
// https://github.com/libp2p/go-libp2p/issues/2616
// The connection manager doesn't reject any connections.
// It just triggers a pruning run once the high watermark is reached (or surpassed).

//
lowWM := conf.ScaledMinConns() // Low Watermark, ex: 64 (max)
highWM := conf.ScaledMaxConns() - conf.ScaledMinConns() // High Watermark, ex: 64 (max) - 16 (min) = 48
lowWM := conf.MinConns() // Low Watermark, ex: 14 (if max_conn = 64)
highWM := conf.MaxConns + conf.MinConns() // High Watermark, ex: 78 (if max_conn = 64)
connMgr, err := lp2pconnmgr.NewConnManager(
lowWM, highWM,
lp2pconnmgr.WithGracePeriod(time.Minute),
Expand Down
Loading

0 comments on commit 87de4b5

Please sign in to comment.