cleans up waiting for storage contract state helper methods.
This commit is contained in:
parent
9a7946c863
commit
8d229f1720
|
@ -60,40 +60,5 @@
|
|||
// file.AssertIsEqual(result);
|
||||
// });
|
||||
// }
|
||||
|
||||
// private void WaitForContractToStart(CodexAccess codexAccess, string purchaseId)
|
||||
// {
|
||||
// var lastState = "";
|
||||
// var waitStart = DateTime.UtcNow;
|
||||
// var filesizeInMb = fileSize.SizeInBytes / (1024 * 1024);
|
||||
// var maxWaitTime = TimeSpan.FromSeconds(filesizeInMb * 10.0);
|
||||
|
||||
// Log.Log($"{nameof(WaitForContractToStart)} for {Time.FormatDuration(maxWaitTime)}");
|
||||
// while (lastState != "started")
|
||||
// {
|
||||
// CancelToken.ThrowIfCancellationRequested();
|
||||
|
||||
// var purchaseStatus = codexAccess.Node.GetPurchaseStatus(purchaseId);
|
||||
// var statusJson = JsonConvert.SerializeObject(purchaseStatus);
|
||||
// if (purchaseStatus != null && purchaseStatus.state != lastState)
|
||||
// {
|
||||
// lastState = purchaseStatus.state;
|
||||
// Log.Log("Purchase status: " + statusJson);
|
||||
// }
|
||||
|
||||
// Thread.Sleep(2000);
|
||||
|
||||
// if (lastState == "errored")
|
||||
// {
|
||||
// Assert.Fail("Contract start failed: " + statusJson);
|
||||
// }
|
||||
|
||||
// if (DateTime.UtcNow - waitStart > maxWaitTime)
|
||||
// {
|
||||
// Assert.Fail($"Contract was not picked up within {maxWaitTime.TotalSeconds} seconds timeout: {statusJson}");
|
||||
// }
|
||||
// }
|
||||
// Log.Log("Contract started.");
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using DistTestCore.Codex;
|
||||
using DistTestCore.Helpers;
|
||||
using Logging;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Constraints;
|
||||
using System.Numerics;
|
||||
|
@ -10,9 +12,8 @@ namespace DistTestCore.Marketplace
|
|||
public interface IMarketplaceAccess
|
||||
{
|
||||
string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan maxDuration);
|
||||
string RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration);
|
||||
StoragePurchaseContract RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration);
|
||||
void AssertThatBalance(IResolveConstraint constraint, string message = "");
|
||||
CodexStoragePurchase GetPurchaseStatus(string purchaseId);
|
||||
TestToken GetBalance();
|
||||
}
|
||||
|
||||
|
@ -31,7 +32,7 @@ namespace DistTestCore.Marketplace
|
|||
this.codexAccess = codexAccess;
|
||||
}
|
||||
|
||||
public string RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
|
||||
public StoragePurchaseContract RequestStorage(ContentId contentId, TestToken pricePerSlotPerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
|
||||
{
|
||||
var request = new CodexSalesRequestStorageRequest
|
||||
{
|
||||
|
@ -58,9 +59,9 @@ namespace DistTestCore.Marketplace
|
|||
throw new InvalidOperationException(response);
|
||||
}
|
||||
|
||||
Log($"Storage requested successfully. PurchaseId: {response}");
|
||||
Log($"Storage requested successfully. PurchaseId: '{response}'.");
|
||||
|
||||
return response;
|
||||
return new StoragePurchaseContract(lifecycle.Log, codexAccess, response, duration);
|
||||
}
|
||||
|
||||
public string MakeStorageAvailable(ByteSize totalSpace, TestToken minPriceForTotalSpace, TestToken maxCollateral, TimeSpan maxDuration)
|
||||
|
@ -103,11 +104,6 @@ namespace DistTestCore.Marketplace
|
|||
AssertHelpers.RetryAssert(constraint, GetBalance, message);
|
||||
}
|
||||
|
||||
public CodexStoragePurchase GetPurchaseStatus(string purchaseId)
|
||||
{
|
||||
return codexAccess.GetPurchaseStatus(purchaseId);
|
||||
}
|
||||
|
||||
public TestToken GetBalance()
|
||||
{
|
||||
var interaction = marketplaceNetwork.StartInteraction(lifecycle);
|
||||
|
@ -127,10 +123,10 @@ namespace DistTestCore.Marketplace
|
|||
|
||||
public class MarketplaceUnavailable : IMarketplaceAccess
|
||||
{
|
||||
public string RequestStorage(ContentId contentId, TestToken pricePerBytePerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
|
||||
public StoragePurchaseContract RequestStorage(ContentId contentId, TestToken pricePerBytePerSecond, TestToken requiredCollateral, uint minRequiredNumberOfNodes, int proofProbability, TimeSpan duration)
|
||||
{
|
||||
Unavailable();
|
||||
return string.Empty;
|
||||
return null!;
|
||||
}
|
||||
|
||||
public string MakeStorageAvailable(ByteSize size, TestToken minPricePerBytePerSecond, TestToken maxCollateral, TimeSpan duration)
|
||||
|
@ -144,12 +140,6 @@ namespace DistTestCore.Marketplace
|
|||
Unavailable();
|
||||
}
|
||||
|
||||
public CodexStoragePurchase GetPurchaseStatus(string purchaseId)
|
||||
{
|
||||
Unavailable();
|
||||
return new CodexStoragePurchase();
|
||||
}
|
||||
|
||||
public TestToken GetBalance()
|
||||
{
|
||||
Unavailable();
|
||||
|
@ -162,4 +152,92 @@ namespace DistTestCore.Marketplace
|
|||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public class StoragePurchaseContract
|
||||
{
|
||||
private readonly BaseLog log;
|
||||
private readonly CodexAccess codexAccess;
|
||||
private DateTime? contractStartUtc;
|
||||
|
||||
public StoragePurchaseContract(BaseLog log, CodexAccess codexAccess, string purchaseId, TimeSpan contractDuration)
|
||||
{
|
||||
this.log = log;
|
||||
this.codexAccess = codexAccess;
|
||||
PurchaseId = purchaseId;
|
||||
ContractDuration = contractDuration;
|
||||
}
|
||||
|
||||
public string PurchaseId { get; }
|
||||
public TimeSpan ContractDuration { get; }
|
||||
|
||||
public void WaitForStorageContractStarted()
|
||||
{
|
||||
WaitForStorageContractStarted(TimeSpan.FromSeconds(30));
|
||||
}
|
||||
|
||||
public void WaitForStorageContractFinished()
|
||||
{
|
||||
if (!contractStartUtc.HasValue)
|
||||
{
|
||||
WaitForStorageContractStarted();
|
||||
}
|
||||
var gracePeriod = TimeSpan.FromSeconds(10);
|
||||
var currentContractTime = DateTime.UtcNow - contractStartUtc!.Value;
|
||||
var timeout = (ContractDuration - currentContractTime) + gracePeriod;
|
||||
WaitForStorageContractState(timeout, "finished");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wait for contract to start. Max timeout depends on contract filesize. Allows more time for larger files.
|
||||
/// </summary>
|
||||
public void WaitForStorageContractStarted(ByteSize contractFileSize)
|
||||
{
|
||||
var filesizeInMb = contractFileSize.SizeInBytes / (1024 * 1024);
|
||||
var maxWaitTime = TimeSpan.FromSeconds(filesizeInMb * 10.0);
|
||||
|
||||
WaitForStorageContractStarted(maxWaitTime);
|
||||
}
|
||||
|
||||
public void WaitForStorageContractStarted(TimeSpan timeout)
|
||||
{
|
||||
WaitForStorageContractState(timeout, "started");
|
||||
contractStartUtc = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
private void WaitForStorageContractState(TimeSpan timeout, string desiredState)
|
||||
{
|
||||
var lastState = "";
|
||||
var waitStart = DateTime.UtcNow;
|
||||
|
||||
log.Log($"Waiting for {Time.FormatDuration(timeout)} for contract '{PurchaseId}' to reach state '{desiredState}'.");
|
||||
while (lastState != desiredState)
|
||||
{
|
||||
var purchaseStatus = codexAccess.GetPurchaseStatus(PurchaseId);
|
||||
var statusJson = JsonConvert.SerializeObject(purchaseStatus);
|
||||
if (purchaseStatus != null && purchaseStatus.state != lastState)
|
||||
{
|
||||
lastState = purchaseStatus.state;
|
||||
log.Debug("Purchase status: " + statusJson);
|
||||
}
|
||||
|
||||
Thread.Sleep(1000);
|
||||
|
||||
if (lastState == "errored")
|
||||
{
|
||||
Assert.Fail("Contract errored: " + statusJson);
|
||||
}
|
||||
|
||||
if (DateTime.UtcNow - waitStart > timeout)
|
||||
{
|
||||
Assert.Fail($"Contract did not reach '{desiredState}' within timeout. {statusJson}");
|
||||
}
|
||||
}
|
||||
log.Log($"Contract '{desiredState}'.");
|
||||
}
|
||||
|
||||
public CodexStoragePurchase GetPurchaseStatus(string purchaseId)
|
||||
{
|
||||
return codexAccess.GetPurchaseStatus(purchaseId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ namespace Tests.BasicTests
|
|||
{
|
||||
var sellerInitialBalance = 234.TestTokens();
|
||||
var buyerInitialBalance = 1000.TestTokens();
|
||||
var fileSize = 10.MB();
|
||||
|
||||
var seller = SetupCodexNode(s => s
|
||||
.WithStorageQuota(11.GB())
|
||||
|
@ -59,7 +60,7 @@ namespace Tests.BasicTests
|
|||
maxCollateral: 20.TestTokens(),
|
||||
maxDuration: TimeSpan.FromMinutes(3));
|
||||
|
||||
var testFile = GenerateTestFile(10.MB());
|
||||
var testFile = GenerateTestFile(fileSize);
|
||||
|
||||
var buyer = SetupCodexNode(s => s
|
||||
.WithBootstrapNode(seller)
|
||||
|
@ -68,55 +69,21 @@ namespace Tests.BasicTests
|
|||
buyer.Marketplace.AssertThatBalance(Is.EqualTo(buyerInitialBalance));
|
||||
|
||||
var contentId = buyer.UploadFile(testFile);
|
||||
var purchaseId = buyer.Marketplace.RequestStorage(contentId,
|
||||
var purchaseContract = buyer.Marketplace.RequestStorage(contentId,
|
||||
pricePerSlotPerSecond: 2.TestTokens(),
|
||||
requiredCollateral: 10.TestTokens(),
|
||||
minRequiredNumberOfNodes: 1,
|
||||
proofProbability: 5,
|
||||
duration: TimeSpan.FromMinutes(1));
|
||||
|
||||
|
||||
WaitForContractToStart(buyer.Marketplace, 10.MB(), purchaseId);
|
||||
purchaseContract.WaitForStorageContractStarted(fileSize);
|
||||
|
||||
seller.Marketplace.AssertThatBalance(Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
|
||||
|
||||
Time.Sleep(TimeSpan.FromMinutes(2));
|
||||
purchaseContract.WaitForStorageContractFinished();
|
||||
|
||||
seller.Marketplace.AssertThatBalance(Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");
|
||||
buyer.Marketplace.AssertThatBalance(Is.LessThan(buyerInitialBalance), "Buyer was not charged for storage.");
|
||||
}
|
||||
|
||||
private void WaitForContractToStart(IMarketplaceAccess access, ByteSize fileSize, string purchaseId)
|
||||
{
|
||||
var lastState = "";
|
||||
var waitStart = DateTime.UtcNow;
|
||||
var filesizeInMb = fileSize.SizeInBytes / (1024 * 1024);
|
||||
var maxWaitTime = TimeSpan.FromSeconds(filesizeInMb * 10.0);
|
||||
|
||||
Log($"{nameof(WaitForContractToStart)} for {Time.FormatDuration(maxWaitTime)}");
|
||||
while (lastState != "started")
|
||||
{
|
||||
var purchaseStatus = access.GetPurchaseStatus(purchaseId);
|
||||
var statusJson = JsonConvert.SerializeObject(purchaseStatus);
|
||||
if (purchaseStatus != null && purchaseStatus.state != lastState)
|
||||
{
|
||||
lastState = purchaseStatus.state;
|
||||
Log("Purchase status: " + statusJson);
|
||||
}
|
||||
|
||||
Thread.Sleep(2000);
|
||||
|
||||
if (lastState == "errored")
|
||||
{
|
||||
Assert.Fail("Contract start failed: " + statusJson);
|
||||
}
|
||||
|
||||
if (DateTime.UtcNow - waitStart > maxWaitTime)
|
||||
{
|
||||
Assert.Fail($"Contract was not picked up within {maxWaitTime.TotalSeconds} seconds timeout: {statusJson}");
|
||||
}
|
||||
}
|
||||
Log("Contract started.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue