Skip to content

Commit

Permalink
Merge branch 'master' into tong/fee
Browse files Browse the repository at this point in the history
  • Loading branch information
37ng authored Aug 19, 2021
2 parents de41a67 + 0390b45 commit ff35883
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
12 changes: 6 additions & 6 deletions cmd/mycelo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,16 @@ func readWorkdir(ctx *cli.Context) (string, error) {
}

func readGethPath(ctx *cli.Context) (string, error) {
buildpath := ctx.String(gethPathFlag.Name)
if buildpath == "" {
buildpath = path.Join(os.Getenv("CELO_BLOCKCHAIN"), "build/bin/geth")
if fileutils.FileExists(buildpath) {
log.Info("Missing --geth flag, using CELO_BLOCKCHAIN derived path", "geth", buildpath)
gethPath := ctx.String(gethPathFlag.Name)
if gethPath == "" {
gethPath = path.Join(os.Getenv("CELO_BLOCKCHAIN"), "build/bin/geth")
if fileutils.FileExists(gethPath) {
log.Info("Missing --geth flag, using CELO_BLOCKCHAIN derived path", "geth", gethPath)
} else {
return "", fmt.Errorf("Missing --geth flag")
}
}
return buildpath, nil
return gethPath, nil
}

func readEnv(ctx *cli.Context) (*env.Environment, error) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,9 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
if lightClient {
ethPeers = 0
}
if lightServer {
cfg.MaxLightClients = lightPeers
}
log.Info("Maximum peer count", "ETH", ethPeers, "LES", lightPeers, "total", cfg.MaxPeers)

if ctx.GlobalIsSet(MaxPendingPeersFlag.Name) {
Expand Down
2 changes: 1 addition & 1 deletion p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (d *dialScheduler) expireHistory() {
// when peers are connected while their task is still running, or because static dials
// are exempt from the limit.
func (d *dialScheduler) freeDialSlots() int {
slots := (d.maxDialPeers - d.dialPeers) * 2
slots := d.maxDialPeers - d.dialPeers
if slots > d.maxActiveDials {
slots = d.maxActiveDials
}
Expand Down
52 changes: 23 additions & 29 deletions p2p/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestDialSchedDynDial(t *testing.T) {
t.Parallel()

config := dialConfig{
maxActiveDials: 5,
maxActiveDials: 2,
maxDialPeers: 4,
}
runDialTest(t, config, []dialTestRound{
// 3 out of 4 peers are connected, leaving 2 dial slots.
// 9 nodes are discovered, but only 2 are dialed.
// 3 out of 4 peers are connected, leaving 1 dial slot.
// 9 nodes are discovered, but only 1 is dialed.
{
update: func(d *dialScheduler) {
d.addStatic(newNode(uintID(0x01), "127.0.0.1:30303"))
Expand All @@ -58,61 +58,55 @@ func TestDialSchedDynDial(t *testing.T) {
newNode(uintID(0x01), "127.0.0.1:30303"), // not dialed because already connected as static peer
newNode(uintID(0x03), "127.0.0.1:30303"), // ...
newNode(uintID(0x04), "127.0.0.1:30303"),
newNode(uintID(0x05), "127.0.0.1:30303"),
newNode(uintID(0x06), "127.0.0.1:30303"), // not dialed because there are only two slots
newNode(uintID(0x05), "127.0.0.1:30303"), // not dialed because there is only one slot
newNode(uintID(0x06), "127.0.0.1:30303"), // ...
newNode(uintID(0x07), "127.0.0.1:30303"), // ...
newNode(uintID(0x08), "127.0.0.1:30303"), // ...
newNode(uintID(0x09), "127.0.0.1:30303"), // ...
},
wantNewDials: []*enode.Node{
newNode(uintID(0x04), "127.0.0.1:30303"),
newNode(uintID(0x05), "127.0.0.1:30303"),
},
},

// One dial completes, freeing one dial slot.
{
failed: []enode.ID{
uintID(0x05),
uintID(0x04),
},
wantNewDials: []*enode.Node{
newNode(uintID(0x06), "127.0.0.1:30303"),
newNode(uintID(0x05), "127.0.0.1:30303"),
},
},

// Dial to 0x03 completes, filling the last remaining peer slot.
// Dial to 0x05 completes, filling the last remaining peer slot.
{
succeeded: []enode.ID{
uintID(0x04),
},
failed: []enode.ID{
uintID(0x06),
},
discovered: []*enode.Node{
newNode(uintID(0x0a), "127.0.0.1:30303"), // not dialed because there are no free slots
uintID(0x05),
},
},

// 3 peers drop off, creating 6 dial slots. Check that 5 of those slots
// 3 peers drop off, creating 3 dial slots. Check that 2 of those slots
// (i.e. up to maxActiveDialTasks) are used.
{
peersRemoved: []enode.ID{
uintID(0x02),
uintID(0x03),
uintID(0x04),
},
discovered: []*enode.Node{
newNode(uintID(0x0b), "127.0.0.1:30303"),
newNode(uintID(0x0c), "127.0.0.1:30303"),
newNode(uintID(0x0d), "127.0.0.1:30303"),
newNode(uintID(0x0e), "127.0.0.1:30303"),
uintID(0x05),
},
wantNewDials: []*enode.Node{
newNode(uintID(0x06), "127.0.0.1:30303"),
newNode(uintID(0x07), "127.0.0.1:30303"),
},
},
// The two dials succeed, check that we now dial to fill the 3rd slot
{
succeeded: []enode.ID{
uintID(0x06),
uintID(0x07),
},
wantNewDials: []*enode.Node{
newNode(uintID(0x08), "127.0.0.1:30303"),
newNode(uintID(0x09), "127.0.0.1:30303"),
newNode(uintID(0x0a), "127.0.0.1:30303"),
newNode(uintID(0x0b), "127.0.0.1:30303"),
},
},
})
Expand Down Expand Up @@ -210,14 +204,14 @@ func TestDialSchedStaticDial(t *testing.T) {
uintID(0x06): nil,
},
},
// Peer 0x01 drops, freeing two dial slots. They're filled by 0x01 (static) and 0x07
// Peer 0x01 drops, freeing one dial slots. It's filled by 0x01 (static).
// 0x07 is not dialed since there is only one slot.
{
peersRemoved: []enode.ID{
uintID(0x01),
},
wantNewDials: []*enode.Node{
newNode(uintID(0x01), "127.0.0.1:30303"),
newNode(uintID(0x07), "127.0.0.7:30303"),
},
},
// 0x05 and 0x06 will be dialed now, having expired from the history
Expand Down
11 changes: 8 additions & 3 deletions p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ type Config struct {
// connected. It must be greater than zero.
MaxPeers int

// MaxLightClients is the maximum number of light clients that can be connected.
// It is zero if the LES server isn't running (includes the case that we are an LES client).
MaxLightClients int

// MaxPendingPeers is the maximum number of peers that can be pending in the
// handshake phase, counted separately for inbound and outbound connections.
// Zero defaults to preset values.
Expand Down Expand Up @@ -732,10 +736,11 @@ func (srv *Server) maxDialedConns() (limit int) {
if srv.NoDial || srv.MaxPeers == 0 {
return 0
}
maxEthPeers := srv.MaxPeers - srv.MaxLightClients
if srv.DialRatio == 0 {
limit = srv.MaxPeers / defaultDialRatio
limit = maxEthPeers / defaultDialRatio
} else {
limit = srv.MaxPeers / srv.DialRatio
limit = maxEthPeers / srv.DialRatio
}
if limit == 0 {
limit = 1
Expand Down Expand Up @@ -780,7 +785,7 @@ func (srv *Server) doPeerOp(fn peerOpFunc) {

// run is the main loop of the server.
func (srv *Server) run() {
srv.log.Info("Started P2P networking", "self", srv.localnode.Node().URLv4())
srv.log.Info("Started P2P networking", "self", srv.localnode.Node().URLv4(), "maxdialed", srv.maxDialedConns(), "maxinbound", srv.maxInboundConns())
defer srv.loopWG.Done()
defer srv.nodedb.Close()
defer srv.discmix.Close()
Expand Down

0 comments on commit ff35883

Please sign in to comment.