Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Sep 19, 2024
1 parent 9c85ac5 commit 143d68c
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using JSSoft.Commands;
using LibplanetConsole.DataAnnotations;
using LibplanetConsole.Frameworks;
using LibplanetConsole.Settings;

Expand All @@ -11,7 +12,9 @@ internal sealed class StartCommand : CommandAsyncBase
private static readonly ApplicationSettingsCollection _settingsCollection = new();

[CommandPropertyRequired]
public string SettingsPath { get; set; } = string.Empty;
[CommandSummary("The path of the repository.")]
[Path(Type = PathType.Directory, ExistsType = PathExistsType.Exist)]
public string RepositoryPath { get; set; } = string.Empty;

[CommandProperty("parent")]
[CommandSummary("Reserved option used by libplanet-console.")]
Expand All @@ -26,8 +29,9 @@ protected override async Task OnExecuteAsync(CancellationToken cancellationToken
{
try
{
var settingsPath = Path.Combine(RepositoryPath, Repository.SettingsFileName);
var components = _settingsCollection.ToArray();
var applicationSettings = Load(SettingsPath) with
var applicationSettings = Load(settingsPath) with
{
ParentProcessId = ParentProcessId,
NoREPL = NoREPL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ internal sealed class NodeRepositoryProcess : NodeProcessBase
AppEndPoint.ToString(EndPoint),
"--genesis-path",
GenesisPath,
"--no-genesis",
];
}
2 changes: 1 addition & 1 deletion src/node/LibplanetConsole.Nodes.Executable/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override async ValueTask OnDisposeAsync()

private static bool GetStartupCondition(ApplicationOptions options)
{
if (options.SeedEndPoint is not null || options.IsSingleNode is true)
if (options.SeedEndPoint is not null)
{
return false;
}
Expand Down
14 changes: 12 additions & 2 deletions src/node/LibplanetConsole.Nodes.Executable/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using LibplanetConsole.Common.DataAnnotations;
using LibplanetConsole.DataAnnotations;
using LibplanetConsole.Frameworks;
using LibplanetConsole.Seeds;

namespace LibplanetConsole.Nodes.Executable;

Expand Down Expand Up @@ -85,17 +86,26 @@ public ApplicationOptions ToOptions(object[] components)
return new ApplicationOptions(endPoint, privateKey, genesis)
{
ParentProcessId = ParentProcessId,
SeedEndPoint = AppEndPoint.ParseOrDefault(SeedEndPoint),
SeedEndPoint = GetSeedEndPoint(),
StorePath = GetFullPath(StorePath),
LogPath = GetFullPath(LogPath),
LibraryLogPath = GetFullPath(LibraryLogPath),
NoREPL = NoREPL,
IsSingleNode = IsSingleNode,
Components = components,
};

static string GetFullPath(string path)
=> path != string.Empty ? Path.GetFullPath(path) : path;

AppEndPoint? GetSeedEndPoint()
{
if (SeedEndPoint != string.Empty)
{
return AppEndPoint.Parse(SeedEndPoint);
}

return IsSingleNode is true ? endPoint : null;
}
}

private static byte[] CreateGenesis(AppPrivateKey privateKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ public InitializeCommand()
[CommandProperty]
[CommandSummary("The file path of the genesis." +
"If omitted, the 'genesis' file is used.")]
[Path(Type = PathType.File, ExistsType = PathExistsType.NotExistOrEmpty, AllowEmpty = true)]
[Path(Type = PathType.File, AllowEmpty = true)]
public string GenesisPath { get; set; } = string.Empty;

[CommandPropertySwitch]
[CommandPropertySwitch("single-node")]
[CommandSummary("If set, the genesis is not stored at the specified genesis path.")]
[Category("Genesis")]
public bool NoGenesis { get; set; }
public bool IsSingleNode { get; set; }

[CommandProperty]
[CommandSummary("The private key of the genesis block. " +
"if omitted, a random private key is used.\n" +
"Mutually exclusive with '--no-genesis' option.")]
[CommandPropertyExclusion(nameof(NoGenesis))]
[CommandPropertyDependency(nameof(IsSingleNode))]
[AppPrivateKey]
[Category("Genesis")]
public string GenesisKey { get; set; } = string.Empty;
Expand All @@ -76,7 +75,7 @@ public InitializeCommand()
[CommandSummary("The timestamp of the genesis block. ex) \"2021-01-01T00:00:00Z\"\n" +
"Mutually exclusive with '--no-genesis' option.")]
[Category("Genesis")]
[CommandPropertyExclusion(nameof(NoGenesis))]
[CommandPropertyDependency(nameof(IsSingleNode))]
public DateTimeOffset DateTimeOffset { get; set; }

[CommandPropertySwitch("quiet", 'q')]
Expand All @@ -100,14 +99,15 @@ protected override void OnExecute()
LogPath = logPath,
LibraryLogPath = libraryLogPath,
GenesisPath = genesisPath,
SeedEndPoint = IsSingleNode is true ? endPoint : null,
};
dynamic info = repository.Save(outputPath);
using var writer = new ConditionalTextWriter(Out)
{
Condition = Quiet is false,
};

if (NoGenesis is false)
if (IsSingleNode is true)
{
var genesisKey = AppPrivateKey.ParseOrRandom(GenesisKey);
var validatorKeys = new AppPublicKey[] { privateKey.PublicKey };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ internal sealed class StartCommand : CommandAsyncBase
private static readonly ApplicationSettingsCollection _settingsCollection = new();

[CommandPropertyRequired]
[Path(Type = PathType.File, ExistsType = PathExistsType.Exist)]
public string SettingsPath { get; set; } = string.Empty;
[CommandSummary("The path of the repository.")]
[Path(Type = PathType.Directory, ExistsType = PathExistsType.Exist)]
public string RepositoryPath { get; set; } = string.Empty;

[CommandProperty("parent")]
[CommandSummary("Reserved option used by libplanet-console.")]
Expand All @@ -33,7 +34,7 @@ protected override async Task OnExecuteAsync(CancellationToken cancellationToken
{
try
{
var settingsPath = Path.GetFullPath(SettingsPath);
var settingsPath = Path.Combine(RepositoryPath, Repository.SettingsFileName);
var components = _settingsCollection.ToArray();
var applicationSettings = Load(settingsPath) with
{
Expand Down
6 changes: 0 additions & 6 deletions src/node/LibplanetConsole.Nodes/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ protected ApplicationBase(ApplicationOptions options)
SeedEndPoint = options.SeedEndPoint,
StorePath = options.StorePath,
LogPath = options.LogPath,
IsSingleNode = options.IsSingleNode,
ParentProcessId = options.ParentProcessId,
};
ApplicationServices = new(_container.GetExportedValues<IApplicationService>());
Expand Down Expand Up @@ -125,10 +124,5 @@ private async Task AutoStartAsync(CancellationToken cancellationToken)
_node.SeedEndPoint = seedEndPoint;
await _node.StartAsync(cancellationToken);
}
else if (_info.IsSingleNode is true)
{
_node.SeedEndPoint = _info.EndPoint;
await _node.StartAsync(cancellationToken);
}
}
}
2 changes: 0 additions & 2 deletions src/node/LibplanetConsole.Nodes/ApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ public readonly record struct ApplicationInfo

public required string LogPath { get; init; }

public bool IsSingleNode { get; init; }

public int ParentProcessId { get; init; }
}
2 changes: 0 additions & 2 deletions src/node/LibplanetConsole.Nodes/ApplicationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ public ApplicationOptions(AppEndPoint endPoint, AppPrivateKey privateKey, byte[]

public bool NoREPL { get; init; }

public bool IsSingleNode { get; init; }

public object[] Components { get; init; } = [];
}
3 changes: 1 addition & 2 deletions src/node/LibplanetConsole.Nodes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ private readonly SynchronizationContext _synchronizationContext
public Node(IServiceProvider serviceProvider, ApplicationOptions options, ILogger logger)
{
_serviceProvider = serviceProvider;
_seedEndPoint = options.SeedEndPoint
?? (options.IsSingleNode is true ? options.EndPoint : null);
_seedEndPoint = options.SeedEndPoint;
_privateKey = options.PrivateKey.ToSecureString();
_storePath = options.StorePath;
PublicKey = options.PrivateKey.PublicKey;
Expand Down
2 changes: 1 addition & 1 deletion src/node/LibplanetConsole.Nodes/Services/SeedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async ValueTask IAsyncDisposable.DisposeAsync()
async Task IApplicationService.InitializeAsync(
IServiceProvider serviceProvider, CancellationToken cancellationToken)
{
if (application.Info.IsSingleNode is true)
if (application.Info.SeedEndPoint == application.Info.EndPoint)
{
_blocksyncSeed = new Seed(new()
{
Expand Down

0 comments on commit 143d68c

Please sign in to comment.