attempt to fix basiccid encodedcid mixup in filesaver

This commit is contained in:
ThatBen 2025-04-02 15:46:51 +02:00
parent 82211cae1a
commit c12132d408
No known key found for this signature in database
GPG Key ID: E020A7DDCD52E1AB
3 changed files with 98 additions and 2 deletions

View File

@ -10,7 +10,7 @@ namespace CodexClient
ContentId = cid;
}
public ContentId ContentId { get; set; }
public ContentId ContentId { get; }
public TestToken PricePerBytePerSecond { get; set; } = 1.TstWei();
public TestToken CollateralPerByte { get; set; } = 1.TstWei();
public uint MinRequiredNumberOfNodes { get; set; }

View File

@ -0,0 +1,47 @@
using System.Security.Cryptography;
using CodexReleaseTests.MarketTests;
using Nethereum.JsonRpc.Client;
using NUnit.Framework;
using Utils;
namespace CodexReleaseTests.DataTests
{
public class DecodeTest : MarketplaceAutoBootstrapDistTest
{
protected override int NumberOfHosts => 0;
protected override int NumberOfClients => 2;
protected override ByteSize HostAvailabilitySize => 0.Bytes();
protected override TimeSpan HostAvailabilityMaxDuration => TimeSpan.FromSeconds(0.0);
[Test]
public void DecodeDataset()
{
var clients = StartClients();
var file = GenerateTestFile(10.MB());
var bCid = clients[0].UploadFile(file);
var request = clients[0].Marketplace.RequestStorage(new CodexClient.StoragePurchaseRequest(bCid)
{
Expiry = TimeSpan.FromMinutes(5.0),
Duration = TimeSpan.FromMinutes(100.0),
CollateralPerByte = 100.Tst(),
MinRequiredNumberOfNodes = 6,
NodeFailureTolerance = 3,
PricePerBytePerSecond = 100.Tst(),
ProofProbability = 20
});
var eCid = request.ContentId;
Assert.That(bCid.Id, Is.Not.EqualTo(eCid.Id));
var basic = clients[0].DownloadManifestOnly(bCid);
var encoded = clients[0].DownloadManifestOnly(eCid);
Assert.That(basic.Manifest.Protected, Is.False);
Assert.That(encoded.Manifest.Protected, Is.True);
var decoded = clients[1].DownloadContent(eCid);
file.AssertIsEqual(decoded);
}
}
}

View File

@ -114,7 +114,16 @@ namespace AutoClient.Modes.FolderStore
if (isFound)
{
Log("BasicCid found in local files.");
return true;
if (IsCidBasic(entry.BasicCid))
{
Log("BasicCid is confirmed to not be encoded.");
return true;
}
entry.BasicCid = string.Empty;
Log("Warning: Cid stored as basic was actually encoded!");
return false;
}
}
catch (Exception exc)
@ -181,6 +190,13 @@ namespace AutoClient.Modes.FolderStore
entry.EncodedCid = request.Purchase.ContentId.Id;
entry.PurchaseId = request.PurchaseId;
entry.PurchaseFinishedUtc = DateTime.UtcNow + request.Purchase.Duration;
if (!IsCidEncoded(entry.EncodedCid))
{
log.Error("CID received from storage request is not protected/encoded.");
throw new Exception("CID received from storage request was not protected.");
}
saveChanges();
Log("Saved new purchaseId: " + entry.PurchaseId);
return request;
@ -257,6 +273,39 @@ namespace AutoClient.Modes.FolderStore
}
}
private bool IsCidEncoded(string cid)
{
try
{
return GetManifestIsProtected(cid);
}
catch (Exception ex)
{
log.Error(nameof(IsCidEncoded) + ": " + ex);
return false;
}
}
private bool IsCidBasic(string cid)
{
try
{
return !GetManifestIsProtected(cid);
}
catch (Exception ex)
{
log.Error(nameof(IsCidBasic) + ": " + ex);
return false;
}
}
private bool GetManifestIsProtected(string cid)
{
var id = new ContentId(cid);
var manifest = instance.Node.DownloadManifestOnly(id);
return manifest.Manifest.Protected;
}
private void Log(string msg)
{
log.Log(msg);