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

[new-cli] Part-1 - Base App #3372

Closed
Closed
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 neo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokensTracker", "src\Plugin
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcClient", "src\Plugins\RpcClient\RpcClient.csproj", "{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Hosting.App", "src\Neo.Hosting.App\Neo.Hosting.App.csproj", "{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -216,6 +218,10 @@ Global
{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0}.Release|Any CPU.Build.0 = Release|Any CPU
{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -255,6 +261,7 @@ Global
{FF76D8A4-356B-461A-8471-BC1B83E57BBC} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{5E4947F3-05D3-4806-B0F3-30DAC71B5986} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{185ADAFC-BFC6-413D-BC2E-97F9FB0A8AF0} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{C2095AD3-2ACA-46F3-89EC-BE52F98DA59B} = {B5339DF7-5D1D-43BA-B332-74B825E1770E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCBA19D9-F868-4C6D-8061-A2B91E06E3EC}
Expand Down
25 changes: 25 additions & 0 deletions src/Neo.Extensions/AssemblyUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// AssemblyUtility.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System.Reflection;

namespace Neo.Extensions
{
public static class AssemblyUtility
{
public static int GetVersionNumber()
{
var version = Assembly.GetCallingAssembly().GetName().Version;
if (version is null) return 0;
return version.Major * 1000 + version.Minor * 100 + version.Build * 10 + version.Revision;
}
}
}
40 changes: 40 additions & 0 deletions src/Neo.Hosting.App/CommandLine/DefaultRootCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// DefaultRootCommand.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;
using System.CommandLine;
using System.IO;

namespace Neo.Hosting.App.CommandLine
{
internal sealed class DefaultRootCommand : Command
{
private static string? s_executablePath;
private static string? s_executableName;

public DefaultRootCommand() : base(ExecutableName, "NEO Blockchain CommandLine Tool")
{
//var exportCommand = new ExportCommand();
//var runCommand = new RunCommand();
//var connectCommand = new ConnectCommand();

//AddCommand(exportCommand);
//AddCommand(runCommand);
//AddCommand(connectCommand);
}

public static string ExecutableName =>
s_executableName ??= Path.GetFileNameWithoutExtension(ExecutablePath).Replace(" ", "");

public static string ExecutablePath =>
s_executablePath ??= Environment.GetCommandLineArgs()[0];
}
}
23 changes: 23 additions & 0 deletions src/Neo.Hosting.App/Configuration/NeoConfigurationSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// NeoConfigurationSource.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.Extensions.Configuration;
using Neo.Hosting.App.Providers;

namespace Neo.Hosting.App.Configuration
{
internal class NeoConfigurationSource
(IConfigurationSection? configurationSection = null) : IConfigurationSource
{
public IConfigurationProvider Build(IConfigurationBuilder builder) =>
new NeoConfigurationProvider(configurationSection);
}
}
110 changes: 110 additions & 0 deletions src/Neo.Hosting.App/Configuration/NeoOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// NeoOptions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Hosting.App.Helpers;
using Neo.Hosting.App.Host;
using Neo.Network.P2P;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security;

namespace Neo.Hosting.App.Configuration
{
internal sealed class NeoOptions
{
public StorageOptions Storage { get; set; } = new();
public P2POptions P2P { get; set; } = new();
public ContractOptions Contract { get; set; } = new("0x50ac1c37690cc2cfc594472833cf57505d5f46de");
public PluginOptions Plugin { get; set; } = new();
public NamedPipeOptions NamedPipe { get; set; } = new();
public List<WalletOptions> Wallets { get; set; } = [];
}

internal sealed class StorageOptions
{
public class ArchiveSettings
{
public string Path { get; set; } = AppContext.BaseDirectory;
public string FileName { get; set; } = "chain.0.acc";
}

public string Engine { get; set; } = NeoDefaults.StoreProviderName;
public string Path { get; set; } = "Data_LevelDB_{0:X2}";
public bool Verify { get; set; } = true;
public ArchiveSettings Archive { get; set; } = new();
}

internal sealed class P2POptions
{
public string Listen { get; set; } = "0.0.0.0";
public ushort Port { get; set; } = 10333;
public int MinDesiredConnections { get; set; } = Peer.DefaultMinDesiredConnections;
public int MaxConnections { get; set; } = Peer.DefaultMaxConnections;
public int MaxConnectionsPerAddress { get; set; } = 3;
}

internal sealed class ContractOptions
(string neoNameService)
{
private static readonly string s_defualtNameServiceString = "0x50ac1c37690cc2cfc594472833cf57505d5f46de";
private static readonly UInt160 s_defaultNameServiceScriptHash = UInt160.Parse(s_defualtNameServiceString);

private UInt160 _neoNameService = s_defaultNameServiceScriptHash;

public UInt160 NeoNameService
{
get => _neoNameService;
set => _neoNameService = ParseUtility.TryParseUInt160(neoNameService) ?? s_defaultNameServiceScriptHash;
}
}

internal sealed class PluginOptions
{
public string DownloadUrl { get; set; } = "https://api.github.com/repos/neo-project/neo/releases";
public bool Prerelease { get; set; } = false;
public Version Version { get; set; } = new(0, 0);
}

internal sealed class NamedPipeOptions
{
public string Name { get; set; } = default!;
}

internal sealed class WalletOptions
(string name, string path, string password, bool isActive)
{
public string Name { get; set; } = name;
public FileInfo Path { get; set; } = new(path);
public bool IsActive { get; set; } = isActive;

public required SecureString Password
{
get => _encryptedPassword;
set
{
var passwordOptionValue = password;

unsafe
{
fixed (char* passwordChars = passwordOptionValue)
{
var securePasswordString = new SecureString(passwordChars, passwordOptionValue.Length);
securePasswordString.MakeReadOnly();
_encryptedPassword = value = securePasswordString;
}
}
}
}

private SecureString _encryptedPassword = new();
}
}
61 changes: 61 additions & 0 deletions src/Neo.Hosting.App/Extensions/CommandLineBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// CommandLineBuilderExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.Linq;

namespace Neo.Hosting.App.Extensions
{
internal static class CommandLineBuilderExtensions
{
private const string ConfigurationDirectiveName = "config";

internal static CommandLineBuilder UseInternalHost(this CommandLineBuilder builder,
Func<string[], IHostBuilder> hostBuilderFactory,
Action<IHostBuilder>? configureHost = null) =>
builder.AddMiddleware(async (invocation, next) =>
{
var argsRemaining = invocation.ParseResult.UnparsedTokens.ToArray();
var hostBuilder = hostBuilderFactory?.Invoke(argsRemaining)
?? new HostBuilder();

hostBuilder.Properties[typeof(InvocationContext)] = invocation;

hostBuilder.ConfigureHostConfiguration(config =>
{
config.AddCommandLineDirectives(invocation.ParseResult, ConfigurationDirectiveName);
});

hostBuilder.ConfigureServices(services =>
{
services.AddSingleton(invocation);
services.AddSingleton(invocation.BindingContext);
services.AddSingleton(invocation.Console);
services.AddTransient(_ => invocation.InvocationResult!);
services.AddTransient(_ => invocation.ParseResult);
});

hostBuilder.UseInvocationLifetime(invocation);
configureHost?.Invoke(hostBuilder);

using var host = hostBuilder.Build();

invocation.BindingContext.AddService(typeof(IHost), _ => host);

await next(invocation);
});
}
}
Loading
Loading