diff --git a/ProjectPlugins/CodexPlugin/ApiChecker.cs b/ProjectPlugins/CodexPlugin/ApiChecker.cs index 437ad22..ae9984e 100644 --- a/ProjectPlugins/CodexPlugin/ApiChecker.cs +++ b/ProjectPlugins/CodexPlugin/ApiChecker.cs @@ -9,7 +9,7 @@ namespace CodexPlugin public class ApiChecker { // - private const string OpenApiYamlHash = "EB-31-10-0C-A5-B9-D2-4A-AD-2C-A7-BF-FD-70-BD-92-32-29-D7-FF-06-B0-52-46-8E-54-D5-EB-17-C8-DB-E3"; + private const string OpenApiYamlHash = "DC-90-1B-63-76-1B-92-01-05-65-33-DA-17-C2-34-83-E1-2E-6C-A9-04-4D-68-ED-96-43-F5-E5-6A-00-0F-5F"; private const string OpenApiFilePath = "/codex/openapi.yaml"; private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK"; diff --git a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs index bf5e724..ba356b2 100644 --- a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs +++ b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs @@ -7,8 +7,6 @@ namespace CodexPlugin { public class CodexContainerRecipe : ContainerRecipeFactory { - private readonly MarketplaceStarter marketplaceStarter = new MarketplaceStarter(); - private const string DefaultDockerImage = "codexstorage/nim-codex:sha-455b95d-dist-tests"; public const string ApiPortTag = "codex_api_port"; public const string ListenPortTag = "codex_listen_port"; @@ -106,13 +104,13 @@ namespace CodexPlugin AddEnvVar("CODEX_ETH_PROVIDER", $"{wsAddress.Host.Replace("http://", "ws://")}:{wsAddress.Port}"); AddEnvVar("CODEX_MARKETPLACE_ADDRESS", marketplaceAddress); + var marketplaceSetup = config.MarketplaceConfig.MarketplaceSetup; + // 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); + AddEnvVar("PRIV_KEY", marketplaceSetup.EthAccount.PrivateKey); + Additional(marketplaceSetup.EthAccount); - var marketplaceSetup = config.MarketplaceConfig.MarketplaceSetup; SetCommandOverride(marketplaceSetup); if (marketplaceSetup.IsValidator) { diff --git a/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs b/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs index f0c7166..05b80b2 100644 --- a/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs +++ b/ProjectPlugins/CodexPlugin/CodexNodeFactory.cs @@ -35,9 +35,9 @@ namespace CodexPlugin private EthAddress? GetEthAddress(CodexAccess access) { - var mStart = access.Container.Recipe.Additionals.Get(); - if (mStart == null) return null; - return mStart.EthAddress; + var ethAccount = access.Container.Recipe.Additionals.Get(); + if (ethAccount == null) return null; + return ethAccount.EthAddress; } public CrashWatcher CreateCrashWatcher(RunningContainer c) diff --git a/ProjectPlugins/CodexPlugin/CodexPlugin.cs b/ProjectPlugins/CodexPlugin/CodexPlugin.cs index 99a9337..9b8586e 100644 --- a/ProjectPlugins/CodexPlugin/CodexPlugin.cs +++ b/ProjectPlugins/CodexPlugin/CodexPlugin.cs @@ -52,8 +52,8 @@ namespace CodexPlugin var mconfig = codexSetup.MarketplaceConfig; foreach (var node in result) { - mconfig.GethNode.SendEth(node, mconfig.InitialEth); - mconfig.CodexContracts.MintTestTokens(node, mconfig.InitialTokens); + mconfig.GethNode.SendEth(node, mconfig.MarketplaceSetup.InitialEth); + mconfig.CodexContracts.MintTestTokens(node, mconfig.MarketplaceSetup.InitialTestTokens); } } diff --git a/ProjectPlugins/CodexPlugin/CodexSetup.cs b/ProjectPlugins/CodexPlugin/CodexSetup.cs index a7afc9f..9cdb2f4 100644 --- a/ProjectPlugins/CodexPlugin/CodexSetup.cs +++ b/ProjectPlugins/CodexPlugin/CodexSetup.cs @@ -17,8 +17,7 @@ namespace CodexPlugin ICodexSetup WithBlockMaintenanceInterval(TimeSpan duration); ICodexSetup WithBlockMaintenanceNumber(int numberOfBlocks); ICodexSetup EnableMetrics(); - ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Ether initialEth, TestToken initialTokens); - ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Ether initialEth, TestToken initialTokens, Action marketplaceSetup); + ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action marketplaceSetup); /// /// Provides an invalid proof every N proofs /// @@ -28,6 +27,8 @@ namespace CodexPlugin public interface IMarketplaceSetup { + IMarketplaceSetup WithInitial(Ether eth, TestToken tokens); + IMarketplaceSetup WithAccount(EthAccount account); IMarketplaceSetup AsStorageNode(); IMarketplaceSetup AsValidator(); } @@ -122,17 +123,12 @@ namespace CodexPlugin return this; } - public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Ether initialEth, TestToken initialTokens) - { - return EnableMarketplace(gethNode, codexContracts, initialEth, initialTokens, s => { }); - } - - public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Ether initialEth, TestToken initialTokens, Action marketplaceSetup) + public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action marketplaceSetup) { var ms = new MarketplaceSetup(); marketplaceSetup(ms); - MarketplaceConfig = new MarketplaceInitialConfig(ms, gethNode, codexContracts, initialEth, initialTokens); + MarketplaceConfig = new MarketplaceInitialConfig(ms, gethNode, codexContracts); return this; } @@ -169,6 +165,9 @@ namespace CodexPlugin { public bool IsStorageNode { get; private set; } public bool IsValidator { get; private set; } + public Ether InitialEth { get; private set; } = 0.Eth(); + public TestToken InitialTestTokens { get; private set; } = 0.TestTokens(); + public EthAccount EthAccount { get; private set; } = EthAccount.GenerateNew(); public IMarketplaceSetup AsStorageNode() { @@ -182,14 +181,28 @@ namespace CodexPlugin return this; } + public IMarketplaceSetup WithAccount(EthAccount account) + { + EthAccount = account; + return this; + } + + public IMarketplaceSetup WithInitial(Ether eth, TestToken tokens) + { + InitialEth = eth; + InitialTestTokens = tokens; + return this; + } + public override string ToString() { var result = "[(clientNode)"; // When marketplace is enabled, being a clientNode is implicit. result += IsStorageNode ? "(storageNode)" : "()"; - result += IsValidator ? "(validator)" : "()"; - result += "]"; + result += IsValidator ? "(validator)" : "() "; + result += $"Address: '{EthAccount.EthAddress}' "; + result += $"InitialEth/TT({InitialEth.Eth}/{InitialTestTokens.Amount})"; + result += "] "; return result; } } - } diff --git a/ProjectPlugins/CodexPlugin/MarketplaceInitialConfig.cs b/ProjectPlugins/CodexPlugin/MarketplaceInitialConfig.cs index 6bd4999..0f55580 100644 --- a/ProjectPlugins/CodexPlugin/MarketplaceInitialConfig.cs +++ b/ProjectPlugins/CodexPlugin/MarketplaceInitialConfig.cs @@ -5,19 +5,15 @@ namespace CodexPlugin { public class MarketplaceInitialConfig { - public MarketplaceInitialConfig(MarketplaceSetup marketplaceSetup, IGethNode gethNode, ICodexContracts codexContracts, Ether initialEth, TestToken initialTokens) + public MarketplaceInitialConfig(MarketplaceSetup marketplaceSetup, IGethNode gethNode, ICodexContracts codexContracts) { MarketplaceSetup = marketplaceSetup; GethNode = gethNode; CodexContracts = codexContracts; - InitialEth = initialEth; - InitialTokens = initialTokens; } public MarketplaceSetup MarketplaceSetup { get; } public IGethNode GethNode { get; } public ICodexContracts CodexContracts { get; } - public Ether InitialEth { get; } - public TestToken InitialTokens { get; } } } diff --git a/ProjectPlugins/CodexPlugin/MarketplaceStartResults.cs b/ProjectPlugins/CodexPlugin/MarketplaceStartResults.cs deleted file mode 100644 index 4bce517..0000000 --- a/ProjectPlugins/CodexPlugin/MarketplaceStartResults.cs +++ /dev/null @@ -1,17 +0,0 @@ -using GethPlugin; - -namespace CodexPlugin -{ - [Serializable] - public class MarketplaceStartResults - { - public MarketplaceStartResults(EthAddress ethAddress, string privateKey) - { - EthAddress = ethAddress; - PrivateKey = privateKey; - } - - public EthAddress EthAddress { get; } - public string PrivateKey { get; } - } -} diff --git a/ProjectPlugins/CodexPlugin/MarketplaceStarter.cs b/ProjectPlugins/CodexPlugin/MarketplaceStarter.cs deleted file mode 100644 index 42a5851..0000000 --- a/ProjectPlugins/CodexPlugin/MarketplaceStarter.cs +++ /dev/null @@ -1,19 +0,0 @@ -using GethPlugin; -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); - var ethAddress = new EthAddress(account.Address); - - return new MarketplaceStartResults(ethAddress, account.PrivateKey); - } - } -} diff --git a/ProjectPlugins/GethPlugin/EthAccount.cs b/ProjectPlugins/GethPlugin/EthAccount.cs new file mode 100644 index 0000000..9f1318b --- /dev/null +++ b/ProjectPlugins/GethPlugin/EthAccount.cs @@ -0,0 +1,28 @@ +using Nethereum.Hex.HexConvertors.Extensions; +using Nethereum.Web3.Accounts; + +namespace GethPlugin +{ + [Serializable] + public class EthAccount + { + public EthAccount(EthAddress ethAddress, string privateKey) + { + EthAddress = ethAddress; + PrivateKey = privateKey; + } + + public EthAddress EthAddress { get; } + public string PrivateKey { get; } + + public static EthAccount GenerateNew() + { + var ecKey = Nethereum.Signer.EthECKey.GenerateKey(); + var privateKey = ecKey.GetPrivateKeyAsBytes().ToHex(); + var account = new Account(privateKey); + var ethAddress = new EthAddress(account.Address); + + return new EthAccount(ethAddress, account.PrivateKey); + } + } +} diff --git a/ProjectPlugins/GethPlugin/EthAddress.cs b/ProjectPlugins/GethPlugin/EthAddress.cs index 803a1f7..9b32cd2 100644 --- a/ProjectPlugins/GethPlugin/EthAddress.cs +++ b/ProjectPlugins/GethPlugin/EthAddress.cs @@ -5,6 +5,7 @@ EthAddress EthAddress { get; } } + [Serializable] public class EthAddress { public EthAddress(string address) diff --git a/Tests/CodexTests/BasicTests/ContinuousSubstitute.cs b/Tests/CodexTests/BasicTests/ContinuousSubstitute.cs index 874c071..299c4b9 100644 --- a/Tests/CodexTests/BasicTests/ContinuousSubstitute.cs +++ b/Tests/CodexTests/BasicTests/ContinuousSubstitute.cs @@ -21,7 +21,8 @@ namespace CodexTests.BasicTests var group = AddCodex(5, o => o .EnableMetrics() - .EnableMarketplace(geth, contract, 10.Eth(), 100000.TestTokens(), s => s + .EnableMarketplace(geth, contract, s => s + .WithInitial(10.Eth(), 100000.TestTokens()) .AsStorageNode() .AsValidator()) .WithBlockTTL(TimeSpan.FromMinutes(5)) diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 203e1f0..0e8c6cc 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -64,7 +64,8 @@ namespace CodexTests.BasicTests .WithName("Seller") .WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)) .WithStorageQuota(11.GB()) - .EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: sellerInitialBalance, s => s + .EnableMarketplace(geth, contracts, m => m + .WithInitial(10.Eth(), sellerInitialBalance) .AsStorageNode() .AsValidator())); @@ -81,9 +82,10 @@ namespace CodexTests.BasicTests var testFile = GenerateTestFile(fileSize); var buyer = AddCodex(s => s - .WithName("Buyer") - .WithBootstrapNode(seller) - .EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: buyerInitialBalance)); + .WithName("Buyer") + .WithBootstrapNode(seller) + .EnableMarketplace(geth, contracts, m => m + .WithInitial(10.Eth(), buyerInitialBalance))); AssertBalance(contracts, buyer, Is.EqualTo(buyerInitialBalance)); diff --git a/Tests/CodexTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs b/Tests/CodexTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs index b4762b9..7f11171 100644 --- a/Tests/CodexTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs +++ b/Tests/CodexTests/DownloadConnectivityTests/FullyConnectedDownloadTests.cs @@ -21,7 +21,8 @@ namespace CodexTests.DownloadConnectivityTests { var geth = Ci.StartGethNode(s => s.IsMiner()); var contracts = Ci.StartCodexContracts(geth); - AddCodex(2, s => s.EnableMarketplace(geth, contracts, 10.Eth(), 1000.TestTokens())); + AddCodex(2, s => s.EnableMarketplace(geth, contracts, m => m + .WithInitial(10.Eth(), 1000.TestTokens()))); AssertAllNodesConnected(); } diff --git a/Tests/CodexTests/PeerDiscoveryTests/PeerDiscoveryTests.cs b/Tests/CodexTests/PeerDiscoveryTests/PeerDiscoveryTests.cs index 5089788..1e0faa4 100644 --- a/Tests/CodexTests/PeerDiscoveryTests/PeerDiscoveryTests.cs +++ b/Tests/CodexTests/PeerDiscoveryTests/PeerDiscoveryTests.cs @@ -31,7 +31,8 @@ namespace CodexTests.PeerDiscoveryTests { var geth = Ci.StartGethNode(s => s.IsMiner()); var contracts = Ci.StartCodexContracts(geth); - AddCodex(2, s => s.EnableMarketplace(geth, contracts, 10.Eth(), 1000.TestTokens())); + AddCodex(2, s => s.EnableMarketplace(geth, contracts, m => m + .WithInitial(10.Eth(), 1000.TestTokens()))); AssertAllNodesConnected(); } diff --git a/Tools/CodexNetDeployer/CodexNodeStarter.cs b/Tools/CodexNetDeployer/CodexNodeStarter.cs index 73e3c68..97dd4fd 100644 --- a/Tools/CodexNetDeployer/CodexNodeStarter.cs +++ b/Tools/CodexNetDeployer/CodexNodeStarter.cs @@ -41,10 +41,11 @@ namespace CodexNetDeployer if (config.ShouldMakeStorageAvailable) { - s.EnableMarketplace(gethNode, contracts, 100.Eth(), config.InitialTestTokens.TestTokens(), s => + s.EnableMarketplace(gethNode, contracts, m => { - if (validatorsLeft > 0) s.AsValidator(); - if (config.ShouldMakeStorageAvailable) s.AsStorageNode(); + m.WithInitial(100.Eth(), config.InitialTestTokens.TestTokens()); + if (validatorsLeft > 0) m.AsValidator(); + if (config.ShouldMakeStorageAvailable) m.AsStorageNode(); }); }