cs-codex-dist-tests/Tests/CodexTests/PeerDiscoveryTests/PeerDiscoveryTests.cs

95 lines
3.0 KiB
C#
Raw Normal View History

using CodexContractsPlugin;
using CodexPlugin;
using GethPlugin;
using NUnit.Framework;
2023-09-12 13:43:30 +00:00
2023-11-12 09:36:48 +00:00
namespace CodexTests.PeerDiscoveryTests
2023-09-12 13:43:30 +00:00
{
[TestFixture]
public class PeerDiscoveryTests : AutoBootstrapDistTest
{
[Test]
public void CanReportUnknownPeerId()
{
var unknownId = "16Uiu2HAkv2CHWpff3dj5iuVNERAp8AGKGNgpGjPexJZHSqUstfsK";
2024-05-09 07:32:48 +00:00
var node = StartCodex();
2023-09-12 13:43:30 +00:00
var result = node.GetDebugPeer(unknownId);
Assert.That(result.IsPeerFound, Is.False);
}
[Test]
public void MetricsDoesNotInterfereWithPeerDiscovery()
{
2024-06-12 13:28:08 +00:00
var nodes = StartCodex(2, s => s.EnableMetrics());
2023-09-12 13:43:30 +00:00
2024-06-12 13:28:08 +00:00
AssertAllNodesConnected(nodes);
2023-09-12 13:43:30 +00:00
}
[Test]
public void MarketplaceDoesNotInterfereWithPeerDiscovery()
{
var geth = Ci.StartGethNode(s => s.IsMiner());
var contracts = Ci.StartCodexContracts(geth);
2024-06-12 13:28:08 +00:00
var nodes = StartCodex(2, s => s.EnableMarketplace(geth, contracts, m => m
.WithInitial(10.Eth(), 1000.TstWei())));
2023-09-12 13:43:30 +00:00
2024-06-12 13:28:08 +00:00
AssertAllNodesConnected(nodes);
2023-09-12 13:43:30 +00:00
}
[TestCase(2)]
[TestCase(3)]
[TestCase(10)]
public void VariableNodes(int number)
{
2024-06-12 13:28:08 +00:00
var nodes = StartCodex(number);
2023-09-12 13:43:30 +00:00
2024-06-12 13:28:08 +00:00
AssertAllNodesConnected(nodes);
2023-09-12 13:43:30 +00:00
}
2024-06-12 13:28:08 +00:00
private void AssertAllNodesConnected(IEnumerable<ICodexNode> nodes)
2023-09-12 13:43:30 +00:00
{
nodes = nodes.Concat(new[] { BootstrapNode }).ToArray()!;
2024-06-12 13:28:08 +00:00
CreatePeerConnectionTestHelpers().AssertFullyConnected(nodes);
CheckRoutingTable(nodes);
}
private void CheckRoutingTable(IEnumerable<ICodexNode> allNodes)
{
var allResponses = allNodes.Select(n => n.GetDebugInfo()).ToArray();
var errors = new List<string>();
foreach (var response in allResponses)
{
var error = AreAllPresent(response, allResponses);
if (!string.IsNullOrEmpty(error)) errors.Add(error);
}
if (errors.Any())
{
Assert.Fail(string.Join(Environment.NewLine, errors));
}
}
2024-03-26 07:58:16 +00:00
private string AreAllPresent(DebugInfo info, DebugInfo[] allResponses)
{
2024-03-26 09:03:52 +00:00
var knownIds = info.Table.Nodes.Select(n => n.NodeId).ToArray();
var allOthers = GetAllOtherResponses(info, allResponses);
2024-03-26 09:03:52 +00:00
var expectedIds = allOthers.Select(i => i.Table.LocalNode.NodeId).ToArray();
if (!expectedIds.All(ex => knownIds.Contains(ex)))
{
2024-03-26 09:03:52 +00:00
return $"Node {info.Id}: Not all of '{string.Join(",", expectedIds)}' were present in routing table: '{string.Join(",", knownIds)}'";
}
return string.Empty;
}
2024-03-26 07:58:16 +00:00
private DebugInfo[] GetAllOtherResponses(DebugInfo exclude, DebugInfo[] allResponses)
{
2024-03-26 07:58:16 +00:00
return allResponses.Where(r => r.Id != exclude.Id).ToArray();
2023-09-12 13:43:30 +00:00
}
}
}