2024-08-30 10:25:41 +00:00
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
|
|
|
|
using Core;
|
2023-09-15 14:27:08 +00:00
|
|
|
|
using GethPlugin;
|
|
|
|
|
using KubernetesWorkflow;
|
2023-11-12 09:07:23 +00:00
|
|
|
|
using KubernetesWorkflow.Types;
|
2023-09-15 14:27:08 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace CodexContractsPlugin
|
|
|
|
|
{
|
|
|
|
|
public class CodexContractsStarter
|
|
|
|
|
{
|
|
|
|
|
private readonly IPluginTools tools;
|
|
|
|
|
|
|
|
|
|
public CodexContractsStarter(IPluginTools tools)
|
|
|
|
|
{
|
|
|
|
|
this.tools = tools;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 05:58:08 +00:00
|
|
|
|
public CodexContractsDeployment Deploy(CoreInterface ci, IGethNode gethNode)
|
2023-09-15 14:27:08 +00:00
|
|
|
|
{
|
2023-09-22 05:58:08 +00:00
|
|
|
|
Log("Starting Codex SmartContracts container...");
|
2023-09-15 14:27:08 +00:00
|
|
|
|
|
|
|
|
|
var workflow = tools.CreateWorkflow();
|
|
|
|
|
var startupConfig = CreateStartupConfig(gethNode);
|
2023-09-22 05:58:08 +00:00
|
|
|
|
startupConfig.NameOverride = "codex-contracts";
|
2023-09-15 14:27:08 +00:00
|
|
|
|
|
2024-04-09 08:23:07 +00:00
|
|
|
|
var containers = workflow.Start(1, new CodexContractsContainerRecipe(), startupConfig).WaitForOnline();
|
2023-09-15 14:27:08 +00:00
|
|
|
|
if (containers.Containers.Length != 1) throw new InvalidOperationException("Expected 1 Codex contracts container to be created. Test infra failure.");
|
|
|
|
|
var container = containers.Containers[0];
|
|
|
|
|
|
2023-09-22 05:58:08 +00:00
|
|
|
|
Log("Container started.");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-10-27 06:33:07 +00:00
|
|
|
|
var result = DeployContract(container, workflow, gethNode);
|
2024-03-13 09:57:26 +00:00
|
|
|
|
workflow.Stop(containers, waitTillStopped: false);
|
2023-10-27 06:33:07 +00:00
|
|
|
|
Log("Container stopped.");
|
|
|
|
|
return result;
|
2023-09-22 05:58:08 +00:00
|
|
|
|
}
|
2023-10-23 13:49:14 +00:00
|
|
|
|
catch (Exception ex)
|
2023-09-22 05:58:08 +00:00
|
|
|
|
{
|
2023-10-23 13:49:14 +00:00
|
|
|
|
Log("Failed to deploy contract: " + ex);
|
2023-09-22 05:58:08 +00:00
|
|
|
|
Log("Downloading Codex SmartContracts container log...");
|
|
|
|
|
ci.DownloadLog(container);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
public ICodexContracts Wrap(IGethNode gethNode, CodexContractsDeployment deployment)
|
2023-09-22 05:58:08 +00:00
|
|
|
|
{
|
2023-10-30 12:30:14 +00:00
|
|
|
|
return new CodexContractsAccess(tools.GetLog(), gethNode, deployment);
|
2023-09-22 05:58:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CodexContractsDeployment DeployContract(RunningContainer container, IStartupWorkflow workflow, IGethNode gethNode)
|
|
|
|
|
{
|
|
|
|
|
Log("Deploying SmartContract...");
|
2023-09-15 14:27:08 +00:00
|
|
|
|
WaitUntil(() =>
|
|
|
|
|
{
|
|
|
|
|
var logHandler = new ContractsReadyLogHandler(tools.GetLog());
|
2023-09-26 12:32:28 +00:00
|
|
|
|
workflow.DownloadContainerLog(container, logHandler, 100);
|
2023-09-15 14:27:08 +00:00
|
|
|
|
return logHandler.Found;
|
2024-04-14 07:17:25 +00:00
|
|
|
|
}, nameof(DeployContract));
|
2023-09-15 14:27:08 +00:00
|
|
|
|
Log("Contracts deployed. Extracting addresses...");
|
|
|
|
|
|
|
|
|
|
var extractor = new ContractsContainerInfoExtractor(tools.GetLog(), workflow, container);
|
|
|
|
|
var marketplaceAddress = extractor.ExtractMarketplaceAddress();
|
2024-08-30 10:25:41 +00:00
|
|
|
|
var (abi, bytecode) = extractor.ExtractMarketplaceAbiAndByteCode();
|
|
|
|
|
EnsureCompatbility(abi, bytecode);
|
2023-09-15 14:27:08 +00:00
|
|
|
|
|
2023-09-19 11:39:24 +00:00
|
|
|
|
var interaction = new ContractInteractions(tools.GetLog(), gethNode);
|
2023-09-15 14:27:08 +00:00
|
|
|
|
var tokenAddress = interaction.GetTokenAddress(marketplaceAddress);
|
|
|
|
|
|
2023-09-19 11:58:45 +00:00
|
|
|
|
Log("Extract completed. Checking sync...");
|
|
|
|
|
|
2024-04-14 07:17:25 +00:00
|
|
|
|
Time.WaitUntil(() => interaction.IsSynced(marketplaceAddress, abi), nameof(DeployContract));
|
2023-09-19 11:58:45 +00:00
|
|
|
|
|
|
|
|
|
Log("Synced. Codex SmartContracts deployed.");
|
2023-09-15 14:27:08 +00:00
|
|
|
|
|
2023-09-20 07:16:57 +00:00
|
|
|
|
return new CodexContractsDeployment(marketplaceAddress, abi, tokenAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 10:25:41 +00:00
|
|
|
|
private void EnsureCompatbility(string abi, string bytecode)
|
|
|
|
|
{
|
|
|
|
|
var expectedByteCode = MarketplaceDeploymentBase.BYTECODE.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
if (bytecode != expectedByteCode)
|
|
|
|
|
{
|
|
|
|
|
Log("Deployed contract is incompatible with current build of CodexContracts plugin. Running self-updater...");
|
|
|
|
|
var selfUpdater = new SelfUpdater();
|
|
|
|
|
selfUpdater.Update(abi, bytecode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 14:27:08 +00:00
|
|
|
|
private void Log(string msg)
|
|
|
|
|
{
|
|
|
|
|
tools.GetLog().Log(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 07:17:25 +00:00
|
|
|
|
private void WaitUntil(Func<bool> predicate, string msg)
|
2023-09-15 14:27:08 +00:00
|
|
|
|
{
|
2024-04-14 07:17:25 +00:00
|
|
|
|
Time.WaitUntil(predicate, TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(2), msg);
|
2023-09-15 14:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 08:24:43 +00:00
|
|
|
|
private StartupConfig CreateStartupConfig(IGethNode gethNode)
|
2023-09-15 14:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
var startupConfig = new StartupConfig();
|
|
|
|
|
var contractsConfig = new CodexContractsContainerConfig(gethNode);
|
|
|
|
|
startupConfig.Add(contractsConfig);
|
|
|
|
|
return startupConfig;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ContractsReadyLogHandler : LogHandler
|
|
|
|
|
{
|
|
|
|
|
// Log should contain 'Compiled 15 Solidity files successfully' at some point.
|
|
|
|
|
private const string RequiredCompiledString = "Solidity files successfully";
|
|
|
|
|
// When script is done, it prints the ready-string.
|
|
|
|
|
private const string ReadyString = "Done! Sleeping indefinitely...";
|
|
|
|
|
private readonly ILog log;
|
|
|
|
|
|
|
|
|
|
public ContractsReadyLogHandler(ILog log)
|
|
|
|
|
{
|
|
|
|
|
this.log = log;
|
|
|
|
|
|
|
|
|
|
log.Debug($"Looking for '{RequiredCompiledString}' and '{ReadyString}' in container logs...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SeenCompileString { get; private set; }
|
|
|
|
|
public bool Found { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override void ProcessLine(string line)
|
|
|
|
|
{
|
|
|
|
|
log.Debug(line);
|
|
|
|
|
if (line.Contains(RequiredCompiledString)) SeenCompileString = true;
|
|
|
|
|
if (line.Contains(ReadyString))
|
|
|
|
|
{
|
|
|
|
|
if (!SeenCompileString) throw new Exception("CodexContracts deployment failed. " +
|
|
|
|
|
"Solidity files not compiled before process exited.");
|
|
|
|
|
|
|
|
|
|
Found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|