From 713f551f44d3adfa5d885c4c8131f26d6038bcc8 Mon Sep 17 00:00:00 2001 From: s2quake Date: Sat, 11 Jan 2025 12:44:44 +0900 Subject: [PATCH] refactor: Improve code and add summary --- .../Commands/BankCommand.cs | 6 ++- .../Commands/BankCommand.cs | 6 ++- .../Commands/ClientBankCommand.cs | 12 +++-- .../Commands/NodeBankCommand.cs | 12 +++-- .../ClientCollection.cs | 2 +- .../Commands/ClientCommand.cs | 13 +++--- .../Commands/ClientCommandAsyncBase.cs | 16 +++---- .../Commands/ClientCommandBase.cs | 16 +++---- .../Commands/ClientCommandMethodBase.cs | 45 ++++++++++--------- .../Commands/NodeCommand.cs | 2 +- .../Commands/NodeCommandAsyncBase.cs | 21 ++++----- .../Commands/NodeCommandBase.cs | 21 ++++----- .../Commands/NodeCommandMethodBase.cs | 44 +++++++++--------- .../Commands/Nodes/StartNodeProcessCommand.cs | 2 +- .../Commands/Nodes/StopNodeProcessCommand.cs | 2 +- .../Extensions/INodeCollectionExtensions.cs | 10 +++++ .../LibplanetConsole.Console/IClient.cs | 2 + .../LibplanetConsole.Console/IConsole.cs | 2 + src/console/LibplanetConsole.Console/INode.cs | 2 + .../NodeCollection.cs | 2 +- .../Commands/BankCommand.cs | 6 ++- .../AddressCollectionBase.cs | 2 +- .../IAddressCollection.cs | 3 ++ 23 files changed, 140 insertions(+), 109 deletions(-) diff --git a/src/client/LibplanetConsole.Client.Bank/Commands/BankCommand.cs b/src/client/LibplanetConsole.Client.Bank/Commands/BankCommand.cs index 647089d8..8a1f6ef6 100644 --- a/src/client/LibplanetConsole.Client.Bank/Commands/BankCommand.cs +++ b/src/client/LibplanetConsole.Client.Bank/Commands/BankCommand.cs @@ -5,7 +5,7 @@ namespace LibplanetConsole.Client.Bank.Commands; -[CommandSummary("Bank Commands.")] +[CommandSummary("Provides bank-related commands for the node.")] [Category("Bank")] internal sealed class BankCommand( IClient client, @@ -20,6 +20,7 @@ internal sealed class BankCommand( public static Address Address { get; set; } [CommandMethod] + [CommandSummary("Transfers the specified amount to the recipient address.")] public async Task TransferAsync( [CommandParameterCompletion(nameof(GetAddresses))] Address recipientAddress, @@ -32,6 +33,8 @@ await bank.TransferAsync( } [CommandMethod] + [CommandSummary("Gets the balance of the specified currency for the client or " + + "specified address.")] [CommandMethodProperty(nameof(Address))] public async Task BalanceAsync( [CommandParameterCompletion(nameof(GetCurrencyCodes))] @@ -45,6 +48,7 @@ public async Task BalanceAsync( } [CommandMethod] + [CommandSummary("Displays information about a specific currency or lists all currency codes.")] public void Currency( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string code = "") diff --git a/src/console/LibplanetConsole.Console.Bank/Commands/BankCommand.cs b/src/console/LibplanetConsole.Console.Bank/Commands/BankCommand.cs index 7e6410cf..4a2d438c 100644 --- a/src/console/LibplanetConsole.Console.Bank/Commands/BankCommand.cs +++ b/src/console/LibplanetConsole.Console.Bank/Commands/BankCommand.cs @@ -5,7 +5,7 @@ namespace LibplanetConsole.Console.Bank.Commands; -[CommandSummary("Bank Commands.")] +[CommandSummary("Provides bank-related commands.")] [Category("Bank")] internal sealed class BankCommand( IConsole console, @@ -22,6 +22,7 @@ internal sealed class BankCommand( public static Address Address { get; set; } [CommandMethod] + [CommandSummary("Transfers the specified amount to the recipient address.")] public async Task TransferAsync( [CommandParameterCompletion(nameof(GetAddresses))] Address recipientAddress, @@ -34,6 +35,8 @@ await bank.TransferAsync( } [CommandMethod] + [CommandSummary("Gets the balance of the specified currency for the console or " + + "specified address.")] [CommandMethodProperty(nameof(Address))] public async Task BalanceAsync( [CommandParameterCompletion(nameof(GetCurrencyCodes))] @@ -47,6 +50,7 @@ public async Task BalanceAsync( } [CommandMethod] + [CommandSummary("Displays information about a specific currency or lists all currency codes.")] public void Currency( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string code = "") diff --git a/src/console/LibplanetConsole.Console.Bank/Commands/ClientBankCommand.cs b/src/console/LibplanetConsole.Console.Bank/Commands/ClientBankCommand.cs index 2cd590d9..3ecf0518 100644 --- a/src/console/LibplanetConsole.Console.Bank/Commands/ClientBankCommand.cs +++ b/src/console/LibplanetConsole.Console.Bank/Commands/ClientBankCommand.cs @@ -7,6 +7,8 @@ namespace LibplanetConsole.Console.Bank.Commands; +[CommandSummary("Provides bank-related commands for the client.")] +[Category("Bank")] internal sealed partial class ClientBankCommand( IServiceProvider serviceProvider, ClientCommand clientCommand, @@ -20,6 +22,7 @@ internal sealed partial class ClientBankCommand( public static Address Address { get; set; } [CommandMethod] + [CommandSummary("Transfers the specified amount to the recipient address.")] [CommandMethodProperty(nameof(ClientAddress))] public async Task TransferAsync( [CommandParameterCompletion(nameof(GetAddresses))] @@ -27,7 +30,7 @@ public async Task TransferAsync( [FungibleAssetValue] string amount, CancellationToken cancellationToken = default) { - var client = GetClientOrCurrent(ClientAddress); + var client = CurrentClient; var bank = client.GetRequiredKeyedService(IClient.Key); var amountValue = currencies.ToFungibleAssetValue(amount); await bank.TransferAsync( @@ -37,14 +40,14 @@ await bank.TransferAsync( [CommandMethod] [CommandMethodProperty(nameof(ClientAddress))] [CommandMethodProperty(nameof(Address))] - [CommandSummary("Display balance of specific address.")] - [Category("Bank")] + [CommandSummary("Gets the balance of the specified currency for the client or " + + "specified address.")] public async Task BalanceAsync( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string currencyCode, CancellationToken cancellationToken) { - var client = GetClientOrCurrent(ClientAddress); + var client = CurrentClient; var address = Address == default ? client.Address : Address; var bank = client.GetRequiredKeyedService(IClient.Key); var currency = currencies[currencyCode]; @@ -53,6 +56,7 @@ public async Task BalanceAsync( } [CommandMethod] + [CommandSummary("Displays information about a specific currency or lists all currency codes.")] public void Currency( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string code = "") diff --git a/src/console/LibplanetConsole.Console.Bank/Commands/NodeBankCommand.cs b/src/console/LibplanetConsole.Console.Bank/Commands/NodeBankCommand.cs index 4642d8eb..d96704a5 100644 --- a/src/console/LibplanetConsole.Console.Bank/Commands/NodeBankCommand.cs +++ b/src/console/LibplanetConsole.Console.Bank/Commands/NodeBankCommand.cs @@ -7,6 +7,8 @@ namespace LibplanetConsole.Console.Bank.Commands; +[CommandSummary("Provides bank-related commands for the node.")] +[Category("Bank")] internal sealed partial class NodeBankCommand( IServiceProvider serviceProvider, NodeCommand nodeCommand, @@ -21,13 +23,14 @@ internal sealed partial class NodeBankCommand( [CommandMethod] [CommandMethodProperty(nameof(NodeAddress))] + [CommandSummary("Transfers the specified amount to the recipient address.")] public async Task TransferAsync( [CommandParameterCompletion(nameof(GetAddresses))] Address recipientAddress, [FungibleAssetValue] string amount, CancellationToken cancellationToken = default) { - var node = GetNodeOrCurrent(NodeAddress); + var node = CurrentNode; var bank = node.GetRequiredKeyedService(INode.Key); var amountValue = currencies.ToFungibleAssetValue(amount); await bank.TransferAsync( @@ -37,14 +40,14 @@ await bank.TransferAsync( [CommandMethod] [CommandMethodProperty(nameof(NodeAddress))] [CommandMethodProperty(nameof(Address))] - [CommandSummary("Display balance of specific address.")] - [Category("Bank")] + [CommandSummary("Gets the balance of the specified currency for the node or " + + "specified address.")] public async Task BalanceAsync( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string currencyCode, CancellationToken cancellationToken) { - var node = GetNodeOrCurrent(NodeAddress); + var node = CurrentNode; var address = Address == default ? node.Address : Address; var bank = node.GetRequiredKeyedService(INode.Key); var currencyValue = currencies[currencyCode]; @@ -53,6 +56,7 @@ public async Task BalanceAsync( } [CommandMethod] + [CommandSummary("Displays information about a specific currency or lists all currency codes.")] public void Currency( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string code = "") diff --git a/src/console/LibplanetConsole.Console/ClientCollection.cs b/src/console/LibplanetConsole.Console/ClientCollection.cs index 04bb0e95..528189c1 100644 --- a/src/console/LibplanetConsole.Console/ClientCollection.cs +++ b/src/console/LibplanetConsole.Console/ClientCollection.cs @@ -213,7 +213,7 @@ private void InsertClient(Client client) var addresses = serviceProvider.GetRequiredService(); if (client.Alias != string.Empty) { - addresses.Add(client.Alias, client.Address, "client"); + addresses.Add(client.Alias, client.Address, IClient.Tag); } _clientList.Add(client); diff --git a/src/console/LibplanetConsole.Console/Commands/ClientCommand.cs b/src/console/LibplanetConsole.Console/Commands/ClientCommand.cs index f2406927..2e8ef16c 100644 --- a/src/console/LibplanetConsole.Console/Commands/ClientCommand.cs +++ b/src/console/LibplanetConsole.Console/Commands/ClientCommand.cs @@ -75,12 +75,13 @@ public async Task DetachAsync(CancellationToken cancellationToken = default) [CommandMethodProperty(nameof(ClientAddress))] [CommandSummary("Starts the client")] public async Task StartAsync( - string nodeAddress = "", CancellationToken cancellationToken = default) + [CommandParameterCompletion(nameof(GetNodeAddresses))] + Address nodeAddress = default, + CancellationToken cancellationToken = default) { var nodes = _serviceProvider.GetRequiredService(); - var clientAddress = addresses.ToAddress(ClientAddress); - var node = nodes.GetNodeOrCurrent(nodeAddress, addresses); - var client = clients.GetClientOrCurrent(clientAddress); + var node = nodes.GetNodeOrCurrent(nodeAddress); + var client = GetClientOrCurrent(ClientAddress); await client.StartAsync(node, cancellationToken); } @@ -98,7 +99,7 @@ public async Task StopAsync(CancellationToken cancellationToken) public void Current( [CommandSummary("The address of the client")] [CommandParameterCompletion(nameof(GetClientAddresses))] - string clientAddress = "") + Address clientAddress = default) { var client = GetClientOrDefault(clientAddress); if (client is not null) @@ -156,4 +157,6 @@ void IExecutable.Execute() return TerminalColorType.BrightBlack; } + + private string[] GetNodeAddresses() => GetAddresses(INode.Tag); } diff --git a/src/console/LibplanetConsole.Console/Commands/ClientCommandAsyncBase.cs b/src/console/LibplanetConsole.Console/Commands/ClientCommandAsyncBase.cs index 2bd243c7..970156a5 100644 --- a/src/console/LibplanetConsole.Console/Commands/ClientCommandAsyncBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/ClientCommandAsyncBase.cs @@ -33,29 +33,23 @@ protected ClientCommandAsyncBase( _serviceProvider = serviceProvider; } - [CommandProperty("client", 'C', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the client to use")] [CommandPropertyCompletion(nameof(GetClientAddresses))] - public string ClientAddress { get; set; } = string.Empty; + public Address ClientAddress { get; set; } protected IClient Client { get { var clients = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - var address = addresses.ToAddress(ClientAddress); - return clients.GetClientOrCurrent(address); + return clients.GetClientOrCurrent(ClientAddress); } } protected string[] GetClientAddresses() { - var clients = _serviceProvider.GetRequiredService(); - return - [ - .. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. clients.Select(client => $"{client.Address}"), - ]; + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(IClient.Tag); } } diff --git a/src/console/LibplanetConsole.Console/Commands/ClientCommandBase.cs b/src/console/LibplanetConsole.Console/Commands/ClientCommandBase.cs index 132a534f..a644dd94 100644 --- a/src/console/LibplanetConsole.Console/Commands/ClientCommandBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/ClientCommandBase.cs @@ -33,29 +33,23 @@ protected ClientCommandBase( _serviceProvider = serviceProvider; } - [CommandProperty("client", 'C', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the client to use")] [CommandPropertyCompletion(nameof(GetClientAddresses))] - public string ClientAddress { get; set; } = string.Empty; + public Address ClientAddress { get; set; } protected IClient Client { get { var clients = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - var address = addresses.ToAddress(ClientAddress); - return clients.GetClientOrCurrent(address); + return clients.GetClientOrCurrent(ClientAddress); } } protected string[] GetClientAddresses() { - var clients = _serviceProvider.GetRequiredService(); - return - [ - .. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. clients.Select(client => $"{client.Address}"), - ]; + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(IClient.Tag); } } diff --git a/src/console/LibplanetConsole.Console/Commands/ClientCommandMethodBase.cs b/src/console/LibplanetConsole.Console/Commands/ClientCommandMethodBase.cs index b87b63ba..20dcf31a 100644 --- a/src/console/LibplanetConsole.Console/Commands/ClientCommandMethodBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/ClientCommandMethodBase.cs @@ -33,40 +33,35 @@ protected ClientCommandMethodBase( _serviceProvider = serviceProvider; } - [CommandProperty("client", 'C', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the client to use")] [CommandPropertyCompletion(nameof(GetClientAddresses))] - public string ClientAddress { get; set; } = string.Empty; + public Address ClientAddress { get; set; } - protected IClient GetClientOrCurrent(string clientAddress) + public IClient CurrentClient + { + get + { + var clients = _serviceProvider.GetRequiredService(); + return clients.GetClientOrCurrent(ClientAddress); + } + } + + protected IClient GetClientOrCurrent(Address clientAddress) { var clients = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - var address = addresses.ToAddress(clientAddress); - return clients.GetClientOrCurrent(address); + return clients.GetClientOrCurrent(clientAddress); } - protected IClient? GetClientOrDefault(string clientAddress) + protected IClient? GetClientOrDefault(Address clientAddress) { - if (clientAddress == string.Empty) + if (clientAddress == default) { return default; } var clients = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - var address = addresses.ToAddress(clientAddress); - return clients[address]; - } - - protected string[] GetClientAddresses() - { - var clients = _serviceProvider.GetRequiredService(); - return - [ - .. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. clients.Select(client => $"{client.Address}"), - ]; + return clients[clientAddress]; } protected Address GetAddress(string address) @@ -79,4 +74,12 @@ protected Address GetAddress(string address) var addresses = _serviceProvider.GetRequiredService(); return addresses.ToAddress(address); } + + protected string[] GetClientAddresses() => GetAddresses(IClient.Tag); + + protected string[] GetAddresses(params string[] tags) + { + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(tags); + } } diff --git a/src/console/LibplanetConsole.Console/Commands/NodeCommand.cs b/src/console/LibplanetConsole.Console/Commands/NodeCommand.cs index 43314969..7e52319c 100644 --- a/src/console/LibplanetConsole.Console/Commands/NodeCommand.cs +++ b/src/console/LibplanetConsole.Console/Commands/NodeCommand.cs @@ -92,7 +92,7 @@ public async Task StopAsync(CancellationToken cancellationToken = default) public void Current( [CommandSummary("The address of the node")] [CommandParameterCompletion(nameof(GetNodeAddresses))] - string nodeAddress = "") + Address nodeAddress = default) { var node = GetNodeOrDefault(nodeAddress); if (node is not null) diff --git a/src/console/LibplanetConsole.Console/Commands/NodeCommandAsyncBase.cs b/src/console/LibplanetConsole.Console/Commands/NodeCommandAsyncBase.cs index 93e65a32..7efec616 100644 --- a/src/console/LibplanetConsole.Console/Commands/NodeCommandAsyncBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/NodeCommandAsyncBase.cs @@ -33,28 +33,25 @@ protected NodeCommandAsyncBase( _serviceProvider = serviceProvider; } - [CommandProperty("node", 'N', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the node to use")] [CommandPropertyCompletion(nameof(GetNodeAddresses))] - public string NodeAddress { get; set; } = string.Empty; + public Address NodeAddress { get; set; } - protected INode Node + protected INode CurrentNode { get { var nodes = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - return nodes.GetNodeOrCurrent(NodeAddress, addresses); + return nodes.GetNodeOrCurrent(NodeAddress); } } - protected string[] GetNodeAddresses() + protected string[] GetNodeAddresses() => GetAddresses(INode.Tag); + + protected string[] GetAddresses(params string[] tags) { - var nodes = _serviceProvider.GetRequiredService(); - return - [ - .. nodes.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. nodes.Select(node => $"{node.Address}"), - ]; + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(tags); } } diff --git a/src/console/LibplanetConsole.Console/Commands/NodeCommandBase.cs b/src/console/LibplanetConsole.Console/Commands/NodeCommandBase.cs index f52daa6a..bb0ecbc8 100644 --- a/src/console/LibplanetConsole.Console/Commands/NodeCommandBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/NodeCommandBase.cs @@ -33,28 +33,25 @@ protected NodeCommandBase( _serviceProvider = serviceProvider; } - [CommandProperty("node", 'N', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the node to use")] [CommandPropertyCompletion(nameof(GetNodeAddresses))] - public string NodeAddress { get; set; } = string.Empty; + public Address NodeAddress { get; set; } - protected INode Node + protected INode CurrentNode { get { var nodes = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - return nodes.GetNodeOrCurrent(NodeAddress, addresses); + return nodes.GetNodeOrCurrent(NodeAddress); } } - protected string[] GetNodeAddresses() + protected string[] GetNodeAddresses() => GetAddresses(INode.Tag); + + protected string[] GetAddresses(params string[] tags) { - var nodes = _serviceProvider.GetRequiredService(); - return - [ - .. nodes.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. nodes.Select(node => $"{node.Address}"), - ]; + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(tags); } } diff --git a/src/console/LibplanetConsole.Console/Commands/NodeCommandMethodBase.cs b/src/console/LibplanetConsole.Console/Commands/NodeCommandMethodBase.cs index e304e051..4e607e9b 100644 --- a/src/console/LibplanetConsole.Console/Commands/NodeCommandMethodBase.cs +++ b/src/console/LibplanetConsole.Console/Commands/NodeCommandMethodBase.cs @@ -33,39 +33,35 @@ protected NodeCommandMethodBase( _serviceProvider = serviceProvider; } - [CommandProperty("node", 'N', InitValue = "")] + [CommandProperty("current", 'C')] [CommandSummary("Specifies the address of the node to use")] [CommandPropertyCompletion(nameof(GetNodeAddresses))] - public string NodeAddress { get; set; } = string.Empty; + public Address NodeAddress { get; set; } - protected INode GetNodeOrCurrent(string nodeAddress) + protected INode CurrentNode + { + get + { + var nodes = _serviceProvider.GetRequiredService(); + return nodes.GetNodeOrCurrent(NodeAddress); + } + } + + protected INode GetNodeOrCurrent(Address nodeAddress) { var nodes = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - return nodes.GetNodeOrCurrent(nodeAddress, addresses); + return nodes.GetNodeOrCurrent(nodeAddress); } - protected INode? GetNodeOrDefault(string nodeAddress) + protected INode? GetNodeOrDefault(Address nodeAddress) { - if (nodeAddress == string.Empty) + if (nodeAddress == default) { return default; } var nodes = _serviceProvider.GetRequiredService(); - var addresses = _serviceProvider.GetRequiredService(); - var address = addresses.ToAddress(nodeAddress); - return nodes[address]; - } - - protected string[] GetNodeAddresses() - { - var nodes = _serviceProvider.GetRequiredService(); - return - [ - .. nodes.Where(item => item.Alias != string.Empty).Select(item => item.Alias), - .. nodes.Select(node => $"{node.Address}"), - ]; + return nodes[nodeAddress]; } protected Address GetAddress(string address) @@ -78,4 +74,12 @@ protected Address GetAddress(string address) var addresses = _serviceProvider.GetRequiredService(); return addresses.ToAddress(address); } + + protected string[] GetNodeAddresses() => GetAddresses(INode.Tag); + + protected string[] GetAddresses(params string[] tags) + { + var addresses = _serviceProvider.GetRequiredService(); + return addresses.GetAddresses(tags); + } } diff --git a/src/console/LibplanetConsole.Console/Commands/Nodes/StartNodeProcessCommand.cs b/src/console/LibplanetConsole.Console/Commands/Nodes/StartNodeProcessCommand.cs index 4c684895..a144da5f 100644 --- a/src/console/LibplanetConsole.Console/Commands/Nodes/StartNodeProcessCommand.cs +++ b/src/console/LibplanetConsole.Console/Commands/Nodes/StartNodeProcessCommand.cs @@ -19,7 +19,7 @@ internal sealed class StartNodeProcessCommand( protected override async Task OnExecuteAsync(CancellationToken cancellationToken) { - var node = Node; + var node = CurrentNode; var options = new ProcessOptions { Detach = Detach, diff --git a/src/console/LibplanetConsole.Console/Commands/Nodes/StopNodeProcessCommand.cs b/src/console/LibplanetConsole.Console/Commands/Nodes/StopNodeProcessCommand.cs index a82677b9..7918d872 100644 --- a/src/console/LibplanetConsole.Console/Commands/Nodes/StopNodeProcessCommand.cs +++ b/src/console/LibplanetConsole.Console/Commands/Nodes/StopNodeProcessCommand.cs @@ -10,7 +10,7 @@ internal sealed class StopNodeProcessCommand( { protected override async Task OnExecuteAsync(CancellationToken cancellationToken) { - var node = Node; + var node = CurrentNode; await node.StopProcessAsync(cancellationToken); } } diff --git a/src/console/LibplanetConsole.Console/Extensions/INodeCollectionExtensions.cs b/src/console/LibplanetConsole.Console/Extensions/INodeCollectionExtensions.cs index 334add9c..8f0f9f7b 100644 --- a/src/console/LibplanetConsole.Console/Extensions/INodeCollectionExtensions.cs +++ b/src/console/LibplanetConsole.Console/Extensions/INodeCollectionExtensions.cs @@ -12,6 +12,16 @@ public static INode GetNodeOrCurrent(this INodeCollection @this, string address) return @this[new Address(address)]; } + public static INode GetNodeOrCurrent(this INodeCollection @this, Address address) + { + if (address == default) + { + return @this.Current ?? throw new InvalidOperationException("No node is selected."); + } + + return @this[address]; + } + public static INode GetNodeOrCurrent( this INodeCollection @this, string address, IAddressCollection addresses) { diff --git a/src/console/LibplanetConsole.Console/IClient.cs b/src/console/LibplanetConsole.Console/IClient.cs index 5950ba17..75c87f84 100644 --- a/src/console/LibplanetConsole.Console/IClient.cs +++ b/src/console/LibplanetConsole.Console/IClient.cs @@ -7,6 +7,8 @@ public interface IClient : IAsyncDisposable, IKeyedServiceProvider, ISigner { const string Key = nameof(IClient); + const string Tag = "client"; + event EventHandler? Attached; event EventHandler? Detached; diff --git a/src/console/LibplanetConsole.Console/IConsole.cs b/src/console/LibplanetConsole.Console/IConsole.cs index 01b92114..d69a9eba 100644 --- a/src/console/LibplanetConsole.Console/IConsole.cs +++ b/src/console/LibplanetConsole.Console/IConsole.cs @@ -2,6 +2,8 @@ namespace LibplanetConsole.Console; public interface IConsole { + const string Tag = "console"; + Address Address { get; } Task SendTransactionAsync(IAction[] actions, CancellationToken cancellationToken); diff --git a/src/console/LibplanetConsole.Console/INode.cs b/src/console/LibplanetConsole.Console/INode.cs index 6bfcf34d..c04949d2 100644 --- a/src/console/LibplanetConsole.Console/INode.cs +++ b/src/console/LibplanetConsole.Console/INode.cs @@ -7,6 +7,8 @@ public interface INode : IAsyncDisposable, IKeyedServiceProvider, ISigner { const string Key = nameof(INode); + const string Tag = "node"; + event EventHandler? Attached; event EventHandler? Detached; diff --git a/src/console/LibplanetConsole.Console/NodeCollection.cs b/src/console/LibplanetConsole.Console/NodeCollection.cs index ce1ffdca..a1a20821 100644 --- a/src/console/LibplanetConsole.Console/NodeCollection.cs +++ b/src/console/LibplanetConsole.Console/NodeCollection.cs @@ -223,7 +223,7 @@ private void InsertNode(Node node) var addresses = serviceProvider.GetRequiredService(); if (node.Alias != string.Empty) { - addresses.Add(node.Alias, node.Address, "node"); + addresses.Add(node.Alias, node.Address, INode.Tag); } _nodeList.Add(node); diff --git a/src/node/LibplanetConsole.Node.Bank/Commands/BankCommand.cs b/src/node/LibplanetConsole.Node.Bank/Commands/BankCommand.cs index 711c6b60..9afe217c 100644 --- a/src/node/LibplanetConsole.Node.Bank/Commands/BankCommand.cs +++ b/src/node/LibplanetConsole.Node.Bank/Commands/BankCommand.cs @@ -5,7 +5,7 @@ namespace LibplanetConsole.Node.Bank.Commands; -[CommandSummary("Bank Commands.")] +[CommandSummary("Provides bank-related commands")] [Category("Bank")] internal sealed class BankCommand( INode node, @@ -20,6 +20,7 @@ internal sealed class BankCommand( public static Address Address { get; set; } [CommandMethod] + [CommandSummary("Transfers the specified amount to the recipient address.")] public async Task TransferAsync( [CommandParameterCompletion(nameof(GetAddresses))] Address recipientAddress, @@ -33,6 +34,8 @@ await bank.TransferAsync( [CommandMethod] [CommandMethodProperty(nameof(Address))] + [CommandSummary("Gets the balance of the specified currency for the node or " + + "specified address.")] public async Task BalanceAsync( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string currencyCode, @@ -45,6 +48,7 @@ public async Task BalanceAsync( } [CommandMethod] + [CommandSummary("Displays information about a specific currency or lists all currency codes.")] public void Currency( [CommandParameterCompletion(nameof(GetCurrencyCodes))] string code = "") diff --git a/src/shared/LibplanetConsole.Blockchain/AddressCollectionBase.cs b/src/shared/LibplanetConsole.Blockchain/AddressCollectionBase.cs index 2535d3a6..9aad3aa8 100644 --- a/src/shared/LibplanetConsole.Blockchain/AddressCollectionBase.cs +++ b/src/shared/LibplanetConsole.Blockchain/AddressCollectionBase.cs @@ -146,7 +146,7 @@ public AddressInfo[] GetAddressInfos(params string[] tags) } var query = from addressInfo in _addressInfoByAlias.Values.OfType() - where tags.Union(addressInfo.Tags).Any() is true + where tags.Intersect(addressInfo.Tags).Any() is true select addressInfo; return [.. query]; } diff --git a/src/shared/LibplanetConsole.Blockchain/IAddressCollection.cs b/src/shared/LibplanetConsole.Blockchain/IAddressCollection.cs index 94d32bc7..d3b8c5be 100644 --- a/src/shared/LibplanetConsole.Blockchain/IAddressCollection.cs +++ b/src/shared/LibplanetConsole.Blockchain/IAddressCollection.cs @@ -56,4 +56,7 @@ Address ToAddress(string text) paramName: nameof(text)); } } + + string[] GetAddresses(params string[] tags) + => GetAddressInfos(tags).Select(addressInfo => addressInfo.Address.ToString()).ToArray(); }