From bdd977d8a9f574914b5200ec87bddf686249920d Mon Sep 17 00:00:00 2001 From: benbierens Date: Wed, 12 Apr 2023 08:40:23 +0200 Subject: [PATCH] wrap up with basic ETH balance control --- .../Marketplace/GethCompanionNodeContainer.cs | 4 +- CodexDistTestCore/Marketplace/K8sGethSpecs.cs | 2 +- .../Marketplace/MarketplaceAccess.cs | 10 ++--- .../Marketplace/MarketplaceController.cs | 38 +++++++++++-------- Tests/BasicTests/SimpleTests.cs | 16 ++++++-- 5 files changed, 44 insertions(+), 26 deletions(-) diff --git a/CodexDistTestCore/Marketplace/GethCompanionNodeContainer.cs b/CodexDistTestCore/Marketplace/GethCompanionNodeContainer.cs index de5e59e..74d687c 100644 --- a/CodexDistTestCore/Marketplace/GethCompanionNodeContainer.cs +++ b/CodexDistTestCore/Marketplace/GethCompanionNodeContainer.cs @@ -15,16 +15,18 @@ public class GethCompanionNodeContainer { - public GethCompanionNodeContainer(string name, int apiPort, int rpcPort, string containerPortName) + public GethCompanionNodeContainer(string name, int apiPort, int rpcPort, string containerPortName, int authRpcPort) { Name = name; ApiPort = apiPort; + AuthRpcPort = authRpcPort; RpcPort = rpcPort; ContainerPortName = containerPortName; } public string Name { get; } public int ApiPort { get; } + public int AuthRpcPort { get; } public int RpcPort { get; } public string ContainerPortName { get; } diff --git a/CodexDistTestCore/Marketplace/K8sGethSpecs.cs b/CodexDistTestCore/Marketplace/K8sGethSpecs.cs index dcd9740..f99d520 100644 --- a/CodexDistTestCore/Marketplace/K8sGethSpecs.cs +++ b/CodexDistTestCore/Marketplace/K8sGethSpecs.cs @@ -185,7 +185,7 @@ namespace CodexDistTestCore.Marketplace new V1EnvVar { Name = "GETH_ARGS", - Value = $"--port {container.ApiPort} --discovery.port {container.ApiPort} --http.port {container.RpcPort}" + Value = $"--port {container.ApiPort} --discovery.port {container.ApiPort} --authrpc.port {container.AuthRpcPort} --http.port {container.RpcPort}" }, new V1EnvVar { diff --git a/CodexDistTestCore/Marketplace/MarketplaceAccess.cs b/CodexDistTestCore/Marketplace/MarketplaceAccess.cs index e1b99b0..e9bf39e 100644 --- a/CodexDistTestCore/Marketplace/MarketplaceAccess.cs +++ b/CodexDistTestCore/Marketplace/MarketplaceAccess.cs @@ -8,7 +8,7 @@ namespace CodexDistTestCore.Marketplace void MakeStorageAvailable(ByteSize size, int minPricePerBytePerSecond, float maxCollateral); void RequestStorage(ContentId contentId, int pricePerBytePerSecond, float requiredCollateral, float minRequiredNumberOfNodes); void AssertThatBalance(IResolveConstraint constraint, string message = ""); - float GetBalance(); + decimal GetBalance(); } public class MarketplaceAccess : IMarketplaceAccess @@ -57,9 +57,9 @@ namespace CodexDistTestCore.Marketplace throw new NotImplementedException(); } - public float GetBalance() + public decimal GetBalance() { - throw new NotImplementedException(); + return marketplaceController.GetBalance(container.Account); } private void EnsureAccount() @@ -96,10 +96,10 @@ namespace CodexDistTestCore.Marketplace Unavailable(); } - public float GetBalance() + public decimal GetBalance() { Unavailable(); - return 0.0f; + return 0; } private void Unavailable() diff --git a/CodexDistTestCore/Marketplace/MarketplaceController.cs b/CodexDistTestCore/Marketplace/MarketplaceController.cs index 17ce1cf..21bfc08 100644 --- a/CodexDistTestCore/Marketplace/MarketplaceController.cs +++ b/CodexDistTestCore/Marketplace/MarketplaceController.cs @@ -62,12 +62,12 @@ namespace CodexDistTestCore.Marketplace return new GethCompanionNodeContainer( name: $"geth-node{n}", apiPort: numberSource.GetNextNumber(), + authRpcPort: numberSource.GetNextNumber(), rpcPort: numberSource.GetNextNumber(), containerPortName: $"geth-{n}" ); } - private readonly K8sCluster k8sCluster = new K8sCluster(); public void AddToBalance(string account, decimal amount) @@ -76,30 +76,38 @@ namespace CodexDistTestCore.Marketplace // call the bootstrap node and convince it to give 'account' 'amount' tokens somehow. - var ip = k8sCluster.GetIp(); - var port = bootstrapInfo!.Spec.ServicePort; + var web3 = CreateWeb3(); - //var bootstrapaccount = new ManagedAccount(bootstrapInfo.Account, "qwerty!@#$%^"); - var web3 = new Web3($"http://{ip}:{port}"); + //var blockNumber1 = Utils.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync()); + //Thread.Sleep(TimeSpan.FromSeconds(5)); + //var blockNumber2 = Utils.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync()); - var blockNumber1 = Utils.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync()); - Thread.Sleep(TimeSpan.FromSeconds(5)); - var blockNumber2 = Utils.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync()); - - var bootstrapBalance = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(bootstrapInfo.Account)); - var targetBalance = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(account)); + //var bootstrapBalance = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(bootstrapInfo.Account)); + var bigint = new BigInteger(amount); var str = bigint.ToString("X"); var value = new Nethereum.Hex.HexTypes.HexBigInteger(str); - var aaa = Utils.Wait(web3.Eth.TransactionManager.SendTransactionAsync(bootstrapInfo.Account, account, value)); + var aaa = Utils.Wait(web3.Eth.TransactionManager.SendTransactionAsync(bootstrapInfo!.Account, account, value)); + var receipt = Utils.Wait(web3.Eth.TransactionManager.TransactionReceiptService.PollForReceiptAsync(aaa)); //var receipt = Utils.Wait(web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(account, amount)); + //var targetBalance = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(account)); + } - targetBalance = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(account)); - - var a = 0; + public decimal GetBalance(string account) + { + var web3 = CreateWeb3(); + var bigInt = Utils.Wait(web3.Eth.GetBalance.SendRequestAsync(account)); + return (decimal)bigInt.Value; + } + private Web3 CreateWeb3() + { + var ip = k8sCluster.GetIp(); + var port = bootstrapInfo!.Spec.ServicePort; + //var bootstrapaccount = new ManagedAccount(bootstrapInfo.Account, "qwerty!@#$%^"); + return new Web3($"http://{ip}:{port}"); } private (string, string) ExtractAccountAndGenesisJson(PodInfo pod) diff --git a/Tests/BasicTests/SimpleTests.cs b/Tests/BasicTests/SimpleTests.cs index 233b730..e15ce98 100644 --- a/Tests/BasicTests/SimpleTests.cs +++ b/Tests/BasicTests/SimpleTests.cs @@ -34,10 +34,20 @@ namespace Tests.BasicTests [Test] public void MarketplaceExample() { - var primary = SetupCodexNodes(1) + var group = SetupCodexNodes(4) .WithStorageQuota(10.GB()) .EnableMarketplace(initialBalance: 20) - .BringOnline()[0]; + .BringOnline(); + + foreach (var node in group) + { + Assert.That(node.Marketplace.GetBalance(), Is.EqualTo(20)); + } + + // WIP: Balance is now only ETH. + // todo: All nodes should have plenty of ETH to pay for transactions. + // todo: Upload our own token, use this exclusively. ETH should be invisibile to the tests. + //var secondary = SetupCodexNodes(1) // .EnableMarketplace(initialBalance: 1000) @@ -56,8 +66,6 @@ namespace Tests.BasicTests //secondary.Marketplace.AssertThatBalance(Is.LessThan(1000), "Contractor was not charged for storage."); //primary.Marketplace.AssertThatBalance(Is.GreaterThan(primaryBalance), "Storer was not paid for storage."); - - var aa = 0; } [Test]