2019-10-28 04:25:52 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-12-07 09:52:19 +00:00
|
|
|
"net"
|
2019-10-28 04:25:52 +00:00
|
|
|
"sort"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-05-22 09:59:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2019-10-28 04:25:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-12-07 09:52:19 +00:00
|
|
|
atomic2 "go.uber.org/atomic"
|
2019-10-28 04:25:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeTransp struct {
|
|
|
|
retFalse int32
|
2020-12-07 09:52:19 +00:00
|
|
|
started atomic2.Bool
|
|
|
|
closed atomic2.Bool
|
2019-10-28 04:25:52 +00:00
|
|
|
dialCh chan string
|
2020-12-07 09:52:19 +00:00
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeTransp(s *Server) Transporter {
|
|
|
|
return &fakeTransp{}
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *fakeTransp) Dial(addr string, timeout time.Duration) error {
|
2021-07-08 08:03:30 +00:00
|
|
|
var ret error
|
2019-10-28 04:25:52 +00:00
|
|
|
if atomic.LoadInt32(&ft.retFalse) > 0 {
|
2021-07-08 08:03:30 +00:00
|
|
|
ret = errors.New("smth bad happened")
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
2021-07-08 08:03:30 +00:00
|
|
|
ft.dialCh <- addr
|
|
|
|
|
|
|
|
return ret
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
func (ft *fakeTransp) Accept() {
|
2020-12-07 09:52:19 +00:00
|
|
|
if ft.started.Load() {
|
|
|
|
panic("started twice")
|
|
|
|
}
|
|
|
|
ft.addr = net.JoinHostPort("0.0.0.0", "42")
|
|
|
|
ft.started.Store(true)
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
func (ft *fakeTransp) Proto() string {
|
|
|
|
return ""
|
|
|
|
}
|
2020-05-22 09:17:17 +00:00
|
|
|
func (ft *fakeTransp) Address() string {
|
2020-12-07 09:52:19 +00:00
|
|
|
return ft.addr
|
2020-05-22 09:17:17 +00:00
|
|
|
}
|
2019-10-28 04:25:52 +00:00
|
|
|
func (ft *fakeTransp) Close() {
|
2020-12-07 09:52:19 +00:00
|
|
|
if ft.closed.Load() {
|
|
|
|
panic("closed twice")
|
|
|
|
}
|
|
|
|
ft.closed.Store(true)
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
func TestDefaultDiscoverer(t *testing.T) {
|
|
|
|
ts := &fakeTransp{}
|
|
|
|
ts.dialCh = make(chan string)
|
2021-07-07 19:57:29 +00:00
|
|
|
d := NewDefaultDiscovery(nil, time.Second/16, ts)
|
2019-10-28 04:25:52 +00:00
|
|
|
|
|
|
|
var set1 = []string{"1.1.1.1:10333", "2.2.2.2:10333"}
|
|
|
|
sort.Strings(set1)
|
|
|
|
|
|
|
|
// Added addresses should end up in the pool and in the unconnected set.
|
|
|
|
// Done twice to check re-adding unconnected addresses, which should be
|
|
|
|
// a no-op.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
d.BackFill(set1...)
|
|
|
|
assert.Equal(t, len(set1), d.PoolCount())
|
|
|
|
set1D := d.UnconnectedPeers()
|
|
|
|
sort.Strings(set1D)
|
|
|
|
assert.Equal(t, 0, len(d.GoodPeers()))
|
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
require.Equal(t, set1, set1D)
|
|
|
|
}
|
2022-10-12 19:57:49 +00:00
|
|
|
require.Equal(t, 2, d.GetFanOut())
|
2019-10-28 04:25:52 +00:00
|
|
|
|
|
|
|
// Request should make goroutines dial our addresses draining the pool.
|
|
|
|
d.RequestRemote(len(set1))
|
|
|
|
dialled := make([]string, 0)
|
|
|
|
for i := 0; i < len(set1); i++ {
|
|
|
|
select {
|
|
|
|
case a := <-ts.dialCh:
|
|
|
|
dialled = append(dialled, a)
|
2021-01-18 18:10:06 +00:00
|
|
|
d.RegisterConnectedAddr(a)
|
2019-10-28 04:25:52 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timeout expecting for transport dial")
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 08:05:42 +00:00
|
|
|
require.Eventually(t, func() bool { return len(d.UnconnectedPeers()) == 0 }, 2*time.Second, 50*time.Millisecond)
|
2019-10-28 04:25:52 +00:00
|
|
|
sort.Strings(dialled)
|
|
|
|
assert.Equal(t, 0, d.PoolCount())
|
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
assert.Equal(t, 0, len(d.GoodPeers()))
|
|
|
|
require.Equal(t, set1, dialled)
|
|
|
|
|
|
|
|
// Registered good addresses should end up in appropriate set.
|
|
|
|
for _, addr := range set1 {
|
2020-05-22 09:59:18 +00:00
|
|
|
d.RegisterGoodAddr(addr, capability.Capabilities{
|
|
|
|
{
|
|
|
|
Type: capability.FullNode,
|
|
|
|
Data: &capability.Node{StartHeight: 123},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
gAddrWithCap := d.GoodPeers()
|
|
|
|
gAddrs := make([]string, len(gAddrWithCap))
|
|
|
|
for i, addr := range gAddrWithCap {
|
|
|
|
require.Equal(t, capability.Capabilities{
|
|
|
|
{
|
|
|
|
Type: capability.FullNode,
|
|
|
|
Data: &capability.Node{StartHeight: 123},
|
|
|
|
},
|
|
|
|
}, addr.Capabilities)
|
|
|
|
gAddrs[i] = addr.Address
|
2019-10-28 04:25:52 +00:00
|
|
|
}
|
|
|
|
sort.Strings(gAddrs)
|
|
|
|
assert.Equal(t, 0, d.PoolCount())
|
|
|
|
assert.Equal(t, 0, len(d.UnconnectedPeers()))
|
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
require.Equal(t, set1, gAddrs)
|
|
|
|
|
|
|
|
// Re-adding connected addresses should be no-op.
|
|
|
|
d.BackFill(set1...)
|
|
|
|
assert.Equal(t, 0, len(d.UnconnectedPeers()))
|
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
assert.Equal(t, len(set1), len(d.GoodPeers()))
|
|
|
|
require.Equal(t, 0, d.PoolCount())
|
|
|
|
|
|
|
|
// Unregistering connected should work.
|
|
|
|
for _, addr := range set1 {
|
|
|
|
d.UnregisterConnectedAddr(addr)
|
|
|
|
}
|
2022-10-13 12:47:55 +00:00
|
|
|
assert.Equal(t, 2, len(d.UnconnectedPeers())) // They're re-added automatically.
|
2019-10-28 04:25:52 +00:00
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
assert.Equal(t, len(set1), len(d.GoodPeers()))
|
2022-10-13 12:47:55 +00:00
|
|
|
require.Equal(t, 2, d.PoolCount())
|
2019-10-28 04:25:52 +00:00
|
|
|
|
|
|
|
// Now make Dial() fail and wait to see addresses in the bad list.
|
|
|
|
atomic.StoreInt32(&ts.retFalse, 1)
|
|
|
|
assert.Equal(t, len(set1), d.PoolCount())
|
|
|
|
set1D := d.UnconnectedPeers()
|
|
|
|
sort.Strings(set1D)
|
|
|
|
assert.Equal(t, 0, len(d.BadPeers()))
|
|
|
|
require.Equal(t, set1, set1D)
|
|
|
|
|
|
|
|
dialledBad := make([]string, 0)
|
|
|
|
d.RequestRemote(len(set1))
|
|
|
|
for i := 0; i < connRetries; i++ {
|
|
|
|
for j := 0; j < len(set1); j++ {
|
|
|
|
select {
|
|
|
|
case a := <-ts.dialCh:
|
|
|
|
dialledBad = append(dialledBad, a)
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timeout expecting for transport dial; i: %d, j: %d", i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 15:26:05 +00:00
|
|
|
require.Eventually(t, func() bool { return d.PoolCount() == 0 }, 2*time.Second, 50*time.Millisecond)
|
2019-10-28 04:25:52 +00:00
|
|
|
sort.Strings(dialledBad)
|
|
|
|
for i := 0; i < len(set1); i++ {
|
|
|
|
for j := 0; j < connRetries; j++ {
|
|
|
|
assert.Equal(t, set1[i], dialledBad[i*connRetries+j])
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 08:05:42 +00:00
|
|
|
require.Eventually(t, func() bool { return len(d.BadPeers()) == len(set1) }, 2*time.Second, 50*time.Millisecond)
|
2020-10-13 11:16:06 +00:00
|
|
|
assert.Equal(t, 0, len(d.GoodPeers()))
|
2019-10-28 04:25:52 +00:00
|
|
|
assert.Equal(t, 0, len(d.UnconnectedPeers()))
|
|
|
|
|
|
|
|
// Re-adding bad addresses is a no-op.
|
|
|
|
d.BackFill(set1...)
|
|
|
|
assert.Equal(t, 0, len(d.UnconnectedPeers()))
|
|
|
|
assert.Equal(t, len(set1), len(d.BadPeers()))
|
2020-10-13 11:16:06 +00:00
|
|
|
assert.Equal(t, 0, len(d.GoodPeers()))
|
2019-10-28 04:25:52 +00:00
|
|
|
require.Equal(t, 0, d.PoolCount())
|
|
|
|
}
|
2020-10-13 13:30:10 +00:00
|
|
|
|
|
|
|
func TestSeedDiscovery(t *testing.T) {
|
|
|
|
var seeds = []string{"1.1.1.1:10333", "2.2.2.2:10333"}
|
|
|
|
ts := &fakeTransp{}
|
|
|
|
ts.dialCh = make(chan string)
|
|
|
|
atomic.StoreInt32(&ts.retFalse, 1) // Fail all dial requests.
|
|
|
|
sort.Strings(seeds)
|
|
|
|
|
|
|
|
d := NewDefaultDiscovery(seeds, time.Second/10, ts)
|
|
|
|
|
|
|
|
d.RequestRemote(len(seeds))
|
|
|
|
for i := 0; i < connRetries*2; i++ {
|
|
|
|
for range seeds {
|
|
|
|
select {
|
2021-05-12 16:29:45 +00:00
|
|
|
case <-ts.dialCh:
|
2020-10-13 13:30:10 +00:00
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timeout expecting for transport dial")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|