From d53597717505482e69265a7c8657909c5e76ea4d Mon Sep 17 00:00:00 2001 From: ThatBen Date: Thu, 3 Apr 2025 14:42:34 +0200 Subject: [PATCH] pluralizes balance checker --- Tools/AutoClient/Configuration.cs | 2 +- .../Modes/FolderStore/BalanceChecker.cs | 60 ++++++++++--------- .../AutoClient/Modes/FolderStore/JsonFile.cs | 47 ++++++++------- 3 files changed, 59 insertions(+), 50 deletions(-) diff --git a/Tools/AutoClient/Configuration.cs b/Tools/AutoClient/Configuration.cs index edb0c658..5b5da689 100644 --- a/Tools/AutoClient/Configuration.cs +++ b/Tools/AutoClient/Configuration.cs @@ -41,7 +41,7 @@ namespace AutoClient [Uniform("folderToStore", "fts", "FOLDERTOSTORE", false, "When set, autoclient will attempt to upload and purchase storage for every non-JSON file in the provided folder.")] public string FolderToStore { get; set; } = "/data/EthereumMainnetPreMergeEraFiles"; - [Uniform("ethAddressFile", "eaf", "ETHADDRESSFILE", false, "File with eth address used by codex node. Used for balance checking if geth/contracts information is provided.")] + [Uniform("ethAddressFile", "eaf", "ETHADDRESSFILE", false, "File(s) with eth address used by codex node. Used for balance checking if geth/contracts information is provided. Semi-colon separated.")] public string EthAddressFile { get; set; } = "/root/codex-testnet-starter/scripts/eth.address"; public string LogPath diff --git a/Tools/AutoClient/Modes/FolderStore/BalanceChecker.cs b/Tools/AutoClient/Modes/FolderStore/BalanceChecker.cs index 502d48cd..102aedbc 100644 --- a/Tools/AutoClient/Modes/FolderStore/BalanceChecker.cs +++ b/Tools/AutoClient/Modes/FolderStore/BalanceChecker.cs @@ -7,37 +7,42 @@ namespace AutoClient.Modes.FolderStore { private readonly LogPrefixer log; private readonly GethConnector.GethConnector? connector; - private readonly EthAddress? address; + private readonly EthAddress[] addresses; public BalanceChecker(App app) { log = new LogPrefixer(app.Log, "(Balance) "); connector = GethConnector.GethConnector.Initialize(app.Log); - address = LoadAddress(app); + addresses = LoadAddresses(app); } - private EthAddress? LoadAddress(App app) + private EthAddress[] LoadAddresses(App app) { try { - if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return null; - if (!File.Exists(app.Config.EthAddressFile)) return null; + if (string.IsNullOrEmpty(app.Config.EthAddressFile)) return Array.Empty(); + if (!File.Exists(app.Config.EthAddressFile)) return Array.Empty(); - return new EthAddress( - File.ReadAllText(app.Config.EthAddressFile) - .Trim() - .Replace("\n", "") - .Replace(Environment.NewLine, "") - ); + var tokens = app.Config.EthAddressFile.Split(";", StringSplitOptions.RemoveEmptyEntries); + return tokens.Select(ConvertToAddress).Where(a => a != null).ToArray(); } catch (Exception exc) { log.Error($"Failed to load eth address from file: {exc}"); - return null; + return Array.Empty(); } } + private EthAddress ConvertToAddress(string t) + { + return new EthAddress( + File.ReadAllText(t) + .Trim() + .Replace("\n", "") + .Replace(Environment.NewLine, "")); + } + public void Check() { if (connector == null) @@ -45,35 +50,32 @@ namespace AutoClient.Modes.FolderStore Log("Connector not configured. Can't check balances."); return; } - if (address == null) - { - Log("EthAddress not found. Can't check balances."); - return; - } - try + foreach (var address in addresses) { - PerformCheck(); - } - catch (Exception exc) - { - Log($"Exception while checking balances: {exc}"); + try + { + PerformCheck(address); + } + catch (Exception exc) + { + Log($"Exception while checking balances: {exc}"); + } } } - private void PerformCheck() + private void PerformCheck(EthAddress address) { var geth = connector!.GethNode; var contracts = connector!.CodexContracts; - var addr = address!; - var eth = geth.GetEthBalance(addr); - var tst = contracts.GetTestTokenBalance(addr); + var eth = geth.GetEthBalance(address); + var tst = contracts.GetTestTokenBalance(address); Log($"Balances: [{eth}] - [{tst}]"); - if (eth.Eth < 1) TryAddEth(geth, addr); - if (tst.Tst < 1) TryAddTst(contracts, addr); + if (eth.Eth < 1) TryAddEth(geth, address); + if (tst.Tst < 1) TryAddTst(contracts, address); } private void TryAddEth(GethPlugin.IGethNode geth, EthAddress addr) diff --git a/Tools/AutoClient/Modes/FolderStore/JsonFile.cs b/Tools/AutoClient/Modes/FolderStore/JsonFile.cs index 6d0f972d..365e77c3 100644 --- a/Tools/AutoClient/Modes/FolderStore/JsonFile.cs +++ b/Tools/AutoClient/Modes/FolderStore/JsonFile.cs @@ -6,6 +6,7 @@ namespace AutoClient.Modes.FolderStore { private readonly App app; private readonly string filePath; + private readonly object fileLock = new object(); public JsonFile(App app, string filePath) { @@ -15,35 +16,41 @@ namespace AutoClient.Modes.FolderStore public T Load() { - try + lock (fileLock) { - if (!File.Exists(filePath)) + try { - var state = new T(); - Save(state); - return state; + if (!File.Exists(filePath)) + { + var state = new T(); + Save(state); + return state; + } + var text = File.ReadAllText(filePath); + return JsonConvert.DeserializeObject(text)!; + } + catch (Exception exc) + { + app.Log.Error("Failed to load state: " + exc); + throw; } - var text = File.ReadAllText(filePath); - return JsonConvert.DeserializeObject(text)!; - } - catch (Exception exc) - { - app.Log.Error("Failed to load state: " + exc); - throw; } } public void Save(T state) { - try + lock (fileLock) { - var json = JsonConvert.SerializeObject(state, Formatting.Indented); - File.WriteAllText(filePath, json); - } - catch (Exception exc) - { - app.Log.Error("Failed to save state: " + exc); - throw; + try + { + var json = JsonConvert.SerializeObject(state, Formatting.Indented); + File.WriteAllText(filePath, json); + } + catch (Exception exc) + { + app.Log.Error("Failed to save state: " + exc); + throw; + } } } }