Moves ethAccount to Geth plugin. Enables setting ethAccount when Codex starts.

This commit is contained in:
Ben 2024-03-26 15:35:26 +01:00
parent e8213b9515
commit a53d2de13b
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
14 changed files with 80 additions and 74 deletions

View File

@ -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)
{

View File

@ -35,9 +35,9 @@ namespace CodexPlugin
private EthAddress? GetEthAddress(CodexAccess access)
{
var mStart = access.Container.Recipe.Additionals.Get<MarketplaceStartResults>();
if (mStart == null) return null;
return mStart.EthAddress;
var ethAccount = access.Container.Recipe.Additionals.Get<EthAccount>();
if (ethAccount == null) return null;
return ethAccount.EthAddress;
}
public CrashWatcher CreateCrashWatcher(RunningContainer c)

View File

@ -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);
}
}

View File

@ -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<IMarketplaceSetup> marketplaceSetup);
ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action<IMarketplaceSetup> marketplaceSetup);
/// <summary>
/// Provides an invalid proof every N proofs
/// </summary>
@ -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<IMarketplaceSetup> marketplaceSetup)
public ICodexSetup EnableMarketplace(IGethNode gethNode, ICodexContracts codexContracts, Action<IMarketplaceSetup> 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;
}
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -5,6 +5,7 @@
EthAddress EthAddress { get; }
}
[Serializable]
public class EthAddress
{
public EthAddress(string address)

View File

@ -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))

View File

@ -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));

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
});
}