Skip to content

Commit

Permalink
Merge pull request #103 from s2quake/fix/run-node-client
Browse files Browse the repository at this point in the history
Change to use genesis-path when running nodes
  • Loading branch information
s2quake authored Dec 10, 2024
2 parents 7aa95ba + ae30e18 commit 9ba7c99
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ dotnet_diagnostic.MEN007.severity = none
# MEN016: Avoid top-level statements
dotnet_diagnostic.MEN016.severity = none

# S3963: "static" fields should be initialized inline
dotnet_diagnostic.S3963.severity = silent

[**/obj/**/*.cs]
# SA1200: Using directives should be placed correctly
dotnet_diagnostic.SA1200.severity = none
Expand Down
40 changes: 40 additions & 0 deletions src/common/LibplanetConsole.Common/IO/DirectoryUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace LibplanetConsole.Common.IO;

public static class DirectoryUtility
{
public static string EnsureDirectory(string path)
{
if (Directory.Exists(path) is false)
{
return Directory.CreateDirectory(path).FullName;
}

return Path.GetFullPath(path);
}

public static void DeleteIfExists(string path)
{
if (Directory.Exists(path) == true)
{
Directory.Delete(path);
}
}

public static bool TryDelete(string path, bool recursive)
{
try
{
if (Directory.Exists(path) == true)
{
Directory.Delete(path, recursive);
return true;
}
}
catch
{
// ignored
}

return false;
}
}
30 changes: 30 additions & 0 deletions src/common/LibplanetConsole.Common/IO/FileUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LibplanetConsole.Common.IO;

public static class FileUtility
{
public static void DeleteIfExists(string path)
{
if (File.Exists(path) == true)
{
File.Delete(path);
}
}

public static bool TryDelete(string path)
{
try
{
if (File.Exists(path) == true)
{
File.Delete(path);
return true;
}
}
catch
{
// ignored
}

return false;
}
}
48 changes: 25 additions & 23 deletions src/common/LibplanetConsole.Common/IO/TempFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@ namespace LibplanetConsole.Common.IO;

public sealed class TempFile : IDisposable
{
public TempFile()
{
FileName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
}

public string FileName { get; }
private static readonly string _directory = Path.Combine(
Path.GetTempPath(),
AppDomain.CurrentDomain.FriendlyName,
$"{Environment.ProcessId}");

public static implicit operator string(TempFile tempFile)
static TempFile()
{
return tempFile.FileName;
}
DirectoryUtility.EnsureDirectory(_directory);
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;

public static TempFile WriteAllText(string content)
{
var tempFile = new TempFile();
File.WriteAllText(tempFile.FileName, content);
return tempFile;
static void CurrentDomain_ProcessExit(object? sender, EventArgs e)
=> DirectoryUtility.TryDelete(_directory, recursive: true);
}

public void Dispose()
public TempFile()
{
if (File.Exists(FileName) == true)
{
File.Delete(FileName);
}
}

public override string ToString()
{
return FileName;
}
private TempFile(string content) => File.WriteAllText(FileName, content);

private TempFile(byte[] bytes) => File.WriteAllBytes(FileName, bytes);

public string FileName { get; } = Path.Combine(_directory, Path.GetRandomFileName());

public static implicit operator string(TempFile tempFile) => tempFile.FileName;

public static TempFile WriteAllText(string content) => new(content);

public static TempFile WriteAllBytes(byte[] bytes) => new(bytes);

public void Dispose() => FileUtility.TryDelete(FileName);

public override string ToString() => FileName;
}
5 changes: 5 additions & 0 deletions src/console/LibplanetConsole.Console/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ public async Task StartProcessAsync(ProcessOptions options, CancellationToken ca
.ContinueWith(
task =>
{
if (task.IsFaulted is true)
{
_logger.LogError(task.Exception, "Failed to run the client process.");
}

_processTask = Task.CompletedTask;
_process = null;
_processCancellationTokenSource?.Dispose();
Expand Down
17 changes: 13 additions & 4 deletions src/console/LibplanetConsole.Console/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Grpc.Net.Client;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Common.IO;
using LibplanetConsole.Common.Threading;
using LibplanetConsole.Console.Services;
using LibplanetConsole.Grpc.Blockchain;
Expand Down Expand Up @@ -312,6 +313,11 @@ public async Task StartProcessAsync(ProcessOptions options, CancellationToken ca
.ContinueWith(
task =>
{
if (task.IsFaulted is true)
{
_logger.LogError(task.Exception, "Failed to run the node process.");
}

_processTask = Task.CompletedTask;
_process = null;
_processCancellationTokenSource?.Dispose();
Expand Down Expand Up @@ -444,10 +450,13 @@ private NodeProcess CreateProcess(ProcessOptions options)
if (nodeOptions.RepositoryPath == string.Empty)
{
var applicationOptions = _serviceProvider.GetRequiredService<IApplicationOptions>();
process.ExtendedArguments.Add("--genesis");
process.ExtendedArguments.Add(BlockUtility.ToString(applicationOptions.GenesisBlock));
process.ExtendedArguments.Add("--apv");
process.ExtendedArguments.Add(applicationOptions.AppProtocolVersion.Token);
var genesisPath = TempFile.WriteAllBytes(
BlockUtility.SerializeBlock(applicationOptions.GenesisBlock));
var apvPath = TempFile.WriteAllText(applicationOptions.AppProtocolVersion.Token);
process.ExtendedArguments.Add("--genesis-path");
process.ExtendedArguments.Add(genesisPath);
process.ExtendedArguments.Add("--apv-path");
process.ExtendedArguments.Add(apvPath);

if (applicationOptions.LogPath != string.Empty)
{
Expand Down

0 comments on commit 9ba7c99

Please sign in to comment.