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

120 lines
4.4 KiB
C#
Raw Normal View History

using DistTestCore.Codex;
using Logging;
2023-04-14 08:51:35 +00:00
using NUnit.Framework;
using NUnit.Framework.Constraints;
using System.Numerics;
2023-04-14 08:51:35 +00:00
namespace DistTestCore.Marketplace
{
public interface IMarketplaceAccess
{
string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan maxDuration);
string RequestStorage(ContentId contentId, TestToken pricePerBytePerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration);
2023-04-14 08:51:35 +00:00
void AssertThatBalance(IResolveConstraint constraint, string message = "");
decimal GetBalance();
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly TestLog log;
private readonly MarketplaceNetwork marketplaceNetwork;
2023-04-14 10:37:05 +00:00
private readonly GethCompanionNodeInfo companionNode;
private readonly CodexAccess codexAccess;
2023-04-14 08:51:35 +00:00
public MarketplaceAccess(TestLog log, MarketplaceNetwork marketplaceNetwork, GethCompanionNodeInfo companionNode, CodexAccess codexAccess)
2023-04-14 08:51:35 +00:00
{
this.log = log;
this.marketplaceNetwork = marketplaceNetwork;
2023-04-14 10:37:05 +00:00
this.companionNode = companionNode;
this.codexAccess = codexAccess;
2023-04-14 08:51:35 +00:00
}
public string RequestStorage(ContentId contentId, TestToken pricePerBytePerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
2023-04-14 08:51:35 +00:00
{
var request = new CodexSalesRequestStorageRequest
{
duration = ToHexBigInt(duration.TotalSeconds),
proofProbability = ToHexBigInt(proofProbability),
reward = ToHexBigInt(pricePerBytePerSecond),
collateral = ToHexBigInt(requiredCollateral),
expiry = null,
nodes = minRequiredNumberOfNodes,
tolerance = null,
};
var response = codexAccess.RequestStorage(request, contentId.Id);
return response.purchaseId;
}
public string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan duration)
{
var request = new CodexSalesAvailabilityRequest
{
size = ToHexBigInt(size.SizeInBytes),
duration = ToHexBigInt(duration.TotalSeconds),
maxCollateral = ToHexBigInt(maxCollateral),
minPrice = ToHexBigInt(minPricePerBytePerSecond)
};
var response = codexAccess.SalesAvailability(request);
return response.id;
}
private string ToHexBigInt(double d)
{
return "0x" + string.Format("{0:X}", Convert.ToInt64(d));
2023-04-14 08:51:35 +00:00
}
public string ToHexBigInt(TestToken t)
2023-04-14 08:51:35 +00:00
{
var bigInt = new BigInteger(t.Amount);
return "0x" + bigInt.ToString("X");
2023-04-14 08:51:35 +00:00
}
public void AssertThatBalance(IResolveConstraint constraint, string message = "")
{
2023-04-14 10:37:05 +00:00
Assert.That(GetBalance(), constraint, message);
2023-04-14 08:51:35 +00:00
}
public decimal GetBalance()
{
var interaction = marketplaceNetwork.StartInteraction(log);
return interaction.GetBalance(marketplaceNetwork.Marketplace.TokenAddress, companionNode.Account);
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();
}
public decimal GetBalance()
{
Unavailable();
return 0;
}
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();
}
}
}