Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var (
utils.Web3QMainnetFlag,
utils.ValPortFlag,
utils.ValNodeKeyFlag,
utils.ValMaxPeerCountFlag,
utils.VMEnableDebugFlag,
utils.NetworkIdFlag,
utils.EthStatsURLFlag,
Expand Down
4 changes: 3 additions & 1 deletion cmd/tendermint/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
timeoutCommitMs *uint64
consensusSyncMs *uint64
proposerRepetition *uint64
maxPeerCount *int
)

var NodeCmd = &cobra.Command{
Expand Down Expand Up @@ -73,6 +74,7 @@ func init() {
consensusSyncMs = NodeCmd.Flags().Uint64("consensusSyncMs", 500, "Consensus sync in ms")
proposerRepetition = NodeCmd.Flags().Uint64("proposerRepetition", 8, "proposer repetition")

maxPeerCount = NodeCmd.Flags().Int("maxPeerCount", 10, "proposer repetition")
}

func runNode(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -191,7 +193,7 @@ func runNode(cmd *cobra.Command, args []string) {
bs := NewDefaultBlockStore(db)
executor := consensus.NewDefaultBlockExecutor(db)

p2pserver, err := p2p.NewP2PServer(rootCtx, bs, obsvC, sendC, p2pPriv, *p2pPort, *p2pNetworkID, *p2pBootstrap, *nodeName, rootCtxCancel)
p2pserver, err := p2p.NewP2PServer(rootCtx, bs, obsvC, sendC, p2pPriv, *p2pPort, *p2pNetworkID, *p2pBootstrap, *nodeName, rootCtxCancel, *maxPeerCount)

go func() {
p2pserver.Run(rootCtx)
Expand Down
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ var (
Name: "validator.nodekey",
Usage: "Validator P2P node key file",
}
ValMaxPeerCountFlag = cli.IntFlag{
Name: "validator.maxpeercount",
Usage: "Validator P2P max peer count",
}
DeveloperFlag = cli.BoolFlag{
Name: "dev",
Usage: "Ephemeral proof-of-authority network with a pre-funded developer account, mining enabled",
Expand Down Expand Up @@ -1427,6 +1431,9 @@ func setTendermint(ctx *cli.Context, cfg *ethconfig.Config) {
if ctx.GlobalIsSet(ValNodeKeyFlag.Name) {
cfg.ValNodeKey = ctx.GlobalString(ValNodeKeyFlag.Name)
}
if ctx.GlobalIsSet(ValMaxPeerCountFlag.Name) {
cfg.MaxPeerCount = ctx.GlobalInt(ValMaxPeerCountFlag.Name)
}
}

func setMiner(ctx *cli.Context, cfg *miner.Config) {
Expand Down
64 changes: 64 additions & 0 deletions consensus/tendermint/p2p/conngater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package p2p

import (
"fmt"
"github.com/ethereum/go-ethereum/log"
"github.com/libp2p/go-libp2p-core/control"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"sync"
)

type connGater struct {
sync.RWMutex
h host.Host
MaxPeerCount int
}

func (cg *connGater) getConnectedPeerCount() int {
c := 0
if cg.h != nil {
for index, p := range cg.h.Network().Peers() {
if cg.h.Network().Connectedness(p) == network.Connected {
log.Debug("Connected Peer List", "index", index, "peer id", p.String())
c++
}
}
}
return c
}

func (cg *connGater) isPeerAtLimit() bool {
cg.RLock()
defer cg.RUnlock()
if cg.getConnectedPeerCount() >= cg.MaxPeerCount {
log.Info(fmt.Sprintf("PeerCount %d exceeds the MaxPeerCount %d.\r\n", len(cg.h.Network().Peers()), cg.MaxPeerCount))
return true
}
return false
}

func (cg *connGater) InterceptPeerDial(p peer.ID) (allow bool) {
log.Debug("InterceptPeerDial", "peer id", p)
return cg.h.Network().Connectedness(p) == network.Connected || !cg.isPeerAtLimit()
}

func (cg *connGater) InterceptAddrDial(p peer.ID, a ma.Multiaddr) (allow bool) {
log.Debug("InterceptAddrDial", "peer id", p)
return cg.h.Network().Connectedness(p) == network.Connected || !cg.isPeerAtLimit()
}

func (cg *connGater) InterceptAccept(cma network.ConnMultiaddrs) (allow bool) {
return true
}

func (cg *connGater) InterceptSecured(dir network.Direction, p peer.ID, cma network.ConnMultiaddrs) (allow bool) {
log.Debug("InterceptSecured", "peer id", p)
return cg.h.Network().Connectedness(p) == network.Connected || !cg.isPeerAtLimit()
}

func (cg *connGater) InterceptUpgraded(network.Conn) (allow bool, reason control.DisconnectReason) {
return true, 0
}
5 changes: 5 additions & 0 deletions consensus/tendermint/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ func NewP2PServer(
bootstrapPeers string,
nodeName string,
rootCtxCancel context.CancelFunc,
maxPeerCount int,
) (*Server, error) {
cg := &connGater{h: nil, MaxPeerCount: maxPeerCount}
h, err := libp2p.New(ctx,
// Use the keypair we generated
libp2p.Identity(priv),
Expand All @@ -331,6 +333,8 @@ func NewP2PServer(
fmt.Sprintf("/ip6/::/udp/%d/quic", port),
),

libp2p.ConnectionGater(cg),

// Enable TLS security as the only security protocol.
libp2p.Security(libp2ptls.ID, libp2ptls.New),

Expand Down Expand Up @@ -362,6 +366,7 @@ func NewP2PServer(

log.Info("Connecting to bootstrap peers", "bootstrap_peers", bootstrapPeers)

cg.h = h
// Add our own bootstrap nodes

// Count number of successful connection attempts. If we fail to connect to any bootstrap peer, kill
Expand Down
2 changes: 1 addition & 1 deletion consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (c *Tendermint) Init(chain *core.BlockChain, makeBlock func(parent common.H
}

// p2p server
p2pserver, err := libp2p.NewP2PServer(rootCtx, store, obsvC, sendC, p2pPriv, c.config.P2pPort, c.config.NetworkID, c.config.P2pBootstrap, c.config.NodeName, rootCtxCancel)
p2pserver, err := libp2p.NewP2PServer(rootCtx, store, obsvC, sendC, p2pPriv, c.config.P2pPort, c.config.NetworkID, c.config.P2pBootstrap, c.config.NodeName, rootCtxCancel, c.config.MaxPeerCount)
if err != nil {
return
}
Expand Down
4 changes: 4 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if config.ValP2pPort != 0 {
chainConfig.Tendermint.P2pPort = config.ValP2pPort
}
// Setup MaxPeerCount
if config.MaxPeerCount != 0 {
chainConfig.Tendermint.MaxPeerCount = config.MaxPeerCount
}
// Setup p2p node key
// TODO: Use node key in default datadir
if config.ValNodeKey != "" {
Expand Down
5 changes: 3 additions & 2 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ type Config struct {
OverrideTerminalTotalDifficulty *big.Int `toml:",omitempty"`

// Validator config
ValP2pPort uint
ValNodeKey string
ValP2pPort uint
ValNodeKey string
MaxPeerCount int
}

// CreateConsensusEngine creates a consensus engine for the given chain configuration.
Expand Down
1 change: 1 addition & 0 deletions miner/stress/tendermint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core
genesis.GasLimit = 25000000
genesis.Config.Tendermint.P2pPort = 0
genesis.Config.Tendermint.P2pBootstrap = ""
genesis.Config.Tendermint.MaxPeerCount = 50

genesis.Alloc = core.GenesisAlloc{}
for _, faucet := range faucets {
Expand Down
2 changes: 2 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ var (
Tendermint: &TendermintConfig{
Epoch: 100800, // expect 6s block interval = one week
P2pPort: 33333,
MaxPeerCount: 50,
ProposerRepetition: 8,
P2pBootstrap: strings.Join(Web3QGalileoValBootnodes, ","),
NodeKeyPath: "",
Expand Down Expand Up @@ -474,6 +475,7 @@ type TendermintConfig struct {
Epoch uint64 `json:"epoch"` // Epoch lengh to vote new validator
NodeKeyPath string
P2pPort uint
MaxPeerCount int
NetworkID string
P2pBootstrap string
NodeName string
Expand Down