2018-01-31 08:27:08 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2020-12-07 09:52:19 +00:00
|
|
|
"errors"
|
2021-07-30 13:57:42 +00:00
|
|
|
"fmt"
|
2020-12-07 09:52:19 +00:00
|
|
|
"math/big"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2020-05-22 09:17:17 +00:00
|
|
|
"strconv"
|
2022-10-20 14:04:39 +00:00
|
|
|
"sync"
|
2023-10-11 10:24:16 +00:00
|
|
|
"sync/atomic"
|
2018-01-31 08:27:08 +00:00
|
|
|
"testing"
|
2020-05-22 09:17:17 +00:00
|
|
|
"time"
|
2018-01-31 19:11:08 +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/internal/random"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/consensus"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-12-11 12:22:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-09-06 12:16:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mpt"
|
2024-06-05 15:21:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativehashes"
|
2020-12-07 09:52:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-10-05 07:45:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-05-22 09:17:17 +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/nspcc-dev/neo-go/pkg/util"
|
2020-11-27 10:55:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-06 09:39:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-14 01:09:54 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-01-31 08:27:08 +00:00
|
|
|
)
|
|
|
|
|
2020-12-07 09:52:19 +00:00
|
|
|
type fakeConsensus struct {
|
|
|
|
started atomic.Bool
|
|
|
|
stopped atomic.Bool
|
2021-01-14 13:38:40 +00:00
|
|
|
payloads []*payload.Extensible
|
2022-10-20 14:04:39 +00:00
|
|
|
txlock sync.Mutex
|
2020-12-07 09:52:19 +00:00
|
|
|
txs []*transaction.Transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ consensus.Service = (*fakeConsensus)(nil)
|
|
|
|
|
2022-04-22 08:33:56 +00:00
|
|
|
func (f *fakeConsensus) Name() string { return "fake" }
|
|
|
|
func (f *fakeConsensus) Start() { f.started.Store(true) }
|
|
|
|
func (f *fakeConsensus) Shutdown() { f.stopped.Store(true) }
|
2022-01-12 18:09:37 +00:00
|
|
|
func (f *fakeConsensus) OnPayload(p *payload.Extensible) error {
|
|
|
|
f.payloads = append(f.payloads, p)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-20 14:04:39 +00:00
|
|
|
func (f *fakeConsensus) OnTransaction(tx *transaction.Transaction) {
|
|
|
|
f.txlock.Lock()
|
|
|
|
defer f.txlock.Unlock()
|
|
|
|
f.txs = append(f.txs, tx)
|
|
|
|
}
|
2021-01-14 13:38:40 +00:00
|
|
|
func (f *fakeConsensus) GetPayload(h util.Uint256) *payload.Extensible { panic("implement me") }
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
func TestNewServer(t *testing.T) {
|
2022-12-06 13:34:38 +00:00
|
|
|
bc := &fakechain.FakeChain{Blockchain: config.Blockchain{
|
|
|
|
ProtocolConfiguration: config.ProtocolConfiguration{
|
|
|
|
P2PStateExchangeExtensions: true,
|
|
|
|
StateRootInHeader: true,
|
|
|
|
}}}
|
2022-01-12 21:20:03 +00:00
|
|
|
s, err := newServerFromConstructors(ServerConfig{}, bc, new(fakechain.FakeStateSync), nil, newFakeTransp, newTestDiscovery)
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
t.Run("set defaults", func(t *testing.T) {
|
|
|
|
s = newTestServer(t, ServerConfig{MinPeers: -1})
|
|
|
|
|
|
|
|
require.True(t, s.ID() != 0)
|
|
|
|
require.Equal(t, defaultMinPeers, s.ServerConfig.MinPeers)
|
|
|
|
require.Equal(t, defaultMaxPeers, s.ServerConfig.MaxPeers)
|
|
|
|
require.Equal(t, defaultAttemptConnPeers, s.ServerConfig.AttemptConnPeers)
|
|
|
|
})
|
|
|
|
t.Run("don't defaults", func(t *testing.T) {
|
|
|
|
cfg := ServerConfig{
|
|
|
|
MinPeers: 1,
|
|
|
|
MaxPeers: 2,
|
|
|
|
AttemptConnPeers: 3,
|
|
|
|
}
|
|
|
|
s = newTestServer(t, cfg)
|
|
|
|
|
|
|
|
require.True(t, s.ID() != 0)
|
|
|
|
require.Equal(t, 1, s.ServerConfig.MinPeers)
|
|
|
|
require.Equal(t, 2, s.ServerConfig.MaxPeers)
|
|
|
|
require.Equal(t, 3, s.ServerConfig.AttemptConnPeers)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerStartAndShutdown(t *testing.T) {
|
|
|
|
t.Run("no consensus", func(t *testing.T) {
|
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
|
2024-02-21 15:07:28 +00:00
|
|
|
s.Start()
|
2020-12-07 09:52:19 +00:00
|
|
|
p := newLocalPeer(t, s)
|
|
|
|
s.register <- p
|
|
|
|
require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
|
|
|
|
2024-02-18 12:27:52 +00:00
|
|
|
require.True(t, s.started.Load())
|
2024-04-04 10:26:44 +00:00
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
return s.transports[0].(*fakeTransp).started.Load()
|
|
|
|
}, 2*time.Second, 200*time.Millisecond)
|
2022-01-12 20:04:07 +00:00
|
|
|
assert.Nil(t, s.txCallback)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
s.Shutdown()
|
|
|
|
|
2024-02-18 12:27:52 +00:00
|
|
|
require.False(t, s.started.Load())
|
2022-11-29 14:43:08 +00:00
|
|
|
require.True(t, s.transports[0].(*fakeTransp).closed.Load())
|
2020-12-07 09:52:19 +00:00
|
|
|
err, ok := p.droppedWith.Load().(error)
|
|
|
|
require.True(t, ok)
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, err, errServerShutdown)
|
2020-12-07 09:52:19 +00:00
|
|
|
})
|
|
|
|
t.Run("with consensus", func(t *testing.T) {
|
2022-07-26 19:41:52 +00:00
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
cons := new(fakeConsensus)
|
2022-07-28 15:30:14 +00:00
|
|
|
s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
2024-02-21 15:07:28 +00:00
|
|
|
s.Start()
|
2020-12-07 09:52:19 +00:00
|
|
|
p := newLocalPeer(t, s)
|
|
|
|
s.register <- p
|
2024-04-04 10:26:44 +00:00
|
|
|
require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
2022-04-22 08:33:56 +00:00
|
|
|
assert.True(t, s.services["fake"].(*fakeConsensus).started.Load())
|
2024-02-18 12:27:52 +00:00
|
|
|
require.True(t, s.started.Load())
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
s.Shutdown()
|
|
|
|
|
2024-02-18 12:27:52 +00:00
|
|
|
require.False(t, s.started.Load())
|
2022-04-22 08:33:56 +00:00
|
|
|
require.True(t, s.services["fake"].(*fakeConsensus).stopped.Load())
|
2020-12-07 09:52:19 +00:00
|
|
|
})
|
2024-02-18 12:27:52 +00:00
|
|
|
t.Run("double start", func(t *testing.T) {
|
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
startWithCleanup(t, s)
|
|
|
|
|
|
|
|
// Attempt to start the server again.
|
|
|
|
s.Start()
|
|
|
|
|
|
|
|
require.True(t, s.started.Load(), "server should still be marked as started after second Start call")
|
|
|
|
})
|
|
|
|
t.Run("double shutdown", func(t *testing.T) {
|
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
s.Start()
|
|
|
|
require.True(t, s.started.Load(), "server should still be marked as started after second Start call")
|
|
|
|
s.Shutdown()
|
|
|
|
|
|
|
|
require.False(t, s.started.Load(), "server should be marked as not started after second Shutdown call")
|
|
|
|
// Attempt to shutdown the server again.
|
|
|
|
s.Shutdown()
|
|
|
|
// Verify the server state remains unchanged and is still considered shutdown.
|
|
|
|
require.False(t, s.started.Load(), "server should remain shutdown after second call")
|
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerRegisterPeer(t *testing.T) {
|
|
|
|
const peerCount = 3
|
|
|
|
|
|
|
|
s := newTestServer(t, ServerConfig{MaxPeers: 2})
|
|
|
|
ps := make([]*localPeer, peerCount)
|
|
|
|
for i := range ps {
|
|
|
|
ps[i] = newLocalPeer(t, s)
|
|
|
|
ps[i].netaddr.Port = i + 1
|
2022-11-17 14:07:19 +00:00
|
|
|
ps[i].version = &payload.Version{Nonce: uint32(i), UserAgent: []byte("fake")}
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:37:01 +00:00
|
|
|
startWithCleanup(t, s)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
s.register <- ps[0]
|
|
|
|
require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
2022-11-17 14:07:19 +00:00
|
|
|
s.handshake <- ps[0]
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
s.register <- ps[1]
|
2022-11-17 14:07:19 +00:00
|
|
|
s.handshake <- ps[1]
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Eventually(t, func() bool { return 2 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
|
|
|
|
|
|
|
require.Equal(t, 0, len(s.discovery.UnconnectedPeers()))
|
|
|
|
s.register <- ps[2]
|
|
|
|
require.Eventually(t, func() bool { return len(s.discovery.UnconnectedPeers()) > 0 }, time.Second, time.Millisecond*100)
|
|
|
|
|
|
|
|
index := -1
|
|
|
|
addrs := s.discovery.UnconnectedPeers()
|
|
|
|
for _, addr := range addrs {
|
|
|
|
for j := range ps {
|
|
|
|
if ps[j].PeerAddr().String() == addr {
|
|
|
|
index = j
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, index >= 0)
|
|
|
|
err, ok := ps[index].droppedWith.Load().(error)
|
|
|
|
require.True(t, ok)
|
2023-05-04 14:03:06 +00:00
|
|
|
require.ErrorIs(t, err, errMaxPeers)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
index = (index + 1) % peerCount
|
|
|
|
s.unregister <- peerDrop{ps[index], errIdenticalID}
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
bad := s.BadPeers()
|
|
|
|
for i := range bad {
|
|
|
|
if bad[i] == ps[index].PeerAddr().String() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}, time.Second, time.Millisecond*50)
|
|
|
|
}
|
|
|
|
|
2020-11-26 15:53:10 +00:00
|
|
|
func TestGetBlocksByIndex(t *testing.T) {
|
2021-09-27 13:13:37 +00:00
|
|
|
testGetBlocksByIndex(t, CMDGetBlockByIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetBlocksByIndex(t *testing.T, cmd CommandType) {
|
2022-11-29 14:43:08 +00:00
|
|
|
s := newTestServer(t, ServerConfig{UserAgent: "/test/"})
|
2021-09-27 13:13:37 +00:00
|
|
|
start := s.chain.BlockHeight()
|
|
|
|
if cmd == CMDGetHeaders {
|
|
|
|
start = s.chain.HeaderHeight()
|
|
|
|
s.stateSync.(*fakechain.FakeStateSync).RequestHeaders.Store(true)
|
|
|
|
}
|
2020-11-26 15:53:10 +00:00
|
|
|
ps := make([]*localPeer, 10)
|
|
|
|
expectsCmd := make([]CommandType, 10)
|
|
|
|
expectedHeight := make([][]uint32, 10)
|
|
|
|
for i := range ps {
|
|
|
|
i := i
|
|
|
|
ps[i] = newLocalPeer(t, s)
|
|
|
|
ps[i].messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
require.Equal(t, expectsCmd[i], msg.Command)
|
2021-09-27 13:13:37 +00:00
|
|
|
if expectsCmd[i] == cmd {
|
2020-11-26 15:53:10 +00:00
|
|
|
p, ok := msg.Payload.(*payload.GetBlockByIndex)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Contains(t, expectedHeight[i], p.IndexStart)
|
|
|
|
expectsCmd[i] = CMDPong
|
|
|
|
} else if expectsCmd[i] == CMDPong {
|
2021-09-27 13:13:37 +00:00
|
|
|
expectsCmd[i] = cmd
|
2020-11-26 15:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 13:13:37 +00:00
|
|
|
expectsCmd[i] = cmd
|
2020-11-26 15:53:10 +00:00
|
|
|
expectedHeight[i] = []uint32{start + 1}
|
|
|
|
}
|
2022-11-29 14:43:08 +00:00
|
|
|
go s.transports[0].Accept()
|
2020-11-26 15:53:10 +00:00
|
|
|
|
|
|
|
nonce := uint32(0)
|
|
|
|
checkPingRespond := func(t *testing.T, peerIndex int, peerHeight uint32, hs ...uint32) {
|
|
|
|
nonce++
|
|
|
|
expectedHeight[peerIndex] = hs
|
|
|
|
require.NoError(t, s.handlePing(ps[peerIndex], payload.NewPing(peerHeight, nonce)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send all requests for all chunks.
|
|
|
|
checkPingRespond(t, 0, 5000, 1)
|
|
|
|
checkPingRespond(t, 1, 5000, 1+payload.MaxHashesCount)
|
|
|
|
checkPingRespond(t, 2, 5000, 1+2*payload.MaxHashesCount)
|
|
|
|
checkPingRespond(t, 3, 5000, 1+3*payload.MaxHashesCount)
|
|
|
|
|
|
|
|
// Receive some blocks.
|
2023-10-11 10:24:16 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).Blockheight.Store(2123)
|
2020-11-26 15:53:10 +00:00
|
|
|
|
|
|
|
// Minimum chunk has priority.
|
|
|
|
checkPingRespond(t, 5, 5000, 2124)
|
|
|
|
checkPingRespond(t, 6, 5000, 2624)
|
|
|
|
// Request minimal height for peers behind.
|
|
|
|
checkPingRespond(t, 7, 3100, 2124)
|
|
|
|
checkPingRespond(t, 8, 5000, 3124)
|
|
|
|
checkPingRespond(t, 9, 5000, 3624)
|
|
|
|
// Request random height after that.
|
|
|
|
checkPingRespond(t, 1, 5000, 2124, 2624, 3124, 3624)
|
|
|
|
checkPingRespond(t, 2, 5000, 2124, 2624, 3124, 3624)
|
|
|
|
checkPingRespond(t, 3, 5000, 2124, 2624, 3124, 3624)
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
func TestSendVersion(t *testing.T) {
|
|
|
|
var (
|
2022-11-29 14:43:08 +00:00
|
|
|
s = newTestServer(t, ServerConfig{UserAgent: "/test/"})
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
2020-05-22 09:17:17 +00:00
|
|
|
// we need to set listener at least to handle dynamic port correctly
|
2022-11-29 14:43:08 +00:00
|
|
|
s.transports[0].Accept()
|
2018-03-14 09:36:59 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
2022-11-29 14:43:08 +00:00
|
|
|
// listener is already set, so Addresses(nil) gives us proper address with port
|
|
|
|
_, prt := s.transports[0].HostPort()
|
|
|
|
port, err := strconv.ParseUint(prt, 10, 16)
|
2020-05-22 09:17:17 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-19 11:54:51 +00:00
|
|
|
assert.Equal(t, CMDVersion, msg.Command)
|
2018-03-14 09:36:59 +00:00
|
|
|
assert.IsType(t, msg.Payload, &payload.Version{})
|
|
|
|
version := msg.Payload.(*payload.Version)
|
|
|
|
assert.NotZero(t, version.Nonce)
|
2020-05-22 09:17:17 +00:00
|
|
|
assert.Equal(t, 1, len(version.Capabilities))
|
|
|
|
assert.ElementsMatch(t, []capability.Capability{
|
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{
|
|
|
|
Port: uint16(port),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, version.Capabilities)
|
2018-03-14 09:36:59 +00:00
|
|
|
assert.Equal(t, uint32(0), version.Version)
|
|
|
|
assert.Equal(t, []byte("/test/"), version.UserAgent)
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, p.SendVersion())
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Server should reply with a verack after receiving a valid version.
|
|
|
|
func TestVerackAfterHandleVersionCmd(t *testing.T) {
|
|
|
|
var (
|
2020-05-22 09:17:17 +00:00
|
|
|
s = newTestServer(t, ServerConfig{})
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
2019-09-09 14:54:38 +00:00
|
|
|
na, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:3000")
|
|
|
|
p.netaddr = *na
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
// Should have a verack
|
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
2020-05-19 11:54:51 +00:00
|
|
|
assert.Equal(t, CMDVerack, msg.Command)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2020-05-22 09:17:17 +00:00
|
|
|
capabilities := []capability.Capability{
|
|
|
|
{
|
|
|
|
Type: capability.FullNode,
|
|
|
|
Data: &capability.Node{
|
|
|
|
StartHeight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{
|
|
|
|
Port: 3000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
version := payload.NewVersion(0, 1337, "/NEO-GO/", capabilities)
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, s.handleVersionCmd(p, version))
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
// Server should not reply with a verack after receiving a
|
|
|
|
// invalid version and disconnects the peer.
|
|
|
|
func TestServerNotSendsVerack(t *testing.T) {
|
|
|
|
var (
|
2020-12-07 09:52:19 +00:00
|
|
|
s = newTestServer(t, ServerConfig{MaxPeers: 10, Net: 56753})
|
2020-01-21 14:26:08 +00:00
|
|
|
p = newLocalPeer(t, s)
|
|
|
|
p2 = newLocalPeer(t, s)
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
s.id = 1
|
2024-03-19 16:21:28 +00:00
|
|
|
startWithCleanup(t, s)
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2019-09-09 14:54:38 +00:00
|
|
|
na, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:3000")
|
|
|
|
p.netaddr = *na
|
2019-11-06 09:39:17 +00:00
|
|
|
p2.netaddr = *na
|
2018-03-14 09:36:59 +00:00
|
|
|
s.register <- p
|
2018-03-09 15:55:25 +00:00
|
|
|
|
2020-05-22 09:17:17 +00:00
|
|
|
capabilities := []capability.Capability{
|
|
|
|
{
|
|
|
|
Type: capability.FullNode,
|
|
|
|
Data: &capability.Node{
|
|
|
|
StartHeight: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{
|
|
|
|
Port: 3000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
// identical id's
|
2020-05-22 09:17:17 +00:00
|
|
|
version := payload.NewVersion(56753, 1, "/NEO-GO/", capabilities)
|
2019-10-17 08:19:24 +00:00
|
|
|
err := s.handleVersionCmd(p, version)
|
2018-03-14 09:36:59 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, errIdenticalID, err)
|
2019-11-06 09:39:17 +00:00
|
|
|
|
2020-05-21 10:35:44 +00:00
|
|
|
// Different IDs, but also different magics
|
2019-11-06 09:39:17 +00:00
|
|
|
version.Nonce = 2
|
2020-05-21 10:35:44 +00:00
|
|
|
version.Magic = 56752
|
|
|
|
err = s.handleVersionCmd(p, version)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, errInvalidNetwork, err)
|
|
|
|
|
|
|
|
// Different IDs and same network, make handshake pass.
|
|
|
|
version.Magic = 56753
|
2019-11-06 09:39:17 +00:00
|
|
|
require.NoError(t, s.handleVersionCmd(p, version))
|
|
|
|
require.NoError(t, p.HandleVersionAck())
|
|
|
|
require.Equal(t, true, p.Handshaked())
|
|
|
|
|
|
|
|
// Second handshake from the same peer should fail.
|
|
|
|
s.register <- p2
|
|
|
|
err = s.handleVersionCmd(p2, version)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
require.Equal(t, errAlreadyConnected, err)
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
func (s *Server) testHandleMessage(t *testing.T, p Peer, cmd CommandType, pl payload.Payload) *Server {
|
|
|
|
if p == nil {
|
|
|
|
p = newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.(*localPeer).handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
msg := NewMessage(cmd, pl)
|
|
|
|
require.NoError(t, s.handleMessage(p, msg))
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-12-06 13:34:38 +00:00
|
|
|
func startTestServer(t *testing.T, protocolCfg ...func(*config.Blockchain)) *Server {
|
2021-09-13 08:41:54 +00:00
|
|
|
var s *Server
|
2022-11-29 14:43:08 +00:00
|
|
|
srvCfg := ServerConfig{UserAgent: "/test/"}
|
2021-09-13 08:41:54 +00:00
|
|
|
if protocolCfg != nil {
|
|
|
|
s = newTestServerWithCustomCfg(t, srvCfg, protocolCfg[0])
|
|
|
|
} else {
|
|
|
|
s = newTestServer(t, srvCfg)
|
|
|
|
}
|
2021-11-15 09:37:01 +00:00
|
|
|
startWithCleanup(t, s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func startWithCleanup(t *testing.T, s *Server) {
|
2024-02-21 15:07:28 +00:00
|
|
|
s.Start()
|
2021-03-01 11:14:15 +00:00
|
|
|
t.Cleanup(func() {
|
2020-12-07 09:52:19 +00:00
|
|
|
s.Shutdown()
|
2021-03-01 11:14:15 +00:00
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlock(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
2023-10-11 10:24:16 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).Blockheight.Store(12344)
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Equal(t, uint32(12344), s.chain.BlockHeight())
|
|
|
|
|
2021-03-25 18:46:52 +00:00
|
|
|
b := block.New(false)
|
2020-12-07 09:52:19 +00:00
|
|
|
b.Index = 12345
|
|
|
|
s.testHandleMessage(t, nil, CMDBlock, b)
|
2022-02-09 17:14:11 +00:00
|
|
|
require.Eventually(t, func() bool { return s.chain.BlockHeight() == 12345 }, 2*time.Second, time.Millisecond*500)
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsensus(t *testing.T) {
|
2022-07-26 19:41:52 +00:00
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
cons := new(fakeConsensus)
|
2022-07-28 15:30:14 +00:00
|
|
|
s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction)
|
2022-01-12 01:11:21 +00:00
|
|
|
startWithCleanup(t, s)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
2023-10-11 10:24:16 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).Blockheight.Store(4)
|
2021-01-14 13:38:40 +00:00
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-04-02 09:55:56 +00:00
|
|
|
s.register <- p
|
|
|
|
require.Eventually(t, func() bool { return 1 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
2021-01-14 13:38:40 +00:00
|
|
|
|
|
|
|
newConsensusMessage := func(start, end uint32) *Message {
|
2021-03-25 18:59:54 +00:00
|
|
|
pl := payload.NewExtensible()
|
2022-07-28 15:30:14 +00:00
|
|
|
pl.Category = payload.ConsensusCategory
|
2021-01-14 13:38:40 +00:00
|
|
|
pl.ValidBlockStart = start
|
|
|
|
pl.ValidBlockEnd = end
|
|
|
|
return NewMessage(CMDExtensible, pl)
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:42:20 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).VerifyWitnessF = func() (int64, error) { return 0, errors.New("invalid") }
|
2021-01-14 13:38:40 +00:00
|
|
|
msg := newConsensusMessage(0, s.chain.BlockHeight()+1)
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
|
2021-10-25 14:42:20 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).VerifyWitnessF = func() (int64, error) { return 0, nil }
|
2021-01-14 13:38:40 +00:00
|
|
|
require.NoError(t, s.handleMessage(p, msg))
|
2022-04-22 08:33:56 +00:00
|
|
|
require.Contains(t, s.services["fake"].(*fakeConsensus).payloads, msg.Payload.(*payload.Extensible))
|
2021-01-14 13:38:40 +00:00
|
|
|
|
|
|
|
t.Run("small ValidUntilBlockEnd", func(t *testing.T) {
|
|
|
|
t.Run("current height", func(t *testing.T) {
|
|
|
|
msg := newConsensusMessage(0, s.chain.BlockHeight())
|
|
|
|
require.NoError(t, s.handleMessage(p, msg))
|
2022-04-22 08:33:56 +00:00
|
|
|
require.NotContains(t, s.services["fake"].(*fakeConsensus).payloads, msg.Payload.(*payload.Extensible))
|
2021-01-14 13:38:40 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
msg := newConsensusMessage(0, s.chain.BlockHeight()-1)
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("big ValidUntiLBlockStart", func(t *testing.T) {
|
|
|
|
msg := newConsensusMessage(s.chain.BlockHeight()+1, s.chain.BlockHeight()+2)
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransaction(t *testing.T) {
|
2022-07-26 19:41:52 +00:00
|
|
|
s := newTestServer(t, ServerConfig{})
|
|
|
|
cons := new(fakeConsensus)
|
2022-07-28 15:30:14 +00:00
|
|
|
s.AddConsensusService(cons, cons.OnPayload, cons.OnTransaction)
|
2022-01-12 01:11:21 +00:00
|
|
|
startWithCleanup(t, s)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
tx := newDummyTx()
|
2022-11-11 12:32:51 +00:00
|
|
|
s.RequestTx(tx.Hash())
|
2020-12-07 09:52:19 +00:00
|
|
|
p := newLocalPeer(t, s)
|
|
|
|
p.isFullNode = true
|
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDInv {
|
|
|
|
inv := msg.Payload.(*payload.Inventory)
|
|
|
|
require.Equal(t, payload.TXType, inv.Type)
|
|
|
|
require.Equal(t, []util.Uint256{tx.Hash()}, inv.Hashes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.register <- p
|
|
|
|
|
|
|
|
s.testHandleMessage(t, nil, CMDTX, tx)
|
2022-10-20 14:04:39 +00:00
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
var fake = s.services["fake"].(*fakeConsensus)
|
|
|
|
fake.txlock.Lock()
|
|
|
|
defer fake.txlock.Unlock()
|
|
|
|
for _, t := range fake.txs {
|
|
|
|
if t == tx {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}, 2*time.Second, time.Millisecond*500)
|
2020-12-07 09:52:19 +00:00
|
|
|
})
|
|
|
|
t.Run("bad", func(t *testing.T) {
|
|
|
|
tx := newDummyTx()
|
2022-11-11 12:32:51 +00:00
|
|
|
s.RequestTx(tx.Hash())
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PoolTxF = func(*transaction.Transaction) error { return core.ErrInsufficientFunds }
|
2020-12-07 09:52:19 +00:00
|
|
|
s.testHandleMessage(t, nil, CMDTX, tx)
|
2022-10-20 14:04:39 +00:00
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
var fake = s.services["fake"].(*fakeConsensus)
|
|
|
|
fake.txlock.Lock()
|
|
|
|
defer fake.txlock.Unlock()
|
|
|
|
for _, t := range fake.txs {
|
|
|
|
if t == tx {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}, 2*time.Second, time.Millisecond*500)
|
2020-12-07 09:52:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) testHandleGetData(t *testing.T, invType payload.InventoryType, hs, notFound []util.Uint256, found payload.Payload) {
|
|
|
|
var recvResponse atomic.Bool
|
|
|
|
var recvNotFound atomic.Bool
|
|
|
|
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
switch msg.Command {
|
2021-01-14 13:38:40 +00:00
|
|
|
case CMDTX, CMDBlock, CMDExtensible, CMDP2PNotaryRequest:
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Equal(t, found, msg.Payload)
|
|
|
|
recvResponse.Store(true)
|
|
|
|
case CMDNotFound:
|
|
|
|
require.Equal(t, notFound, msg.Payload.(*payload.Inventory).Hashes)
|
|
|
|
recvNotFound.Store(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.testHandleMessage(t, p, CMDGetData, payload.NewInventory(invType, hs))
|
|
|
|
|
2022-02-09 17:14:11 +00:00
|
|
|
require.Eventually(t, func() bool { return recvResponse.Load() }, 2*time.Second, time.Millisecond)
|
|
|
|
require.Eventually(t, func() bool { return recvNotFound.Load() }, 2*time.Second, time.Millisecond)
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetData(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(1000000)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
t.Run("block", func(t *testing.T) {
|
|
|
|
b := newDummyBlock(2, 0)
|
|
|
|
hs := []util.Uint256{random.Uint256(), b.Hash(), random.Uint256()}
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PutBlock(b)
|
2020-12-07 09:52:19 +00:00
|
|
|
notFound := []util.Uint256{hs[0], hs[2]}
|
|
|
|
s.testHandleGetData(t, payload.BlockType, hs, notFound, b)
|
|
|
|
})
|
|
|
|
t.Run("transaction", func(t *testing.T) {
|
|
|
|
tx := newDummyTx()
|
|
|
|
hs := []util.Uint256{random.Uint256(), tx.Hash(), random.Uint256()}
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PutTx(tx)
|
2020-12-07 09:52:19 +00:00
|
|
|
notFound := []util.Uint256{hs[0], hs[2]}
|
|
|
|
s.testHandleGetData(t, payload.TXType, hs, notFound, tx)
|
|
|
|
})
|
2020-11-27 10:55:48 +00:00
|
|
|
t.Run("p2pNotaryRequest", func(t *testing.T) {
|
|
|
|
mainTx := &transaction.Transaction{
|
|
|
|
Attributes: []transaction.Attribute{{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 1}}},
|
|
|
|
Script: []byte{0, 1, 2},
|
|
|
|
ValidUntilBlock: 123,
|
|
|
|
Signers: []transaction.Signer{{Account: random.Uint160()}},
|
|
|
|
Scripts: []transaction.Witness{{InvocationScript: []byte{1, 2, 3}, VerificationScript: []byte{1, 2, 3}}},
|
|
|
|
}
|
|
|
|
mainTx.Size()
|
|
|
|
mainTx.Hash()
|
|
|
|
fallbackTx := &transaction.Transaction{
|
|
|
|
Script: []byte{1, 2, 3},
|
|
|
|
ValidUntilBlock: 123,
|
|
|
|
Attributes: []transaction.Attribute{
|
|
|
|
{Type: transaction.NotValidBeforeT, Value: &transaction.NotValidBefore{Height: 123}},
|
|
|
|
{Type: transaction.ConflictsT, Value: &transaction.Conflicts{Hash: mainTx.Hash()}},
|
|
|
|
{Type: transaction.NotaryAssistedT, Value: &transaction.NotaryAssisted{NKeys: 0}},
|
|
|
|
},
|
|
|
|
Signers: []transaction.Signer{{Account: random.Uint160()}, {Account: random.Uint160()}},
|
2022-10-05 07:45:52 +00:00
|
|
|
Scripts: []transaction.Witness{{InvocationScript: append([]byte{byte(opcode.PUSHDATA1), keys.SignatureLen}, make([]byte, keys.SignatureLen)...), VerificationScript: make([]byte, 0)}, {InvocationScript: []byte{}, VerificationScript: []byte{}}},
|
2020-11-27 10:55:48 +00:00
|
|
|
}
|
|
|
|
fallbackTx.Size()
|
|
|
|
fallbackTx.Hash()
|
|
|
|
r := &payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: mainTx,
|
|
|
|
FallbackTransaction: fallbackTx,
|
|
|
|
Witness: transaction.Witness{
|
|
|
|
InvocationScript: []byte{1, 2, 3},
|
|
|
|
VerificationScript: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r.Hash()
|
|
|
|
require.NoError(t, s.notaryRequestPool.Add(r.FallbackTransaction, s.chain, r))
|
|
|
|
hs := []util.Uint256{random.Uint256(), r.FallbackTransaction.Hash(), random.Uint256()}
|
|
|
|
notFound := []util.Uint256{hs[0], hs[2]}
|
|
|
|
s.testHandleGetData(t, payload.P2PNotaryRequestType, hs, notFound, r)
|
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 11:14:15 +00:00
|
|
|
func initGetBlocksTest(t *testing.T) (*Server, []*block.Block) {
|
|
|
|
s := startTestServer(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
var blocks []*block.Block
|
|
|
|
for i := uint32(12); i <= 15; i++ {
|
|
|
|
b := newDummyBlock(i, 3)
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PutBlock(b)
|
2020-12-07 09:52:19 +00:00
|
|
|
blocks = append(blocks, b)
|
|
|
|
}
|
2021-03-01 11:14:15 +00:00
|
|
|
return s, blocks
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlocks(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s, blocks := initGetBlocksTest(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
expected := make([]util.Uint256, len(blocks))
|
|
|
|
for i := range blocks {
|
|
|
|
expected[i] = blocks[i].Hash()
|
|
|
|
}
|
|
|
|
var actual []util.Uint256
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDInv {
|
|
|
|
actual = msg.Payload.(*payload.Inventory).Hashes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("2", func(t *testing.T) {
|
|
|
|
s.testHandleMessage(t, p, CMDGetBlocks, &payload.GetBlocks{HashStart: expected[0], Count: 2})
|
|
|
|
require.Equal(t, expected[1:3], actual)
|
|
|
|
})
|
|
|
|
t.Run("-1", func(t *testing.T) {
|
|
|
|
s.testHandleMessage(t, p, CMDGetBlocks, &payload.GetBlocks{HashStart: expected[0], Count: -1})
|
|
|
|
require.Equal(t, expected[1:], actual)
|
|
|
|
})
|
|
|
|
t.Run("invalid start", func(t *testing.T) {
|
|
|
|
msg := NewMessage(CMDGetBlocks, &payload.GetBlocks{HashStart: util.Uint256{}, Count: -1})
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBlockByIndex(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s, blocks := initGetBlocksTest(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
var expected []*block.Block
|
|
|
|
var actual []*block.Block
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDBlock {
|
|
|
|
actual = append(actual, msg.Payload.(*block.Block))
|
|
|
|
if len(actual) == len(expected) {
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("2", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected = blocks[:2]
|
|
|
|
s.testHandleMessage(t, p, CMDGetBlockByIndex, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: 2})
|
|
|
|
})
|
|
|
|
t.Run("-1", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected = blocks
|
|
|
|
s.testHandleMessage(t, p, CMDGetBlockByIndex, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: -1})
|
|
|
|
})
|
|
|
|
t.Run("-1, last header", func(t *testing.T) {
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PutHeader(newDummyBlock(16, 2))
|
2020-12-07 09:52:19 +00:00
|
|
|
actual = nil
|
|
|
|
expected = blocks
|
|
|
|
s.testHandleMessage(t, p, CMDGetBlockByIndex, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: -1})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHeaders(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s, blocks := initGetBlocksTest(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
expected := make([]*block.Header, len(blocks))
|
|
|
|
for i := range blocks {
|
2021-03-01 13:44:47 +00:00
|
|
|
expected[i] = &blocks[i].Header
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var actual *payload.Headers
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDHeaders {
|
|
|
|
actual = msg.Payload.(*payload.Headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("2", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
s.testHandleMessage(t, p, CMDGetHeaders, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: 2})
|
|
|
|
require.Equal(t, expected[:2], actual.Hdrs)
|
|
|
|
})
|
|
|
|
t.Run("more, than we have", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
s.testHandleMessage(t, p, CMDGetHeaders, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: 10})
|
|
|
|
require.Equal(t, expected, actual.Hdrs)
|
|
|
|
})
|
|
|
|
t.Run("-1", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
s.testHandleMessage(t, p, CMDGetHeaders, &payload.GetBlockByIndex{IndexStart: blocks[0].Index, Count: -1})
|
|
|
|
require.Equal(t, expected, actual.Hdrs)
|
|
|
|
})
|
|
|
|
t.Run("no headers", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
s.testHandleMessage(t, p, CMDGetHeaders, &payload.GetBlockByIndex{IndexStart: 123, Count: -1})
|
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
2021-09-27 13:13:37 +00:00
|
|
|
t.Run("distribute requests between peers", func(t *testing.T) {
|
|
|
|
testGetBlocksByIndex(t, CMDGetHeaders)
|
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInv(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).UtilityTokenBalance = big.NewInt(10000000)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
var actual []util.Uint256
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDGetData {
|
|
|
|
actual = msg.Payload.(*payload.Inventory).Hashes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("blocks", func(t *testing.T) {
|
|
|
|
b := newDummyBlock(10, 3)
|
2021-02-01 10:50:08 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).PutBlock(b)
|
2020-12-07 09:52:19 +00:00
|
|
|
hs := []util.Uint256{random.Uint256(), b.Hash(), random.Uint256()}
|
|
|
|
s.testHandleMessage(t, p, CMDInv, &payload.Inventory{
|
|
|
|
Type: payload.BlockType,
|
|
|
|
Hashes: hs,
|
|
|
|
})
|
|
|
|
require.Equal(t, []util.Uint256{hs[0], hs[2]}, actual)
|
|
|
|
})
|
|
|
|
t.Run("transaction", func(t *testing.T) {
|
|
|
|
tx := newDummyTx()
|
2021-08-03 19:28:16 +00:00
|
|
|
require.NoError(t, s.chain.GetMemPool().Add(tx, s.chain))
|
2020-12-07 09:52:19 +00:00
|
|
|
hs := []util.Uint256{random.Uint256(), tx.Hash(), random.Uint256()}
|
|
|
|
s.testHandleMessage(t, p, CMDInv, &payload.Inventory{
|
|
|
|
Type: payload.TXType,
|
|
|
|
Hashes: hs,
|
|
|
|
})
|
|
|
|
require.Equal(t, []util.Uint256{hs[0], hs[2]}, actual)
|
|
|
|
})
|
2021-01-18 12:52:51 +00:00
|
|
|
t.Run("extensible", func(t *testing.T) {
|
2021-03-25 18:59:54 +00:00
|
|
|
ep := payload.NewExtensible()
|
2021-10-25 14:42:20 +00:00
|
|
|
s.chain.(*fakechain.FakeChain).VerifyWitnessF = func() (int64, error) { return 0, nil }
|
2021-02-01 10:50:08 +00:00
|
|
|
ep.ValidBlockEnd = s.chain.(*fakechain.FakeChain).BlockHeight() + 1
|
2021-01-18 12:52:51 +00:00
|
|
|
ok, err := s.extensiblePool.Add(ep)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
|
|
|
s.testHandleMessage(t, p, CMDInv, &payload.Inventory{
|
|
|
|
Type: payload.ExtensibleType,
|
|
|
|
Hashes: []util.Uint256{ep.Hash()},
|
|
|
|
})
|
|
|
|
})
|
2020-11-27 10:55:48 +00:00
|
|
|
t.Run("p2pNotaryRequest", func(t *testing.T) {
|
2021-03-25 16:18:01 +00:00
|
|
|
fallbackTx := transaction.New(random.Bytes(100), 123)
|
2020-11-27 10:55:48 +00:00
|
|
|
fallbackTx.Signers = []transaction.Signer{{Account: random.Uint160()}, {Account: random.Uint160()}}
|
|
|
|
fallbackTx.Size()
|
|
|
|
fallbackTx.Hash()
|
|
|
|
r := &payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: newDummyTx(),
|
|
|
|
FallbackTransaction: fallbackTx,
|
|
|
|
}
|
|
|
|
require.NoError(t, s.notaryRequestPool.Add(r.FallbackTransaction, s.chain, r))
|
|
|
|
hs := []util.Uint256{random.Uint256(), r.FallbackTransaction.Hash(), random.Uint256()}
|
|
|
|
s.testHandleMessage(t, p, CMDInv, &payload.Inventory{
|
|
|
|
Type: payload.P2PNotaryRequestType,
|
|
|
|
Hashes: hs,
|
|
|
|
})
|
|
|
|
require.Equal(t, []util.Uint256{hs[0], hs[2]}, actual)
|
|
|
|
})
|
2020-12-07 09:52:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 12:16:47 +00:00
|
|
|
func TestHandleGetMPTData(t *testing.T) {
|
|
|
|
t.Run("P2PStateExchange extensions off", func(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-09-06 12:16:47 +00:00
|
|
|
msg := NewMessage(CMDGetMPTData, &payload.MPTInventory{
|
|
|
|
Hashes: []util.Uint256{{1, 2, 3}},
|
|
|
|
})
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
|
2022-07-14 11:13:38 +00:00
|
|
|
check := func(t *testing.T, s *Server) {
|
2021-09-06 12:16:47 +00:00
|
|
|
var recvResponse atomic.Bool
|
|
|
|
r1 := random.Uint256()
|
|
|
|
r2 := random.Uint256()
|
|
|
|
r3 := random.Uint256()
|
|
|
|
node := []byte{1, 2, 3}
|
|
|
|
s.stateSync.(*fakechain.FakeStateSync).TraverseFunc = func(root util.Uint256, process func(node mpt.Node, nodeBytes []byte) bool) error {
|
|
|
|
if !(root.Equals(r1) || root.Equals(r2)) {
|
|
|
|
t.Fatal("unexpected root")
|
|
|
|
}
|
|
|
|
require.False(t, process(mpt.NewHashNode(r3), node))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
found := &payload.MPTData{
|
|
|
|
Nodes: [][]byte{node}, // no duplicates expected
|
|
|
|
}
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-09-06 12:16:47 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
switch msg.Command {
|
|
|
|
case CMDMPTData:
|
|
|
|
require.Equal(t, found, msg.Payload)
|
|
|
|
recvResponse.Store(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hs := []util.Uint256{r1, r2}
|
|
|
|
s.testHandleMessage(t, p, CMDGetMPTData, payload.NewMPTInventory(hs))
|
|
|
|
|
|
|
|
require.Eventually(t, recvResponse.Load, time.Second, time.Millisecond)
|
2022-07-14 11:13:38 +00:00
|
|
|
}
|
|
|
|
t.Run("KeepOnlyLatestState on", func(t *testing.T) {
|
2022-12-06 13:34:38 +00:00
|
|
|
s := startTestServer(t, func(c *config.Blockchain) {
|
2022-07-14 11:13:38 +00:00
|
|
|
c.P2PStateExchangeExtensions = true
|
2022-12-06 13:34:38 +00:00
|
|
|
c.Ledger.KeepOnlyLatestState = true
|
2022-07-14 11:13:38 +00:00
|
|
|
})
|
|
|
|
check(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
2022-12-06 13:34:38 +00:00
|
|
|
s := startTestServer(t, func(c *config.Blockchain) {
|
2022-07-14 11:13:38 +00:00
|
|
|
c.P2PStateExchangeExtensions = true
|
|
|
|
})
|
|
|
|
check(t, s)
|
2021-09-06 12:16:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleMPTData(t *testing.T) {
|
|
|
|
t.Run("P2PStateExchange extensions off", func(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-09-06 12:16:47 +00:00
|
|
|
msg := NewMessage(CMDMPTData, &payload.MPTData{
|
|
|
|
Nodes: [][]byte{{1, 2, 3}},
|
|
|
|
})
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
expected := [][]byte{{1, 2, 3}, {2, 3, 4}}
|
2022-11-29 14:43:08 +00:00
|
|
|
s := newTestServer(t, ServerConfig{UserAgent: "/test/"})
|
2022-01-20 18:14:42 +00:00
|
|
|
s.config.P2PStateExchangeExtensions = true
|
2021-09-06 12:16:47 +00:00
|
|
|
s.stateSync = &fakechain.FakeStateSync{
|
|
|
|
AddMPTNodesFunc: func(nodes [][]byte) error {
|
|
|
|
require.Equal(t, expected, nodes)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2021-11-15 09:37:01 +00:00
|
|
|
startWithCleanup(t, s)
|
2021-09-06 12:16:47 +00:00
|
|
|
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-09-06 12:16:47 +00:00
|
|
|
msg := NewMessage(CMDMPTData, &payload.MPTData{
|
|
|
|
Nodes: expected,
|
|
|
|
})
|
|
|
|
require.NoError(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestMPTNodes(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
|
|
|
|
|
|
|
var actual []util.Uint256
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-09-06 12:16:47 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDGetMPTData {
|
|
|
|
actual = append(actual, msg.Payload.(*payload.MPTInventory).Hashes...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.register <- p
|
|
|
|
s.register <- p // ensure previous send was handled
|
|
|
|
|
|
|
|
t.Run("no hashes, no message", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
require.NoError(t, s.requestMPTNodes(p, nil))
|
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, small", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := []util.Uint256{random.Uint256(), random.Uint256()}
|
|
|
|
require.NoError(t, s.requestMPTNodes(p, expected))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, exactly one chunk", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := make([]util.Uint256, payload.MaxMPTHashesCount)
|
|
|
|
for i := range expected {
|
|
|
|
expected[i] = random.Uint256()
|
|
|
|
}
|
|
|
|
require.NoError(t, s.requestMPTNodes(p, expected))
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, too large chunk", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := make([]util.Uint256, payload.MaxMPTHashesCount+1)
|
|
|
|
for i := range expected {
|
|
|
|
expected[i] = random.Uint256()
|
|
|
|
}
|
|
|
|
require.NoError(t, s.requestMPTNodes(p, expected))
|
|
|
|
require.Equal(t, expected[:payload.MaxMPTHashesCount], actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:52:19 +00:00
|
|
|
func TestRequestTx(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
var actual []util.Uint256
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDGetData {
|
|
|
|
actual = append(actual, msg.Payload.(*payload.Inventory).Hashes...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.register <- p
|
|
|
|
s.register <- p // ensure previous send was handled
|
|
|
|
|
|
|
|
t.Run("no hashes, no message", func(t *testing.T) {
|
|
|
|
actual = nil
|
2022-01-12 20:04:07 +00:00
|
|
|
s.RequestTx()
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, small", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := []util.Uint256{random.Uint256(), random.Uint256()}
|
2022-01-12 20:04:07 +00:00
|
|
|
s.RequestTx(expected...)
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, exactly one chunk", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := make([]util.Uint256, payload.MaxHashesCount)
|
|
|
|
for i := range expected {
|
|
|
|
expected[i] = random.Uint256()
|
|
|
|
}
|
2022-01-12 20:04:07 +00:00
|
|
|
s.RequestTx(expected...)
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
t.Run("good, multiple chunks", func(t *testing.T) {
|
|
|
|
actual = nil
|
|
|
|
expected := make([]util.Uint256, payload.MaxHashesCount*2+payload.MaxHashesCount/2)
|
|
|
|
for i := range expected {
|
|
|
|
expected[i] = random.Uint256()
|
|
|
|
}
|
2022-01-12 20:04:07 +00:00
|
|
|
s.RequestTx(expected...)
|
2020-12-07 09:52:19 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddrs(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
ips := make([][16]byte, 4)
|
|
|
|
copy(ips[0][:], net.IPv4(1, 2, 3, 4))
|
|
|
|
copy(ips[1][:], net.IPv4(7, 8, 9, 0))
|
|
|
|
for i := range ips[2] {
|
|
|
|
ips[2][i] = byte(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.getAddrSent = 1
|
|
|
|
pl := &payload.AddressList{
|
|
|
|
Addrs: []*payload.AddressAndTime{
|
|
|
|
{
|
|
|
|
IP: ips[0],
|
|
|
|
Capabilities: capability.Capabilities{{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: 12},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: ips[1],
|
|
|
|
Capabilities: capability.Capabilities{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IP: ips[2],
|
|
|
|
Capabilities: capability.Capabilities{{
|
|
|
|
Type: capability.TCPServer,
|
|
|
|
Data: &capability.Server{Port: 42},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.testHandleMessage(t, p, CMDAddr, pl)
|
|
|
|
|
|
|
|
addrs := s.discovery.(*testDiscovery).backfill
|
|
|
|
require.Equal(t, 2, len(addrs))
|
|
|
|
require.Equal(t, "1.2.3.4:12", addrs[0])
|
|
|
|
require.Equal(t, net.JoinHostPort(net.IP(ips[2][:]).String(), "42"), addrs[1])
|
|
|
|
|
|
|
|
t.Run("CMDAddr not requested", func(t *testing.T) {
|
|
|
|
msg := NewMessage(CMDAddr, pl)
|
|
|
|
require.Error(t, s.handleMessage(p, msg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type feerStub struct {
|
|
|
|
blockHeight uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f feerStub) FeePerByte() int64 { return 1 }
|
|
|
|
func (f feerStub) GetUtilityTokenBalance(util.Uint160) *big.Int { return big.NewInt(100000000) }
|
|
|
|
func (f feerStub) BlockHeight() uint32 { return f.blockHeight }
|
2020-12-11 12:22:49 +00:00
|
|
|
func (f feerStub) GetBaseExecFee() int64 { return interop.DefaultBaseExecFee }
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
func TestMemPool(t *testing.T) {
|
2021-03-01 11:14:15 +00:00
|
|
|
s := startTestServer(t)
|
2020-12-07 09:52:19 +00:00
|
|
|
|
|
|
|
var actual []util.Uint256
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2020-12-07 09:52:19 +00:00
|
|
|
p.messageHandler = func(t *testing.T, msg *Message) {
|
|
|
|
if msg.Command == CMDInv {
|
|
|
|
actual = append(actual, msg.Payload.(*payload.Inventory).Hashes...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 10:50:08 +00:00
|
|
|
bc := s.chain.(*fakechain.FakeChain)
|
2020-12-07 09:52:19 +00:00
|
|
|
expected := make([]util.Uint256, 4)
|
|
|
|
for i := range expected {
|
|
|
|
tx := newDummyTx()
|
|
|
|
require.NoError(t, bc.Pool.Add(tx, &feerStub{blockHeight: 10}))
|
|
|
|
expected[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.testHandleMessage(t, p, CMDMempool, payload.NullPayload{})
|
|
|
|
require.ElementsMatch(t, expected, actual)
|
|
|
|
}
|
2020-11-27 10:55:48 +00:00
|
|
|
|
|
|
|
func TestVerifyNotaryRequest(t *testing.T) {
|
2021-02-01 10:50:08 +00:00
|
|
|
bc := fakechain.NewFakeChain()
|
|
|
|
bc.MaxVerificationGAS = 10
|
2022-11-29 14:43:08 +00:00
|
|
|
s, err := newServerFromConstructors(ServerConfig{Addresses: []config.AnnounceableAddress{{Address: ":0"}}}, bc, new(fakechain.FakeStateSync), zaptest.NewLogger(t), newFakeTransp, newTestDiscovery)
|
2022-01-14 01:09:54 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-27 10:55:48 +00:00
|
|
|
newNotaryRequest := func() *payload.P2PNotaryRequest {
|
|
|
|
return &payload.P2PNotaryRequest{
|
2023-07-20 07:55:11 +00:00
|
|
|
MainTransaction: &transaction.Transaction{
|
|
|
|
Script: []byte{0, 1, 2},
|
|
|
|
Signers: []transaction.Signer{{Account: random.Uint160()}},
|
|
|
|
},
|
2020-11-27 10:55:48 +00:00
|
|
|
FallbackTransaction: &transaction.Transaction{
|
|
|
|
ValidUntilBlock: 321,
|
2024-06-05 15:21:27 +00:00
|
|
|
Signers: []transaction.Signer{{Account: nativehashes.Notary}, {Account: random.Uint160()}},
|
2020-11-27 10:55:48 +00:00
|
|
|
},
|
|
|
|
Witness: transaction.Witness{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("bad payload witness", func(t *testing.T) {
|
2021-10-25 14:42:20 +00:00
|
|
|
bc.VerifyWitnessF = func() (int64, error) { return 0, errors.New("bad witness") }
|
2022-01-14 01:09:54 +00:00
|
|
|
require.Error(t, s.verifyNotaryRequest(nil, newNotaryRequest()))
|
2020-11-27 10:55:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad fallback sender", func(t *testing.T) {
|
2021-10-25 14:42:20 +00:00
|
|
|
bc.VerifyWitnessF = func() (int64, error) { return 0, nil }
|
2020-11-27 10:55:48 +00:00
|
|
|
r := newNotaryRequest()
|
|
|
|
r.FallbackTransaction.Signers[0] = transaction.Signer{Account: util.Uint160{7, 8, 9}}
|
2022-01-14 01:09:54 +00:00
|
|
|
require.Error(t, s.verifyNotaryRequest(nil, r))
|
2020-11-27 10:55:48 +00:00
|
|
|
})
|
|
|
|
|
2023-07-20 07:55:11 +00:00
|
|
|
t.Run("bad main sender", func(t *testing.T) {
|
|
|
|
bc.VerifyWitnessF = func() (int64, error) { return 0, nil }
|
|
|
|
r := newNotaryRequest()
|
2024-06-05 15:21:27 +00:00
|
|
|
r.MainTransaction.Signers[0] = transaction.Signer{Account: nativehashes.Notary}
|
2023-07-20 07:55:11 +00:00
|
|
|
require.Error(t, s.verifyNotaryRequest(nil, r))
|
|
|
|
})
|
|
|
|
|
2020-11-27 10:55:48 +00:00
|
|
|
t.Run("expired deposit", func(t *testing.T) {
|
|
|
|
r := newNotaryRequest()
|
2021-02-01 10:50:08 +00:00
|
|
|
bc.NotaryDepositExpiration = r.FallbackTransaction.ValidUntilBlock
|
2022-01-14 01:09:54 +00:00
|
|
|
require.Error(t, s.verifyNotaryRequest(nil, r))
|
2020-11-27 10:55:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
r := newNotaryRequest()
|
2021-02-01 10:50:08 +00:00
|
|
|
bc.NotaryDepositExpiration = r.FallbackTransaction.ValidUntilBlock + 1
|
2022-01-14 01:09:54 +00:00
|
|
|
require.NoError(t, s.verifyNotaryRequest(nil, r))
|
2020-11-27 10:55:48 +00:00
|
|
|
})
|
|
|
|
}
|
2021-07-30 13:57:42 +00:00
|
|
|
|
|
|
|
func TestTryInitStateSync(t *testing.T) {
|
|
|
|
t.Run("module inactive", func(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
|
|
|
s.tryInitStateSync()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("module already initialized", func(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
2021-09-06 12:16:47 +00:00
|
|
|
ss := &fakechain.FakeStateSync{}
|
|
|
|
ss.IsActiveFlag.Store(true)
|
|
|
|
ss.IsInitializedFlag.Store(true)
|
|
|
|
s.stateSync = ss
|
2021-07-30 13:57:42 +00:00
|
|
|
s.tryInitStateSync()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good", func(t *testing.T) {
|
|
|
|
s := startTestServer(t)
|
|
|
|
for _, h := range []uint32{10, 8, 7, 4, 11, 4} {
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 1
|
2021-07-30 13:57:42 +00:00
|
|
|
p.lastBlockIndex = h
|
2021-09-13 08:16:39 +00:00
|
|
|
s.register <- p
|
2021-07-30 13:57:42 +00:00
|
|
|
}
|
|
|
|
p := newLocalPeer(t, s)
|
2022-10-12 12:46:58 +00:00
|
|
|
p.handshaked = 0 // one disconnected peer to check it won't be taken into attention
|
2021-07-30 13:57:42 +00:00
|
|
|
p.lastBlockIndex = 5
|
2021-09-13 08:16:39 +00:00
|
|
|
s.register <- p
|
|
|
|
require.Eventually(t, func() bool { return 7 == s.PeerCount() }, time.Second, time.Millisecond*10)
|
2021-07-30 13:57:42 +00:00
|
|
|
|
2021-09-13 08:16:39 +00:00
|
|
|
var expectedH uint32 = 8 // median peer
|
2021-09-06 12:16:47 +00:00
|
|
|
ss := &fakechain.FakeStateSync{InitFunc: func(h uint32) error {
|
2021-07-30 13:57:42 +00:00
|
|
|
if h != expectedH {
|
|
|
|
return fmt.Errorf("invalid height: expected %d, got %d", expectedH, h)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}}
|
2021-09-06 12:16:47 +00:00
|
|
|
ss.IsActiveFlag.Store(true)
|
|
|
|
s.stateSync = ss
|
2021-07-30 13:57:42 +00:00
|
|
|
s.tryInitStateSync()
|
|
|
|
})
|
|
|
|
}
|
2022-11-29 14:43:08 +00:00
|
|
|
|
|
|
|
func TestServer_Port(t *testing.T) {
|
|
|
|
s := newTestServer(t, ServerConfig{
|
|
|
|
Addresses: []config.AnnounceableAddress{
|
|
|
|
{Address: "1.2.3.4:10"}, // some random address
|
|
|
|
{Address: ":1"}, // listen all IPs
|
|
|
|
{Address: "127.0.0.1:2"}, // address without announced port
|
|
|
|
{Address: "123.123.0.123:3", AnnouncedPort: 123}}, // address with announced port
|
|
|
|
})
|
|
|
|
|
|
|
|
// Default addr => first port available
|
|
|
|
actual, err := s.Port(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint16(10), actual)
|
|
|
|
|
|
|
|
// Specified address with direct match => port of matched address
|
|
|
|
actual, err = s.Port(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 123})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint16(2), actual)
|
|
|
|
|
|
|
|
// No address match => 0.0.0.0's port
|
|
|
|
actual, err = s.Port(&net.TCPAddr{IP: net.IPv4(5, 6, 7, 8), Port: 123})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint16(1), actual)
|
|
|
|
|
|
|
|
// Specified address with match on announceable address => announced port
|
|
|
|
actual, err = s.Port(&net.TCPAddr{IP: net.IPv4(123, 123, 0, 123), Port: 123})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint16(123), actual)
|
|
|
|
}
|