Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new records for use in the application layer #48

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libplanet-console.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibplanetConsole.Nodes.Expl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibplanetConsole.Consoles.Explorer", "src\console\LibplanetConsole.Consoles.Explorer\LibplanetConsole.Consoles.Explorer.csproj", "{AF1A0011-2795-42FD-B67B-7F1956268577}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibplanetConsole.Common.Tests", "test\LibplanetConsole.Common.Tests\LibplanetConsole.Common.Tests.csproj", "{65341396-A058-4577-9B70-C1DD3D146501}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +121,10 @@ Global
{AF1A0011-2795-42FD-B67B-7F1956268577}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF1A0011-2795-42FD-B67B-7F1956268577}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF1A0011-2795-42FD-B67B-7F1956268577}.Release|Any CPU.Build.0 = Release|Any CPU
{65341396-A058-4577-9B70-C1DD3D146501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65341396-A058-4577-9B70-C1DD3D146501}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65341396-A058-4577-9B70-C1DD3D146501}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65341396-A058-4577-9B70-C1DD3D146501}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -147,6 +153,7 @@ Global
{1ACB9269-98AD-4E25-9DEA-F27150A38084} = {480BDA5A-FA96-4097-9E20-3841831413BB}
{FEF8E9D4-CBB7-4EFC-A5C2-2C9E91498D79} = {4A8F8EE9-769C-4C97-89BC-19D038E69998}
{AF1A0011-2795-42FD-B67B-7F1956268577} = {CAB76DA9-6E57-4422-98C6-DD2D6299F675}
{65341396-A058-4577-9B70-C1DD3D146501} = {56942891-CFBD-41E4-8881-47F455D7BEFD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
{1778FDCB-AC26-43E4-97FE-FC4F0C427672} = {23C68389-8F13-48E7-9878-440917F70DAF}
Expand Down
7 changes: 3 additions & 4 deletions src/client/LibplanetConsole.Clients.Examples/ExampleClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.Composition;
using Libplanet.Crypto;
using LibplanetConsole.Common;
using LibplanetConsole.Examples.Services;
using LibplanetConsole.Frameworks;
Expand All @@ -14,14 +13,14 @@ internal sealed class ExampleClient(
{
private readonly ExampleRemoteNodeService _remoteNodeService = remoteNodeService;

public Address Address => client.Address;
public AppAddress Address => client.Address;

public bool IsExample { get; }
= ApplicationSettingsParser.Peek<ExampleClientSettings>().IsExample;

private IExampleNodeService Server => _remoteNodeService.Service;

public void Subscribe() => Server.Subscribe(AddressUtility.ToString(Address));
public void Subscribe() => Server.Subscribe(Address);

public void Unsubscribe() => Server.Unsubscribe(AddressUtility.ToString(Address));
public void Unsubscribe() => Server.Unsubscribe(Address);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.Composition;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Services;
using LibplanetConsole.Examples.Services;

Expand All @@ -9,11 +10,11 @@ internal sealed class ExampleRemoteNodeService
: RemoteService<IExampleNodeService, IExampleNodeCallback>,
IExampleNodeCallback
{
public void OnSubscribed(string address)
public void OnSubscribed(AppAddress address)
{
}

public void OnUnsubscribed(string address)
public void OnUnsubscribed(AppAddress address)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ internal sealed record class ApplicationSettings

public static implicit operator ApplicationOptions(ApplicationSettings settings)
{
var endPoint = EndPointUtility.ParseWithFallback(settings.EndPoint);
var privateKey = PrivateKeyUtility.ParseWithFallback(settings.PrivateKey);
var endPoint = AppEndPoint.ParseOrNext(settings.EndPoint);
var privateKey = AppPrivateKey.ParseOrRandom(settings.PrivateKey);
return new ApplicationOptions(endPoint, privateKey)
{
ParentProcessId = settings.ParentProcessId,
IsSeed = settings.IsSeed,
NodeEndPoint = DnsEndPointUtility.GetSafeEndPoint(settings.NodeEndPoint),
NodeEndPoint = AppEndPoint.ParseOrDefault(settings.NodeEndPoint),
LogPath = GetFullPath(settings.LogPath),
NoREPL = settings.NoREPL,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private void Client_BlockAppended(object? sender, BlockEventArgs e)
{
var blockInfo = e.BlockInfo;
var hash = (ShortBlockHash)blockInfo.Hash;
var miner = (ShortAddress)blockInfo.Miner;
var message = $"Block #{blockInfo.Index} '{hash}' Appended by '{miner}'";
var miner = blockInfo.Miner;
var message = $"Block #{blockInfo.Index} '{hash}' Appended by '{miner:S}'";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}

Expand Down
9 changes: 4 additions & 5 deletions src/client/LibplanetConsole.Clients/ApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Net;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Clients.Services;
using LibplanetConsole.Common;
Expand Down Expand Up @@ -43,8 +42,8 @@ protected ApplicationBase(ApplicationOptions options)
_container.GetValue<IApplicationConfigurations>();
_info = new()
{
EndPoint = EndPointUtility.ToString(_clientServiceContext.EndPoint),
NodeEndPoint = EndPointUtility.ToSafeString(options.NodeEndPoint),
EndPoint = _clientServiceContext.EndPoint,
NodeEndPoint = options.NodeEndPoint,
LogPath = options.LogPath,
};
ApplicationServices = new(_container.GetExportedValues<IApplicationService>());
Expand All @@ -60,7 +59,7 @@ protected ApplicationBase(ApplicationOptions options)

public override ApplicationServiceCollection ApplicationServices { get; }

public EndPoint EndPoint => _clientServiceContext.EndPoint;
public AppEndPoint EndPoint => _clientServiceContext.EndPoint;

public ApplicationInfo Info => _info;

Expand Down Expand Up @@ -137,7 +136,7 @@ private static Logger CreateLogger(string logFilename)

private async Task AutoStartAsync(CancellationToken cancellationToken)
{
if (EndPointUtility.TryParse(_info.NodeEndPoint, out var nodeEndPoint) == true)
if (_info.NodeEndPoint is { } nodeEndPoint)
{
var isSeed = _isSeed;
_client.NodeEndPoint = await SeedUtility.GetNodeEndPointAsync(
Expand Down
11 changes: 5 additions & 6 deletions src/client/LibplanetConsole.Clients/ApplicationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using System.Net;
using Libplanet.Crypto;
using LibplanetConsole.Common;

namespace LibplanetConsole.Clients;

public sealed record class ApplicationOptions
{
public ApplicationOptions(EndPoint endPoint, PrivateKey privateKey)
public ApplicationOptions(AppEndPoint endPoint, AppPrivateKey privateKey)
{
EndPoint = endPoint;
PrivateKey = privateKey;
}

public EndPoint EndPoint { get; }
public AppEndPoint EndPoint { get; }

public PrivateKey PrivateKey { get; }
public AppPrivateKey PrivateKey { get; }

public int ParentProcessId { get; init; }

public bool IsSeed { get; init; }

public EndPoint? NodeEndPoint { get; init; }
public AppEndPoint? NodeEndPoint { get; init; }

public string LogPath { get; init; } = string.Empty;

Expand Down
20 changes: 9 additions & 11 deletions src/client/LibplanetConsole.Clients/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using System.Security;
using Libplanet.Action;
using Libplanet.Crypto;
Expand All @@ -18,7 +17,7 @@ internal sealed class Client : IClient, INodeCallback
private readonly ApplicationBase _application;
private readonly SecureString _privateKey;
private readonly ILogger _logger;
private EndPoint? _nodeEndPoint;
private AppEndPoint? _nodeEndPoint;
private RemoteNodeContext? _remoteNodeContext;
private Guid _closeToken;
private ClientInfo _info;
Expand All @@ -29,8 +28,8 @@ public Client(ApplicationBase application, ApplicationOptions options)
_logger.Debug("Client is creating...: {Address}", options.PrivateKey.Address);
_application = application;
_nodeEndPoint = options.NodeEndPoint;
_privateKey = PrivateKeyUtility.ToSecureString(options.PrivateKey);
_info = new() { Address = options.PrivateKey.PublicKey.Address };
_privateKey = options.PrivateKey.ToSecureString();
_info = new() { Address = options.PrivateKey.Address };
PublicKey = options.PrivateKey.PublicKey;
_logger.Debug("Client is created: {Address}", Address);
}
Expand All @@ -41,17 +40,17 @@ public Client(ApplicationBase application, ApplicationOptions options)

public event EventHandler<StopEventArgs>? Stopped;

public PublicKey PublicKey { get; }
public AppPublicKey PublicKey { get; }

public Address Address => PublicKey.Address;
public AppAddress Address => PublicKey.Address;

public TextWriter Out { get; set; } = Console.Out;

public ClientInfo Info => _info;

public NodeInfo NodeInfo { get; private set; }

public EndPoint NodeEndPoint
public AppEndPoint NodeEndPoint
{
get => _nodeEndPoint ??
throw new InvalidOperationException($"{nameof(NodeEndPoint)} is not initialized.");
Expand All @@ -72,8 +71,7 @@ public EndPoint NodeEndPoint

public override string ToString() => $"[{Address}]";

public bool Verify(object obj, byte[] signature)
=> PublicKeyUtility.Verify(PublicKey, obj, signature);
public bool Verify(object obj, byte[] signature) => PublicKey.Verify(obj, signature);

public async Task StartAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -113,13 +111,13 @@ public async Task StopAsync(CancellationToken cancellationToken)
public async Task<TxId> SendTransactionAsync(
IAction[] actions, CancellationToken cancellationToken)
{
var privateKey = PrivateKeyUtility.FromSecureString(_privateKey);
var privateKey = AppPrivateKey.FromSecureString(_privateKey);
var address = privateKey.Address;
var nonce = await RemoteNodeService.GetNextNonceAsync(address, cancellationToken);
var genesisHash = NodeInfo.GenesisHash;
var tx = Transaction.Create(
nonce: nonce,
privateKey: privateKey,
privateKey: (PrivateKey)privateKey,
genesisHash: genesisHash,
actions: [.. actions.Select(item => item.PlainValue)]);
_logger.Debug("Client sends a transaction: {TxId}", tx.Id);
Expand Down
4 changes: 1 addition & 3 deletions src/client/LibplanetConsole.Clients/Commands/StartCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ protected override async Task OnExecuteAsync(
CancellationToken cancellationToken, IProgress<ProgressInfo> progress)
{
var isSeed = IsSeed;
var nodeEndPoint = NodeEndPoint != string.Empty
? EndPointUtility.Parse(NodeEndPoint)
: client.NodeEndPoint;
var nodeEndPoint = AppEndPoint.ParseOrFallback(NodeEndPoint, client.NodeEndPoint);
client.NodeEndPoint = await SeedUtility.GetNodeEndPointAsync(
nodeEndPoint, isSeed, cancellationToken);
await client.StartAsync(cancellationToken);
Expand Down
3 changes: 1 addition & 2 deletions src/client/LibplanetConsole.Clients/Commands/TxCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.Composition;
using JSSoft.Commands;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Actions;

namespace LibplanetConsole.Clients.Commands;
Expand All @@ -21,6 +20,6 @@ protected override async Task OnExecuteAsync(
Value = Text,
};
await client.SendTransactionAsync([action], cancellationToken);
await Out.WriteLineAsync($"{(ShortAddress)client.Address}: {Text}");
await Out.WriteLineAsync($"{client.Address:S}: {Text}");
}
}
8 changes: 3 additions & 5 deletions src/client/LibplanetConsole.Clients/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Net;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Common;
Expand All @@ -22,11 +20,11 @@ public interface IClient : IVerifier

bool IsRunning { get; }

PublicKey PublicKey { get; }
AppPublicKey PublicKey { get; }

Address Address { get; }
AppAddress Address { get; }

EndPoint NodeEndPoint { get; set; }
AppEndPoint NodeEndPoint { get; set; }

Task StartAsync(CancellationToken cancellationToken);

Expand Down
11 changes: 5 additions & 6 deletions src/client/LibplanetConsole.Clients/SeedUtility.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Net;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Services;

namespace LibplanetConsole.Clients;

internal static class SeedUtility
{
public static async Task<EndPoint> GetNodeEndPointAsync(
EndPoint seedEndPoint, CancellationToken cancellationToken)
public static async Task<AppEndPoint> GetNodeEndPointAsync(
AppEndPoint seedEndPoint, CancellationToken cancellationToken)
{
var remoteService = new RemoteService<ISeedService>();
var remoteServiceContext = new RemoteServiceContext([remoteService])
Expand All @@ -17,11 +16,11 @@ public static async Task<EndPoint> GetNodeEndPointAsync(
var closeToken = await remoteServiceContext.OpenAsync(cancellationToken);
var nodeEndPoint = await remoteService.Service.GetNodeEndPointAsync(cancellationToken);
await remoteServiceContext.CloseAsync(closeToken, cancellationToken);
return EndPointUtility.Parse(nodeEndPoint);
return nodeEndPoint;
}

public static async Task<EndPoint> GetNodeEndPointAsync(
EndPoint nodEndPoint, bool isSeed, CancellationToken cancellationToken)
public static async Task<AppEndPoint> GetNodeEndPointAsync(
AppEndPoint nodEndPoint, bool isSeed, CancellationToken cancellationToken)
{
if (isSeed == true)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using LibplanetConsole.Common;

namespace LibplanetConsole.Clients.Serializations;

public readonly record struct ApplicationInfo
{
public required string EndPoint { get; init; }
public required AppEndPoint EndPoint { get; init; }

public required string NodeEndPoint { get; init; }
public AppEndPoint? NodeEndPoint { get; init; }

public required string LogPath { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<ClientInfo> GetInfoAsync(CancellationToken cancellationToken)
public async Task<ClientInfo> StartAsync(
string nodeEndPoint, CancellationToken cancellationToken)
{
_client.NodeEndPoint = EndPointUtility.Parse(nodeEndPoint);
_client.NodeEndPoint = AppEndPoint.Parse(nodeEndPoint);
await _client.StartAsync(cancellationToken);
return _client.Info;
}
Expand Down
30 changes: 0 additions & 30 deletions src/common/LibplanetConsole.Common/AddressUtility.cs

This file was deleted.

Loading
Loading