From a20fc6864bcc5d2c06bfb9e47012b0912307d18f Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 19 Sep 2023 10:24:43 +0200 Subject: [PATCH] working out marketplace details --- CodexContractsPlugin/CodexContractsAccess.cs | 21 ++++++++++ .../CodexContractsContainerConfig.cs | 4 +- CodexContractsPlugin/CodexContractsPlugin.cs | 2 +- CodexContractsPlugin/CodexContractsStarter.cs | 6 +-- .../CoreInterfaceExtensions.cs | 2 +- CodexContractsPlugin/MarketplaceInfo.cs | 20 ---------- .../MarketplaceInitialConfig.cs | 17 --------- CodexPlugin/CodexContainerRecipe.cs | 38 ++++++++++--------- CodexPlugin/CodexPlugin.csproj | 2 + CodexPlugin/CodexSetup.cs | 12 +++++- CodexPlugin/CodexStartupConfig.cs | 2 +- CodexPlugin/MarketplaceInitialConfig.cs | 31 +++++++++++++++ CodexPlugin/MarketplaceStartResults.cs | 15 ++++++++ CodexPlugin/MarketplaceStarter.cs | 17 +++++++++ GethPlugin/CoreInterfaceExtensions.cs | 2 +- GethPlugin/{GethNodeInfo.cs => GethNode.cs} | 6 +-- GethPlugin/GethPlugin.cs | 2 +- GethPlugin/GethStarter.cs | 4 +- Tests/BasicTests/ExampleTests.cs | 15 ++++++++ 19 files changed, 148 insertions(+), 70 deletions(-) create mode 100644 CodexContractsPlugin/CodexContractsAccess.cs delete mode 100644 CodexContractsPlugin/MarketplaceInfo.cs delete mode 100644 CodexContractsPlugin/MarketplaceInitialConfig.cs create mode 100644 CodexPlugin/MarketplaceInitialConfig.cs create mode 100644 CodexPlugin/MarketplaceStartResults.cs create mode 100644 CodexPlugin/MarketplaceStarter.cs rename GethPlugin/{GethNodeInfo.cs => GethNode.cs} (84%) diff --git a/CodexContractsPlugin/CodexContractsAccess.cs b/CodexContractsPlugin/CodexContractsAccess.cs new file mode 100644 index 0000000..ba84a70 --- /dev/null +++ b/CodexContractsPlugin/CodexContractsAccess.cs @@ -0,0 +1,21 @@ +namespace CodexContractsPlugin +{ + public interface ICodexContracts + { + string MarketplaceAddress { get; } + } + + public class CodexContractsAccess : ICodexContracts + { + public CodexContractsAccess(string marketplaceAddress, string abi, string tokenAddress) + { + MarketplaceAddress = marketplaceAddress; + Abi = abi; + TokenAddress = tokenAddress; + } + + public string MarketplaceAddress { get; } + public string Abi { get; } + public string TokenAddress { get; } + } +} diff --git a/CodexContractsPlugin/CodexContractsContainerConfig.cs b/CodexContractsPlugin/CodexContractsContainerConfig.cs index ee710d1..876c9b1 100644 --- a/CodexContractsPlugin/CodexContractsContainerConfig.cs +++ b/CodexContractsPlugin/CodexContractsContainerConfig.cs @@ -5,11 +5,11 @@ namespace CodexContractsPlugin { public class CodexContractsContainerConfig { - public CodexContractsContainerConfig(IGethNodeInfo gethNode) + public CodexContractsContainerConfig(IGethNode gethNode) { GethNode = gethNode; } - public IGethNodeInfo GethNode { get; } + public IGethNode GethNode { get; } } } diff --git a/CodexContractsPlugin/CodexContractsPlugin.cs b/CodexContractsPlugin/CodexContractsPlugin.cs index 312b6d1..beeb233 100644 --- a/CodexContractsPlugin/CodexContractsPlugin.cs +++ b/CodexContractsPlugin/CodexContractsPlugin.cs @@ -30,7 +30,7 @@ namespace CodexContractsPlugin { } - public IMarketplaceInfo DeployContracts(IGethNodeInfo gethNode) + public ICodexContracts DeployContracts(IGethNode gethNode) { return starter.Start(gethNode); } diff --git a/CodexContractsPlugin/CodexContractsStarter.cs b/CodexContractsPlugin/CodexContractsStarter.cs index 8f9600c..8dcce36 100644 --- a/CodexContractsPlugin/CodexContractsStarter.cs +++ b/CodexContractsPlugin/CodexContractsStarter.cs @@ -15,7 +15,7 @@ namespace CodexContractsPlugin this.tools = tools; } - public IMarketplaceInfo Start(IGethNodeInfo gethNode) + public ICodexContracts Start(IGethNode gethNode) { Log("Deploying Codex Marketplace..."); @@ -43,7 +43,7 @@ namespace CodexContractsPlugin Log("Extract completed. Marketplace deployed."); - return new MarketplaceInfo(marketplaceAddress, abi, tokenAddress); + return new CodexContractsAccess(marketplaceAddress, abi, tokenAddress); } private void Log(string msg) @@ -56,7 +56,7 @@ namespace CodexContractsPlugin Time.WaitUntil(predicate, TimeSpan.FromMinutes(3), TimeSpan.FromSeconds(2)); } - private StartupConfig CreateStartupConfig(IGethNodeInfo gethNode) + private StartupConfig CreateStartupConfig(IGethNode gethNode) { var startupConfig = new StartupConfig(); var contractsConfig = new CodexContractsContainerConfig(gethNode); diff --git a/CodexContractsPlugin/CoreInterfaceExtensions.cs b/CodexContractsPlugin/CoreInterfaceExtensions.cs index 80a97aa..20c4645 100644 --- a/CodexContractsPlugin/CoreInterfaceExtensions.cs +++ b/CodexContractsPlugin/CoreInterfaceExtensions.cs @@ -5,7 +5,7 @@ namespace CodexContractsPlugin { public static class CoreInterfaceExtensions { - public static IMarketplaceInfo DeployCodexContracts(this CoreInterface ci, IGethNodeInfo gethNode) + public static ICodexContracts DeployCodexContracts(this CoreInterface ci, IGethNode gethNode) { return Plugin(ci).DeployContracts(gethNode); } diff --git a/CodexContractsPlugin/MarketplaceInfo.cs b/CodexContractsPlugin/MarketplaceInfo.cs deleted file mode 100644 index cac1983..0000000 --- a/CodexContractsPlugin/MarketplaceInfo.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace CodexContractsPlugin -{ - public interface IMarketplaceInfo - { - } - - public class MarketplaceInfo : IMarketplaceInfo - { - public MarketplaceInfo(string address, string abi, string tokenAddress) - { - Address = address; - Abi = abi; - TokenAddress = tokenAddress; - } - - public string Address { get; } - public string Abi { get; } - public string TokenAddress { get; } - } -} diff --git a/CodexContractsPlugin/MarketplaceInitialConfig.cs b/CodexContractsPlugin/MarketplaceInitialConfig.cs deleted file mode 100644 index da16bca..0000000 --- a/CodexContractsPlugin/MarketplaceInitialConfig.cs +++ /dev/null @@ -1,17 +0,0 @@ -//namespace DistTestCore.Marketplace -//{ -// public class MarketplaceInitialConfig -// { -// public MarketplaceInitialConfig(Ether initialEth, TestToken initialTestTokens, bool isValidator) -// { -// InitialEth = initialEth; -// InitialTestTokens = initialTestTokens; -// IsValidator = isValidator; -// } - -// public Ether InitialEth { get; } -// public TestToken InitialTestTokens { get; } -// public bool IsValidator { get; } -// public int? AccountIndexOverride { get; set; } -// } -//} diff --git a/CodexPlugin/CodexContainerRecipe.cs b/CodexPlugin/CodexContainerRecipe.cs index 2225d59..6e82eb3 100644 --- a/CodexPlugin/CodexContainerRecipe.cs +++ b/CodexPlugin/CodexContainerRecipe.cs @@ -5,6 +5,8 @@ namespace CodexPlugin { public class CodexContainerRecipe : ContainerRecipeFactory { + private readonly MarketplaceStarter marketplaceStarter = new MarketplaceStarter(); + private const string DefaultDockerImage = "codexstorage/nim-codex:latest-dist-tests"; public const string MetricsPortTag = "metrics_port"; @@ -75,26 +77,28 @@ namespace CodexPlugin AddPodAnnotation("prometheus.io/port", metricsPort.Number.ToString()); } - //if (config.MarketplaceConfig != null) - //{ - // var gethConfig = startupConfig.Get(); - // var companionNode = gethConfig.CompanionNode; - // var companionNodeAccount = companionNode.Accounts[GetAccountIndex(config.MarketplaceConfig)]; - // Additional(companionNodeAccount); + if (config.MarketplaceConfig != null) + { + var mconfig = config.MarketplaceConfig; + var ip = mconfig.GethNode.RunningContainer.Pod.PodInfo.Ip; + var port = mconfig.GethNode.WsPort.Number; + var marketplaceAddress = mconfig.CodexContracts.MarketplaceAddress; - // var ip = companionNode.RunningContainer.Pod.PodInfo.Ip; - // var port = companionNode.RunningContainer.Recipe.GetPortByTag(GethContainerRecipe.HttpPortTag).Number; + AddEnvVar("CODEX_ETH_PROVIDER", $"ws://{ip}:{port}"); + AddEnvVar("CODEX_MARKETPLACE_ADDRESS", marketplaceAddress); + AddEnvVar("CODEX_PERSISTENCE", "true"); - // AddEnvVar("CODEX_ETH_PROVIDER", $"ws://{ip}:{port}"); - // AddEnvVar("CODEX_ETH_ACCOUNT", companionNodeAccount.Account); - // AddEnvVar("CODEX_MARKETPLACE_ADDRESS", gethConfig.MarketplaceNetwork.Marketplace.Address); - // AddEnvVar("CODEX_PERSISTENCE", "true"); + // Custom scripting in the Codex test image will write this variable to a private-key file, + // and pass the correct filename to Codex. + var mStart = marketplaceStarter.Start(); + AddEnvVar("PRIV_KEY", mStart.PrivateKey); + Additional(mStart); - // if (config.MarketplaceConfig.IsValidator) - // { - // AddEnvVar("CODEX_VALIDATOR", "true"); - // } - //} + if (config.MarketplaceConfig.IsValidator) + { + AddEnvVar("CODEX_VALIDATOR", "true"); + } + } AddPodLabel("codexid", Image); } diff --git a/CodexPlugin/CodexPlugin.csproj b/CodexPlugin/CodexPlugin.csproj index a00f24f..aac490c 100644 --- a/CodexPlugin/CodexPlugin.csproj +++ b/CodexPlugin/CodexPlugin.csproj @@ -11,7 +11,9 @@ + + diff --git a/CodexPlugin/CodexSetup.cs b/CodexPlugin/CodexSetup.cs index d6356e1..672165d 100644 --- a/CodexPlugin/CodexSetup.cs +++ b/CodexPlugin/CodexSetup.cs @@ -1,4 +1,6 @@ -using KubernetesWorkflow; +using CodexContractsPlugin; +using GethPlugin; +using KubernetesWorkflow; using Utils; namespace CodexPlugin @@ -14,6 +16,8 @@ namespace CodexPlugin ICodexSetup WithBlockMaintenanceInterval(TimeSpan duration); ICodexSetup WithBlockMaintenanceNumber(int numberOfBlocks); ICodexSetup EnableMetrics(); + ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, bool isValidator = false); + //ICodexSetup EnableMarketplace(TestToken initialBalance); //ICodexSetup EnableMarketplace(TestToken initialBalance, Ether initialEther); //ICodexSetup EnableMarketplace(TestToken initialBalance, Ether initialEther, bool isValidator); @@ -82,6 +86,12 @@ namespace CodexPlugin return this; } + public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, bool isValidator = false) + { + MarketplaceConfig = new MarketplaceInitialConfig(gethNode, codexContracts, isValidator); + return this; + } + //public ICodexSetup EnableMarketplace(TestToken initialBalance) //{ // return EnableMarketplace(initialBalance, 1000.Eth()); diff --git a/CodexPlugin/CodexStartupConfig.cs b/CodexPlugin/CodexStartupConfig.cs index 56b41d9..0cabfaf 100644 --- a/CodexPlugin/CodexStartupConfig.cs +++ b/CodexPlugin/CodexStartupConfig.cs @@ -10,7 +10,7 @@ namespace CodexPlugin public CodexLogLevel LogLevel { get; set; } public ByteSize? StorageQuota { get; set; } public bool MetricsEnabled { get; set; } - //public MarketplaceInitialConfig? MarketplaceConfig { get; set; } + public MarketplaceInitialConfig? MarketplaceConfig { get; set; } public string? BootstrapSpr { get; set; } public int? BlockTTL { get; set; } public TimeSpan? BlockMaintenanceInterval { get; set; } diff --git a/CodexPlugin/MarketplaceInitialConfig.cs b/CodexPlugin/MarketplaceInitialConfig.cs new file mode 100644 index 0000000..f1f6af6 --- /dev/null +++ b/CodexPlugin/MarketplaceInitialConfig.cs @@ -0,0 +1,31 @@ +using CodexContractsPlugin; +using GethPlugin; + +namespace CodexPlugin +{ + public class MarketplaceInitialConfig + { + public MarketplaceInitialConfig(IGethNode gethNode, ICodexContracts codexContracts, bool isValidator) + { + GethNode = gethNode; + CodexContracts = codexContracts; + IsValidator = isValidator; + } + + public IGethNode GethNode { get; } + public ICodexContracts CodexContracts { get; } + public bool IsValidator { get; } + + //public MarketplaceInitialConfig(Ether initialEth, TestToken initialTestTokens, bool isValidator) + //{ + // InitialEth = initialEth; + // InitialTestTokens = initialTestTokens; + // IsValidator = isValidator; + //} + + //public Ether InitialEth { get; } + //public TestToken InitialTestTokens { get; } + //public bool IsValidator { get; } + //public int? AccountIndexOverride { get; set; } + } +} diff --git a/CodexPlugin/MarketplaceStartResults.cs b/CodexPlugin/MarketplaceStartResults.cs new file mode 100644 index 0000000..dd73ed6 --- /dev/null +++ b/CodexPlugin/MarketplaceStartResults.cs @@ -0,0 +1,15 @@ +namespace CodexPlugin +{ + [Serializable] + public class MarketplaceStartResults + { + public MarketplaceStartResults(string ethAddress, string privateKey) + { + EthAddress = ethAddress; + PrivateKey = privateKey; + } + + public string EthAddress { get; } + public string PrivateKey { get; } + } +} diff --git a/CodexPlugin/MarketplaceStarter.cs b/CodexPlugin/MarketplaceStarter.cs new file mode 100644 index 0000000..dc9d525 --- /dev/null +++ b/CodexPlugin/MarketplaceStarter.cs @@ -0,0 +1,17 @@ +using Nethereum.Hex.HexConvertors.Extensions; +using Nethereum.Web3.Accounts; + +namespace CodexPlugin +{ + public class MarketplaceStarter + { + public MarketplaceStartResults Start() + { + var ecKey = Nethereum.Signer.EthECKey.GenerateKey(); + var privateKey = ecKey.GetPrivateKeyAsBytes().ToHex(); + var account = new Account(privateKey); + + return new MarketplaceStartResults(account.Address, account.PrivateKey); + } + } +} diff --git a/GethPlugin/CoreInterfaceExtensions.cs b/GethPlugin/CoreInterfaceExtensions.cs index e5049a9..34d9d2f 100644 --- a/GethPlugin/CoreInterfaceExtensions.cs +++ b/GethPlugin/CoreInterfaceExtensions.cs @@ -4,7 +4,7 @@ namespace GethPlugin { public static class CoreInterfaceExtensions { - public static IGethNodeInfo StartGethNode(this CoreInterface ci, Action setup) + public static IGethNode StartGethNode(this CoreInterface ci, Action setup) { return Plugin(ci).StartGeth(setup); } diff --git a/GethPlugin/GethNodeInfo.cs b/GethPlugin/GethNode.cs similarity index 84% rename from GethPlugin/GethNodeInfo.cs rename to GethPlugin/GethNode.cs index 4bd6b11..bb096b2 100644 --- a/GethPlugin/GethNodeInfo.cs +++ b/GethPlugin/GethNode.cs @@ -4,7 +4,7 @@ using NethereumWorkflow; namespace GethPlugin { - public interface IGethNodeInfo + public interface IGethNode { RunningContainer RunningContainer { get; } Port DiscoveryPort { get; } @@ -14,9 +14,9 @@ namespace GethPlugin NethereumInteraction StartInteraction(ILog log); } - public class GethNodeInfo : IGethNodeInfo + public class GethNode : IGethNode { - public GethNodeInfo(RunningContainer runningContainer, AllGethAccounts allAccounts, string pubKey, Port discoveryPort, Port httpPort, Port wsPort) + public GethNode(RunningContainer runningContainer, AllGethAccounts allAccounts, string pubKey, Port discoveryPort, Port httpPort, Port wsPort) { RunningContainer = runningContainer; AllAccounts = allAccounts; diff --git a/GethPlugin/GethPlugin.cs b/GethPlugin/GethPlugin.cs index 08f36a4..645e93a 100644 --- a/GethPlugin/GethPlugin.cs +++ b/GethPlugin/GethPlugin.cs @@ -30,7 +30,7 @@ namespace GethPlugin { } - public IGethNodeInfo StartGeth(Action setup) + public IGethNode StartGeth(Action setup) { var startupConfig = new GethStartupConfig(); setup(startupConfig); diff --git a/GethPlugin/GethStarter.cs b/GethPlugin/GethStarter.cs index 8ce2b3b..8cc1b8e 100644 --- a/GethPlugin/GethStarter.cs +++ b/GethPlugin/GethStarter.cs @@ -12,7 +12,7 @@ namespace GethPlugin this.tools = tools; } - public IGethNodeInfo StartGeth(GethStartupConfig gethStartupConfig) + public IGethNode StartGeth(GethStartupConfig gethStartupConfig) { Log("Starting Geth bootstrap node..."); @@ -36,7 +36,7 @@ namespace GethPlugin var wsPort = container.Recipe.GetPortByTag(GethContainerRecipe.wsPortTag); if (wsPort == null) throw new Exception("Expected ws port to be created."); - var result = new GethNodeInfo(container, accounts, pubKey, discoveryPort, httpPort, wsPort); + var result = new GethNode(container, accounts, pubKey, discoveryPort, httpPort, wsPort); Log($"Geth bootstrap node started with account '{result.Account.Account}'"); diff --git a/Tests/BasicTests/ExampleTests.cs b/Tests/BasicTests/ExampleTests.cs index 3a5c1fc..ef519af 100644 --- a/Tests/BasicTests/ExampleTests.cs +++ b/Tests/BasicTests/ExampleTests.cs @@ -52,6 +52,21 @@ namespace Tests.BasicTests var contracts = Ci.DeployCodexContracts(geth); + var node = Ci.SetupCodexNode(s => s.EnableMarketplace(geth, contracts)); + + var i = 0; + + //geth.SendEth(node.EthAddress, 10.Eth()); + + //contracts.MintTestTokens(geth, node.EthAddress, 100.TestTokens()); + + //geth.GetEthBalance(node.EthAddress); + + //contracts.GetTestTokenBalance(geth, node.EthAddress); + + + + //var sellerInitialBalance = 234.TestTokens(); //var buyerInitialBalance = 1000.TestTokens(); //var fileSize = 10.MB();