2023-04-18 13:33:12 +00:00
using DistTestCore.Codex ;
using Logging ;
2023-04-14 08:51:35 +00:00
using NUnit.Framework ;
using NUnit.Framework.Constraints ;
2023-04-18 13:33:12 +00:00
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
{
2023-04-18 13:33:12 +00:00
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 = "" ) ;
2023-04-19 07:57:37 +00:00
TestToken GetBalance ( ) ;
2023-04-14 08:51:35 +00:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly TestLog log ;
2023-04-18 08:22:11 +00:00
private readonly MarketplaceNetwork marketplaceNetwork ;
2023-05-03 08:21:15 +00:00
private readonly GethAccount account ;
2023-04-18 13:33:12 +00:00
private readonly CodexAccess codexAccess ;
2023-04-14 08:51:35 +00:00
2023-05-03 08:21:15 +00:00
public MarketplaceAccess ( TestLog log , MarketplaceNetwork marketplaceNetwork , GethAccount account , CodexAccess codexAccess )
2023-04-14 08:51:35 +00:00
{
this . log = log ;
2023-04-18 08:22:11 +00:00
this . marketplaceNetwork = marketplaceNetwork ;
2023-04-26 09:12:33 +00:00
this . account = account ;
2023-04-18 13:33:12 +00:00
this . codexAccess = codexAccess ;
2023-04-14 08:51:35 +00:00
}
2023-04-18 13:33:12 +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
{
2023-04-18 13:33:12 +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 ,
} ;
2023-04-19 07:57:37 +00:00
Log ( $"Requesting storage for: {contentId.Id}... (" +
2023-04-19 08:42:08 +00:00
$"pricePerBytePerSecond: {pricePerBytePerSecond}, " +
$"requiredCollateral: {requiredCollateral}, " +
$"minRequiredNumberOfNodes: {minRequiredNumberOfNodes}, " +
$"proofProbability: {proofProbability}, " +
2023-04-19 07:57:37 +00:00
$"duration: {Time.FormatDuration(duration)})" ) ;
2023-04-18 13:33:12 +00:00
var response = codexAccess . RequestStorage ( request , contentId . Id ) ;
2023-04-24 14:07:32 +00:00
if ( response = = "Purchasing not available" )
{
throw new InvalidOperationException ( response ) ;
}
Log ( $"Storage requested successfully. PurchaseId: {response}" ) ;
2023-04-19 07:57:37 +00:00
2023-04-24 14:07:32 +00:00
return response ;
2023-04-18 13:33:12 +00:00
}
2023-04-19 07:57:37 +00:00
public string MakeStorageAvailable ( ByteSize size , TestToken minPricePerBytePerSecond , TestToken maxCollateral , TimeSpan maxDuration )
2023-04-18 13:33:12 +00:00
{
var request = new CodexSalesAvailabilityRequest
{
size = ToHexBigInt ( size . SizeInBytes ) ,
2023-04-19 07:57:37 +00:00
duration = ToHexBigInt ( maxDuration . TotalSeconds ) ,
2023-04-18 13:33:12 +00:00
maxCollateral = ToHexBigInt ( maxCollateral ) ,
minPrice = ToHexBigInt ( minPricePerBytePerSecond )
} ;
2023-04-19 07:57:37 +00:00
Log ( $"Making storage available... (" +
2023-04-19 08:42:08 +00:00
$"size: {size}, " +
$"minPricePerBytePerSecond: {minPricePerBytePerSecond}, " +
$"maxCollateral: {maxCollateral}, " +
$"maxDuration: {Time.FormatDuration(maxDuration)})" ) ;
2023-04-19 07:57:37 +00:00
2023-04-18 13:33:12 +00:00
var response = codexAccess . SalesAvailability ( request ) ;
2023-04-19 07:57:37 +00:00
Log ( $"Storage successfully made available. Id: {response.id}" ) ;
2023-04-18 13:33:12 +00:00
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
}
2023-04-18 13:33:12 +00:00
public string ToHexBigInt ( TestToken t )
2023-04-14 08:51:35 +00:00
{
2023-04-18 13:33:12 +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
}
2023-04-19 07:57:37 +00:00
public TestToken GetBalance ( )
2023-04-14 08:51:35 +00:00
{
2023-04-25 09:31:15 +00:00
var interaction = marketplaceNetwork . StartInteraction ( log ) ;
2023-04-26 09:12:33 +00:00
var amount = interaction . GetBalance ( marketplaceNetwork . Marketplace . TokenAddress , account . Account ) ;
2023-04-19 07:57:37 +00:00
var balance = new TestToken ( amount ) ;
2023-04-26 09:12:33 +00:00
Log ( $"Balance of {account.Account} is {balance}." ) ;
2023-04-19 07:57:37 +00:00
return balance ;
}
private void Log ( string msg )
{
2023-04-30 08:56:19 +00:00
log . Log ( $"{codexAccess.Container.Name} {msg}" ) ;
2023-04-14 08:51:35 +00:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2023-04-18 13:33:12 +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
{
Unavailable ( ) ;
2023-04-18 13:33:12 +00:00
return string . Empty ;
2023-04-14 08:51:35 +00:00
}
2023-04-18 13:33:12 +00:00
public string MakeStorageAvailable ( ByteSize size , TestToken minPricePerBytePerSecond , TestToken maxCollateral , TimeSpan duration )
2023-04-14 08:51:35 +00:00
{
Unavailable ( ) ;
2023-04-18 13:33:12 +00:00
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 ( ) ;
}
}
}