2023-04-10 07:05:27 +00:00
using NUnit.Framework ;
using NUnit.Framework.Constraints ;
namespace CodexDistTestCore.Marketplace
{
public interface IMarketplaceAccess
{
void AdvertiseStorage ( ByteSize size , float pricePerMBPerSecond , float collateral ) ;
void AdvertiseContract ( ContentId contentId , float maxPricePerMBPerSecond , float minRequiredCollateral , float minRequiredNumberOfDuplicates ) ;
void AssertThatBalance ( IResolveConstraint constraint , string message = "" ) ;
float GetBalance ( ) ;
}
public class MarketplaceAccess : IMarketplaceAccess
{
2023-04-10 13:54:13 +00:00
private readonly K8sManager k8sManager ;
2023-04-11 08:12:24 +00:00
private readonly MarketplaceController marketplaceController ;
2023-04-10 13:54:13 +00:00
private readonly TestLog log ;
2023-04-11 08:12:24 +00:00
private readonly CodexNodeGroup group ;
private readonly GethCompanionNodeContainer gethCompanionNodeContainer ;
2023-04-10 13:54:13 +00:00
private string account = string . Empty ;
2023-04-11 08:12:24 +00:00
public MarketplaceAccess (
K8sManager k8sManager ,
MarketplaceController marketplaceController ,
TestLog log ,
CodexNodeGroup group ,
GethCompanionNodeContainer gethCompanionNodeContainer )
2023-04-10 13:54:13 +00:00
{
this . k8sManager = k8sManager ;
2023-04-11 08:12:24 +00:00
this . marketplaceController = marketplaceController ;
2023-04-10 13:54:13 +00:00
this . log = log ;
2023-04-11 08:12:24 +00:00
this . group = group ;
this . gethCompanionNodeContainer = gethCompanionNodeContainer ;
2023-04-10 13:54:13 +00:00
}
2023-04-11 08:12:24 +00:00
public void Initialize ( )
2023-04-10 13:54:13 +00:00
{
2023-04-11 08:12:24 +00:00
EnsureAccount ( ) ;
2023-04-10 13:54:13 +00:00
2023-04-11 08:12:24 +00:00
marketplaceController . AddToBalance ( account , group . Origin . MarketplaceConfig ! . InitialBalance ) ;
log . Log ( $"Initialized Geth companion node with account '{account}' and initial balance {group.Origin.MarketplaceConfig!.InitialBalance}" ) ;
2023-04-10 13:54:13 +00:00
}
2023-04-10 07:05:27 +00:00
public void AdvertiseContract ( ContentId contentId , float maxPricePerMBPerSecond , float minRequiredCollateral , float minRequiredNumberOfDuplicates )
{
throw new NotImplementedException ( ) ;
}
public void AdvertiseStorage ( ByteSize size , float pricePerMBPerSecond , float collateral )
{
throw new NotImplementedException ( ) ;
}
public void AssertThatBalance ( IResolveConstraint constraint , string message = "" )
{
throw new NotImplementedException ( ) ;
}
public float GetBalance ( )
{
throw new NotImplementedException ( ) ;
}
2023-04-11 08:12:24 +00:00
private void EnsureAccount ( )
{
FetchAccount ( ) ;
if ( string . IsNullOrEmpty ( account ) )
{
Thread . Sleep ( TimeSpan . FromSeconds ( 15 ) ) ;
FetchAccount ( ) ;
}
Assert . That ( account , Is . Not . Empty , "Unable to fetch account for geth companion node. Test infra failure." ) ;
}
private void FetchAccount ( )
{
account = k8sManager . ExecuteCommand ( group . PodInfo ! , gethCompanionNodeContainer . Name , "cat" , GethDockerImage . AccountFilename ) ;
}
2023-04-10 07:05:27 +00:00
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
public void AdvertiseContract ( ContentId contentId , float maxPricePerMBPerSecond , float minRequiredCollateral , float minRequiredNumberOfDuplicates )
{
Unavailable ( ) ;
}
public void AdvertiseStorage ( ByteSize size , float pricePerMBPerSecond , float collateral )
{
Unavailable ( ) ;
}
public void AssertThatBalance ( IResolveConstraint constraint , string message = "" )
{
Unavailable ( ) ;
}
public float GetBalance ( )
{
Unavailable ( ) ;
return 0.0f ;
}
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 ( ) ;
}
}
}