cs-codex-dist-tests/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs

106 lines
3.3 KiB
C#
Raw Normal View History

2024-08-20 09:44:15 +00:00
using CodexPlugin.Hooks;
using Logging;
2023-09-20 06:45:55 +00:00
using Utils;
namespace CodexPlugin
{
public interface IMarketplaceAccess
{
2024-03-20 10:11:41 +00:00
string MakeStorageAvailable(StorageAvailability availability);
2024-09-23 08:52:12 +00:00
StorageAvailability[] GetAvailabilities();
2024-05-30 09:33:16 +00:00
IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase);
2023-09-20 06:45:55 +00:00
}
public class MarketplaceAccess : IMarketplaceAccess
{
private readonly ILog log;
private readonly CodexAccess codexAccess;
2024-08-20 09:44:15 +00:00
private readonly ICodexNodeHooks hooks;
2023-09-20 06:45:55 +00:00
2024-08-20 09:44:15 +00:00
public MarketplaceAccess(ILog log, CodexAccess codexAccess, ICodexNodeHooks hooks)
2023-09-20 06:45:55 +00:00
{
this.log = log;
this.codexAccess = codexAccess;
2024-08-20 09:44:15 +00:00
this.hooks = hooks;
2023-09-20 06:45:55 +00:00
}
2024-05-30 09:33:16 +00:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 06:45:55 +00:00
{
2024-03-20 10:11:41 +00:00
purchase.Log(log);
2023-11-23 13:10:59 +00:00
2024-03-26 11:23:38 +00:00
var response = codexAccess.RequestStorage(purchase);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
if (string.IsNullOrEmpty(response) ||
2024-07-02 08:08:13 +00:00
response == "Unable to encode manifest" ||
2024-03-26 11:23:38 +00:00
response == "Purchasing not available" ||
2023-11-23 13:10:59 +00:00
response == "Expiry required" ||
response == "Expiry needs to be in future" ||
response == "Expiry has to be before the request's end (now + duration)")
2023-09-20 06:45:55 +00:00
{
throw new InvalidOperationException(response);
}
Log($"Storage requested successfully. PurchaseId: '{response}'.");
2024-08-20 09:44:15 +00:00
var contract = new StoragePurchaseContract(log, codexAccess, response, purchase, hooks);
contract.WaitForStorageContractSubmitted();
2024-08-20 09:44:15 +00:00
hooks.OnStorageContractSubmitted(contract);
return contract;
2023-09-20 06:45:55 +00:00
}
2024-03-20 10:11:41 +00:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-09-20 06:45:55 +00:00
{
2024-03-20 10:11:41 +00:00
availability.Log(log);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
var response = codexAccess.SalesAvailability(availability);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
Log($"Storage successfully made available. Id: {response.Id}");
2024-08-20 09:44:15 +00:00
hooks.OnStorageAvailabilityCreated(response);
2023-09-20 06:45:55 +00:00
2024-03-26 11:23:38 +00:00
return response.Id;
2023-09-20 06:45:55 +00:00
}
2024-09-23 08:52:12 +00:00
public StorageAvailability[] GetAvailabilities()
{
var result = codexAccess.GetAvailabilities();
Log($"Got {result.Length} availabilities:");
foreach (var a in result) a.Log(log);
return result;
}
2023-09-20 06:45:55 +00:00
private void Log(string msg)
{
log.Log($"{codexAccess.Container.Containers.Single().Name} {msg}");
2023-09-20 06:45:55 +00:00
}
}
public class MarketplaceUnavailable : IMarketplaceAccess
{
2024-03-20 10:11:41 +00:00
public string MakeStorageAvailable(StorageAvailability availability)
2023-11-23 13:10:59 +00:00
{
Unavailable();
throw new NotImplementedException();
}
2024-05-30 09:33:16 +00:00
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
2023-09-20 06:45:55 +00:00
{
Unavailable();
2024-03-20 10:11:41 +00:00
throw new NotImplementedException();
2023-09-20 06:45:55 +00:00
}
2024-09-23 08:52:12 +00:00
public StorageAvailability[] GetAvailabilities()
{
Unavailable();
throw new NotImplementedException();
}
2023-09-20 06:45:55 +00:00
private void Unavailable()
{
2023-09-20 08:51:47 +00:00
FrameworkAssert.Fail("Incorrect test setup: Marketplace was not enabled for this group of Codex nodes. Add 'EnableMarketplace(...)' after 'SetupCodexNodes()' to enable it.");
2023-09-20 06:45:55 +00:00
throw new InvalidOperationException();
}
}
}