2022-08-29 14:25:30 +00:00
|
|
|
// stm: #integration
|
2022-02-12 17:44:00 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-16 16:24:26 +00:00
|
|
|
"fmt"
|
2022-02-12 17:44:00 +00:00
|
|
|
"testing"
|
2023-05-10 20:35:36 +00:00
|
|
|
"time"
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-02-12 17:44:00 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2022-02-16 16:24:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-12 17:44:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-23 18:02:10 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
2022-02-12 17:44:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetConn(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_ID_001
|
2022-02-12 17:44:00 +00:00
|
|
|
secondNodeID, err := secondNode.ID(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
connState := getConnState(ctx, t, firstNode, secondNodeID)
|
|
|
|
|
|
|
|
if connState != network.NotConnected {
|
|
|
|
t.Errorf("node should be not connected to peers. %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_ADDRS_LISTEN_001
|
2022-02-12 17:44:00 +00:00
|
|
|
addrInfo, err := secondNode.NetAddrsListen(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_CONNECT_001
|
2022-02-12 17:44:00 +00:00
|
|
|
err = firstNode.NetConnect(ctx, addrInfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("nodes failed to connect. %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_PEER_INFO_001
|
2022-02-12 17:44:00 +00:00
|
|
|
netPeerInfo, err := firstNode.NetPeerInfo(ctx, secondNodeID)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_AGENT_VERSION_001
|
2022-02-12 17:44:00 +00:00
|
|
|
agent, err := firstNode.NetAgentVersion(ctx, secondNodeID)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
if netPeerInfo.Agent != agent {
|
|
|
|
t.Errorf("agents not matching. %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_FIND_PEER_001
|
2022-02-12 17:44:00 +00:00
|
|
|
secondNodePeer, err := firstNode.NetFindPeer(ctx, secondNodeID)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
if secondNodePeer.ID != addrInfo.ID {
|
|
|
|
t.Errorf("peer id doesn't match with listen address.")
|
|
|
|
}
|
|
|
|
|
|
|
|
connState = getConnState(ctx, t, firstNode, secondNodeID)
|
|
|
|
|
|
|
|
if connState != network.Connected {
|
|
|
|
t.Errorf("peer does not have connected state")
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_PEERS_001
|
2022-03-01 00:58:49 +00:00
|
|
|
addrs, err := firstNode.NetPeers(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-23 18:02:10 +00:00
|
|
|
require.NotEqual(t, 0, len(addrs))
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_DISCONNECT_001
|
2022-02-12 17:44:00 +00:00
|
|
|
err = firstNode.NetDisconnect(ctx, secondNodeID)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("nodes failed to disconnect. %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
connState = getConnState(ctx, t, firstNode, secondNodeID)
|
|
|
|
|
|
|
|
if connState != network.NotConnected {
|
|
|
|
t.Errorf("peer should have disconnected")
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_PEERS_001
|
2022-03-01 00:58:49 +00:00
|
|
|
addrs, err = firstNode.NetPeers(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2022-03-01 00:58:49 +00:00
|
|
|
if len(addrs) > 0 {
|
2022-02-12 17:44:00 +00:00
|
|
|
t.Errorf("there should be no peers in network after disconnecting node")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-16 16:24:26 +00:00
|
|
|
func TestNetStat(t *testing.T) {
|
|
|
|
|
|
|
|
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
sId, err := secondNode.ID(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
withScope := func(api interface{}, scope string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
|
|
|
|
stat, err := firstNode.NetStat(ctx, scope)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
switch scope {
|
|
|
|
case "all":
|
|
|
|
assert.NotNil(t, stat.System)
|
|
|
|
assert.NotNil(t, stat.Transient)
|
|
|
|
case "system":
|
|
|
|
assert.NotNil(t, stat.System)
|
|
|
|
case "transient":
|
|
|
|
assert.NotNil(t, stat.Transient)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_STAT_001
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("all", withScope(t, "all"))
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_STAT_002
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("system", withScope(t, "system"))
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_STAT_003
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("transient", withScope(t, "transient"))
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_STAT_004
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("peer", withScope(t, fmt.Sprintf("peer:%s", sId)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetLimit(t *testing.T) {
|
|
|
|
|
|
|
|
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
sId, err := secondNode.ID(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
withScope := func(api interface{}, scope string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
_, err := firstNode.NetLimit(ctx, scope)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_LIMIT_001
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("system", withScope(t, "system"))
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_LIMIT_002
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("transient", withScope(t, "transient"))
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_LIMIT_003
|
2022-02-16 16:24:26 +00:00
|
|
|
t.Run("peer", withScope(t, fmt.Sprintf("peer:%s", sId)))
|
|
|
|
}
|
|
|
|
|
2022-02-12 17:44:00 +00:00
|
|
|
func TestNetBlockPeer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_ID_001
|
2022-06-23 18:02:10 +00:00
|
|
|
firstAddrInfo, _ := firstNode.NetAddrsListen(ctx)
|
|
|
|
firstNodeID, err := firstNode.ID(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
secondNodeID, err := secondNode.ID(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
// Sanity check that we're not already connected somehow
|
|
|
|
connectedness, err := secondNode.NetConnectedness(ctx, firstNodeID)
|
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
require.NotEqual(t, connectedness, network.Connected, "shouldn't already be connected")
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_ADD_001
|
2022-02-12 17:44:00 +00:00
|
|
|
err = firstNode.NetBlockAdd(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_LIST_001
|
2022-02-12 17:44:00 +00:00
|
|
|
list, err := firstNode.NetBlockList(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
if len(list.Peers) == 0 || list.Peers[0] != secondNodeID {
|
|
|
|
t.Errorf("blocked peer not in blocked peer list")
|
|
|
|
}
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
err = secondNode.NetConnect(ctx, firstAddrInfo)
|
|
|
|
// With early muxer selection, we'll only learn that the handshake failed
|
|
|
|
// when we do something with the connection, for example when we open a stream.
|
|
|
|
if err == nil {
|
|
|
|
_, err = secondNode.NetPing(context.Background(), firstAddrInfo.ID)
|
|
|
|
}
|
|
|
|
require.Error(t, err, "shouldn't be able to connect to second node")
|
|
|
|
connectedness, err = firstNode.NetConnectedness(ctx, secondNodeID)
|
2022-06-23 18:02:10 +00:00
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
require.NotEqual(t, connectedness, network.Connected)
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_REMOVE_001
|
2022-02-12 17:44:00 +00:00
|
|
|
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{Peers: []peer.ID{secondNodeID}})
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_LIST_001
|
2022-02-12 17:44:00 +00:00
|
|
|
list, err = firstNode.NetBlockList(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
if len(list.Peers) > 0 {
|
|
|
|
t.Errorf("failed to remove blocked peer from blocked peer list")
|
|
|
|
}
|
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
require.NoError(t, secondNode.NetConnect(ctx, firstAddrInfo), "failed to connect to second node")
|
|
|
|
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
|
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
require.Equal(t, connectedness, network.Connected)
|
2022-02-12 17:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetBlockIPAddr(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
firstNode, secondNode, _, _ := kit.EnsembleTwoOne(t)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_ADDRS_LISTEN_001
|
2022-06-23 18:02:10 +00:00
|
|
|
firstAddrInfo, _ := firstNode.NetAddrsListen(ctx)
|
|
|
|
secondAddrInfo, _ := secondNode.NetAddrsListen(ctx)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-05-10 20:28:09 +00:00
|
|
|
secondNodeIPsMap := map[string]struct{}{} // so we can deduplicate
|
2022-06-23 18:02:10 +00:00
|
|
|
for _, addr := range secondAddrInfo.Addrs {
|
2022-02-12 17:44:00 +00:00
|
|
|
ip, err := manet.ToIP(addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-10 20:28:09 +00:00
|
|
|
secondNodeIPsMap[ip.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
var secondNodeIPs []string
|
|
|
|
for s := range secondNodeIPsMap {
|
|
|
|
secondNodeIPs = append(secondNodeIPs, s)
|
2022-02-12 17:44:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
// Sanity check that we're not already connected somehow
|
|
|
|
connectedness, err := secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
|
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
require.NotEqual(t, connectedness, network.Connected, "shouldn't already be connected")
|
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_ADD_001
|
2022-06-23 18:02:10 +00:00
|
|
|
require.NoError(t, firstNode.NetBlockAdd(ctx, api.NetBlockList{
|
|
|
|
IPAddrs: secondNodeIPs}), "failed to add blocked IPs")
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_LIST_001
|
2022-02-12 17:44:00 +00:00
|
|
|
list, err := firstNode.NetBlockList(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-05-10 20:28:09 +00:00
|
|
|
fmt.Println(list.IPAddrs)
|
|
|
|
fmt.Println(secondNodeIPs)
|
2022-06-23 18:02:10 +00:00
|
|
|
require.Equal(t, len(list.IPAddrs), len(secondNodeIPs), "expected %d blocked IPs", len(secondNodeIPs))
|
|
|
|
for _, blockedIP := range list.IPAddrs {
|
|
|
|
found := false
|
|
|
|
for _, secondNodeIP := range secondNodeIPs {
|
|
|
|
if blockedIP == secondNodeIP {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.True(t, found, "blocked IP %s is not one of secondNodeIPs", blockedIP)
|
2022-02-12 17:44:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 20:28:09 +00:00
|
|
|
// a QUIC connection might still succeed when gated, but will be killed right after the handshake
|
|
|
|
_ = secondNode.NetConnect(ctx, firstAddrInfo)
|
2023-05-10 20:35:36 +00:00
|
|
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
|
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
return connectedness != network.Connected
|
|
|
|
}, time.Second*5, time.Millisecond*10)
|
2022-06-23 18:02:10 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_REMOVE_001
|
2022-02-12 17:44:00 +00:00
|
|
|
err = firstNode.NetBlockRemove(ctx, api.NetBlockList{IPAddrs: secondNodeIPs})
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_BLOCK_LIST_001
|
2022-02-12 17:44:00 +00:00
|
|
|
list, err = firstNode.NetBlockList(ctx)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
if len(list.IPAddrs) > 0 {
|
|
|
|
t.Errorf("failed to remove blocked ip from blocked ip list")
|
|
|
|
}
|
|
|
|
|
2022-06-23 18:02:10 +00:00
|
|
|
require.NoError(t, secondNode.NetConnect(ctx, firstAddrInfo), "failed to connect to second node")
|
|
|
|
connectedness, err = secondNode.NetConnectedness(ctx, firstAddrInfo.ID)
|
|
|
|
require.NoError(t, err, "failed to determine connectedness")
|
|
|
|
require.Equal(t, connectedness, network.Connected)
|
2022-02-12 17:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getConnState(ctx context.Context, t *testing.T, node *kit.TestFullNode, peer peer.ID) network.Connectedness {
|
2023-03-15 05:10:29 +00:00
|
|
|
// stm: @NETWORK_COMMON_CONNECTEDNESS_001
|
2022-02-12 17:44:00 +00:00
|
|
|
connState, err := node.NetConnectedness(ctx, peer)
|
2022-02-16 16:24:26 +00:00
|
|
|
require.NoError(t, err)
|
2022-02-12 17:44:00 +00:00
|
|
|
|
|
|
|
return connState
|
|
|
|
}
|