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

Hide Libplanet dependencies #49

Merged
merged 3 commits into from
Jul 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public ValueTask DisposeAsync()
private void Client_BlockAppended(object? sender, BlockEventArgs e)
{
var blockInfo = e.BlockInfo;
var hash = (ShortBlockHash)blockInfo.Hash;
var hash = blockInfo.Hash;
var miner = blockInfo.Miner;
var message = $"Block #{blockInfo.Index} '{hash}' Appended by '{miner:S}'";
var message = $"Block #{blockInfo.Index} '{hash:S}' Appended by '{miner:S}'";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}

Expand Down
7 changes: 4 additions & 3 deletions src/client/LibplanetConsole.Clients/Client.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Clients.Services;
Expand Down Expand Up @@ -108,7 +109,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
Stopped?.Invoke(this, new(StopReason.None));
}

public async Task<TxId> SendTransactionAsync(
public async Task<AppId> SendTransactionAsync(
IAction[] actions, CancellationToken cancellationToken)
{
var privateKey = AppPrivateKey.FromSecureString(_privateKey);
Expand All @@ -118,9 +119,9 @@ public async Task<TxId> SendTransactionAsync(
var tx = Transaction.Create(
nonce: nonce,
privateKey: (PrivateKey)privateKey,
genesisHash: genesisHash,
genesisHash: (BlockHash)genesisHash,
actions: [.. actions.Select(item => item.PlainValue)]);
_logger.Debug("Client sends a transaction: {TxId}", tx.Id);
_logger.Debug("Client sends a transaction: {AppId}", tx.Id);
return await RemoteNodeService.SendTransactionAsync(tx.Serialize(), cancellationToken);
}

Expand Down
3 changes: 1 addition & 2 deletions src/client/LibplanetConsole.Clients/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Libplanet.Action;
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Common;
using LibplanetConsole.Nodes.Serializations;
Expand Down Expand Up @@ -30,5 +29,5 @@ public interface IClient : IVerifier

Task StopAsync(CancellationToken cancellationToken);

Task<TxId> SendTransactionAsync(IAction[] actions, CancellationToken cancellationToken);
Task<AppId> SendTransactionAsync(IAction[] actions, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.Composition;
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Actions;
Expand Down Expand Up @@ -38,7 +37,7 @@ public async Task<ClientInfo> StartAsync(
public Task StopAsync(CancellationToken cancellationToken)
=> _client.StopAsync(cancellationToken);

public async Task<TxId> SendTransactionAsync(
public async Task<AppId> SendTransactionAsync(
TransactionOptions transactionOptions, CancellationToken cancellationToken)
{
if (transactionOptions.TryVerify(_client) == true)
Expand Down
64 changes: 64 additions & 0 deletions src/common/LibplanetConsole.Common/AppHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Libplanet.Common;
using Libplanet.Types.Blocks;
using LibplanetConsole.Common.Converters;

namespace LibplanetConsole.Common;

[JsonConverter(typeof(AppHashJsonConverter))]
public readonly record struct AppHash : IFormattable
{
private readonly ImmutableArray<byte> _bytes;

public AppHash(byte[] bytes)
{
_bytes = [.. bytes];
}

public AppHash(ImmutableArray<byte> bytes)
{
_bytes = bytes;
}

public AppHash(BlockHash blockHash) => _bytes = blockHash.ByteArray;

public static explicit operator AppHash(BlockHash blockHash) => new(blockHash);

public static explicit operator BlockHash(AppHash id) => new(id._bytes);

public static string ToString(AppHash? blockHash)
=> blockHash?.ToString() ?? string.Empty;

public static AppHash Parse(string text) => new(ByteUtil.ParseHexToImmutable(text));

public static AppHash? ParseOrDefault(string text)
=> text == string.Empty ? null : Parse(text);

public static bool TryParse(string text, [MaybeNullWhen(false)] out AppHash blockHash)
{
try
{
blockHash = Parse(text);
return true;
}
catch
{
blockHash = default;
return false;
}
}

public override string ToString() => ByteUtil.Hex(_bytes);

public string ToString(string? format, IFormatProvider? formatProvider)
{
if (format is "S")
{
return ToString()[..8];
}

return ToString();
}
}
64 changes: 64 additions & 0 deletions src/common/LibplanetConsole.Common/AppId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Libplanet.Common;
using Libplanet.Types.Tx;
using LibplanetConsole.Common.Converters;

namespace LibplanetConsole.Common;

[JsonConverter(typeof(AppIdJsonConverter))]
public readonly record struct AppId : IFormattable
{
private readonly ImmutableArray<byte> _bytes;

public AppId(byte[] bytes)
{
_bytes = [.. bytes];
}

public AppId(ImmutableArray<byte> bytes)
{
_bytes = bytes;
}

public AppId(TxId txId) => _bytes = txId.ByteArray;

public static explicit operator AppId(TxId txId) => new(txId);

public static explicit operator TxId(AppId id) => new(id._bytes);

public static string ToString(AppId? blockHash)
=> blockHash?.ToString() ?? string.Empty;

public static AppId Parse(string text) => new(ByteUtil.ParseHexToImmutable(text));

public static AppId? ParseOrDefault(string text)
=> text == string.Empty ? null : Parse(text);

public static bool TryParse(string text, [MaybeNullWhen(false)] out AppId blockHash)
{
try
{
blockHash = Parse(text);
return true;
}
catch
{
blockHash = default;
return false;
}
}

public override string ToString() => ByteUtil.Hex(_bytes);

public string ToString(string? format, IFormatProvider? formatProvider)
{
if (format is "S")
{
return ToString()[..8];
}

return ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LibplanetConsole.Common.Converters;

public sealed class AppHashJsonConverter : JsonConverter<AppHash>
{
public override AppHash Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.GetString() is string text)
{
return AppHash.Parse(text);
}

throw new JsonException("Cannot read AppHash from JSON.");
}

public override void Write(
Utf8JsonWriter writer, AppHash value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LibplanetConsole.Common.Converters;

public sealed class AppIdJsonConverter : JsonConverter<AppId>
{
public override AppId Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.GetString() is string text)
{
return AppId.Parse(text);
}

throw new JsonException("Cannot read AppId from JSON.");
}

public override void Write(
Utf8JsonWriter writer, AppId value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
16 changes: 0 additions & 16 deletions src/common/LibplanetConsole.Common/ShortBlockHash.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using JSSoft.Terminals;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Frameworks;

Expand Down Expand Up @@ -104,9 +103,9 @@ private void Nodes_CollectionChanged(object? sender, NotifyCollectionChangedEven
private void Node_BlockAppended(object? sender, BlockEventArgs e)
{
var blockInfo = e.BlockInfo;
var hash = (ShortBlockHash)blockInfo.Hash;
var hash = blockInfo.Hash;
var miner = blockInfo.Miner;
var message = $"Block #{blockInfo.Index} '{hash}' Appended by '{miner:S}'";
var message = $"Block #{blockInfo.Index} '{hash:S}' Appended by '{miner:S}'";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightBlue);
}

Expand Down
3 changes: 1 addition & 2 deletions src/console/LibplanetConsole.Consoles/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Security;
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Clients.Services;
using LibplanetConsole.Common;
Expand Down Expand Up @@ -184,7 +183,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
Stopped?.Invoke(this, EventArgs.Empty);
}

public async Task<TxId> SendTransactionAsync(string text, CancellationToken cancellationToken)
public async Task<AppId> SendTransactionAsync(string text, CancellationToken cancellationToken)
{
var transactionOptions = new TransactionOptions
{
Expand Down
3 changes: 1 addition & 2 deletions src/console/LibplanetConsole.Consoles/IClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Libplanet.Types.Tx;
using LibplanetConsole.Clients.Serializations;
using LibplanetConsole.Common;

Expand Down Expand Up @@ -34,5 +33,5 @@ public interface IClient : IAddressable, IAsyncDisposable, IServiceProvider, ISi

Task StopAsync(CancellationToken cancellationToken);

Task<TxId> SendTransactionAsync(string text, CancellationToken cancellationToken);
Task<AppId> SendTransactionAsync(string text, CancellationToken cancellationToken);
}
3 changes: 1 addition & 2 deletions src/console/LibplanetConsole.Consoles/INode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Libplanet.Action;
using Libplanet.Types.Tx;
using LibplanetConsole.Common;
using LibplanetConsole.Nodes.Serializations;

Expand Down Expand Up @@ -39,7 +38,7 @@ public interface INode : IAddressable, IAsyncDisposable, IServiceProvider, ISign

Task StopAsync(CancellationToken cancellationToken);

Task<TxId> SendTransactionAsync(IAction[] actions, CancellationToken cancellationToken);
Task<AppId> SendTransactionAsync(IAction[] actions, CancellationToken cancellationToken);

Task<long> GetNextNonceAsync(AppAddress address, CancellationToken cancellationToken);
}
7 changes: 4 additions & 3 deletions src/console/LibplanetConsole.Consoles/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Security;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Exceptions;
Expand Down Expand Up @@ -197,7 +198,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
public Task<long> GetNextNonceAsync(AppAddress address, CancellationToken cancellationToken)
=> _remoteService.Service.GetNextNonceAsync(address, cancellationToken);

public async Task<TxId> SendTransactionAsync(
public async Task<AppId> SendTransactionAsync(
IAction[] actions, CancellationToken cancellationToken)
{
var privateKey = AppPrivateKey.FromSecureString(_privateKey);
Expand All @@ -207,7 +208,7 @@ public async Task<TxId> SendTransactionAsync(
var tx = Transaction.Create(
nonce: nonce,
privateKey: (PrivateKey)privateKey,
genesisHash: genesisHash,
genesisHash: (BlockHash)genesisHash,
actions: [.. actions.Select(item => item.PlainValue)]);
var txId = await _remoteService.Service.SendTransactionAsync(
transaction: tx.Serialize(),
Expand All @@ -216,7 +217,7 @@ public async Task<TxId> SendTransactionAsync(
return txId;
}

public Task<TxId> SendTransactionAsync(
public Task<AppId> SendTransactionAsync(
Transaction transaction, CancellationToken cancellationToken)
{
return _remoteService.Service.SendTransactionAsync(
Expand Down
5 changes: 2 additions & 3 deletions src/node/LibplanetConsole.Nodes.Executable/NodeEventTracer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.Composition;
using JSSoft.Terminals;
using LibplanetConsole.Common;
using LibplanetConsole.Common.Extensions;
using LibplanetConsole.Frameworks;

Expand Down Expand Up @@ -30,9 +29,9 @@ public ValueTask DisposeAsync()
private void Node_BlockAppended(object? sender, BlockEventArgs e)
{
var blockInfo = e.BlockInfo;
var hash = (ShortBlockHash)blockInfo.Hash;
var hash = blockInfo.Hash;
var miner = blockInfo.Miner;
var message = $"Block #{blockInfo.Index} '{hash}' Appended by '{miner:S}'";
var message = $"Block #{blockInfo.Index} '{hash:S}' Appended by '{miner:S}'";
Console.Out.WriteColoredLine(message, TerminalColorType.BrightGreen);
}

Expand Down
Loading