Skip to content

Commit f344fac

Browse files
committed
Applies purchase-paramters type to ensure slot sizes
1 parent d3642ff commit f344fac

File tree

7 files changed

+115
-48
lines changed

7 files changed

+115
-48
lines changed

Framework/Utils/ByteSize.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ public ByteSize Multiply(double factor)
2525
return new ByteSize(Convert.ToInt64(result));
2626
}
2727

28+
public int DivUp(ByteSize div)
29+
{
30+
var d = div.SizeInBytes;
31+
var remaining = SizeInBytes;
32+
var result = 0;
33+
while (remaining > d)
34+
{
35+
remaining -= d;
36+
result++;
37+
}
38+
39+
if (remaining > 0) result++;
40+
return result;
41+
}
42+
2843
public override bool Equals(object? obj)
2944
{
3045
return obj is ByteSize size && SizeInBytes == size.SizeInBytes;

ProjectPlugins/CodexPlugin/CodexDockerImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public class CodexDockerImage
44
{
5-
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-28a83db-dist-tests";
5+
private const string DefaultDockerImage = "codexstorage/nim-codex:0.2.3-dist-tests";
66

77
public static string Override { get; set; } = string.Empty;
88

Tests/CodexReleaseTests/MarketTests/FinishTest.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ public class FinishTest : MarketplaceAutoBootstrapDistTest
1212
public FinishTest(int hosts, int slots, int tolerance)
1313
{
1414
this.hosts = hosts;
15-
this.slots = slots;
16-
this.tolerance = tolerance;
15+
purchaseParams = new PurchaseParams(slots, tolerance, uploadFilesize: 10.MB());
1716
}
1817

19-
private const int FilesizeMb = 10;
2018
private readonly TestToken pricePerBytePerSecond = 10.TstWei();
2119
private readonly int hosts;
22-
private readonly int slots;
23-
private readonly int tolerance;
20+
private readonly PurchaseParams purchaseParams;
2421

2522
protected override int NumberOfHosts => hosts;
2623
protected override int NumberOfClients => 1;
27-
protected override ByteSize HostAvailabilitySize => (5 * FilesizeMb).MB();
24+
protected override ByteSize HostAvailabilitySize => purchaseParams.SlotSize.Multiply(5.1);
2825
protected override TimeSpan HostAvailabilityMaxDuration => Get8TimesConfiguredPeriodDuration() * 12;
2926

3027
[Test]
@@ -53,14 +50,14 @@ public void Finish(
5350

5451
private IStoragePurchaseContract CreateStorageRequest(ICodexNode client)
5552
{
56-
var cid = client.UploadFile(GenerateTestFile(FilesizeMb.MB()));
53+
var cid = client.UploadFile(GenerateTestFile(purchaseParams.UploadFilesize));
5754
var config = GetContracts().Deployment.Config;
5855
return client.Marketplace.RequestStorage(new StoragePurchaseRequest(cid)
5956
{
6057
Duration = GetContractDuration(),
6158
Expiry = GetContractExpiry(),
62-
MinRequiredNumberOfNodes = (uint)slots,
63-
NodeFailureTolerance = (uint)tolerance,
59+
MinRequiredNumberOfNodes = (uint)purchaseParams.Nodes,
60+
NodeFailureTolerance = (uint)purchaseParams.Tolerance,
6461
PricePerBytePerSecond = pricePerBytePerSecond,
6562
ProofProbability = 20,
6663
CollateralPerByte = 100.TstWei()

Tests/CodexReleaseTests/MarketTests/RepairTest.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,22 @@ public class RepairTest : MarketplaceAutoBootstrapDistTest
1111
{
1212
#region Setup
1313

14-
private readonly ByteSize Filesize;
15-
private readonly uint Slots;
16-
private readonly uint Tolerance;
17-
private readonly ByteSize EncodedFilesize;
18-
private readonly ByteSize SlotSize;
14+
private readonly PurchaseParams purchaseParams = new PurchaseParams(
15+
nodes: 4,
16+
tolerance: 2,
17+
uploadFilesize: 32.MB()
18+
);
1919

2020
public RepairTest()
2121
{
22-
Filesize = 32.MB();
23-
Slots = 4;
24-
Tolerance = 2;
25-
26-
EncodedFilesize = new ByteSize(Filesize.SizeInBytes * (Slots / Tolerance));
27-
SlotSize = new ByteSize(EncodedFilesize.SizeInBytes / Slots);
28-
Assert.That(IsPowerOfTwo(SlotSize));
29-
Assert.That(Slots, Is.LessThan(NumberOfHosts));
22+
Assert.That(purchaseParams.Nodes, Is.LessThan(NumberOfHosts));
3023
}
3124

3225
protected override int NumberOfHosts => 5;
3326
protected override int NumberOfClients => 1;
34-
protected override ByteSize HostAvailabilitySize => SlotSize.Multiply(1.1); // Each host can hold 1 slot.
27+
protected override ByteSize HostAvailabilitySize => purchaseParams.SlotSize.Multiply(1.1); // Each host can hold 1 slot.
3528
protected override TimeSpan HostAvailabilityMaxDuration => TimeSpan.FromDays(5.0);
3629

37-
private static bool IsPowerOfTwo(ByteSize size)
38-
{
39-
var x = size.SizeInBytes;
40-
return (x != 0) && ((x & (x - 1)) == 0);
41-
}
42-
4330
#endregion
4431

4532
[Ignore("Test is ready. Waiting for repair implementation. " +
@@ -173,14 +160,14 @@ private SlotFill GetSlotFillByOldestHost(List<ICodexNode> hosts)
173160

174161
private IStoragePurchaseContract CreateStorageRequest(ICodexNode client)
175162
{
176-
var cid = client.UploadFile(GenerateTestFile(Filesize));
163+
var cid = client.UploadFile(GenerateTestFile(purchaseParams.UploadFilesize));
177164
var config = GetContracts().Deployment.Config;
178165
return client.Marketplace.RequestStorage(new StoragePurchaseRequest(cid)
179166
{
180167
Duration = HostAvailabilityMaxDuration / 2,
181168
Expiry = TimeSpan.FromMinutes(10.0),
182-
MinRequiredNumberOfNodes = Slots,
183-
NodeFailureTolerance = Tolerance,
169+
MinRequiredNumberOfNodes = (uint)purchaseParams.Nodes,
170+
NodeFailureTolerance = (uint)purchaseParams.Tolerance,
184171
PricePerBytePerSecond = 10.TstWei(),
185172
ProofProbability = 1, // One proof every period. Free slot as quickly as possible.
186173
CollateralPerByte = 1.TstWei()

Tests/CodexReleaseTests/MarketTests/SequentialContracts.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ public class SequentialContracts : MarketplaceAutoBootstrapDistTest
1212
public SequentialContracts(int hosts, int slots, int tolerance)
1313
{
1414
this.hosts = hosts;
15-
this.slots = slots;
16-
this.tolerance = tolerance;
15+
purchaseParams = new PurchaseParams(slots, tolerance, 10.MB());
1716
}
1817

19-
private const int FilesizeMb = 10;
2018
private readonly int hosts;
21-
private readonly int slots;
22-
private readonly int tolerance;
19+
private readonly PurchaseParams purchaseParams;
2320

2421
protected override int NumberOfHosts => hosts;
2522
protected override int NumberOfClients => 8;
26-
protected override ByteSize HostAvailabilitySize => (1000 * FilesizeMb).MB();
23+
protected override ByteSize HostAvailabilitySize => purchaseParams.SlotSize.Multiply(100.0);
2724
protected override TimeSpan HostAvailabilityMaxDuration => Get8TimesConfiguredPeriodDuration() * 12;
2825
private readonly TestToken pricePerBytePerSecond = 10.TstWei();
2926

@@ -80,14 +77,14 @@ private TResult[] All<T, TResult>(T[] items, Func<T, TResult> action)
8077

8178
private IStoragePurchaseContract CreateStorageRequest(ICodexNode client)
8279
{
83-
var cid = client.UploadFile(GenerateTestFile(FilesizeMb.MB()));
80+
var cid = client.UploadFile(GenerateTestFile(purchaseParams.UploadFilesize));
8481
var config = GetContracts().Deployment.Config;
8582
return client.Marketplace.RequestStorage(new StoragePurchaseRequest(cid)
8683
{
8784
Duration = GetContractDuration(),
8885
Expiry = GetContractExpiry(),
89-
MinRequiredNumberOfNodes = (uint)slots,
90-
NodeFailureTolerance = (uint)tolerance,
86+
MinRequiredNumberOfNodes = (uint)purchaseParams.Nodes,
87+
NodeFailureTolerance = (uint)purchaseParams.Tolerance,
9188
PricePerBytePerSecond = pricePerBytePerSecond,
9289
ProofProbability = 10000,
9390
CollateralPerByte = 1.TstWei()
@@ -107,7 +104,7 @@ private TimeSpan GetContractDuration()
107104
private TimeSpan Get8TimesConfiguredPeriodDuration()
108105
{
109106
var config = GetContracts().Deployment.Config;
110-
return TimeSpan.FromSeconds(((double)config.Proofs.Period) * 8.0);
107+
return TimeSpan.FromSeconds(config.Proofs.Period * 8.0);
111108
}
112109
}
113110
}

Tests/CodexReleaseTests/MarketTests/StartTest.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ namespace CodexReleaseTests.MarketTests
88
[TestFixture]
99
public class StartTest : MarketplaceAutoBootstrapDistTest
1010
{
11-
private const int FilesizeMb = 10;
11+
private readonly PurchaseParams purchaseParams = new PurchaseParams(
12+
nodes: 3,
13+
tolerance: 1,
14+
uploadFilesize: 10.MB()
15+
);
1216
private readonly TestToken pricePerBytePerSecond = 10.TstWei();
1317

1418
protected override int NumberOfHosts => 5;
1519
protected override int NumberOfClients => 1;
16-
protected override ByteSize HostAvailabilitySize => (5 * FilesizeMb).MB();
20+
protected override ByteSize HostAvailabilitySize => purchaseParams.SlotSize.Multiply(10.0);
1721
protected override TimeSpan HostAvailabilityMaxDuration => Get8TimesConfiguredPeriodDuration() * 12;
1822

1923
[Test]
@@ -36,14 +40,14 @@ public void Start(
3640

3741
private IStoragePurchaseContract CreateStorageRequest(ICodexNode client)
3842
{
39-
var cid = client.UploadFile(GenerateTestFile(FilesizeMb.MB()));
43+
var cid = client.UploadFile(GenerateTestFile(purchaseParams.UploadFilesize));
4044
var config = GetContracts().Deployment.Config;
4145
return client.Marketplace.RequestStorage(new StoragePurchaseRequest(cid)
4246
{
4347
Duration = GetContractDuration(),
4448
Expiry = GetContractExpiry(),
45-
MinRequiredNumberOfNodes = 3,
46-
NodeFailureTolerance = 1,
49+
MinRequiredNumberOfNodes = (uint)purchaseParams.Nodes,
50+
NodeFailureTolerance = (uint)purchaseParams.Tolerance,
4751
PricePerBytePerSecond = pricePerBytePerSecond,
4852
ProofProbability = 20,
4953
CollateralPerByte = 100.TstWei()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using NUnit.Framework;
2+
using Utils;
3+
4+
namespace CodexReleaseTests.Utils
5+
{
6+
public class PurchaseParams
7+
{
8+
private readonly ByteSize blockSize = 64.KB();
9+
10+
public PurchaseParams(int nodes, int tolerance, ByteSize uploadFilesize)
11+
{
12+
Nodes = nodes;
13+
Tolerance = tolerance;
14+
UploadFilesize = uploadFilesize;
15+
16+
EncodedDatasetSize = CalculateEncodedDatasetSize();
17+
SlotSize = CalculateSlotSize();
18+
19+
Assert.That(IsPowerOfTwo(SlotSize));
20+
}
21+
22+
public int Nodes { get; }
23+
public int Tolerance { get; }
24+
public ByteSize UploadFilesize { get; }
25+
public ByteSize EncodedDatasetSize { get; }
26+
public ByteSize SlotSize { get; }
27+
28+
private ByteSize CalculateSlotSize()
29+
{
30+
// encoded dataset is divided over the nodes.
31+
// then each slot is rounded up to the nearest power-of-two blocks.
32+
var numBlocks = EncodedDatasetSize.DivUp(blockSize);
33+
var numSlotBlocks = 1 + ((numBlocks - 1) / Nodes); // round-up div.
34+
35+
// Next power of two:
36+
var numSlotBlocksPow2 = NextPowerOf2(numSlotBlocks);
37+
return new ByteSize(blockSize.SizeInBytes * numSlotBlocksPow2);
38+
}
39+
40+
private ByteSize CalculateEncodedDatasetSize()
41+
{
42+
var numBlocks = UploadFilesize.DivUp(blockSize);
43+
44+
var ecK = Nodes - Tolerance;
45+
var ecM = Tolerance;
46+
47+
// for each K blocks, we generate M parity blocks
48+
var numParityBlocks = (numBlocks / ecK) * ecM;
49+
var totalBlocks = numBlocks + numParityBlocks;
50+
51+
return new ByteSize(blockSize.SizeInBytes * totalBlocks);
52+
}
53+
54+
private int NextPowerOf2(int n)
55+
{
56+
n = n - 1;
57+
var lg = Convert.ToInt32(Math.Round(Math.Log2(Convert.ToDouble(n))));
58+
return 1 << (lg + 1);
59+
}
60+
61+
private static bool IsPowerOfTwo(ByteSize size)
62+
{
63+
var x = size.SizeInBytes;
64+
return (x != 0) && ((x & (x - 1)) == 0);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)