|
|
@@ -14,7 +14,7 @@
|
|
|
// 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
|
|
|
+package server
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
@@ -24,12 +24,13 @@ import (
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
|
|
- vfs "github.com/ethereum/go-ethereum/les/vflux/server"
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
"github.com/ethereum/go-ethereum/p2p/nodestate"
|
|
|
)
|
|
|
|
|
|
+const defaultConnectedBias = time.Minute * 3
|
|
|
+
|
|
|
func TestClientPoolL10C100Free(t *testing.T) {
|
|
|
testClientPool(t, 10, 100, 0, true)
|
|
|
}
|
|
|
@@ -64,11 +65,6 @@ type poolTestPeer struct {
|
|
|
inactiveAllowed bool
|
|
|
}
|
|
|
|
|
|
-func testStateMachine() *nodestate.NodeStateMachine {
|
|
|
- return nodestate.NewNodeStateMachine(nil, nil, mclock.System{}, serverSetup)
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
func newPoolTestPeer(i int, disconnCh chan int) *poolTestPeer {
|
|
|
return &poolTestPeer{
|
|
|
index: i,
|
|
|
@@ -81,36 +77,39 @@ func (i *poolTestPeer) Node() *enode.Node {
|
|
|
return i.node
|
|
|
}
|
|
|
|
|
|
-func (i *poolTestPeer) freeClientId() string {
|
|
|
+func (i *poolTestPeer) FreeClientId() string {
|
|
|
return fmt.Sprintf("addr #%d", i.index)
|
|
|
}
|
|
|
|
|
|
-func (i *poolTestPeer) updateCapacity(cap uint64) {
|
|
|
- i.cap = cap
|
|
|
+func (i *poolTestPeer) InactiveAllowance() time.Duration {
|
|
|
+ if i.inactiveAllowed {
|
|
|
+ return time.Second * 10
|
|
|
+ }
|
|
|
+ return 0
|
|
|
}
|
|
|
|
|
|
-func (i *poolTestPeer) freeze() {}
|
|
|
-
|
|
|
-func (i *poolTestPeer) allowInactive() bool {
|
|
|
- return i.inactiveAllowed
|
|
|
+func (i *poolTestPeer) UpdateCapacity(capacity uint64, requested bool) {
|
|
|
+ i.cap = capacity
|
|
|
}
|
|
|
|
|
|
-func getBalance(pool *clientPool, p *poolTestPeer) (pos, neg uint64) {
|
|
|
- temp := pool.ns.GetField(p.node, clientInfoField) == nil
|
|
|
- if temp {
|
|
|
- pool.ns.SetField(p.node, connAddressField, p.freeClientId())
|
|
|
- }
|
|
|
- n, _ := pool.ns.GetField(p.node, pool.BalanceField).(*vfs.NodeBalance)
|
|
|
- pos, neg = n.GetBalance()
|
|
|
- if temp {
|
|
|
- pool.ns.SetField(p.node, connAddressField, nil)
|
|
|
+func (i *poolTestPeer) Disconnect() {
|
|
|
+ if i.disconnCh == nil {
|
|
|
+ return
|
|
|
}
|
|
|
+ id := i.node.ID()
|
|
|
+ i.disconnCh <- int(id[0]) + int(id[1])<<8
|
|
|
+}
|
|
|
+
|
|
|
+func getBalance(pool *ClientPool, p *poolTestPeer) (pos, neg uint64) {
|
|
|
+ pool.BalanceOperation(p.node.ID(), p.FreeClientId(), func(nb AtomicBalanceOperator) {
|
|
|
+ pos, neg = nb.GetBalance()
|
|
|
+ })
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func addBalance(pool *clientPool, id enode.ID, amount int64) {
|
|
|
- pool.forClients([]enode.ID{id}, func(c *clientInfo) {
|
|
|
- c.balance.AddBalance(amount)
|
|
|
+func addBalance(pool *ClientPool, id enode.ID, amount int64) {
|
|
|
+ pool.BalanceOperation(id, "", func(nb AtomicBalanceOperator) {
|
|
|
+ nb.AddBalance(amount)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -122,6 +121,19 @@ func checkDiff(a, b uint64) bool {
|
|
|
return a > b+maxDiff || b > a+maxDiff
|
|
|
}
|
|
|
|
|
|
+func connect(pool *ClientPool, peer *poolTestPeer) uint64 {
|
|
|
+ pool.Register(peer)
|
|
|
+ return peer.cap
|
|
|
+}
|
|
|
+
|
|
|
+func disconnect(pool *ClientPool, peer *poolTestPeer) {
|
|
|
+ pool.Unregister(peer)
|
|
|
+}
|
|
|
+
|
|
|
+func alwaysTrueFn() bool {
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
func testClientPool(t *testing.T, activeLimit, clientCount, paidCount int, randomDisconnect bool) {
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
var (
|
|
|
@@ -130,19 +142,17 @@ func testClientPool(t *testing.T, activeLimit, clientCount, paidCount int, rando
|
|
|
connected = make([]bool, clientCount)
|
|
|
connTicks = make([]int, clientCount)
|
|
|
disconnCh = make(chan int, clientCount)
|
|
|
- disconnFn = func(id enode.ID) {
|
|
|
- disconnCh <- int(id[0]) + int(id[1])<<8
|
|
|
- }
|
|
|
- pool = newClientPool(testStateMachine(), db, 1, 0, &clock, disconnFn, alwaysTrueFn)
|
|
|
+ pool = NewClientPool(db, 1, 0, &clock, alwaysTrueFn)
|
|
|
)
|
|
|
- pool.ns.Start()
|
|
|
+ pool.Start()
|
|
|
+ pool.SetExpirationTCs(0, 1000)
|
|
|
|
|
|
- pool.setLimits(activeLimit, uint64(activeLimit))
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool.SetLimits(uint64(activeLimit), uint64(activeLimit))
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
// pool should accept new peers up to its connected limit
|
|
|
for i := 0; i < activeLimit; i++ {
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(i, disconnCh)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(i, disconnCh)); cap != 0 {
|
|
|
connected[i] = true
|
|
|
} else {
|
|
|
t.Fatalf("Test peer #%d rejected", i)
|
|
|
@@ -163,23 +173,23 @@ func testClientPool(t *testing.T, activeLimit, clientCount, paidCount int, rando
|
|
|
i := rand.Intn(clientCount)
|
|
|
if connected[i] {
|
|
|
if randomDisconnect {
|
|
|
- pool.disconnect(newPoolTestPeer(i, disconnCh))
|
|
|
+ disconnect(pool, newPoolTestPeer(i, disconnCh))
|
|
|
connected[i] = false
|
|
|
connTicks[i] += tickCounter
|
|
|
}
|
|
|
} else {
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(i, disconnCh)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(i, disconnCh)); cap != 0 {
|
|
|
connected[i] = true
|
|
|
connTicks[i] -= tickCounter
|
|
|
} else {
|
|
|
- pool.disconnect(newPoolTestPeer(i, disconnCh))
|
|
|
+ disconnect(pool, newPoolTestPeer(i, disconnCh))
|
|
|
}
|
|
|
}
|
|
|
pollDisconnects:
|
|
|
for {
|
|
|
select {
|
|
|
case i := <-disconnCh:
|
|
|
- pool.disconnect(newPoolTestPeer(i, disconnCh))
|
|
|
+ disconnect(pool, newPoolTestPeer(i, disconnCh))
|
|
|
if connected[i] {
|
|
|
connTicks[i] += tickCounter
|
|
|
connected[i] = false
|
|
|
@@ -211,18 +221,18 @@ func testClientPool(t *testing.T, activeLimit, clientCount, paidCount int, rando
|
|
|
t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], min, max)
|
|
|
}
|
|
|
}
|
|
|
- pool.stop()
|
|
|
+ pool.Stop()
|
|
|
}
|
|
|
|
|
|
-func testPriorityConnect(t *testing.T, pool *clientPool, p *poolTestPeer, cap uint64, expSuccess bool) {
|
|
|
- if cap, _ := pool.connect(p); cap == 0 {
|
|
|
+func testPriorityConnect(t *testing.T, pool *ClientPool, p *poolTestPeer, cap uint64, expSuccess bool) {
|
|
|
+ if cap := connect(pool, p); cap == 0 {
|
|
|
if expSuccess {
|
|
|
t.Fatalf("Failed to connect paid client")
|
|
|
} else {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- if _, err := pool.setCapacity(p.node, "", cap, defaultConnectedBias, true); err != nil {
|
|
|
+ if newCap, _ := pool.SetCapacity(p.node, cap, defaultConnectedBias, true); newCap != cap {
|
|
|
if expSuccess {
|
|
|
t.Fatalf("Failed to raise capacity of paid client")
|
|
|
} else {
|
|
|
@@ -239,11 +249,11 @@ func TestConnectPaidClient(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, func(id enode.ID) {}, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10))
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10))
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
// Add balance for an external client and mark it as paid client
|
|
|
addBalance(pool, newPoolTestPeer(0, nil).node.ID(), int64(time.Minute))
|
|
|
@@ -255,16 +265,16 @@ func TestConnectPaidClientToSmallPool(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, func(id enode.ID) {}, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
// Add balance for an external client and mark it as paid client
|
|
|
addBalance(pool, newPoolTestPeer(0, nil).node.ID(), int64(time.Minute))
|
|
|
|
|
|
- // Connect a fat paid client to pool, should reject it.
|
|
|
+ // connect a fat paid client to pool, should reject it.
|
|
|
testPriorityConnect(t, pool, newPoolTestPeer(0, nil), 100, false)
|
|
|
}
|
|
|
|
|
|
@@ -273,24 +283,23 @@ func TestConnectPaidClientToFullPool(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- removeFn := func(enode.ID) {} // Noop
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
addBalance(pool, newPoolTestPeer(i, nil).node.ID(), int64(time.Second*20))
|
|
|
- pool.connect(newPoolTestPeer(i, nil))
|
|
|
+ connect(pool, newPoolTestPeer(i, nil))
|
|
|
}
|
|
|
addBalance(pool, newPoolTestPeer(11, nil).node.ID(), int64(time.Second*2)) // Add low balance to new paid client
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(11, nil)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(11, nil)); cap != 0 {
|
|
|
t.Fatalf("Low balance paid client should be rejected")
|
|
|
}
|
|
|
clock.Run(time.Second)
|
|
|
addBalance(pool, newPoolTestPeer(12, nil).node.ID(), int64(time.Minute*5)) // Add high balance to new paid client
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(12, nil)); cap == 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(12, nil)); cap == 0 {
|
|
|
t.Fatalf("High balance paid client should be accepted")
|
|
|
}
|
|
|
}
|
|
|
@@ -301,23 +310,20 @@ func TestPaidClientKickedOut(t *testing.T) {
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
kickedCh = make(chan int, 100)
|
|
|
)
|
|
|
- removeFn := func(id enode.ID) {
|
|
|
- kickedCh <- int(id[0])
|
|
|
- }
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- pool.bt.SetExpirationTCs(0, 0)
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ pool.SetExpirationTCs(0, 0)
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
addBalance(pool, newPoolTestPeer(i, kickedCh).node.ID(), 10000000000) // 10 second allowance
|
|
|
- pool.connect(newPoolTestPeer(i, kickedCh))
|
|
|
+ connect(pool, newPoolTestPeer(i, kickedCh))
|
|
|
clock.Run(time.Millisecond)
|
|
|
}
|
|
|
clock.Run(defaultConnectedBias + time.Second*11)
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(11, kickedCh)); cap == 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(11, kickedCh)); cap == 0 {
|
|
|
t.Fatalf("Free client should be accepted")
|
|
|
}
|
|
|
select {
|
|
|
@@ -335,12 +341,12 @@ func TestConnectFreeClient(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, func(id enode.ID) {}, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10))
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(0, nil)); cap == 0 {
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10))
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ if cap := connect(pool, newPoolTestPeer(0, nil)); cap == 0 {
|
|
|
t.Fatalf("Failed to connect free client")
|
|
|
}
|
|
|
testPriorityConnect(t, pool, newPoolTestPeer(0, nil), 2, false)
|
|
|
@@ -351,26 +357,25 @@ func TestConnectFreeClientToFullPool(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- removeFn := func(enode.ID) {} // Noop
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.connect(newPoolTestPeer(i, nil))
|
|
|
+ connect(pool, newPoolTestPeer(i, nil))
|
|
|
}
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(11, nil)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(11, nil)); cap != 0 {
|
|
|
t.Fatalf("New free client should be rejected")
|
|
|
}
|
|
|
clock.Run(time.Minute)
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(12, nil)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(12, nil)); cap != 0 {
|
|
|
t.Fatalf("New free client should be rejected")
|
|
|
}
|
|
|
clock.Run(time.Millisecond)
|
|
|
clock.Run(4 * time.Minute)
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(13, nil)); cap == 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(13, nil)); cap == 0 {
|
|
|
t.Fatalf("Old client connects more than 5min should be kicked")
|
|
|
}
|
|
|
}
|
|
|
@@ -381,18 +386,17 @@ func TestFreeClientKickedOut(t *testing.T) {
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
kicked = make(chan int, 100)
|
|
|
)
|
|
|
- removeFn := func(id enode.ID) { kicked <- int(id[0]) }
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.connect(newPoolTestPeer(i, kicked))
|
|
|
+ connect(pool, newPoolTestPeer(i, kicked))
|
|
|
clock.Run(time.Millisecond)
|
|
|
}
|
|
|
- if cap, _ := pool.connect(newPoolTestPeer(10, kicked)); cap != 0 {
|
|
|
+ if cap := connect(pool, newPoolTestPeer(10, kicked)); cap != 0 {
|
|
|
t.Fatalf("New free client should be rejected")
|
|
|
}
|
|
|
select {
|
|
|
@@ -400,10 +404,10 @@ func TestFreeClientKickedOut(t *testing.T) {
|
|
|
case <-time.NewTimer(time.Second).C:
|
|
|
t.Fatalf("timeout")
|
|
|
}
|
|
|
- pool.disconnect(newPoolTestPeer(10, kicked))
|
|
|
+ disconnect(pool, newPoolTestPeer(10, kicked))
|
|
|
clock.Run(5 * time.Minute)
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.connect(newPoolTestPeer(i+10, kicked))
|
|
|
+ connect(pool, newPoolTestPeer(i+10, kicked))
|
|
|
}
|
|
|
for i := 0; i < 10; i++ {
|
|
|
select {
|
|
|
@@ -423,18 +427,17 @@ func TestPositiveBalanceCalculation(t *testing.T) {
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
kicked = make(chan int, 10)
|
|
|
)
|
|
|
- removeFn := func(id enode.ID) { kicked <- int(id[0]) } // Noop
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
addBalance(pool, newPoolTestPeer(0, kicked).node.ID(), int64(time.Minute*3))
|
|
|
testPriorityConnect(t, pool, newPoolTestPeer(0, kicked), 10, true)
|
|
|
clock.Run(time.Minute)
|
|
|
|
|
|
- pool.disconnect(newPoolTestPeer(0, kicked))
|
|
|
+ disconnect(pool, newPoolTestPeer(0, kicked))
|
|
|
pb, _ := getBalance(pool, newPoolTestPeer(0, kicked))
|
|
|
if checkDiff(pb, uint64(time.Minute*2)) {
|
|
|
t.Fatalf("Positive balance mismatch, want %v, got %v", uint64(time.Minute*2), pb)
|
|
|
@@ -447,12 +450,11 @@ func TestDowngradePriorityClient(t *testing.T) {
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
kicked = make(chan int, 10)
|
|
|
)
|
|
|
- removeFn := func(id enode.ID) { kicked <- int(id[0]) } // Noop
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, removeFn, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
p := newPoolTestPeer(0, kicked)
|
|
|
addBalance(pool, p.node.ID(), int64(time.Minute))
|
|
|
@@ -483,30 +485,31 @@ func TestNegativeBalanceCalculation(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, func(id enode.ID) {}, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1e-3, CapacityFactor: 0, RequestFactor: 1}, vfs.PriceFactors{TimeFactor: 1e-3, CapacityFactor: 0, RequestFactor: 1})
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetExpirationTCs(0, 3600)
|
|
|
+ pool.SetLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1e-3, CapacityFactor: 0, RequestFactor: 1}, PriceFactors{TimeFactor: 1e-3, CapacityFactor: 0, RequestFactor: 1})
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.connect(newPoolTestPeer(i, nil))
|
|
|
+ connect(pool, newPoolTestPeer(i, nil))
|
|
|
}
|
|
|
clock.Run(time.Second)
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.disconnect(newPoolTestPeer(i, nil))
|
|
|
+ disconnect(pool, newPoolTestPeer(i, nil))
|
|
|
_, nb := getBalance(pool, newPoolTestPeer(i, nil))
|
|
|
if nb != 0 {
|
|
|
t.Fatalf("Short connection shouldn't be recorded")
|
|
|
}
|
|
|
}
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.connect(newPoolTestPeer(i, nil))
|
|
|
+ connect(pool, newPoolTestPeer(i, nil))
|
|
|
}
|
|
|
clock.Run(time.Minute)
|
|
|
for i := 0; i < 10; i++ {
|
|
|
- pool.disconnect(newPoolTestPeer(i, nil))
|
|
|
+ disconnect(pool, newPoolTestPeer(i, nil))
|
|
|
_, nb := getBalance(pool, newPoolTestPeer(i, nil))
|
|
|
exp := uint64(time.Minute) / 1000
|
|
|
exp -= exp / 120 // correct for negative balance expiration
|
|
|
@@ -521,10 +524,10 @@ func TestInactiveClient(t *testing.T) {
|
|
|
clock mclock.Simulated
|
|
|
db = rawdb.NewMemoryDatabase()
|
|
|
)
|
|
|
- pool := newClientPool(testStateMachine(), db, 1, defaultConnectedBias, &clock, func(id enode.ID) {}, alwaysTrueFn)
|
|
|
- pool.ns.Start()
|
|
|
- defer pool.stop()
|
|
|
- pool.setLimits(2, uint64(2))
|
|
|
+ pool := NewClientPool(db, 1, defaultConnectedBias, &clock, alwaysTrueFn)
|
|
|
+ pool.Start()
|
|
|
+ defer pool.Stop()
|
|
|
+ pool.SetLimits(2, uint64(2))
|
|
|
|
|
|
p1 := newPoolTestPeer(1, nil)
|
|
|
p1.inactiveAllowed = true
|
|
|
@@ -535,15 +538,15 @@ func TestInactiveClient(t *testing.T) {
|
|
|
addBalance(pool, p1.node.ID(), 1000*int64(time.Second))
|
|
|
addBalance(pool, p3.node.ID(), 2000*int64(time.Second))
|
|
|
// p1: 1000 p2: 0 p3: 2000
|
|
|
- p1.cap, _ = pool.connect(p1)
|
|
|
+ p1.cap = connect(pool, p1)
|
|
|
if p1.cap != 1 {
|
|
|
t.Fatalf("Failed to connect peer #1")
|
|
|
}
|
|
|
- p2.cap, _ = pool.connect(p2)
|
|
|
+ p2.cap = connect(pool, p2)
|
|
|
if p2.cap != 1 {
|
|
|
t.Fatalf("Failed to connect peer #2")
|
|
|
}
|
|
|
- p3.cap, _ = pool.connect(p3)
|
|
|
+ p3.cap = connect(pool, p3)
|
|
|
if p3.cap != 1 {
|
|
|
t.Fatalf("Failed to connect peer #3")
|
|
|
}
|
|
|
@@ -566,11 +569,11 @@ func TestInactiveClient(t *testing.T) {
|
|
|
if p2.cap != 0 {
|
|
|
t.Fatalf("Failed to deactivate peer #2")
|
|
|
}
|
|
|
- pool.setDefaultFactors(vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 0}, vfs.PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 0})
|
|
|
+ pool.SetDefaultFactors(PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 0}, PriceFactors{TimeFactor: 1, CapacityFactor: 0, RequestFactor: 0})
|
|
|
p4 := newPoolTestPeer(4, nil)
|
|
|
addBalance(pool, p4.node.ID(), 1500*int64(time.Second))
|
|
|
// p1: 1000 p2: 500 p3: 2000 p4: 1500
|
|
|
- p4.cap, _ = pool.connect(p4)
|
|
|
+ p4.cap = connect(pool, p4)
|
|
|
if p4.cap != 1 {
|
|
|
t.Fatalf("Failed to activate peer #4")
|
|
|
}
|
|
|
@@ -579,8 +582,8 @@ func TestInactiveClient(t *testing.T) {
|
|
|
}
|
|
|
clock.Run(time.Second * 600)
|
|
|
// manually trigger a check to avoid a long real-time wait
|
|
|
- pool.ns.SetState(p1.node, pool.UpdateFlag, nodestate.Flags{}, 0)
|
|
|
- pool.ns.SetState(p1.node, nodestate.Flags{}, pool.UpdateFlag, 0)
|
|
|
+ pool.ns.SetState(p1.node, pool.setup.updateFlag, nodestate.Flags{}, 0)
|
|
|
+ pool.ns.SetState(p1.node, nodestate.Flags{}, pool.setup.updateFlag, 0)
|
|
|
// p1: 1000 p2: 500 p3: 2000 p4: 900
|
|
|
if p1.cap != 1 {
|
|
|
t.Fatalf("Failed to activate peer #1")
|
|
|
@@ -588,8 +591,8 @@ func TestInactiveClient(t *testing.T) {
|
|
|
if p4.cap != 0 {
|
|
|
t.Fatalf("Failed to deactivate peer #4")
|
|
|
}
|
|
|
- pool.disconnect(p2)
|
|
|
- pool.disconnect(p4)
|
|
|
+ disconnect(pool, p2)
|
|
|
+ disconnect(pool, p4)
|
|
|
addBalance(pool, p1.node.ID(), -1000*int64(time.Second))
|
|
|
if p1.cap != 1 {
|
|
|
t.Fatalf("Should not deactivate peer #1")
|