2025-01-16 13:24:57 +01:00
|
|
|
using CodexClient;
|
|
|
|
|
using CodexClient.Hooks;
|
2024-03-25 13:48:20 +01:00
|
|
|
using Core;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
|
|
|
|
namespace CodexPlugin
|
|
|
|
|
{
|
2023-09-13 16:06:05 +02:00
|
|
|
public class CodexPlugin : IProjectPlugin, IHasLogPrefix, IHasMetadata
|
2023-09-11 16:57:57 +02:00
|
|
|
{
|
2025-02-21 13:57:25 +01:00
|
|
|
private const bool UseContainers = true;
|
|
|
|
|
|
2025-01-27 09:59:19 +01:00
|
|
|
private readonly ICodexStarter codexStarter;
|
2023-09-13 10:03:11 +02:00
|
|
|
private readonly IPluginTools tools;
|
2023-09-13 14:37:53 +02:00
|
|
|
private readonly CodexLogLevel defaultLogLevel = CodexLogLevel.Trace;
|
2025-01-27 09:59:19 +01:00
|
|
|
private readonly CodexHooksFactory hooksFactory = new CodexHooksFactory();
|
2025-01-27 10:22:34 +01:00
|
|
|
private readonly ProcessControlMap processControlMap = new ProcessControlMap();
|
2025-04-18 15:47:44 +02:00
|
|
|
private readonly CodexDockerImage codexDockerImage = new CodexDockerImage();
|
|
|
|
|
private readonly CodexContainerRecipe recipe;
|
2025-01-27 10:22:34 +01:00
|
|
|
private readonly CodexWrapper codexWrapper;
|
2023-09-11 16:57:57 +02:00
|
|
|
|
2023-09-13 10:03:11 +02:00
|
|
|
public CodexPlugin(IPluginTools tools)
|
2023-09-11 16:57:57 +02:00
|
|
|
{
|
2023-09-13 10:03:11 +02:00
|
|
|
this.tools = tools;
|
2025-02-21 13:57:25 +01:00
|
|
|
|
2025-04-18 15:47:44 +02:00
|
|
|
recipe = new CodexContainerRecipe(codexDockerImage);
|
2025-02-21 13:57:25 +01:00
|
|
|
codexStarter = CreateCodexStarter();
|
|
|
|
|
codexWrapper = new CodexWrapper(tools, processControlMap, hooksFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ICodexStarter CreateCodexStarter()
|
|
|
|
|
{
|
|
|
|
|
if (UseContainers)
|
|
|
|
|
{
|
|
|
|
|
Log("Using Containerized Codex instances");
|
2025-04-18 15:47:44 +02:00
|
|
|
return new ContainerCodexStarter(tools, recipe, processControlMap);
|
2025-02-21 13:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log("Using Binary Codex instances");
|
|
|
|
|
return new BinaryCodexStarter(tools, processControlMap);
|
2023-09-12 10:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-13 10:23:05 +02:00
|
|
|
public string LogPrefix => "(Codex) ";
|
2023-09-13 10:03:11 +02:00
|
|
|
|
2025-04-18 15:47:44 +02:00
|
|
|
public void Awake(IPluginAccess access)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 10:03:11 +02:00
|
|
|
public void Announce()
|
2023-09-12 10:31:55 +02:00
|
|
|
{
|
2025-04-18 15:47:44 +02:00
|
|
|
// give codex docker image to contracts plugin.
|
|
|
|
|
|
2025-01-27 10:22:34 +01:00
|
|
|
Log($"Loaded with Codex ID: '{codexWrapper.GetCodexId()}' - Revision: {codexWrapper.GetCodexRevision()}");
|
2023-09-13 16:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddMetadata(IAddMetadata metadata)
|
|
|
|
|
{
|
2025-01-27 10:22:34 +01:00
|
|
|
metadata.Add("codexid", codexWrapper.GetCodexId());
|
|
|
|
|
metadata.Add("codexrevision", codexWrapper.GetCodexRevision());
|
2023-09-11 16:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-13 10:03:11 +02:00
|
|
|
public void Decommission()
|
2023-09-12 10:31:55 +02:00
|
|
|
{
|
2025-01-28 11:21:47 +01:00
|
|
|
codexStarter.Decommission();
|
2023-09-12 10:31:55 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-27 09:49:21 +01:00
|
|
|
public ICodexInstance[] DeployCodexNodes(int numberOfNodes, Action<ICodexSetup> setup)
|
2023-09-11 16:57:57 +02:00
|
|
|
{
|
2023-09-20 08:45:55 +02:00
|
|
|
var codexSetup = GetSetup(numberOfNodes, setup);
|
2023-09-11 16:57:57 +02:00
|
|
|
return codexStarter.BringOnline(codexSetup);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 09:59:19 +01:00
|
|
|
public ICodexNodeGroup WrapCodexContainers(ICodexInstance[] instances)
|
2023-09-11 16:57:57 +02:00
|
|
|
{
|
2025-01-27 09:49:21 +01:00
|
|
|
instances = instances.Select(c => SerializeGate.Gate(c as CodexInstance)).ToArray();
|
2025-01-27 10:22:34 +01:00
|
|
|
return codexWrapper.WrapCodexInstances(instances);
|
2023-09-11 16:57:57 +02:00
|
|
|
}
|
2023-09-20 08:45:55 +02:00
|
|
|
|
|
|
|
|
public void WireUpMarketplace(ICodexNodeGroup result, Action<ICodexSetup> setup)
|
|
|
|
|
{
|
|
|
|
|
var codexSetup = GetSetup(1, setup);
|
|
|
|
|
if (codexSetup.MarketplaceConfig == null) return;
|
|
|
|
|
|
|
|
|
|
var mconfig = codexSetup.MarketplaceConfig;
|
|
|
|
|
foreach (var node in result)
|
|
|
|
|
{
|
2024-03-26 15:35:26 +01:00
|
|
|
mconfig.GethNode.SendEth(node, mconfig.MarketplaceSetup.InitialEth);
|
|
|
|
|
mconfig.CodexContracts.MintTestTokens(node, mconfig.MarketplaceSetup.InitialTestTokens);
|
2024-11-25 15:45:09 +01:00
|
|
|
|
|
|
|
|
Log($"Send {mconfig.MarketplaceSetup.InitialEth} and " +
|
|
|
|
|
$"minted {mconfig.MarketplaceSetup.InitialTestTokens} for " +
|
|
|
|
|
$"{node.GetName()} (address: {node.EthAddress})");
|
2023-09-20 08:45:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 14:44:19 +01:00
|
|
|
public void AddCodexHooksProvider(ICodexHooksProvider hooksProvider)
|
2024-07-26 08:39:27 +02:00
|
|
|
{
|
2025-01-29 14:44:19 +01:00
|
|
|
if (hooksFactory.Providers.Contains(hooksProvider)) return;
|
|
|
|
|
hooksFactory.Providers.Add(hooksProvider);
|
2024-07-26 08:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-20 08:45:55 +02:00
|
|
|
private CodexSetup GetSetup(int numberOfNodes, Action<ICodexSetup> setup)
|
|
|
|
|
{
|
|
|
|
|
var codexSetup = new CodexSetup(numberOfNodes);
|
|
|
|
|
codexSetup.LogLevel = defaultLogLevel;
|
|
|
|
|
setup(codexSetup);
|
|
|
|
|
return codexSetup;
|
|
|
|
|
}
|
2024-11-25 15:45:09 +01:00
|
|
|
|
|
|
|
|
private void Log(string msg)
|
|
|
|
|
{
|
|
|
|
|
tools.GetLog().Log(msg);
|
|
|
|
|
}
|
2023-09-11 16:57:57 +02:00
|
|
|
}
|
|
|
|
|
}
|