cs-codex-dist-tests/ProjectPlugins/CodexContractsPlugin/ContractsContainerInfoExtra...

75 lines
2.6 KiB
C#
Raw Normal View History

using CodexContractsPlugin.Marketplace;
using KubernetesWorkflow;
using KubernetesWorkflow.Types;
2023-09-15 14:27:08 +00:00
using Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Utils;
namespace CodexContractsPlugin
{
public class ContractsContainerInfoExtractor
{
private readonly ILog log;
private readonly IStartupWorkflow workflow;
private readonly RunningContainer container;
public ContractsContainerInfoExtractor(ILog log, IStartupWorkflow workflow, RunningContainer container)
{
this.log = log;
this.workflow = workflow;
this.container = container;
}
public string ExtractMarketplaceAddress()
{
log.Debug();
var marketplaceAddress = Retry(FetchMarketplaceAddress);
if (string.IsNullOrEmpty(marketplaceAddress)) throw new InvalidOperationException("Unable to fetch marketplace account from codex-contracts node. Test infra failure.");
log.Debug("Got MarketplaceAddress: " + marketplaceAddress);
2023-09-15 14:27:08 +00:00
return marketplaceAddress;
}
public (string, string) ExtractMarketplaceAbiAndByteCode()
2023-09-15 14:27:08 +00:00
{
log.Debug();
var (abi, bytecode) = Retry(FetchMarketplaceAbiAndByteCode);
if (string.IsNullOrEmpty(abi)) throw new InvalidOperationException("Unable to fetch marketplace artifacts from codex-contracts node. Test infra failure.");
2023-09-15 14:27:08 +00:00
log.Debug("Got Marketplace ABI: " + abi);
return (abi, bytecode);
2023-09-15 14:27:08 +00:00
}
private string FetchMarketplaceAddress()
{
var json = workflow.ExecuteCommand(container, "cat", CodexContractsContainerRecipe.MarketplaceAddressFilename);
var marketplace = JsonConvert.DeserializeObject<MarketplaceJson>(json);
return marketplace!.address;
}
private (string, string) FetchMarketplaceAbiAndByteCode()
2023-09-15 14:27:08 +00:00
{
var json = workflow.ExecuteCommand(container, "cat", CodexContractsContainerRecipe.MarketplaceArtifactFilename);
var artifact = JObject.Parse(json);
var abi = artifact["abi"];
var byteCode = artifact["bytecode"];
var abiResult = abi!.ToString(Formatting.None);
2024-08-30 09:17:36 +00:00
var byteCodeResult = byteCode!.ToString(Formatting.None).ToLowerInvariant().Replace("\"", "");
return (abiResult, byteCodeResult);
2023-09-15 14:27:08 +00:00
}
private static T Retry<T>(Func<T> fetch)
2023-09-15 14:27:08 +00:00
{
return Time.Retry(fetch, nameof(ContractsContainerInfoExtractor));
}
}
public class MarketplaceJson
{
public string address { get; set; } = string.Empty;
}
}