From a53d2de13b0ed783525045bee7d032a1832526a3 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Mar 2024 15:35:26 +0100 Subject: [PATCH] Moves ethAccount to Geth plugin. Enables setting ethAccount when Codex starts. --- .../CodexPlugin/CodexContainerRecipe.cs | 10 ++--- .../CodexPlugin/CodexNodeFactory.cs | 6 +-- ProjectPlugins/CodexPlugin/CodexPlugin.cs | 4 +- ProjectPlugins/CodexPlugin/CodexSetup.cs | 37 +++++++++++++------ .../CodexPlugin/MarketplaceInitialConfig.cs | 6 +-- .../CodexPlugin/MarketplaceStartResults.cs | 17 --------- .../CodexPlugin/MarketplaceStarter.cs | 19 ---------- ProjectPlugins/GethPlugin/EthAccount.cs | 28 ++++++++++++++ ProjectPlugins/GethPlugin/EthAddress.cs | 1 + .../BasicTests/ContinuousSubstitute.cs | 3 +- Tests/CodexTests/BasicTests/ExampleTests.cs | 10 +++-- .../FullyConnectedDownloadTests.cs | 3 +- .../PeerDiscoveryTests/PeerDiscoveryTests.cs | 3 +- Tools/CodexNetDeployer/CodexNodeStarter.cs | 7 ++-- 14 files changed, 80 insertions(+), 74 deletions(-) delete mode 100644 ProjectPlugins/CodexPlugin/MarketplaceStartResults.cs delete mode 100644 ProjectPlugins/CodexPlugin/MarketplaceStarter.cs create mode 100644 ProjectPlugins/GethPlugin/EthAccount.cs diff --git a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs index 9d041c6..7ff1bde 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-c219a5f-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 3e40a99..9c16af9 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 54be6a9..d2560c4 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 5f4c300..71ce58e 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 4973e2a..1d4d5d1 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 10c1cde..82aad30 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 663659b..d25e373 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(); }); }