cs-codex-dist-tests/DistTestCore/Marketplace/MarketplaceAccess.cs

154 lines
5.6 KiB
C#
Raw Normal View History

using DistTestCore.Codex;
using DistTestCore.Helpers;
2023-04-14 08:51:35 +00:00
using NUnit.Framework;
using NUnit.Framework.Constraints;
using System.Numerics;
2023-04-19 07:57:37 +00:00
using Utils;
2023-04-14 08:51:35 +00:00
namespace DistTestCore.Marketplace
{
public interface IMarketplaceAccess
{
string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan maxDuration);
2023-06-27 13:28:00 +00:00
string RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration);
2023-04-14 08:51:35 +00:00
void AssertThatBalance(IResolveConstraint constraint, string message = "");
2023-04-19 07:57:37 +00:00
TestToken GetBalance();
2023-04-14 08:51:35 +00:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly TestLifecycle lifecycle;
private readonly MarketplaceNetwork marketplaceNetwork;
2023-05-03 08:21:15 +00:00
private readonly GethAccount account;
private readonly CodexAccess codexAccess;
2023-04-14 08:51:35 +00:00
public MarketplaceAccess(TestLifecycle lifecycle, MarketplaceNetwork marketplaceNetwork, GethAccount account, CodexAccess codexAccess)
2023-04-14 08:51:35 +00:00
{
this.lifecycle = lifecycle;
this.marketplaceNetwork = marketplaceNetwork;
this.account = account;
this.codexAccess = codexAccess;
2023-04-14 08:51:35 +00:00
}
2023-06-27 13:28:00 +00:00
public string RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
2023-04-14 08:51:35 +00:00
{
var request = new CodexSalesRequestStorageRequest
{
duration = ToDecInt(duration.TotalSeconds),
proofProbability = ToDecInt(proofProbability),
reward = ToDecInt(pricePerSlotPerSecond),
collateral = ToDecInt(requiredCollateral),
expiry = null,
nodes = minRequiredNumberOfNodes,
tolerance = null,
};
2023-04-19 07:57:37 +00:00
Log($"Requesting storage for: {contentId.Id}... (" +
2023-06-27 13:28:00 +00:00
$"pricePerSlotPerSecond: {pricePerSlotPerSecond}, " +
$"requiredCollateral: {requiredCollateral}, " +
$"minRequiredNumberOfNodes: {minRequiredNumberOfNodes}, " +
$"proofProbability: {proofProbability}, " +
2023-04-19 07:57:37 +00:00
$"duration: {Time.FormatDuration(duration)})");
var response = codexAccess.Node.RequestStorage(request, contentId.Id);
if (response == "Purchasing not available")
{
throw new InvalidOperationException(response);
}
Log($"Storage requested successfully. PurchaseId: {response}");
2023-04-19 07:57:37 +00:00
return response;
}
2023-04-19 07:57:37 +00:00
public string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan maxDuration)
{
var request = new CodexSalesAvailabilityRequest
{
size = ToDecInt(size.SizeInBytes),
duration = ToDecInt(maxDuration.TotalSeconds),
maxCollateral = ToDecInt(maxCollateral),
minPrice = ToDecInt(minPricePerBytePerSecond)
};
2023-04-19 07:57:37 +00:00
Log($"Making storage available... (" +
$"size: {size}, " +
$"minPricePerBytePerSecond: {minPricePerBytePerSecond}, " +
$"maxCollateral: {maxCollateral}, " +
$"maxDuration: {Time.FormatDuration(maxDuration)})");
2023-04-19 07:57:37 +00:00
var response = codexAccess.Node.SalesAvailability(request);
2023-04-19 07:57:37 +00:00
Log($"Storage successfully made available. Id: {response.id}");
return response.id;
}
private string ToDecInt(double d)
{
var i = new BigInteger(d);
return i.ToString("D");
2023-04-14 08:51:35 +00:00
}
public string ToDecInt(TestToken t)
2023-04-14 08:51:35 +00:00
{
var i = new BigInteger(t.Amount);
return i.ToString("D");
2023-04-14 08:51:35 +00:00
}
public void AssertThatBalance(IResolveConstraint constraint, string message = "")
{
AssertHelpers.RetryAssert(constraint, GetBalance, message);
2023-04-14 08:51:35 +00:00
}
2023-04-19 07:57:37 +00:00
public TestToken GetBalance()
2023-04-14 08:51:35 +00:00
{
var interaction = marketplaceNetwork.StartInteraction(lifecycle);
var amount = interaction.GetBalance(marketplaceNetwork.Marketplace.TokenAddress, account.Account);
2023-04-19 07:57:37 +00:00
var balance = new TestToken(amount);
Log($"Balance of {account.Account} is {balance}.");
2023-04-19 07:57:37 +00:00
return balance;
}
private void Log(string msg)
{
lifecycle.Log.Log($"{codexAccess.Container.Name} {msg}");
2023-04-14 08:51:35 +00:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
public string RequestStorage(ContentId contentId, TestToken pricePerBytePerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
2023-04-14 08:51:35 +00:00
{
Unavailable();
return string.Empty;
2023-04-14 08:51:35 +00:00
}
public string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan duration)
2023-04-14 08:51:35 +00:00
{
Unavailable();
return string.Empty;
2023-04-14 08:51:35 +00:00
}
public void AssertThatBalance(IResolveConstraint constraint, string message = "")
{
Unavailable();
}
2023-04-19 07:57:37 +00:00
public TestToken GetBalance()
2023-04-14 08:51:35 +00:00
{
Unavailable();
2023-04-19 07:57:37 +00:00
return new TestToken(0);
2023-04-14 08:51:35 +00:00
}
private void Unavailable()
{
Assert.Fail("Incorrect test setup: Marketplace was not enabled for this group of Codex nodes. Add 'EnableMarketplace(...)' after 'SetupCodexNodes()' to enable it.");
throw new InvalidOperationException();
}
}
}