This commit is contained in:
benbierens 2023-04-11 11:00:39 +02:00
parent 8a6ababba6
commit c977e37ab8
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 75 additions and 94 deletions

View File

@ -1,10 +1,8 @@
using CodexDistTestCore.Marketplace; namespace CodexDistTestCore
namespace CodexDistTestCore
{ {
public class CodexNodeContainer public class CodexNodeContainer
{ {
public CodexNodeContainer(string name, int servicePort, string servicePortName, int apiPort, string containerPortName, int discoveryPort, int listenPort, string dataDir, int metricsPort, GethCompanionNodeContainer? gethCompanionNodeContainer) public CodexNodeContainer(string name, int servicePort, string servicePortName, int apiPort, string containerPortName, int discoveryPort, int listenPort, string dataDir, int metricsPort)
{ {
Name = name; Name = name;
ServicePort = servicePort; ServicePort = servicePort;
@ -15,7 +13,6 @@ namespace CodexDistTestCore
ListenPort = listenPort; ListenPort = listenPort;
DataDir = dataDir; DataDir = dataDir;
MetricsPort = metricsPort; MetricsPort = metricsPort;
GethCompanionNodeContainer = gethCompanionNodeContainer;
} }
public string Name { get; } public string Name { get; }
@ -27,8 +24,6 @@ namespace CodexDistTestCore
public int ListenPort { get; } public int ListenPort { get; }
public string DataDir { get; } public string DataDir { get; }
public int MetricsPort { get; } public int MetricsPort { get; }
public GethCompanionNodeContainer? GethCompanionNodeContainer { get; }
} }
public class CodexGroupNumberSource public class CodexGroupNumberSource
@ -57,11 +52,11 @@ namespace CodexDistTestCore
{ {
private readonly NumberSource containerNameSource = new NumberSource(1); private readonly NumberSource containerNameSource = new NumberSource(1);
private readonly NumberSource codexPortSource = new NumberSource(8080); private readonly NumberSource codexPortSource = new NumberSource(8080);
private readonly CodexGroupNumberSource groupContainerFactory; private readonly CodexGroupNumberSource numberSource;
public CodexNodeContainerFactory(CodexGroupNumberSource groupContainerFactory) public CodexNodeContainerFactory(CodexGroupNumberSource numberSource)
{ {
this.groupContainerFactory = groupContainerFactory; this.numberSource = numberSource;
} }
public CodexNodeContainer CreateNext(OfflineCodexNodes offline) public CodexNodeContainer CreateNext(OfflineCodexNodes offline)
@ -69,15 +64,14 @@ namespace CodexDistTestCore
var n = containerNameSource.GetNextNumber(); var n = containerNameSource.GetNextNumber();
return new CodexNodeContainer( return new CodexNodeContainer(
name: $"codex-node{n}", name: $"codex-node{n}",
servicePort: groupContainerFactory.GetNextServicePort(), servicePort: numberSource.GetNextServicePort(),
servicePortName: groupContainerFactory.GetNextServicePortName(), servicePortName: numberSource.GetNextServicePortName(),
apiPort: codexPortSource.GetNextNumber(), apiPort: codexPortSource.GetNextNumber(),
containerPortName: $"api-{n}", containerPortName: $"api-{n}",
discoveryPort: codexPortSource.GetNextNumber(), discoveryPort: codexPortSource.GetNextNumber(),
listenPort: codexPortSource.GetNextNumber(), listenPort: codexPortSource.GetNextNumber(),
dataDir: $"datadir{n}", dataDir: $"datadir{n}",
metricsPort: GetMetricsPort(offline), metricsPort: GetMetricsPort(offline)
CreateGethNodeContainer(offline, n)
); );
} }
@ -87,18 +81,5 @@ namespace CodexDistTestCore
return 0; return 0;
} }
private GethCompanionNodeContainer? CreateGethNodeContainer(OfflineCodexNodes offline, int n)
{
if (offline.MarketplaceConfig == null) return null;
return new GethCompanionNodeContainer(
name: $"geth-node{n}",
servicePort: groupContainerFactory.GetNextServicePort(),
servicePortName: groupContainerFactory.GetNextServicePortName(),
apiPort: codexPortSource.GetNextNumber(),
rpcPort: codexPortSource.GetNextNumber(),
containerPortName: $"geth-{n}"
);
}
} }
} }

View File

@ -34,7 +34,7 @@ namespace CodexDistTestCore
if (offline.MarketplaceConfig != null) if (offline.MarketplaceConfig != null)
{ {
group.GethInfo = BringOnlineMarketplace(); group.GethInfo = marketplaceController.BringOnlineMarketplace(offline);
} }
K8s(k => k.BringOnline(group, offline)); K8s(k => k.BringOnline(group, offline));
@ -108,11 +108,6 @@ namespace CodexDistTestCore
metricsAggregator.BeginCollectingMetricsFor(DowncastNodes(group)); metricsAggregator.BeginCollectingMetricsFor(DowncastNodes(group));
} }
private GethInfo BringOnlineMarketplace()
{
return marketplaceController.BringOnlineMarketplace();
}
private void ConnectMarketplace(CodexNodeGroup group) private void ConnectMarketplace(CodexNodeGroup group)
{ {
foreach (var node in DowncastNodes(group)) foreach (var node in DowncastNodes(group))

View File

@ -4,64 +4,18 @@ namespace CodexDistTestCore.Marketplace
{ {
public class GethCompanionNodeContainer public class GethCompanionNodeContainer
{ {
public GethCompanionNodeContainer(string name, int servicePort, string servicePortName, int apiPort, int rpcPort, string containerPortName) public GethCompanionNodeContainer(string name, int apiPort, int rpcPort, string containerPortName)
{ {
Name = name; Name = name;
ServicePort = servicePort;
ServicePortName = servicePortName;
ApiPort = apiPort; ApiPort = apiPort;
RpcPort = rpcPort; RpcPort = rpcPort;
ContainerPortName = containerPortName; ContainerPortName = containerPortName;
} }
public string Name { get; } public string Name { get; }
public int ServicePort { get; }
public string ServicePortName { get; }
public int ApiPort { get; } public int ApiPort { get; }
public int RpcPort { get; } public int RpcPort { get; }
public string ContainerPortName { get; } public string ContainerPortName { get; }
public V1Container CreateDeploymentContainer(GethInfo gethInfo)
{
return new V1Container
{
Name = Name,
Image = GethDockerImage.Image,
Ports = new List<V1ContainerPort>
{
new V1ContainerPort
{
ContainerPort = ApiPort,
Name = ContainerPortName
}
},
// todo: use env vars to connect this node to the bootstrap node provided by gethInfo.podInfo & gethInfo.servicePort & gethInfo.genesisJsonBase64
Env = new List<V1EnvVar>
{
new V1EnvVar
{
Name = "GETH_ARGS",
Value = $"--port {ApiPort} --discovery.port {ApiPort} --authrpc.port {RpcPort}"
},
new V1EnvVar
{
Name = "GENESIS_JSON",
Value = gethInfo.GenesisJsonBase64
}
}
};
}
public V1ServicePort CreateServicePort()
{
return new V1ServicePort
{
Name = ServicePortName,
Protocol = "TCP",
Port = ApiPort,
TargetPort = ContainerPortName,
NodePort = ServicePort
};
}
} }
} }

View File

@ -1,5 +1,6 @@
using CodexDistTestCore.Config; using CodexDistTestCore.Config;
using k8s.Models; using k8s.Models;
using System.Xml.Linq;
namespace CodexDistTestCore.Marketplace namespace CodexDistTestCore.Marketplace
{ {
@ -126,6 +127,37 @@ namespace CodexDistTestCore.Marketplace
return serviceSpec; return serviceSpec;
} }
public V1Deployment CreateGethCompanionDeployment(GethInfo gethInfo)
{
return new V1Container
{
Name = Name,
Image = GethDockerImage.Image,
Ports = new List<V1ContainerPort>
{
new V1ContainerPort
{
ContainerPort = ApiPort,
Name = ContainerPortName
}
},
// todo: use env vars to connect this node to the bootstrap node provided by gethInfo.podInfo & gethInfo.servicePort & gethInfo.genesisJsonBase64
Env = new List<V1EnvVar>
{
new V1EnvVar
{
Name = "GETH_ARGS",
Value = $"--port {ApiPort} --discovery.port {ApiPort} --authrpc.port {RpcPort}"
},
new V1EnvVar
{
Name = "GENESIS_JSON",
Value = gethInfo.GenesisJsonBase64
}
}
};
}
private Dictionary<string, string> CreateSelector() private Dictionary<string, string> CreateSelector()
{ {
return new Dictionary<string, string> { { "test-gethb", "dtest-gethb" } }; return new Dictionary<string, string> { { "test-gethb", "dtest-gethb" } };

View File

@ -5,8 +5,8 @@ namespace CodexDistTestCore.Marketplace
{ {
public interface IMarketplaceAccess public interface IMarketplaceAccess
{ {
void AdvertiseStorage(ByteSize size, float pricePerMBPerSecond, float collateral); void MakeStorageAvailable(ByteSize size, int minPricePerBytePerSecond, float maxCollateral);
void AdvertiseContract(ContentId contentId, float maxPricePerMBPerSecond, float minRequiredCollateral, float minRequiredNumberOfDuplicates); void RequestStorage(ContentId contentId, int pricePerBytePerSecond, float requiredCollateral, float minRequiredNumberOfNodes);
void AssertThatBalance(IResolveConstraint constraint, string message = ""); void AssertThatBalance(IResolveConstraint constraint, string message = "");
float GetBalance(); float GetBalance();
} }
@ -48,7 +48,7 @@ namespace CodexDistTestCore.Marketplace
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void AdvertiseStorage(ByteSize size, float pricePerMBPerSecond, float collateral) public void MakeStorageAvailable(ByteSize size, float pricePerMBPerSecond, float collateral)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -87,7 +87,7 @@ namespace CodexDistTestCore.Marketplace
Unavailable(); Unavailable();
} }
public void AdvertiseStorage(ByteSize size, float pricePerMBPerSecond, float collateral) public void MakeStorageAvailable(ByteSize size, float pricePerMBPerSecond, float collateral)
{ {
Unavailable(); Unavailable();
} }

View File

@ -17,18 +17,34 @@ namespace CodexDistTestCore.Marketplace
this.k8sManager = k8sManager; this.k8sManager = k8sManager;
} }
public GethInfo BringOnlineMarketplace() public GethInfo BringOnlineMarketplace(OfflineCodexNodes offline)
{ {
if (gethBootstrapNode != null) return gethBootstrapNode; if (gethBootstrapNode != null) return gethBootstrapNode;
log.Log("Starting Geth bootstrap node..."); log.Log("Starting Geth bootstrap node...");
gethBootstrapNode = k8sManager.BringOnlineGethBootstrapNode(); gethBootstrapNode = k8sManager.BringOnlineGethBootstrapNode();
ExtractAccountAndGenesisJson(); ExtractAccountAndGenesisJson();
log.Log("Geth boothstrap node started."); log.Log($"Geth boothstrap node started. Initializing companions for {offline.NumberOfNodes} Codex nodes.");
return gethBootstrapNode; return gethBootstrapNode;
} }
private GethCompanionNodeContainer? CreateGethNodeContainer(OfflineCodexNodes offline, int n)
{
return new GethCompanionNodeContainer(
name: $"geth-node{n}",
servicePort: numberSource.GetNextServicePort(),
servicePortName: numberSource.GetNextServicePortName(),
apiPort: codexPortSource.GetNextNumber(),
rpcPort: codexPortSource.GetNextNumber(),
containerPortName: $"geth-{n}"
);
}
public void AddToBalance(string account, int amount) public void AddToBalance(string account, int amount)
{ {
if (amount < 1 || string.IsNullOrEmpty(account)) Assert.Fail("Invalid arguments for AddToBalance"); if (amount < 1 || string.IsNullOrEmpty(account)) Assert.Fail("Invalid arguments for AddToBalance");
@ -62,20 +78,22 @@ namespace CodexDistTestCore.Marketplace
private string ExecuteCommand(string command, params string[] arguments) private string ExecuteCommand(string command, params string[] arguments)
{ {
return k8sManager.ExecuteCommand(gethBootstrapNode!.Pod, K8sGethBoostrapSpecs.ContainerName, command, arguments); return k8sManager.ExecuteCommand(gethBootstrapNode!.BootstrapPod, K8sGethBoostrapSpecs.ContainerName, command, arguments);
} }
} }
public class GethInfo public class GethInfo
{ {
public GethInfo(K8sGethBoostrapSpecs spec, PodInfo pod) public GethInfo(K8sGethBoostrapSpecs spec, PodInfo bootstrapPod, PodInfo companionPod)
{ {
Spec = spec; Spec = spec;
Pod = pod; BootstrapPod = bootstrapPod;
CompanionPod = companionPod;
} }
public K8sGethBoostrapSpecs Spec { get; } public K8sGethBoostrapSpecs Spec { get; }
public PodInfo Pod { get; } public PodInfo BootstrapPod { get; }
public PodInfo CompanionPod { get; }
public string GenesisJsonBase64 { get; set; } = string.Empty; public string GenesisJsonBase64 { get; set; } = string.Empty;
} }
} }

View File

@ -34,7 +34,7 @@ namespace Tests.BasicTests
[Test] [Test]
public void MarketplaceExample() public void MarketplaceExample()
{ {
var primary = SetupCodexNodes(4) var primary = SetupCodexNodes(1)
.WithStorageQuota(10.GB()) .WithStorageQuota(10.GB())
.EnableMarketplace(initialBalance: 20) .EnableMarketplace(initialBalance: 20)
.BringOnline()[0]; .BringOnline()[0];
@ -44,11 +44,12 @@ namespace Tests.BasicTests
// .BringOnline()[0]; // .BringOnline()[0];
//primary.ConnectToPeer(secondary); //primary.ConnectToPeer(secondary);
//primary.Marketplace.AdvertiseStorage(10.GB(), pricePerMBPerSecond: 0.01f, collateral: 20); //primary.Marketplace.MakeStorageAvailable(10.GB(), minPricePerBytePerSecond: 1, maxCollateral: 20);
//var testFile = GenerateTestFile(10.MB()); //var testFile = GenerateTestFile(10.MB());
//var contentId = secondary.UploadFile(testFile); //var contentId = secondary.UploadFile(testFile);
//secondary.Marketplace.AdvertiseContract(contentId, maxPricePerMBPerSecond: 0.02f, minRequiredCollateral: 10, minRequiredNumberOfDuplicates: 1); //secondary.Marketplace.RequestStorage(contentId, pricePerBytePerSecond: 2,
// requiredCollateral: 10, minRequiredNumberOfNodes: 1);
//primary.Marketplace.AssertThatBalance(Is.LessThan(20), "Collateral was not placed."); //primary.Marketplace.AssertThatBalance(Is.LessThan(20), "Collateral was not placed.");
//var primaryBalance = primary.Marketplace.GetBalance(); //var primaryBalance = primary.Marketplace.GetBalance();