2018-08-14 20:44:46 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2019-02-26 11:32:48 +00:00
|
|
|
"strconv"
|
2018-08-14 20:44:46 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2018-09-24 12:57:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2018-08-14 20:44:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFreeClientPoolL10C100(t *testing.T) {
|
|
|
|
testFreeClientPool(t, 10, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFreeClientPoolL40C200(t *testing.T) {
|
|
|
|
testFreeClientPool(t, 40, 200)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFreeClientPoolL100C300(t *testing.T) {
|
|
|
|
testFreeClientPool(t, 100, 300)
|
|
|
|
}
|
|
|
|
|
|
|
|
const testFreeClientPoolTicks = 500000
|
|
|
|
|
|
|
|
func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
|
|
|
|
var (
|
2019-02-26 11:32:48 +00:00
|
|
|
clock mclock.Simulated
|
2018-09-24 12:57:49 +00:00
|
|
|
db = rawdb.NewMemoryDatabase()
|
2019-02-26 11:32:48 +00:00
|
|
|
connected = make([]bool, clientCount)
|
|
|
|
connTicks = make([]int, clientCount)
|
|
|
|
disconnCh = make(chan int, clientCount)
|
|
|
|
peerAddress = func(i int) string {
|
|
|
|
return fmt.Sprintf("addr #%d", i)
|
|
|
|
}
|
|
|
|
peerId = func(i int) string {
|
|
|
|
return fmt.Sprintf("id #%d", i)
|
|
|
|
}
|
|
|
|
disconnFn = func(id string) {
|
|
|
|
i, err := strconv.Atoi(id[4:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-14 20:44:46 +00:00
|
|
|
disconnCh <- i
|
|
|
|
}
|
2019-05-30 18:51:13 +00:00
|
|
|
pool = newFreeClientPool(db, 1, 10000, &clock, disconnFn, nil, nil)
|
2019-02-26 11:32:48 +00:00
|
|
|
)
|
|
|
|
pool.setLimits(connLimit, uint64(connLimit))
|
2018-08-14 20:44:46 +00:00
|
|
|
|
|
|
|
// pool should accept new peers up to its connected limit
|
|
|
|
for i := 0; i < connLimit; i++ {
|
2019-02-26 11:32:48 +00:00
|
|
|
if pool.connect(peerAddress(i), peerId(i)) {
|
2018-08-14 20:44:46 +00:00
|
|
|
connected[i] = true
|
|
|
|
} else {
|
|
|
|
t.Fatalf("Test peer #%d rejected", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// since all accepted peers are new and should not be kicked out, the next one should be rejected
|
2019-02-26 11:32:48 +00:00
|
|
|
if pool.connect(peerAddress(connLimit), peerId(connLimit)) {
|
2018-08-14 20:44:46 +00:00
|
|
|
connected[connLimit] = true
|
|
|
|
t.Fatalf("Peer accepted over connected limit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// randomly connect and disconnect peers, expect to have a similar total connection time at the end
|
|
|
|
for tickCounter := 0; tickCounter < testFreeClientPoolTicks; tickCounter++ {
|
|
|
|
clock.Run(1 * time.Second)
|
|
|
|
|
|
|
|
i := rand.Intn(clientCount)
|
|
|
|
if connected[i] {
|
2019-02-26 11:32:48 +00:00
|
|
|
pool.disconnect(peerAddress(i))
|
2018-08-14 20:44:46 +00:00
|
|
|
connected[i] = false
|
|
|
|
connTicks[i] += tickCounter
|
|
|
|
} else {
|
2019-02-26 11:32:48 +00:00
|
|
|
if pool.connect(peerAddress(i), peerId(i)) {
|
2018-08-14 20:44:46 +00:00
|
|
|
connected[i] = true
|
|
|
|
connTicks[i] -= tickCounter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pollDisconnects:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case i := <-disconnCh:
|
2019-02-26 11:32:48 +00:00
|
|
|
pool.disconnect(peerAddress(i))
|
2018-08-14 20:44:46 +00:00
|
|
|
if connected[i] {
|
|
|
|
connTicks[i] += tickCounter
|
|
|
|
connected[i] = false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break pollDisconnects
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expTicks := testFreeClientPoolTicks * connLimit / clientCount
|
|
|
|
expMin := expTicks - expTicks/10
|
|
|
|
expMax := expTicks + expTicks/10
|
|
|
|
|
|
|
|
// check if the total connected time of peers are all in the expected range
|
|
|
|
for i, c := range connected {
|
|
|
|
if c {
|
|
|
|
connTicks[i] += testFreeClientPoolTicks
|
|
|
|
}
|
|
|
|
if connTicks[i] < expMin || connTicks[i] > expMax {
|
|
|
|
t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a previously unknown peer should be accepted now
|
2019-02-26 11:32:48 +00:00
|
|
|
if !pool.connect("newAddr", "newId") {
|
2018-08-14 20:44:46 +00:00
|
|
|
t.Fatalf("Previously unknown peer rejected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// close and restart pool
|
|
|
|
pool.stop()
|
2019-05-30 18:51:13 +00:00
|
|
|
pool = newFreeClientPool(db, 1, 10000, &clock, disconnFn, nil, nil)
|
2019-02-26 11:32:48 +00:00
|
|
|
pool.setLimits(connLimit, uint64(connLimit))
|
2018-08-14 20:44:46 +00:00
|
|
|
|
|
|
|
// try connecting all known peers (connLimit should be filled up)
|
|
|
|
for i := 0; i < clientCount; i++ {
|
2019-02-26 11:32:48 +00:00
|
|
|
pool.connect(peerAddress(i), peerId(i))
|
2018-08-14 20:44:46 +00:00
|
|
|
}
|
|
|
|
// expect pool to remember known nodes and kick out one of them to accept a new one
|
2019-02-26 11:32:48 +00:00
|
|
|
if !pool.connect("newAddr2", "newId2") {
|
2018-08-14 20:44:46 +00:00
|
|
|
t.Errorf("Previously unknown peer rejected after restarting pool")
|
|
|
|
}
|
|
|
|
pool.stop()
|
|
|
|
}
|