diff --git a/core/Network/Broadcast.cs b/core/Network/Broadcast.cs index a5bd2a5d..19a1eab9 100644 --- a/core/Network/Broadcast.cs +++ b/core/Network/Broadcast.cs @@ -77,7 +77,7 @@ await Parallel.ForEachAsync(peers, (knownPeer, cancellationToken) => try { if (cancellationToken.IsCancellationRequested) return ValueTask.CompletedTask; - var _ = _cypherSystemCore.P2PDeviceReq().SendAsync(knownPeer.IpAddress, + var _ = _cypherSystemCore.P2PDeviceReq().SendAsync(knownPeer.IpAddress, knownPeer.TcpPort, knownPeer.PublicKey, msg).SafeForgetAsync(_logger).ConfigureAwait(false); } diff --git a/core/Network/P2PDeviceReq.cs b/core/Network/P2PDeviceReq.cs index e2858694..1808293f 100644 --- a/core/Network/P2PDeviceReq.cs +++ b/core/Network/P2PDeviceReq.cs @@ -23,8 +23,8 @@ Task SendAsync(ReadOnlyMemory ipAddress, ReadOnlyMemory tcpPor ReadOnlyMemory value, bool deserialize = true); } -public class Nothing { } -public class Ok { } +public class EmptyMessage { } +public class Ping { } /// /// @@ -33,17 +33,13 @@ public class P2PDeviceReq : IP2PDeviceReq { private readonly ICypherSystemCore _cypherSystemCore; private readonly ILogger _logger; - + private readonly Ping _ping = new(); + public P2PDeviceReq(ICypherSystemCore cypherSystemCore) { _cypherSystemCore = cypherSystemCore; using var serviceScope = cypherSystemCore.ServiceScopeFactory.CreateScope(); _logger = serviceScope.ServiceProvider.GetService()?.ForContext("SourceContext", nameof(P2PDeviceReq)); - - var soc = NngFactorySingleton.Instance.Factory.RequesterOpen(); - - var ireq = soc.Unwrap(); - } /// @@ -88,8 +84,9 @@ public async Task SendAsync(ReadOnlyMemory ipAddress, ReadOnlyMemory foreach (var memory in packetStream.GetReadOnlySequence()) nngMsg.Append(memory.Span); var nngResult = await ctx.Send(nngMsg); - if (typeof(T) == typeof(Nothing)) return default; if (!nngResult.IsOk()) return default; + if (typeof(T) == typeof(EmptyMessage)) return default; + if (typeof(T) == typeof(Ping)) return (T)(object)_ping; var nngRecvMsg = nngResult.Unwrap(); var message = await _cypherSystemCore.P2PDevice().DecryptAsync(nngRecvMsg); nngRecvMsg.Dispose(); diff --git a/core/Network/PeerDiscovery.cs b/core/Network/PeerDiscovery.cs index 4573139f..f145f14a 100644 --- a/core/Network/PeerDiscovery.cs +++ b/core/Network/PeerDiscovery.cs @@ -78,7 +78,6 @@ public interface IPeerDiscovery /// public sealed class PeerDiscovery : IDisposable, IPeerDiscovery { - private const int PrunedTimeoutFromSeconds = 10; private readonly Caching _caching = new(); private readonly Caching _peerCooldownCaching = new(); private readonly ICypherSystemCore _cypherSystemCore; @@ -255,7 +254,7 @@ private async Task BootstrapSeedsAsync() var seedNode = _seedNodes[i]; try { - var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync(seedNode.IpAddress, + var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync(seedNode.IpAddress, seedNode.TcpPort, seedNode.PublicKey, readOnlySequenceMsg.IsSingleSegment @@ -306,22 +305,18 @@ private async Task OnReadyAsync() { try { - var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync(peer.IpAddress, peer.TcpPort, - peer.PublicKey, readOnlySequenceMsg.IsSingleSegment - ? readOnlySequenceMsg.First - : readOnlySequenceMsg.ToArray(), false); + if (await _cypherSystemCore.P2PDeviceReq().SendAsync(peer.IpAddress, peer.TcpPort, peer.PublicKey, + readOnlySequenceMsg.IsSingleSegment ? readOnlySequenceMsg.First : readOnlySequenceMsg.ToArray(), + false) is null) + { + _caching.Remove(peer.IpAddress); + } + } catch (Exception ex) { _logger.Here().Error("{@Message}", ex.Message); } - finally - { - if (Util.GetAdjustedTimeAsUnixTimestamp() > peer.Timestamp + PrunedTimeoutFromSeconds || peer.Timestamp == 0) - { - _caching.Remove(peer.IpAddress); - } - } } } diff --git a/core/core.csproj b/core/core.csproj index 2c2cbacb..87bf42a9 100644 --- a/core/core.csproj +++ b/core/core.csproj @@ -4,7 +4,7 @@ net6.0 AnyCPU;x64 CypherNetwork - 0.0.70.0 + 0.0.71.0 CypherNetwork core cyphernetworkcore diff --git a/node/Program.cs b/node/Program.cs index 74094059..acba6920 100644 --- a/node/Program.cs +++ b/node/Program.cs @@ -30,48 +30,48 @@ public static class Program /// public static async Task Main(string[] args) { - var config = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile(AppSettingsFile, false, true) - .AddCommandLine(args) - .Build(); - - // args = new string[] { "--configure", "--showkey" }; - // args = new string[] { "--configure" }; - var settingsExists = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppSettingsFile)); - if (!settingsExists) - { - await Console.Error.WriteLineAsync($"{AppSettingsFile} not found."); - return 1; - } - if (args.FirstOrDefault(arg => arg == "--configure") != null) + try { - var commands = args.Where(x => x != "--configure").ToArray(); - if (commands.Contains("--showkey")) + var config = new ConfigurationBuilder() + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .AddJsonFile(AppSettingsFile, false, true) + .AddCommandLine(args) + .Build(); + + // args = new string[] { "--configure", "--showkey" }; + // args = new string[] { "--configure" }; + var settingsExists = File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppSettingsFile)); + if (!settingsExists) { - Startup.ShowPrivateKey = true; + await Console.Error.WriteLineAsync($"{AppSettingsFile} not found."); + return 1; } - else + if (args.FirstOrDefault(arg => arg == "--configure") != null) { - var _ = new Utility(config.Get()); - return 0; + var commands = args.Where(x => x != "--configure").ToArray(); + if (commands.Contains("--showkey")) + { + Startup.ShowPrivateKey = true; + } + else + { + var _ = new Utility(config.Get()); + return 0; + } } - } - - const string logSectionName = "Log"; - if (config.GetSection(logSectionName) != null) - { - Log.Logger = new LoggerConfiguration() - .ReadFrom.Configuration(config, logSectionName) - .CreateLogger(); - } - else - { - throw new Exception($"No \"{logSectionName}\" section found in appsettings.json"); - } - try - { + const string logSectionName = "Log"; + if (config.GetSection(logSectionName) != null) + { + Log.Logger = new LoggerConfiguration() + .ReadFrom.Configuration(config, logSectionName) + .CreateLogger(); + } + else + { + throw new Exception($"No \"{logSectionName}\" section found in appsettings.json"); + } + Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine(@$" ______ __ __ diff --git a/node/appsettings.json b/node/appsettings.json index dd60adbc..c26db4df 100644 --- a/node/appsettings.json +++ b/node/appsettings.json @@ -24,7 +24,7 @@ ], "Environment": "testnet", "SigningKeyRingName": "DefaultSigning.cyp3.Key", - "AutoSyncEveryMinutes": "10", + "AutoSyncEveryMinutes": 10, "PeerCooldownMinutes": 30, "CertificateMode": "self", "MaxBlockSize" : 2102912, diff --git a/node/node.csproj b/node/node.csproj index bcfac165..a8656d2b 100644 --- a/node/node.csproj +++ b/node/node.csproj @@ -2,7 +2,7 @@ true - 0.0.70.0 + 0.0.71.0 en node