2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2020-12-07 09:52:19 +00:00
|
|
|
"fmt"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2020-12-07 09:52:19 +00:00
|
|
|
"sync"
|
2019-09-25 16:54:31 +00:00
|
|
|
"sync/atomic"
|
2018-03-14 09:36:59 +00:00
|
|
|
"testing"
|
2020-12-07 09:52:19 +00:00
|
|
|
"time"
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2021-02-01 10:50:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/fakechain"
|
2020-12-07 09:52:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-05-22 09:59:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/capability"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2020-12-07 09:52:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
2020-12-07 09:52:19 +00:00
|
|
|
type testDiscovery struct {
|
|
|
|
sync.Mutex
|
|
|
|
bad []string
|
|
|
|
good []string
|
|
|
|
connected []string
|
|
|
|
unregistered []string
|
|
|
|
backfill []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestDiscovery([]string, time.Duration, Transporter) Discoverer { return new(testDiscovery) }
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2020-12-07 09:52:19 +00:00
|
|
|
func (d *testDiscovery) BackFill(addrs ...string) {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
d.backfill = append(d.backfill, addrs...)
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) Close() {}
|
|
|
|
func (d *testDiscovery) PoolCount() int { return 0 }
|
|
|
|
func (d *testDiscovery) RegisterBadAddr(addr string) {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
d.bad = append(d.bad, addr)
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) RegisterGoodAddr(string, capability.Capabilities) {}
|
|
|
|
func (d *testDiscovery) RegisterConnectedAddr(addr string) {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
d.connected = append(d.connected, addr)
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) UnregisterConnectedAddr(addr string) {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
d.unregistered = append(d.unregistered, addr)
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) UnconnectedPeers() []string {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
return d.unregistered
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) RequestRemote(n int) {}
|
|
|
|
func (d *testDiscovery) BadPeers() []string {
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
return d.bad
|
|
|
|
}
|
|
|
|
func (d *testDiscovery) GoodPeers() []AddressWithCapabilities { return []AddressWithCapabilities{} }
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
var defaultMessageHandler = func(t *testing.T, msg *Message) {}
|
|
|
|
|
|
|
|
type localPeer struct {
|
2019-09-09 14:54:38 +00:00
|
|
|
netaddr net.TCPAddr
|
2020-01-21 14:26:08 +00:00
|
|
|
server *Server
|
2018-03-14 09:36:59 +00:00
|
|
|
version *payload.Version
|
2020-01-17 10:17:19 +00:00
|
|
|
lastBlockIndex uint32
|
2019-09-13 12:43:22 +00:00
|
|
|
handshaked bool
|
2020-05-22 09:17:17 +00:00
|
|
|
isFullNode bool
|
2018-03-14 09:36:59 +00:00
|
|
|
t *testing.T
|
|
|
|
messageHandler func(t *testing.T, msg *Message)
|
2020-01-20 16:02:19 +00:00
|
|
|
pingSent int
|
2020-11-25 10:34:38 +00:00
|
|
|
getAddrSent int
|
2020-12-07 09:52:19 +00:00
|
|
|
droppedWith atomic.Value
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:26:08 +00:00
|
|
|
func newLocalPeer(t *testing.T, s *Server) *localPeer {
|
2019-09-09 14:54:38 +00:00
|
|
|
naddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
2018-03-14 09:36:59 +00:00
|
|
|
return &localPeer{
|
|
|
|
t: t,
|
2020-01-21 14:26:08 +00:00
|
|
|
server: s,
|
2019-09-09 14:54:38 +00:00
|
|
|
netaddr: *naddr,
|
2018-03-14 09:36:59 +00:00
|
|
|
messageHandler: defaultMessageHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 07:55:21 +00:00
|
|
|
func (p *localPeer) RemoteAddr() net.Addr {
|
|
|
|
return &p.netaddr
|
|
|
|
}
|
|
|
|
func (p *localPeer) PeerAddr() net.Addr {
|
2019-09-09 14:54:38 +00:00
|
|
|
return &p.netaddr
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2020-12-07 09:52:19 +00:00
|
|
|
func (p *localPeer) StartProtocol() {}
|
|
|
|
func (p *localPeer) Disconnect(err error) {
|
|
|
|
if p.droppedWith.Load() == nil {
|
|
|
|
p.droppedWith.Store(err)
|
|
|
|
}
|
|
|
|
fmt.Println("peer dropped:", err)
|
|
|
|
p.server.unregister <- peerDrop{p, err}
|
|
|
|
}
|
2020-01-16 18:16:31 +00:00
|
|
|
|
|
|
|
func (p *localPeer) EnqueueMessage(msg *Message) error {
|
|
|
|
b, err := msg.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 12:55:55 +00:00
|
|
|
return p.EnqueuePacket(true, b)
|
2020-01-16 18:16:31 +00:00
|
|
|
}
|
2020-12-22 12:55:55 +00:00
|
|
|
func (p *localPeer) EnqueuePacket(block bool, m []byte) error {
|
|
|
|
return p.EnqueueHPPacket(block, m)
|
2020-01-16 18:16:31 +00:00
|
|
|
}
|
2020-01-23 16:40:40 +00:00
|
|
|
func (p *localPeer) EnqueueP2PMessage(msg *Message) error {
|
|
|
|
return p.EnqueueMessage(msg)
|
|
|
|
}
|
|
|
|
func (p *localPeer) EnqueueP2PPacket(m []byte) error {
|
2020-12-22 12:55:55 +00:00
|
|
|
return p.EnqueueHPPacket(true, m)
|
2020-01-23 16:40:40 +00:00
|
|
|
}
|
2020-12-22 12:55:55 +00:00
|
|
|
func (p *localPeer) EnqueueHPPacket(_ bool, m []byte) error {
|
2020-12-07 09:52:19 +00:00
|
|
|
msg := &Message{Network: netmode.UnitTestNet}
|
2020-01-16 18:16:31 +00:00
|
|
|
r := io.NewBinReaderFromBuf(m)
|
|
|
|
err := msg.Decode(r)
|
|
|
|
if err == nil {
|
|
|
|
p.messageHandler(p.t, msg)
|
|
|
|
}
|
2018-04-13 10:14:08 +00:00
|
|
|
return nil
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) Version() *payload.Version {
|
|
|
|
return p.version
|
|
|
|
}
|
2020-01-17 10:17:19 +00:00
|
|
|
func (p *localPeer) LastBlockIndex() uint32 {
|
|
|
|
return p.lastBlockIndex
|
|
|
|
}
|
2019-09-13 12:43:22 +00:00
|
|
|
func (p *localPeer) HandleVersion(v *payload.Version) error {
|
2018-04-13 10:14:08 +00:00
|
|
|
p.version = v
|
2019-09-13 12:43:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-21 14:26:08 +00:00
|
|
|
func (p *localPeer) SendVersion() error {
|
2020-05-22 09:17:17 +00:00
|
|
|
m, err := p.server.getVersionMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-16 18:16:31 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
|
|
|
return nil
|
2019-09-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) SendVersionAck(m *Message) error {
|
2020-01-16 18:16:31 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
|
|
|
return nil
|
2019-09-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) HandleVersionAck() error {
|
|
|
|
p.handshaked = true
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-27 09:44:05 +00:00
|
|
|
func (p *localPeer) SendPing(m *Message) error {
|
2020-01-20 16:02:19 +00:00
|
|
|
p.pingSent++
|
2020-01-27 09:44:05 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
2020-01-20 16:02:19 +00:00
|
|
|
return nil
|
2020-01-17 10:17:19 +00:00
|
|
|
}
|
2020-08-14 13:22:15 +00:00
|
|
|
func (p *localPeer) HandlePing(ping *payload.Ping) error {
|
|
|
|
p.lastBlockIndex = ping.LastBlockIndex
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:02:19 +00:00
|
|
|
func (p *localPeer) HandlePong(pong *payload.Ping) error {
|
|
|
|
p.lastBlockIndex = pong.LastBlockIndex
|
|
|
|
p.pingSent--
|
|
|
|
return nil
|
2020-01-17 10:17:19 +00:00
|
|
|
}
|
2019-09-13 12:43:22 +00:00
|
|
|
|
|
|
|
func (p *localPeer) Handshaked() bool {
|
|
|
|
return p.handshaked
|
2018-04-13 10:14:08 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2020-05-22 09:17:17 +00:00
|
|
|
func (p *localPeer) IsFullNode() bool {
|
|
|
|
return p.isFullNode
|
|
|
|
}
|
|
|
|
|
2020-11-25 10:34:38 +00:00
|
|
|
func (p *localPeer) AddGetAddrSent() {
|
|
|
|
p.getAddrSent++
|
|
|
|
}
|
|
|
|
func (p *localPeer) CanProcessAddr() bool {
|
|
|
|
p.getAddrSent--
|
|
|
|
return p.getAddrSent >= 0
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:17:17 +00:00
|
|
|
func newTestServer(t *testing.T, serverConfig ServerConfig) *Server {
|
2021-02-01 10:50:08 +00:00
|
|
|
s, err := newServerFromConstructors(serverConfig, fakechain.NewFakeChain(), zaptest.NewLogger(t),
|
2020-12-07 09:52:19 +00:00
|
|
|
newFakeTransp, newFakeConsensus, newTestDiscovery)
|
|
|
|
require.NoError(t, err)
|
2020-05-22 09:17:17 +00:00
|
|
|
return s
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|