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

Remove AppPrivateKey, AppPublicKey, and App... classes #70

Merged
merged 9 commits into from
Sep 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
1 change: 1 addition & 0 deletions .submodules/lib9c
Submodule lib9c added at 178eaf
15 changes: 15 additions & 0 deletions GlobalUsings.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>

<ItemGroup>
<Using Include="Libplanet.Crypto" />
<Using Include="Libplanet.Common" />
<Using Include="Libplanet.Types.Blocks" />
<Using Include="Libplanet.Types.Tx" />
<Using Include="Libplanet.Action" />
<Using Include="Libplanet.Action.Loader" />
<Using Include="Bencodex" />
<Using Include="Bencodex.Types" />
<Using Include="System.Net" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/client/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
<DefineConstants>LIBPLANET_CLIENT</DefineConstants>
</PropertyGroup>

<Import Project="..\..\GlobalUsings.props" />

</Project>
3 changes: 1 addition & 2 deletions src/client/LibplanetConsole.Client.Example/ExampleClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.Composition;
using LibplanetConsole.Common;
using LibplanetConsole.Example.Services;

namespace LibplanetConsole.Client.Example;
Expand All @@ -15,7 +14,7 @@ internal sealed class ExampleClient(
{
private readonly ExampleRemoteNodeService _remoteNodeService = remoteNodeService;

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

public bool IsExample { get; } = settings.IsExample;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using System.ComponentModel.Composition;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Services;
using LibplanetConsole.Example.Services;

namespace LibplanetConsole.Client.Example;

[Export]
internal sealed class ExampleRemoteNodeService
: RemoteService<IExampleNodeService, IExampleNodeCallback>,
IExampleNodeCallback
: RemoteService<IExampleNodeService, IExampleNodeCallback>, IExampleNodeCallback
{
public void OnSubscribed(AppAddress address)
public void OnSubscribed(Address address)
{
}

public void OnUnsubscribed(AppAddress address)
public void OnUnsubscribed(Address address)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ internal sealed record class ApplicationSettings
[CommandProperty]
[CommandSummary("Indicates the EndPoint on which the client will run. " +
"If omitted, a random endpoint is used.")]
[AppEndPoint]
[EndPoint]
public string EndPoint { get; init; } = string.Empty;

[CommandProperty]
[CommandSummary("Indicates the private key of the client. " +
"If omitted, a random private key is used.")]
[AppPrivateKey]
[PrivateKey]
public string PrivateKey { get; init; } = string.Empty;

[CommandProperty("parent")]
Expand All @@ -31,7 +31,7 @@ internal sealed record class ApplicationSettings

[CommandProperty]
[CommandSummary("Indicates the EndPoint of the node to connect to.")]
[AppEndPoint]
[EndPoint]
public string NodeEndPoint { get; init; } = string.Empty;

[CommandProperty]
Expand All @@ -47,12 +47,12 @@ internal sealed record class ApplicationSettings

public ApplicationOptions ToOptions(object[] components)
{
var endPoint = AppEndPoint.ParseOrNext(EndPoint);
var privateKey = AppPrivateKey.ParseOrRandom(PrivateKey);
var endPoint = EndPointUtility.ParseOrNext(EndPoint);
var privateKey = PrivateKeyUtility.ParseOrRandom(PrivateKey);
return new ApplicationOptions(endPoint, privateKey)
{
ParentProcessId = ParentProcessId,
NodeEndPoint = AppEndPoint.ParseOrDefault(NodeEndPoint),
NodeEndPoint = EndPointUtility.ParseOrDefault(NodeEndPoint),
LogPath = GetFullPath(LogPath),
NoREPL = NoREPL,
Components = components,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public InitializeCommand()
[CommandProperty]
[CommandSummary("Indicates the private key of the client. " +
"If omitted, a random private key is used.")]
[AppPrivateKey]
[PrivateKey]
public string PrivateKey { get; init; } = string.Empty;

[CommandProperty]
[CommandSummary("The endpoint of the client. " +
"If omitted, a random endpoint is used.")]
[AppEndPoint]
[EndPoint]
public string EndPoint { get; set; } = string.Empty;

[CommandProperty]
Expand All @@ -45,8 +45,8 @@ public InitializeCommand()
protected override void OnExecute()
{
var outputPath = Path.GetFullPath(OutputPath);
var endPoint = AppEndPoint.ParseOrNext(EndPoint);
var privateKey = AppPrivateKey.ParseOrRandom(PrivateKey);
var endPoint = EndPointUtility.ParseOrNext(EndPoint);
var privateKey = PrivateKeyUtility.ParseOrRandom(PrivateKey);
var logPath = Path.Combine(outputPath, LogPath.Fallback("app.log"));
var repository = new Repository
{
Expand Down
18 changes: 9 additions & 9 deletions src/client/LibplanetConsole.Client.Executable/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public sealed record class Repository
public const string SettingsFileName = "client-settings.json";
public const string SettingsSchemaFileName = "client-settings-schema.json";

public required AppEndPoint EndPoint { get; init; }
public required EndPoint EndPoint { get; init; }

public required AppPrivateKey PrivateKey { get; init; }
public required PrivateKey PrivateKey { get; init; }

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

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

Expand All @@ -37,11 +37,11 @@ public static Repository Load(string settingsPath)

return new()
{
EndPoint = AppEndPoint.Parse(applicationSettings.EndPoint),
PrivateKey = AppPrivateKey.Parse(applicationSettings.PrivateKey),
EndPoint = EndPointUtility.Parse(applicationSettings.EndPoint),
PrivateKey = new PrivateKey(applicationSettings.PrivateKey),
LogPath = Path.GetFullPath(applicationSettings.LogPath, directoryName),
Source = settingsPath,
NodeEndPoint = AppEndPoint.ParseOrDefault(applicationSettings.NodeEndPoint),
NodeEndPoint = EndPointUtility.ParseOrDefault(applicationSettings.NodeEndPoint),
};
}

Expand Down Expand Up @@ -80,10 +80,10 @@ public dynamic Save(string repositoryPath)
Schema = SettingsSchemaFileName,
Application = new ApplicationSettings
{
EndPoint = EndPoint.ToString(),
PrivateKey = AppPrivateKey.ToString(privateKey),
EndPoint = EndPointUtility.ToString(EndPoint),
PrivateKey = PrivateKeyUtility.ToString(privateKey),
LogPath = GetRelativePathFromDirectory(repositoryPath, LogPath),
NodeEndPoint = AppEndPoint.ToString(NodeEndPoint),
NodeEndPoint = EndPointUtility.ToString(NodeEndPoint),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ private void BlockChain_BlockAppended(object? sender, BlockEventArgs e)
var blockInfo = e.BlockInfo;
var hash = blockInfo.Hash;
var miner = blockInfo.Miner;
var message = $"Block #{blockInfo.Height} '{hash:S}' Appended by '{miner:S}'";
var message = $"Block #{blockInfo.Height} '{hash.ToShortString()}' " +
$"Appended by '{miner.ToShortString()}'";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}
}
3 changes: 1 addition & 2 deletions src/client/LibplanetConsole.Client/ApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ComponentModel.Composition;
using System.Diagnostics;
using LibplanetConsole.Client.Services;
using LibplanetConsole.Common;
using LibplanetConsole.Framework;
using LibplanetConsole.Framework.Extensions;
using Serilog;
Expand Down Expand Up @@ -57,7 +56,7 @@ protected ApplicationBase(ApplicationOptions options)

public override ApplicationServiceCollection ApplicationServices { get; }

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

public ApplicationInfo Info => _info;

Expand Down
6 changes: 2 additions & 4 deletions src/client/LibplanetConsole.Client/ApplicationInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using LibplanetConsole.Common;

namespace LibplanetConsole.Client;

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

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

public required string LogPath { get; init; }
}
10 changes: 4 additions & 6 deletions src/client/LibplanetConsole.Client/ApplicationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using LibplanetConsole.Common;

namespace LibplanetConsole.Client;

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

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

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

public int ParentProcessId { get; init; }

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

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

Expand Down
37 changes: 16 additions & 21 deletions src/client/LibplanetConsole.Client/Client.BlockChain.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Bencodex;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using System.Security.Cryptography;
using LibplanetConsole.Common;
using LibplanetConsole.Node;

Expand All @@ -13,36 +8,36 @@ internal sealed partial class Client : IBlockChain
{
private static readonly Codec _codec = new();

public async Task<AppId> SendTransactionAsync(
public async Task<TxId> SendTransactionAsync(
IAction[] actions, CancellationToken cancellationToken)
{
var privateKey = AppPrivateKey.FromSecureString(_privateKey);
var privateKey = PrivateKeyUtility.FromSecureString(_privateKey);
var address = privateKey.Address;
var nonce = await RemoteBlockChainService.GetNextNonceAsync(address, cancellationToken);
var genesisHash = NodeInfo.GenesisHash;
var tx = Transaction.Create(
nonce: nonce,
privateKey: (PrivateKey)privateKey,
genesisHash: (BlockHash)genesisHash,
privateKey: privateKey,
genesisHash: genesisHash,
actions: [.. actions.Select(item => item.PlainValue)]);
var txData = tx.Serialize();
_logger.Debug("Client sends a transaction: {AppId}", tx.Id);
_logger.Debug("Client sends a transaction: {TxId}", tx.Id);
return await RemoteBlockChainService.SendTransactionAsync(txData, cancellationToken);
}

public Task<AppHash> GetBlockHashAsync(long height, CancellationToken cancellationToken)
public Task<BlockHash> GetBlockHashAsync(long height, CancellationToken cancellationToken)
=> RemoteBlockChainService.GetBlockHashAsync(height, cancellationToken);

public Task<long> GetNextNonceAsync(AppAddress address, CancellationToken cancellationToken)
public Task<long> GetNextNonceAsync(Address address, CancellationToken cancellationToken)
=> RemoteBlockChainService.GetNextNonceAsync(address, cancellationToken);

public Task<AppHash> GetTipHashAsync(CancellationToken cancellationToken)
public Task<BlockHash> GetTipHashAsync(CancellationToken cancellationToken)
=> RemoteBlockChainService.GetTipHashAsync(cancellationToken);

public async Task<IValue> GetStateAsync(
AppHash blockHash,
AppAddress accountAddress,
AppAddress address,
BlockHash? blockHash,
Address accountAddress,
Address address,
CancellationToken cancellationToken)
{
var value = await RemoteBlockChainService.GetStateAsync(
Expand All @@ -51,9 +46,9 @@ public async Task<IValue> GetStateAsync(
}

public async Task<IValue> GetStateByStateRootHashAsync(
AppHash stateRootHash,
AppAddress accountAddress,
AppAddress address,
HashDigest<SHA256> stateRootHash,
Address accountAddress,
Address address,
CancellationToken cancellationToken)
{
var value = await RemoteBlockChainService.GetStateByStateRootHashAsync(
Expand All @@ -62,7 +57,7 @@ public async Task<IValue> GetStateByStateRootHashAsync(
}

public async Task<T> GetActionAsync<T>(
AppId txId, int actionIndex, CancellationToken cancellationToken)
TxId txId, int actionIndex, CancellationToken cancellationToken)
where T : IAction
{
var bytes = await RemoteBlockChainService.GetActionAsync(
Expand Down
9 changes: 5 additions & 4 deletions src/client/LibplanetConsole.Client/Client.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security;
using LibplanetConsole.Client.Services;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Node;
using LibplanetConsole.Node.Services;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -13,7 +14,7 @@ internal sealed partial class Client : IClient, INodeCallback, IBlockChainCallba
private readonly ApplicationBase _application;
private readonly SecureString _privateKey;
private readonly ILogger _logger;
private AppEndPoint? _nodeEndPoint;
private EndPoint? _nodeEndPoint;
private RemoteNodeContext? _remoteNodeContext;
private Guid _closeToken;
private ClientInfo _info;
Expand All @@ -36,17 +37,17 @@ public Client(ApplicationBase application, ApplicationOptions options)

public event EventHandler<StopEventArgs>? Stopped;

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

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

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

public ClientInfo Info => _info;

public NodeInfo NodeInfo { get; private set; }

public AppEndPoint NodeEndPoint
public EndPoint NodeEndPoint
{
get => _nodeEndPoint ??
throw new InvalidOperationException($"{nameof(NodeEndPoint)} is not initialized.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class StartCommand(Client client) : CommandAsyncBase

protected override async Task OnExecuteAsync(CancellationToken cancellationToken)
{
var nodeEndPoint = AppEndPoint.ParseOrFallback(NodeEndPoint, client.NodeEndPoint);
var nodeEndPoint = EndPointUtility.ParseOrFallback(NodeEndPoint, client.NodeEndPoint);
client.NodeEndPoint = nodeEndPoint;
await client.StartAsync(cancellationToken);
}
Expand Down
3 changes: 2 additions & 1 deletion src/client/LibplanetConsole.Client/Commands/TxCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.Composition;
using JSSoft.Commands;
using LibplanetConsole.Common.Actions;
using LibplanetConsole.Common.Extensions;

namespace LibplanetConsole.Client.Commands;

Expand All @@ -19,6 +20,6 @@ protected override async Task OnExecuteAsync(CancellationToken cancellationToken
Value = Text,
};
await blockChain.SendTransactionAsync([action], cancellationToken);
await Out.WriteLineAsync($"{client.Address:S}: {Text}");
await Out.WriteLineAsync($"{client.Address.ToShortString()}: {Text}");
}
}
Loading
Loading