From 3a38a4c5be63ade74ebc09255fef8848b502b25e Mon Sep 17 00:00:00 2001 From: Amaury Hernandez-Aguila Date: Tue, 27 Dec 2022 15:35:33 -0800 Subject: [PATCH 1/2] Extracting duplicated code in BuildRandom*. --- Discreet/Coin/Block.cs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/Discreet/Coin/Block.cs b/Discreet/Coin/Block.cs index 7fffffb..10fbd8a 100644 --- a/Discreet/Coin/Block.cs +++ b/Discreet/Coin/Block.cs @@ -107,11 +107,11 @@ public uint Size() return size; } - /* for testing purposes only */ - public static Block BuildRandom(StealthAddress[] addresses, int[] numOutputs) + private static Block BuildTransactions( + List txs, + StealthAddress[] addresses, + int[] numOutputs) { - List txs = new(); - for (int i = 0; i < addresses.Length; i++) { for (int j = 0; j < numOutputs[i] / 16; j++) @@ -128,24 +128,19 @@ public static Block BuildRandom(StealthAddress[] addresses, int[] numOutputs) return Build(txs, null, default); } - public static Block BuildRandomPlus(StealthAddress[] addresses, int[] numOutputs, List txExtras) + /* for testing purposes only */ + public static Block BuildRandom(StealthAddress[] addresses, int[] numOutputs) { - List txs = txExtras; + List txs = new(); - for (int i = 0; i < addresses.Length; i++) - { - for (int j = 0; j < numOutputs[i] / 16; j++) - { - txs.Add(Transaction.GenerateRandomNoSpend(addresses[i], 16).ToFull()); - } + return BuildTransactions(txs, addresses, numOutputs); + } - if (numOutputs[i] % 16 != 0) - { - txs.Add(Transaction.GenerateRandomNoSpend(addresses[i], numOutputs[i] % 16).ToFull()); - } - } + public static Block BuildRandomPlus(StealthAddress[] addresses, int[] numOutputs, List txExtras) + { + List txs = txExtras; - return Build(txs, null, default); + return BuildTransactions(txs, addresses, numOutputs); } public static Block Build(List txs, StealthAddress miner, Key signingKey) From 672410f4e403e7c3ca2bc9fb8bb85296fc988213 Mon Sep 17 00:00:00 2001 From: Amaury Hernandez-Aguila Date: Tue, 27 Dec 2022 22:27:36 -0800 Subject: [PATCH 2/2] Refactored out asking for bootstrap node address. --- Discreet/Daemon/Daemon.cs | 43 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/Discreet/Daemon/Daemon.cs b/Discreet/Daemon/Daemon.cs index 5536f67..7c991f0 100644 --- a/Discreet/Daemon/Daemon.cs +++ b/Discreet/Daemon/Daemon.cs @@ -852,6 +852,30 @@ public void BuildGenesis() Logger.Info("Successfully created the genesis block."); } + /// + /// AskBootstrapNode repeatedly asks the user for a valid bootstrap node + /// address, until the user provides a valid address. + /// + /// An object holding the node's configuration. + /// A validated address. + private static string AskBootstrapNode(DaemonConfig config) + { + while (true) + { + try + { + Console.Write("Boostrap node: "); + IPAddress[] resolvedDNS = Dns.GetHostAddresses(Console.ReadLine()); + + return resolvedDNS[0].ToString(); + } + catch (Exception) + { + Logger.Error("Not a valid bootstrap node. Network might be temporarily down for maintenance."); + } + } + } + public static Daemon Init() { var config = DaemonConfig.GetConfig(); @@ -859,24 +883,7 @@ public static Daemon Init() if (config.BootstrapNode == null) { - - bool didResolve = false; - - while (!didResolve) - { - try - { - Console.Write("Boostrap node: "); - IPAddress[] resolvedDNS = Dns.GetHostAddresses(Console.ReadLine()); - - config.BootstrapNode = resolvedDNS[0].ToString(); - didResolve = true; - } - catch (Exception) - { - Logger.Error("Not a valid bootstrap node. Network might be temporarily down for maintenance."); - } - } + config.BootstrapNode = AskBootstrapNode(config); } DaemonConfig.SetConfig(config);