Skip to content

Commit 6368577

Browse files
committed
Updates to latest codex api
1 parent 582c732 commit 6368577

File tree

11 files changed

+48
-56
lines changed

11 files changed

+48
-56
lines changed

Framework/Utils/Retry.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ public class Retry
66
private readonly TimeSpan maxTimeout;
77
private readonly TimeSpan sleepAfterFail;
88
private readonly Action<Failure> onFail;
9+
private readonly bool failFast;
910

10-
public Retry(string description, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail)
11+
public Retry(string description, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail, bool failFast)
1112
{
1213
this.description = description;
1314
this.maxTimeout = maxTimeout;
1415
this.sleepAfterFail = sleepAfterFail;
1516
this.onFail = onFail;
17+
this.failFast = failFast;
1618
}
1719

1820
public void Run(Action task)
1921
{
20-
var run = new RetryRun(description, task, maxTimeout, sleepAfterFail, onFail);
22+
var run = new RetryRun(description, task, maxTimeout, sleepAfterFail, onFail, failFast);
2123
run.Run();
2224
}
2325

@@ -28,7 +30,7 @@ public T Run<T>(Func<T> task)
2830
var run = new RetryRun(description, () =>
2931
{
3032
result = task();
31-
}, maxTimeout, sleepAfterFail, onFail);
33+
}, maxTimeout, sleepAfterFail, onFail, failFast);
3234
run.Run();
3335

3436
return result!;
@@ -43,16 +45,18 @@ private class RetryRun
4345
private readonly Action<Failure> onFail;
4446
private readonly DateTime start = DateTime.UtcNow;
4547
private readonly List<Failure> failures = new List<Failure>();
48+
private readonly bool failFast;
4649
private int tryNumber;
4750
private DateTime tryStart;
4851

49-
public RetryRun(string description, Action task, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail)
52+
public RetryRun(string description, Action task, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail, bool failFast)
5053
{
5154
this.description = description;
5255
this.task = task;
5356
this.maxTimeout = maxTimeout;
5457
this.sleepAfterFail = sleepAfterFail;
5558
this.onFail = onFail;
59+
this.failFast = failFast;
5660

5761
tryNumber = 0;
5862
tryStart = DateTime.UtcNow;
@@ -97,7 +101,7 @@ private void CheckMaximums()
97101

98102
// If we have a few very fast failures, retrying won't help us. There's probably something wrong with our operation.
99103
// In this case, don't wait the full duration and fail quickly.
100-
if (failures.Count > 5 && failures.All(f => f.Duration < TimeSpan.FromSeconds(1.0))) Fail();
104+
if (failFast && failures.Count > 5 && failures.All(f => f.Duration < TimeSpan.FromSeconds(1.0))) Fail();
101105
}
102106

103107
private void Fail()

Framework/Utils/Time.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ public static T Retry<T>(Func<T> action, TimeSpan maxTimeout, TimeSpan retryTime
124124

125125
public static void Retry(Action action, TimeSpan maxTimeout, TimeSpan retryTime, string description, Action<Failure> onFail)
126126
{
127-
var r = new Retry(description, maxTimeout, retryTime, onFail);
127+
var r = new Retry(description, maxTimeout, retryTime, onFail, failFast: true);
128128
r.Run(action);
129129
}
130130

131131
public static T Retry<T>(Func<T> action, TimeSpan maxTimeout, TimeSpan retryTime, string description, Action<Failure> onFail)
132132
{
133-
var r = new Retry(description, maxTimeout, retryTime, onFail);
133+
var r = new Retry(description, maxTimeout, retryTime, onFail, failFast: true);
134134
return r.Run(action);
135135
}
136136
}

Framework/WebUtils/Http.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public T OnClient<T>(Func<HttpClient, T> action)
4040

4141
public T OnClient<T>(Func<HttpClient, T> action, string description)
4242
{
43-
var retry = new Retry(description, timeSet.HttpRetryTimeout(), timeSet.HttpCallRetryDelay(), f => { });
43+
var retry = new Retry(description, timeSet.HttpRetryTimeout(), timeSet.HttpCallRetryDelay(), f => { }, failFast: true);
4444
return OnClient(action, retry);
4545
}
4646

ProjectPlugins/CodexClient/CodexAccess.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,8 @@ public CodexSpace Space()
170170

171171
public StoragePurchase? GetPurchaseStatus(string purchaseId)
172172
{
173-
return CrashCheck(() =>
174-
{
175-
var endpoint = GetEndpoint();
176-
try
177-
{
178-
return Time.Retry(() =>
179-
{
180-
var str = endpoint.HttpGetString($"storage/purchases/{purchaseId}");
181-
if (string.IsNullOrEmpty(str)) throw new Exception("Empty response.");
182-
return JsonConvert.DeserializeObject<StoragePurchase>(str)!;
183-
}, nameof(GetPurchaseStatus));
184-
}
185-
catch (Exception exc)
186-
{
187-
log.Error($"Failed to fetch purchase information for id: '{purchaseId}'. Exception: {exc.Message}");
188-
return null;
189-
}
190-
});
191-
192-
// TODO: current getpurchase api does not line up with its openapi spec.
193-
// return mapper.Map(OnCodex(api => api.GetPurchaseAsync(purchaseId)));
173+
var purchase = OnCodex(api => api.GetPurchaseAsync(purchaseId));
174+
return mapper.Map(purchase);
194175
}
195176

196177
public string GetName()

ProjectPlugins/CodexClient/CodexNode.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,9 @@ public void WaitUntilQuotaUsedIncreased(
380380

381381
var retry = new Retry($"Checking local space for quotaUsed increase of {expectedIncreaseOfQuotaUsed}",
382382
maxTimeout: maxTimeout,
383-
sleepAfterFail: TimeSpan.FromSeconds(3),
384-
onFail: f => { });
383+
sleepAfterFail: TimeSpan.FromSeconds(10),
384+
onFail: f => { },
385+
failFast: false);
385386

386387
retry.Run(() =>
387388
{

ProjectPlugins/CodexClient/Mapper.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,21 @@ public StoragePurchase Map(CodexOpenApi.Purchase purchase)
8686
return new StoragePurchase
8787
{
8888
Request = Map(purchase.Request),
89-
State = purchase.State.ToString(), //Map(purchase.State),
89+
State = Map(purchase.State),
9090
Error = purchase.Error
9191
};
9292
}
9393

9494
public StoragePurchaseState Map(CodexOpenApi.PurchaseState purchaseState)
9595
{
96-
// TODO: to be re-enabled when marketplace api lines up with openapi.yaml.
97-
9896
// Explicit mapping: If the API changes, we will get compile errors here.
9997
// That's what we want.
10098
switch (purchaseState)
10199
{
102100
case CodexOpenApi.PurchaseState.Cancelled:
103101
return StoragePurchaseState.Cancelled;
104-
case CodexOpenApi.PurchaseState.Error:
105-
return StoragePurchaseState.Error;
102+
case CodexOpenApi.PurchaseState.Errored:
103+
return StoragePurchaseState.Errored;
106104
case CodexOpenApi.PurchaseState.Failed:
107105
return StoragePurchaseState.Failed;
108106
case CodexOpenApi.PurchaseState.Finished:

ProjectPlugins/CodexClient/MarketplaceTypes.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ public void Log(ILog log)
3434

3535
public class StoragePurchase
3636
{
37-
public string State { get; set; } = string.Empty;
37+
public StoragePurchaseState State { get; set; } = StoragePurchaseState.Unknown;
3838
public string Error { get; set; } = string.Empty;
3939
public StorageRequest Request { get; set; } = null!;
4040

41-
public bool IsCancelled => State.ToLowerInvariant().Contains("cancel");
42-
public bool IsError => State.ToLowerInvariant().Contains("error");
43-
public bool IsFinished => State.ToLowerInvariant().Contains("finished");
44-
public bool IsStarted => State.ToLowerInvariant().Contains("started");
45-
public bool IsSubmitted => State.ToLowerInvariant().Contains("submitted");
41+
public bool IsCancelled => State == StoragePurchaseState.Cancelled;
42+
public bool IsError => State == StoragePurchaseState.Errored;
43+
public bool IsFinished => State == StoragePurchaseState.Finished;
44+
public bool IsStarted => State == StoragePurchaseState.Started;
45+
public bool IsSubmitted => State == StoragePurchaseState.Submitted;
4646
}
4747

4848
public enum StoragePurchaseState
4949
{
5050
Cancelled = 0,
51-
Error = 1,
51+
Errored = 1,
5252
Failed = 2,
5353
Finished = 3,
5454
Pending = 4,

ProjectPlugins/CodexClient/StoragePurchaseContract.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class StoragePurchaseContract : IStoragePurchaseContract
2727
private DateTime? contractSubmittedUtc = DateTime.UtcNow;
2828
private DateTime? contractStartedUtc;
2929
private DateTime? contractFinishedUtc;
30-
private string lastState = string.Empty;
30+
private StoragePurchaseState lastState = StoragePurchaseState.Unknown;
3131
private ContentId encodedContentId = new ContentId();
3232

3333
public StoragePurchaseContract(ILog log, CodexAccess codexAccess, string purchaseId, StoragePurchaseRequest purchase, ICodexNodeHooks hooks)
@@ -67,8 +67,8 @@ public ContentId ContentId
6767
public void WaitForStorageContractSubmitted()
6868
{
6969
var timeout = Purchase.Expiry + gracePeriod;
70-
var raiseHook = lastState != "submitted";
71-
WaitForStorageContractState(timeout, "submitted", sleep: 200);
70+
var raiseHook = lastState != StoragePurchaseState.Submitted;
71+
WaitForStorageContractState(timeout, StoragePurchaseState.Submitted, sleep: 200);
7272
contractSubmittedUtc = DateTime.UtcNow;
7373
if (raiseHook) hooks.OnStorageContractSubmitted(this);
7474
LogSubmittedDuration();
@@ -79,7 +79,7 @@ public void WaitForStorageContractStarted()
7979
{
8080
var timeout = Purchase.Expiry + gracePeriod;
8181

82-
WaitForStorageContractState(timeout, "started");
82+
WaitForStorageContractState(timeout, StoragePurchaseState.Started);
8383
contractStartedUtc = DateTime.UtcNow;
8484
LogStartedDuration();
8585
AssertDuration(SubmittedToStarted, timeout, nameof(SubmittedToStarted));
@@ -93,7 +93,7 @@ public void WaitForStorageContractFinished()
9393
}
9494
var currentContractTime = DateTime.UtcNow - contractSubmittedUtc!.Value;
9595
var timeout = (Purchase.Duration - currentContractTime) + gracePeriod;
96-
WaitForStorageContractState(timeout, "finished");
96+
WaitForStorageContractState(timeout, StoragePurchaseState.Finished);
9797
contractFinishedUtc = DateTime.UtcNow;
9898
LogFinishedDuration();
9999
AssertDuration(SubmittedToFinished, timeout, nameof(SubmittedToFinished));
@@ -107,10 +107,10 @@ public void WaitForContractFailed()
107107
}
108108
var currentContractTime = DateTime.UtcNow - contractSubmittedUtc!.Value;
109109
var timeout = (Purchase.Duration - currentContractTime) + gracePeriod;
110-
WaitForStorageContractState(timeout, "failed");
110+
WaitForStorageContractState(timeout, StoragePurchaseState.Failed);
111111
}
112112

113-
private void WaitForStorageContractState(TimeSpan timeout, string desiredState, int sleep = 1000)
113+
private void WaitForStorageContractState(TimeSpan timeout, StoragePurchaseState desiredState, int sleep = 1000)
114114
{
115115
var waitStart = DateTime.UtcNow;
116116

@@ -128,7 +128,7 @@ private void WaitForStorageContractState(TimeSpan timeout, string desiredState,
128128
hooks.OnStorageContractUpdated(purchaseStatus);
129129
}
130130

131-
if (lastState == "errored")
131+
if (lastState == StoragePurchaseState.Errored)
132132
{
133133
FrameworkAssert.Fail("Contract errored: " + statusJson);
134134
}

ProjectPlugins/CodexClient/openapi.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ components:
5050
type: string
5151
description: The amount of tokens paid per byte per second per slot to hosts the client is willing to pay
5252

53+
CollateralPerByte:
54+
type: string
55+
description: Number as decimal string that represents how much collateral per byte is asked from hosts that wants to fill a slots
56+
5357
Duration:
5458
type: integer
5559
format: int64
@@ -320,8 +324,7 @@ components:
320324
default: 1
321325
minimum: 1
322326
collateralPerByte:
323-
type: string
324-
description: Number as decimal string that represents how much collateral per byte is asked from hosts that wants to fill a slots
327+
$ref: "#/components/schemas/CollateralPerByte"
325328
expiry:
326329
type: integer
327330
format: int64
@@ -351,6 +354,8 @@ components:
351354
$ref: "#/components/schemas/ProofProbability"
352355
pricePerBytePerSecond:
353356
$ref: "#/components/schemas/PricePerBytePerSecond"
357+
collateralPerByte:
358+
$ref: "#/components/schemas/CollateralPerByte"
354359
maxSlotLoss:
355360
type: integer
356361
format: int64
@@ -392,7 +397,7 @@ components:
392397
description: Description of the Request's state
393398
enum:
394399
- cancelled
395-
- error
400+
- errored
396401
- failed
397402
- finished
398403
- pending
@@ -586,6 +591,8 @@ paths:
586591
text/plain:
587592
schema:
588593
type: string
594+
"422":
595+
description: The mimetype of the filename is invalid
589596
"500":
590597
description: Well it was bad-bad and the upload did not work out
591598

ProjectPlugins/CodexPlugin/ApiChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace CodexPlugin
1010
public class ApiChecker
1111
{
1212
// <INSERT-OPENAPI-YAML-HASH>
13-
private const string OpenApiYamlHash = "EE-A5-6C-F9-F2-81-21-63-AB-F0-8D-63-0C-30-E8-55-F0-CC-7A-B0-69-6E-7F-77-C1-88-B0-31-F3-64-40-1A";
13+
private const string OpenApiYamlHash = "1A-F7-DF-C3-E1-C6-98-FF-32-20-21-9B-26-40-B0-51-08-35-C2-E7-DB-41-49-93-60-A9-CE-47-B5-AD-3D-A3";
1414
private const string OpenApiFilePath = "/codex/openapi.yaml";
1515
private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK";
1616

Tests/CodexReleaseTests/MarketTests/MarketplaceAutoBootstrapDistTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ private Retry GetBalanceAssertRetry()
118118
return new Retry("AssertBalance",
119119
maxTimeout: TimeSpan.FromMinutes(10.0),
120120
sleepAfterFail: TimeSpan.FromSeconds(10.0),
121-
onFail: f => { });
121+
onFail: f => { },
122+
failFast: false);
122123
}
123124

124125
private TestToken GetTstBalance(ICodexNode node)

0 commit comments

Comments
 (0)