| | | 1 | | using MessagePack; |
| | | 2 | | using Microsoft.Extensions.Configuration; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using NBitcoin; |
| | | 7 | | using NLightning.Daemon.Contracts.Helpers; |
| | | 8 | | using NLightning.Daemon.Contracts.Utilities; |
| | | 9 | | using NLightning.Daemon.Extensions; |
| | | 10 | | using NLightning.Daemon.Utilities; |
| | | 11 | | using NLightning.Domain.Node.Options; |
| | | 12 | | using NLightning.Domain.Protocol.ValueObjects; |
| | | 13 | | using NLightning.Infrastructure.Bitcoin.Managers; |
| | | 14 | | using NLightning.Infrastructure.Bitcoin.Options; |
| | | 15 | | using NLightning.Infrastructure.Bitcoin.Wallet; |
| | | 16 | | using NLightning.Transport.Ipc.MessagePack; |
| | | 17 | | using Serilog; |
| | | 18 | | |
| | | 19 | | try |
| | | 20 | | { |
| | | 21 | | // Bootstrap logger for startup messages |
| | 0 | 22 | | Log.Logger = new LoggerConfiguration() |
| | 0 | 23 | | .WriteTo.Console() |
| | 0 | 24 | | .CreateBootstrapLogger(); |
| | | 25 | | |
| | 0 | 26 | | AppDomain.CurrentDomain.UnhandledException += (_, e) => |
| | 0 | 27 | | { |
| | 0 | 28 | | var exception = (Exception)e.ExceptionObject; |
| | 0 | 29 | | Log.Logger.Error("An unhandled exception occurred: {exception}", exception); |
| | 0 | 30 | | }; |
| | | 31 | | |
| | | 32 | | // Read the configuration file to check for daemon setting |
| | 0 | 33 | | var (initialConfig, network, configPath) = NodeConfigurationExtensions.ReadInitialConfiguration(args); |
| | | 34 | | |
| | | 35 | | // Get PID file path |
| | 0 | 36 | | var pidFilePath = DaemonUtils.GetPidFilePath(configPath); |
| | | 37 | | |
| | | 38 | | // Check for the stop command |
| | 0 | 39 | | if (DaemonUtils.IsStopRequested(args)) |
| | | 40 | | { |
| | 0 | 41 | | var stopped = DaemonUtils.StopDaemon(pidFilePath, Log.Logger); |
| | 0 | 42 | | return stopped ? 0 : 1; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // Check for status command |
| | 0 | 46 | | if (DaemonUtils.IsStatusRequested(args)) |
| | | 47 | | { |
| | 0 | 48 | | ReportDaemonStatus(pidFilePath); |
| | 0 | 49 | | return 0; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // Check if help is requested |
| | 0 | 53 | | if (CommandLineHelper.IsHelpRequested(args)) |
| | | 54 | | { |
| | 0 | 55 | | DaemonUtils.ShowUsage(); |
| | 0 | 56 | | return 0; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | string? password = null; |
| | | 60 | | |
| | | 61 | | // Try to get password from args or prompt |
| | 0 | 62 | | if (args.Contains("--password")) |
| | | 63 | | { |
| | 0 | 64 | | var idx = Array.IndexOf(args, "--password"); |
| | 0 | 65 | | if (idx >= 0 && idx + 1 < args.Length) |
| | 0 | 66 | | password = args[idx + 1]; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (string.IsNullOrWhiteSpace(password)) |
| | | 70 | | { |
| | 0 | 71 | | password = ConsoleUtils.ReadPassword("Enter password for key encryption: "); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | if (string.IsNullOrWhiteSpace(password)) |
| | | 75 | | { |
| | 0 | 76 | | Log.Error("Password cannot be empty."); |
| | 0 | 77 | | return 1; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | SecureKeyManager keyManager; |
| | 0 | 81 | | var keyFilePath = SecureKeyManager.GetKeyFilePath(configPath); |
| | 0 | 82 | | if (!File.Exists(keyFilePath)) |
| | | 83 | | { |
| | | 84 | | // Get current Block Height for key birth |
| | | 85 | | try |
| | | 86 | | { |
| | | 87 | | // Create the logger for the wallet service using Serilog |
| | 0 | 88 | | var loggerFactory = LoggerFactory.Create(b => b.AddSerilog(Log.Logger, dispose: false)); |
| | 0 | 89 | | var walletLogger = loggerFactory.CreateLogger<BitcoinChainService>(); |
| | | 90 | | |
| | | 91 | | // Bind options from initialConfig |
| | 0 | 92 | | var bitcoinOptions = initialConfig.GetSection("Bitcoin").Get<BitcoinOptions>() |
| | 0 | 93 | | ?? throw new InvalidOperationException( |
| | 0 | 94 | | "Bitcoin configuration section is missing or invalid."); |
| | 0 | 95 | | var nodeOptions = initialConfig.GetSection("Node").Get<NodeOptions>() |
| | 0 | 96 | | ?? throw new InvalidOperationException("Node configuration section is missing or invalid."); |
| | | 97 | | |
| | | 98 | | // Instantiate the service |
| | 0 | 99 | | var bitcoinChainService = new BitcoinChainService(Options.Create(bitcoinOptions), walletLogger, |
| | 0 | 100 | | Options.Create(nodeOptions) |
| | 0 | 101 | | ); |
| | | 102 | | |
| | 0 | 103 | | var heightOfBirth = await bitcoinChainService.GetCurrentBlockHeightAsync(); |
| | | 104 | | |
| | | 105 | | // Creates new key |
| | 0 | 106 | | var key = new Key(); |
| | 0 | 107 | | keyManager = new SecureKeyManager(key.ToBytes(), new BitcoinNetwork(network), keyFilePath, heightOfBirth); |
| | 0 | 108 | | keyManager.SaveToFile(password); |
| | 0 | 109 | | Console.WriteLine($"New key created and saved to {keyFilePath}"); |
| | 0 | 110 | | } |
| | 0 | 111 | | catch (Exception e) |
| | | 112 | | { |
| | 0 | 113 | | Log.Logger.Error(e, "An error occurred while creating new key."); |
| | 0 | 114 | | return 1; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | | 119 | | // Load the existing key |
| | 0 | 120 | | keyManager = SecureKeyManager.FromFilePath(keyFilePath, new BitcoinNetwork(network), password); |
| | 0 | 121 | | Console.WriteLine($"Loaded key from {keyFilePath}"); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Start as a daemon if requested |
| | 0 | 125 | | if (DaemonUtils.StartDaemonIfRequested(args, initialConfig, pidFilePath, Log.Logger)) |
| | | 126 | | { |
| | | 127 | | // The parent process exits immediately after starting the daemon |
| | 0 | 128 | | return 0; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | // Register the default formatter for MessagePackSerializer |
| | 0 | 132 | | MessagePackSerializer.DefaultOptions = NLightningMessagePackOptions.Options; |
| | | 133 | | |
| | 0 | 134 | | Log.Information("Starting NLTG..."); |
| | | 135 | | |
| | | 136 | | // Create and run host |
| | 0 | 137 | | var host = Host.CreateDefaultBuilder(args) |
| | 0 | 138 | | .ConfigureNltg(initialConfig) |
| | 0 | 139 | | .ConfigureNltgServices(keyManager, configPath) |
| | 0 | 140 | | .Build(); |
| | | 141 | | |
| | | 142 | | // Run migrations if configured |
| | 0 | 143 | | await host.MigrateDatabaseIfConfiguredAsync(); |
| | | 144 | | |
| | | 145 | | // Run the host |
| | 0 | 146 | | await host.RunAsync(); |
| | | 147 | | |
| | 0 | 148 | | return 0; |
| | | 149 | | } |
| | | 150 | | catch (Exception e) |
| | | 151 | | { |
| | 0 | 152 | | Log.Fatal(e, "Application terminated unexpectedly"); |
| | 0 | 153 | | return 1; |
| | | 154 | | } |
| | | 155 | | finally |
| | | 156 | | { |
| | 0 | 157 | | Log.CloseAndFlush(); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | static void ReportDaemonStatus(string pidFilePath) |
| | | 161 | | { |
| | | 162 | | try |
| | | 163 | | { |
| | 0 | 164 | | if (!File.Exists(pidFilePath)) |
| | | 165 | | { |
| | 0 | 166 | | Console.WriteLine("NLTG daemon is not running"); |
| | 0 | 167 | | return; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | var pidText = File.ReadAllText(pidFilePath).Trim(); |
| | 0 | 171 | | if (!int.TryParse(pidText, out var pid)) |
| | | 172 | | { |
| | 0 | 173 | | Console.WriteLine("Invalid PID in file, daemon may not be running"); |
| | 0 | 174 | | return; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | try |
| | | 178 | | { |
| | 0 | 179 | | var process = System.Diagnostics.Process.GetProcessById(pid); |
| | 0 | 180 | | var runTime = DateTime.Now - process.StartTime; |
| | | 181 | | |
| | 0 | 182 | | Console.WriteLine("NLTG daemon is running"); |
| | 0 | 183 | | Console.WriteLine($"PID: {pid}"); |
| | 0 | 184 | | Console.WriteLine($"Started: {process.StartTime}"); |
| | 0 | 185 | | Console.WriteLine($"Uptime: {runTime.Days}d {runTime.Hours}h {runTime.Minutes}m"); |
| | 0 | 186 | | Console.WriteLine($"Memory: {process.WorkingSet64 / (1024 * 1024)} MB"); |
| | 0 | 187 | | } |
| | 0 | 188 | | catch (ArgumentException) |
| | | 189 | | { |
| | 0 | 190 | | Console.WriteLine("NLTG daemon is not running (stale PID file)"); |
| | 0 | 191 | | } |
| | 0 | 192 | | } |
| | 0 | 193 | | catch (Exception e) |
| | | 194 | | { |
| | 0 | 195 | | Console.WriteLine($"Error checking daemon status: {e.Message}"); |
| | 0 | 196 | | } |
| | 0 | 197 | | } |