Skip to content

Commit

Permalink
Merge pull request #117 from pingpongsneak/master
Browse files Browse the repository at this point in the history
Fixes for RC1
  • Loading branch information
pingpongsneak authored Oct 8, 2022
2 parents 7fb84d1 + 12378ae commit 9da0b5f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 62 deletions.
2 changes: 1 addition & 1 deletion core/Network/Broadcast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ await Parallel.ForEachAsync(peers, (knownPeer, cancellationToken) =>
try
{
if (cancellationToken.IsCancellationRequested) return ValueTask.CompletedTask;
var _ = _cypherSystemCore.P2PDeviceReq().SendAsync<Nothing>(knownPeer.IpAddress,
var _ = _cypherSystemCore.P2PDeviceReq().SendAsync<EmptyMessage>(knownPeer.IpAddress,
knownPeer.TcpPort,
knownPeer.PublicKey, msg).SafeForgetAsync(_logger).ConfigureAwait(false);
}
Expand Down
15 changes: 6 additions & 9 deletions core/Network/P2PDeviceReq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Task<T> SendAsync<T>(ReadOnlyMemory<byte> ipAddress, ReadOnlyMemory<byte> tcpPor
ReadOnlyMemory<byte> value, bool deserialize = true);
}

public class Nothing { }
public class Ok { }
public class EmptyMessage { }
public class Ping { }

/// <summary>
///
Expand All @@ -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<ILogger>()?.ForContext("SourceContext", nameof(P2PDeviceReq));

var soc = NngFactorySingleton.Instance.Factory.RequesterOpen();

var ireq = soc.Unwrap();

}

/// <summary>
Expand Down Expand Up @@ -88,8 +84,9 @@ public async Task<T> SendAsync<T>(ReadOnlyMemory<byte> 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();
Expand Down
21 changes: 8 additions & 13 deletions core/Network/PeerDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public interface IPeerDiscovery
/// </summary>
public sealed class PeerDiscovery : IDisposable, IPeerDiscovery
{
private const int PrunedTimeoutFromSeconds = 10;
private readonly Caching<Peer> _caching = new();
private readonly Caching<PeerCooldown> _peerCooldownCaching = new();
private readonly ICypherSystemCore _cypherSystemCore;
Expand Down Expand Up @@ -255,7 +254,7 @@ private async Task BootstrapSeedsAsync()
var seedNode = _seedNodes[i];
try
{
var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync<Nothing>(seedNode.IpAddress,
var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync<EmptyMessage>(seedNode.IpAddress,
seedNode.TcpPort,
seedNode.PublicKey,
readOnlySequenceMsg.IsSingleSegment
Expand Down Expand Up @@ -306,22 +305,18 @@ private async Task OnReadyAsync()
{
try
{
var _ = await _cypherSystemCore.P2PDeviceReq().SendAsync<Nothing>(peer.IpAddress, peer.TcpPort,
peer.PublicKey, readOnlySequenceMsg.IsSingleSegment
? readOnlySequenceMsg.First
: readOnlySequenceMsg.ToArray(), false);
if (await _cypherSystemCore.P2PDeviceReq().SendAsync<Ping>(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);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
<RootNamespace>CypherNetwork</RootNamespace>
<AssemblyVersion>0.0.70.0</AssemblyVersion>
<AssemblyVersion>0.0.71.0</AssemblyVersion>
<Company>CypherNetwork</Company>
<PackageId>core</PackageId>
<AssemblyName>cyphernetworkcore</AssemblyName>
Expand Down
72 changes: 36 additions & 36 deletions node/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,48 @@ public static class Program
/// <returns></returns>
public static async Task<int> 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<Config>());
return 0;
var commands = args.Where(x => x != "--configure").ToArray();
if (commands.Contains("--showkey"))
{
Startup.ShowPrivateKey = true;
}
else
{
var _ = new Utility(config.Get<Config>());
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(@$"
______ __ __
Expand Down
2 changes: 1 addition & 1 deletion node/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"Environment": "testnet",
"SigningKeyRingName": "DefaultSigning.cyp3.Key",
"AutoSyncEveryMinutes": "10",
"AutoSyncEveryMinutes": 10,
"PeerCooldownMinutes": 30,
"CertificateMode": "self",
"MaxBlockSize" : 2102912,
Expand Down
2 changes: 1 addition & 1 deletion node/node.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<ServerGarbageCollection>true</ServerGarbageCollection>
<AssemblyVersion>0.0.70.0</AssemblyVersion>
<AssemblyVersion>0.0.71.0</AssemblyVersion>
<NeutralLanguage>en</NeutralLanguage>
<PackageId>node</PackageId>
</PropertyGroup>
Expand Down

0 comments on commit 9da0b5f

Please sign in to comment.