From c73fa186fc321928a571545d80725652de06ae84 Mon Sep 17 00:00:00 2001 From: ThatBen Date: Thu, 16 Jan 2025 13:24:57 +0100 Subject: [PATCH] wip --- .../ContainerCrashWatcher.cs | 3 +- Framework/Utils/CrashWatcher.cs | 12 +++--- ProjectPlugins/CodexClient/CodexAccess.cs | 31 +++++++------- ProjectPlugins/CodexClient/CodexNode.cs | 20 +++++----- .../CodexClient/CodexNodeFactory.cs | 40 +++++++++++++++++++ ProjectPlugins/CodexClient/ProcessControl.cs | 12 ++++-- .../ChainStateChangeHandlerMux.cs | 4 +- .../ChainMonitor/ChainStateRequest.cs | 2 +- .../DoNothingChainEventHandler.cs | 4 +- .../ContractInteractions.cs | 2 +- .../Marketplace/Customizations.cs | 2 +- .../CodexPlugin/CodexNodeFactory.cs | 35 ---------------- ProjectPlugins/CodexPlugin/CodexNodeGroup.cs | 5 ++- ProjectPlugins/CodexPlugin/CodexPlugin.cs | 3 +- ProjectPlugins/CodexPlugin/CodexSetup.cs | 5 ++- ProjectPlugins/MetricsPlugin/MetricsQuery.cs | 1 + 16 files changed, 96 insertions(+), 85 deletions(-) create mode 100644 ProjectPlugins/CodexClient/CodexNodeFactory.cs delete mode 100644 ProjectPlugins/CodexPlugin/CodexNodeFactory.cs diff --git a/Framework/KubernetesWorkflow/ContainerCrashWatcher.cs b/Framework/KubernetesWorkflow/ContainerCrashWatcher.cs index 1cb9cde8..2932b01f 100644 --- a/Framework/KubernetesWorkflow/ContainerCrashWatcher.cs +++ b/Framework/KubernetesWorkflow/ContainerCrashWatcher.cs @@ -1,10 +1,9 @@ using k8s; using Logging; -using Utils; namespace KubernetesWorkflow { - public class ContainerCrashWatcher : ICrashWatcher + public class ContainerCrashWatcher { private readonly ILog log; private readonly KubernetesClientConfiguration config; diff --git a/Framework/Utils/CrashWatcher.cs b/Framework/Utils/CrashWatcher.cs index 2be46ab9..9477bdf8 100644 --- a/Framework/Utils/CrashWatcher.cs +++ b/Framework/Utils/CrashWatcher.cs @@ -1,9 +1,9 @@ namespace Utils { - public interface ICrashWatcher - { - void Start(); - void Stop(); - bool HasCrashed(); - } + //public interface ICrashWatcher + //{ + // void Start(); + // void Stop(); + // bool HasCrashed(); + //} } diff --git a/ProjectPlugins/CodexClient/CodexAccess.cs b/ProjectPlugins/CodexClient/CodexAccess.cs index f700331d..434d7946 100644 --- a/ProjectPlugins/CodexClient/CodexAccess.cs +++ b/ProjectPlugins/CodexClient/CodexAccess.cs @@ -14,30 +14,24 @@ namespace CodexClient private ICodexInstance instance; private readonly Mapper mapper = new Mapper(); - public CodexAccess(ILog log, IHttpFactory httpFactory, IProcessControl processControl, ICodexInstance instance, ICrashWatcher crashWatcher) + public CodexAccess(ILog log, IHttpFactory httpFactory, IProcessControl processControl, ICodexInstance instance) { this.log = log; this.httpFactory = httpFactory; this.processControl = processControl; this.instance = instance; - CrashWatcher = crashWatcher; - - CrashWatcher.Start(); } - public ICrashWatcher CrashWatcher { get; } - public void Stop(bool waitTillStopped) { - CrashWatcher.Stop(); - processControl.Stop(instance, waitTillStopped); + processControl.Stop(waitTillStopped); // Prevents accidental use after stop: instance = null!; } public IDownloadedLog DownloadLog(string additionalName = "") { - return processControl.DownloadLog(instance, log.CreateSubfile(GetName() + additionalName)); + return processControl.DownloadLog(log.CreateSubfile(GetName() + additionalName)); } public string GetImageName() @@ -205,6 +199,11 @@ namespace CodexClient return instance.ListenEndpoint; } + public bool HasCrashed() + { + return processControl.HasCrashed(); + } + public Address? GetMetricsEndpoint() { return instance.MetricsEndpoint; @@ -217,18 +216,18 @@ namespace CodexClient public void DeleteDataDirFolder() { - processControl.DeleteDataDirFolder(instance); + processControl.DeleteDataDirFolder(); } private T OnCodex(Func> action) { - var result = httpFactory.CreateHttp(GetHttpId(), CheckContainerCrashed).OnClient(client => CallCodex(client, action)); + var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action)); return result; } private T OnCodex(Func> action, Retry retry) { - var result = httpFactory.CreateHttp(GetHttpId(), CheckContainerCrashed).OnClient(client => CallCodex(client, action), retry); + var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action), retry); return result; } @@ -248,14 +247,14 @@ namespace CodexClient } finally { - CrashWatcher.HasCrashed(); + CheckContainerCrashed(); } } private IEndpoint GetEndpoint() { return httpFactory - .CreateHttp(GetHttpId(), CheckContainerCrashed) + .CreateHttp(GetHttpId(), h => CheckContainerCrashed()) .CreateEndpoint(GetAddress(), "/api/codex/v1/", GetName()); } @@ -269,9 +268,9 @@ namespace CodexClient return GetAddress().ToString(); } - private void CheckContainerCrashed(HttpClient client) + private void CheckContainerCrashed() { - if (CrashWatcher.HasCrashed()) throw new Exception($"Container {GetName()} has crashed."); + if (processControl.HasCrashed()) throw new Exception($"Container {GetName()} has crashed."); } private void Throw(Failure failure) diff --git a/ProjectPlugins/CodexClient/CodexNode.cs b/ProjectPlugins/CodexClient/CodexNode.cs index 90f09557..45cfcd7b 100644 --- a/ProjectPlugins/CodexClient/CodexNode.cs +++ b/ProjectPlugins/CodexClient/CodexNode.cs @@ -34,6 +34,7 @@ namespace CodexClient Address GetDiscoveryEndpoint(); Address GetApiEndpoint(); Address GetListenEndpoint(); + Address GetMetricsScrapeTarget(); /// /// Warning! The node is not usable after this. @@ -85,16 +86,6 @@ namespace CodexClient public DebugInfoVersion Version { get; private set; } public ITransferSpeeds TransferSpeeds { get => transferSpeeds; } - public Address MetricsScrapeTarget - { - get - { - var address = codexAccess.GetMetricsEndpoint(); - if (address == null) throw new Exception("Metrics ScrapeTarget accessed, but node was not started with EnableMetrics()"); - return address; - } - } - public EthAddress EthAddress { get @@ -278,9 +269,16 @@ namespace CodexClient return codexAccess.GetListenEndpoint(); } + public Address GetMetricsScrapeTarget() + { + var address = codexAccess.GetMetricsEndpoint(); + if (address == null) throw new Exception("Metrics ScrapeTarget accessed, but node was not started with EnableMetrics()"); + return address; + } + public bool HasCrashed() { - return codexAccess.CrashWatcher.HasCrashed(); + return codexAccess.HasCrashed(); } public override string ToString() diff --git a/ProjectPlugins/CodexClient/CodexNodeFactory.cs b/ProjectPlugins/CodexClient/CodexNodeFactory.cs new file mode 100644 index 00000000..447ae018 --- /dev/null +++ b/ProjectPlugins/CodexClient/CodexNodeFactory.cs @@ -0,0 +1,40 @@ +using CodexClient.Hooks; +using FileUtils; +using Logging; +using WebUtils; + +namespace CodexClient +{ + public class CodexNodeFactory + { + private readonly ILog log; + private readonly IFileManager fileManager; + private readonly ICodexHooksProvider hooksProvider; + private readonly IHttpFactory httpFactory; + private readonly IIProcessControlFactory processControlFactory; + + public CodexNodeFactory(ILog log, IFileManager fileManager, ICodexHooksProvider hooksProvider, IHttpFactory httpFactory, IIProcessControlFactory processControlFactory) + { + this.log = log; + this.fileManager = fileManager; + this.hooksProvider = hooksProvider; + this.httpFactory = httpFactory; + this.processControlFactory = processControlFactory; + } + + public ICodexNode CreateCodexNode(ICodexInstance instance) + { + var processControl = processControlFactory.CreateProcessControl(instance); + var access = new CodexAccess(log, httpFactory, processControl, instance); + var hooks = hooksProvider.CreateHooks(access.GetName()); + var marketplaceAccess = CreateMarketplaceAccess(instance, access, hooks); + return new CodexNode(log, access, fileManager, marketplaceAccess, hooks); + } + + private IMarketplaceAccess CreateMarketplaceAccess(ICodexInstance instance, CodexAccess access, ICodexNodeHooks hooks) + { + if (instance.EthAccount == null) return new MarketplaceUnavailable(); + return new MarketplaceAccess(log, access, hooks); + } + } +} diff --git a/ProjectPlugins/CodexClient/ProcessControl.cs b/ProjectPlugins/CodexClient/ProcessControl.cs index 61dcb8f2..f431f58b 100644 --- a/ProjectPlugins/CodexClient/ProcessControl.cs +++ b/ProjectPlugins/CodexClient/ProcessControl.cs @@ -2,10 +2,16 @@ namespace CodexClient { + public interface IIProcessControlFactory + { + IProcessControl CreateProcessControl(ICodexInstance instance); + } + public interface IProcessControl { - void Stop(ICodexInstance instance, bool waitTillStopped); - IDownloadedLog DownloadLog(ICodexInstance instance, LogFile file); - void DeleteDataDirFolder(ICodexInstance instance); + void Stop(bool waitTillStopped); + IDownloadedLog DownloadLog(LogFile file); + void DeleteDataDirFolder(); + bool HasCrashed(); } } diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateChangeHandlerMux.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateChangeHandlerMux.cs index 799573f6..215eeeff 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateChangeHandlerMux.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateChangeHandlerMux.cs @@ -1,5 +1,5 @@ -using GethPlugin; -using System.Numerics; +using System.Numerics; +using Utils; namespace CodexContractsPlugin.ChainMonitor { diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateRequest.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateRequest.cs index d908d193..408d2746 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateRequest.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainStateRequest.cs @@ -1,6 +1,6 @@ using CodexContractsPlugin.Marketplace; -using GethPlugin; using Logging; +using Utils; namespace CodexContractsPlugin.ChainMonitor { diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs index 749f30c4..c15b0c7d 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs @@ -1,5 +1,5 @@ -using GethPlugin; -using System.Numerics; +using System.Numerics; +using Utils; namespace CodexContractsPlugin.ChainMonitor { diff --git a/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs b/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs index feecd054..289db637 100644 --- a/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs +++ b/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs @@ -5,8 +5,8 @@ using Logging; using Nethereum.ABI.FunctionEncoding.Attributes; using Nethereum.Contracts; using Nethereum.Hex.HexConvertors.Extensions; -using NethereumWorkflow; using System.Numerics; +using Utils; namespace CodexContractsPlugin { diff --git a/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs index b04bf59d..9a311909 100644 --- a/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs +++ b/ProjectPlugins/CodexContractsPlugin/Marketplace/Customizations.cs @@ -1,7 +1,7 @@ #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. using BlockchainUtils; -using GethPlugin; using Newtonsoft.Json; +using Utils; namespace CodexContractsPlugin.Marketplace { diff --git a/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs b/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs deleted file mode 100644 index 719b0282..00000000 --- a/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs +++ /dev/null @@ -1,35 +0,0 @@ -using CodexPlugin.Hooks; -using Core; - -namespace CodexPlugin -{ - public interface ICodexNodeFactory - { - CodexNode CreateOnlineCodexNode(CodexAccess access); - } - - public class CodexNodeFactory : ICodexNodeFactory - { - private readonly IPluginTools tools; - private readonly CodexHooksFactory codexHooksFactory; - - public CodexNodeFactory(IPluginTools tools, CodexHooksFactory codexHooksFactory) - { - this.tools = tools; - this.codexHooksFactory = codexHooksFactory; - } - - public CodexNode CreateOnlineCodexNode(CodexAccess access) - { - var hooks = codexHooksFactory.CreateHooks(access.GetName()); - var marketplaceAccess = GetMarketplaceAccess(access, hooks); - return new CodexNode(tools, access, marketplaceAccess, hooks); - } - - private IMarketplaceAccess GetMarketplaceAccess(CodexAccess codexAccess, ICodexNodeHooks hooks) - { - if (codexAccess.GetEthAccount() == null) return new MarketplaceUnavailable(); - return new MarketplaceAccess(tools.GetLog(), codexAccess, hooks); - } - } -} diff --git a/ProjectPlugins/CodexPlugin/CodexNodeGroup.cs b/ProjectPlugins/CodexPlugin/CodexNodeGroup.cs index be90581a..c178f459 100644 --- a/ProjectPlugins/CodexPlugin/CodexNodeGroup.cs +++ b/ProjectPlugins/CodexPlugin/CodexNodeGroup.cs @@ -1,4 +1,5 @@ -using Core; +using CodexClient; +using Core; using MetricsPlugin; using System.Collections; using Utils; @@ -41,7 +42,7 @@ namespace CodexPlugin public ICodexNode[] Nodes => nodes; public DebugInfoVersion Version { get; private set; } - public Address[] ScrapeTargets => Nodes.Select(n => n.MetricsScrapeTarget).ToArray(); + public Address[] ScrapeTargets => Nodes.Select(n => n.GetMetricsScrapeTarget()).ToArray(); public IEnumerator GetEnumerator() { diff --git a/ProjectPlugins/CodexPlugin/CodexPlugin.cs b/ProjectPlugins/CodexPlugin/CodexPlugin.cs index 04c2d5f5..d47da7c7 100644 --- a/ProjectPlugins/CodexPlugin/CodexPlugin.cs +++ b/ProjectPlugins/CodexPlugin/CodexPlugin.cs @@ -1,4 +1,5 @@ -using CodexPlugin.Hooks; +using CodexClient; +using CodexClient.Hooks; using Core; using KubernetesWorkflow.Types; diff --git a/ProjectPlugins/CodexPlugin/CodexSetup.cs b/ProjectPlugins/CodexPlugin/CodexSetup.cs index b8ef3bb4..d56c10db 100644 --- a/ProjectPlugins/CodexPlugin/CodexSetup.cs +++ b/ProjectPlugins/CodexPlugin/CodexSetup.cs @@ -1,4 +1,5 @@ -using CodexContractsPlugin; +using CodexClient; +using CodexContractsPlugin; using GethPlugin; using KubernetesWorkflow; using Utils; @@ -223,7 +224,7 @@ namespace CodexPlugin { if (pinned) return accounts.Last(); - var a = EthAccount.GenerateNew(); + var a = EthAccountGenerator.GenerateNew(); accounts.Add(a); return a; } diff --git a/ProjectPlugins/MetricsPlugin/MetricsQuery.cs b/ProjectPlugins/MetricsPlugin/MetricsQuery.cs index 7b70bd5c..0f0d11db 100644 --- a/ProjectPlugins/MetricsPlugin/MetricsQuery.cs +++ b/ProjectPlugins/MetricsPlugin/MetricsQuery.cs @@ -4,6 +4,7 @@ using KubernetesWorkflow.Types; using Logging; using System.Globalization; using Utils; +using WebUtils; namespace MetricsPlugin {