mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
Two packages were named for the same word and meant different things. network/ dialed peers, exchanged keys, throttled and tracked them; nets/ tracked which chains share a validator set and when they have all bootstrapped. One letter apart, and neither name said which was which. The peer layer is mesh — the word this estate already uses for it, in lux-rs/node's own mesh.rs. That frees network for the thing that is actually a network: a set of chains sharing a validator set. A network holds chains; a chain belongs to one network. github.com/luxfi/p2p stays what it is, the gossip library the VMs use. Three concerns, three names, none of them borrowed from another. Co-authored-by: Hanzo Dev <[email protected]>
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package throttling
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var (
|
|
host1 = netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 9631)
|
|
host2 = netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 5}), 9653)
|
|
host3 = netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 6}), 9655)
|
|
host4 = netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 7}), 9657)
|
|
loopbackIP = netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 9657)
|
|
)
|
|
|
|
func TestNoInboundConnUpgradeThrottler(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
{
|
|
throttler := NewInboundConnUpgradeThrottler(
|
|
InboundConnUpgradeThrottlerConfig{
|
|
UpgradeCooldown: 0,
|
|
MaxRecentConnsUpgraded: 5,
|
|
},
|
|
)
|
|
// throttler should allow all
|
|
for i := 0; i < 10; i++ {
|
|
require.True(throttler.ShouldUpgrade(host1))
|
|
}
|
|
}
|
|
{
|
|
throttler := NewInboundConnUpgradeThrottler(
|
|
InboundConnUpgradeThrottlerConfig{
|
|
UpgradeCooldown: time.Second,
|
|
MaxRecentConnsUpgraded: 0,
|
|
},
|
|
)
|
|
// throttler should allow all
|
|
for i := 0; i < 10; i++ {
|
|
require.True(throttler.ShouldUpgrade(host1))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInboundConnUpgradeThrottler(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
cooldown := 5 * time.Second
|
|
throttlerIntf := NewInboundConnUpgradeThrottler(
|
|
InboundConnUpgradeThrottlerConfig{
|
|
UpgradeCooldown: cooldown,
|
|
MaxRecentConnsUpgraded: 3,
|
|
},
|
|
)
|
|
|
|
// Allow should always return true
|
|
// when called with a given IP for the first time
|
|
require.True(throttlerIntf.ShouldUpgrade(host1))
|
|
require.True(throttlerIntf.ShouldUpgrade(host2))
|
|
require.True(throttlerIntf.ShouldUpgrade(host3))
|
|
|
|
// Shouldn't allow this IP because the number of connections
|
|
// within the last [cooldown] is at [MaxRecentConns]
|
|
require.False(throttlerIntf.ShouldUpgrade(host4))
|
|
|
|
// Shouldn't allow these IPs again until [cooldown] has passed
|
|
require.False(throttlerIntf.ShouldUpgrade(host1))
|
|
require.False(throttlerIntf.ShouldUpgrade(host2))
|
|
require.False(throttlerIntf.ShouldUpgrade(host3))
|
|
|
|
// Local host should never be rate-limited
|
|
require.True(throttlerIntf.ShouldUpgrade(loopbackIP))
|
|
require.True(throttlerIntf.ShouldUpgrade(loopbackIP))
|
|
require.True(throttlerIntf.ShouldUpgrade(loopbackIP))
|
|
require.True(throttlerIntf.ShouldUpgrade(loopbackIP))
|
|
require.True(throttlerIntf.ShouldUpgrade(loopbackIP))
|
|
|
|
// Make sure [throttler.done] isn't closed
|
|
throttler := throttlerIntf.(*inboundConnUpgradeThrottler)
|
|
select {
|
|
case <-throttler.done:
|
|
require.FailNow("shouldn't be done")
|
|
default:
|
|
}
|
|
|
|
throttler.Stop()
|
|
|
|
// Make sure [throttler.done] is closed
|
|
select {
|
|
case _, chanOpen := <-throttler.done:
|
|
require.False(chanOpen)
|
|
default:
|
|
require.FailNow("should be done")
|
|
}
|
|
}
|