285 lines
9.0 KiB
C#
Raw Normal View History

2025-04-03 09:15:39 +02:00
using Newtonsoft.Json.Linq;
2024-03-26 11:39:59 +01:00
using System.Numerics;
2024-03-26 14:07:06 +01:00
using Utils;
2024-03-26 08:58:16 +01:00
2025-01-16 11:31:50 +01:00
namespace CodexClient
2024-03-26 08:58:16 +01:00
{
public class Mapper
{
public DebugInfo Map(CodexOpenApi.DebugInfo debugInfo)
{
return new DebugInfo
{
Id = debugInfo.Id,
Spr = debugInfo.Spr,
Addrs = debugInfo.Addrs.ToArray(),
2025-01-08 11:52:48 +01:00
AnnounceAddresses = debugInfo.AnnounceAddresses.ToArray(),
2024-10-29 14:02:00 +01:00
Version = Map(debugInfo.Codex),
Table = Map(debugInfo.Table)
2024-03-26 08:58:16 +01:00
};
}
2024-03-26 10:03:52 +01:00
2024-03-26 14:07:06 +01:00
public LocalDatasetList Map(CodexOpenApi.DataList dataList)
2024-03-26 11:39:59 +01:00
{
2024-03-26 14:07:06 +01:00
return new LocalDatasetList
{
Content = dataList.Content.Select(Map).ToArray()
};
}
public LocalDataset Map(CodexOpenApi.DataItem dataItem)
{
return new LocalDataset
{
Cid = new ContentId(dataItem.Cid),
Manifest = MapManifest(dataItem.Manifest)
};
2024-03-26 11:39:59 +01:00
}
2025-04-03 09:15:39 +02:00
public CodexOpenApi.SalesAvailability Map(StorageAvailability availability)
2024-03-26 11:39:59 +01:00
{
2025-04-03 09:15:39 +02:00
return new CodexOpenApi.SalesAvailability
2024-03-26 11:39:59 +01:00
{
2025-04-03 09:15:39 +02:00
Duration = ToLong(availability.MaxDuration.TotalSeconds),
2025-01-25 14:07:15 +01:00
MinPricePerBytePerSecond = ToDecInt(availability.MinPricePerBytePerSecond),
TotalCollateral = ToDecInt(availability.TotalCollateral),
2025-04-03 09:15:39 +02:00
TotalSize = availability.TotalSpace.SizeInBytes
2024-03-26 11:39:59 +01:00
};
}
public CodexOpenApi.StorageRequestCreation Map(StoragePurchaseRequest purchase)
{
return new CodexOpenApi.StorageRequestCreation
{
2025-04-03 09:15:39 +02:00
Duration = ToLong(purchase.Duration.TotalSeconds),
2024-03-26 11:39:59 +01:00
ProofProbability = ToDecInt(purchase.ProofProbability),
2025-01-25 14:07:15 +01:00
PricePerBytePerSecond = ToDecInt(purchase.PricePerBytePerSecond),
CollateralPerByte = ToDecInt(purchase.CollateralPerByte),
2025-04-03 09:15:39 +02:00
Expiry = ToLong(purchase.Expiry.TotalSeconds),
Nodes = Convert.ToInt32(purchase.MinRequiredNumberOfNodes),
Tolerance = Convert.ToInt32(purchase.NodeFailureTolerance)
2024-03-26 11:39:59 +01:00
};
}
2025-01-16 11:31:50 +01:00
public StorageAvailability[] Map(ICollection<CodexOpenApi.SalesAvailabilityREAD> availabilities)
2024-09-23 10:52:12 +02:00
{
return availabilities.Select(a => Map(a)).ToArray();
}
2025-01-16 11:31:50 +01:00
public StorageAvailability Map(CodexOpenApi.SalesAvailabilityREAD availability)
2024-09-23 10:52:12 +02:00
{
return new StorageAvailability
(
ToByteSize(availability.TotalSize),
ToTimespan(availability.Duration),
2025-04-03 09:15:39 +02:00
new TestToken(ToBigInt(availability.MinPricePerBytePerSecond)),
new TestToken(ToBigInt(availability.TotalCollateral))
2024-09-23 10:52:12 +02:00
)
{
Id = availability.Id,
2024-09-24 15:21:33 +02:00
FreeSpace = ToByteSize(availability.FreeSize),
2024-09-23 10:52:12 +02:00
};
}
public StoragePurchase Map(CodexOpenApi.Purchase purchase)
{
return new StoragePurchase
{
Request = Map(purchase.Request),
2025-04-08 13:07:55 +02:00
State = Map(purchase.State),
Error = purchase.Error
};
}
2025-04-03 09:15:39 +02:00
public StoragePurchaseState Map(CodexOpenApi.PurchaseState purchaseState)
{
// Explicit mapping: If the API changes, we will get compile errors here.
// That's what we want.
switch (purchaseState)
{
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Cancelled:
return StoragePurchaseState.Cancelled;
2025-04-08 13:07:55 +02:00
case CodexOpenApi.PurchaseState.Errored:
return StoragePurchaseState.Errored;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Failed:
return StoragePurchaseState.Failed;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Finished:
return StoragePurchaseState.Finished;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Pending:
return StoragePurchaseState.Pending;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Started:
return StoragePurchaseState.Started;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Submitted:
return StoragePurchaseState.Submitted;
2025-04-03 09:15:39 +02:00
case CodexOpenApi.PurchaseState.Unknown:
return StoragePurchaseState.Unknown;
}
throw new Exception("API incompatibility detected. Unknown purchaseState: " + purchaseState.ToString());
}
public StorageRequest Map(CodexOpenApi.StorageRequest request)
{
return new StorageRequest
{
Ask = Map(request.Ask),
Content = Map(request.Content),
Id = request.Id,
Client = request.Client,
Expiry = request.Expiry,
Nonce = request.Nonce
};
}
public StorageAsk Map(CodexOpenApi.StorageAsk ask)
{
return new StorageAsk
{
Duration = ask.Duration,
MaxSlotLoss = ask.MaxSlotLoss,
ProofProbability = ask.ProofProbability,
PricePerBytePerSecond = ask.PricePerBytePerSecond,
Slots = ask.Slots,
SlotSize = ask.SlotSize
};
}
public StorageContent Map(CodexOpenApi.Content content)
{
return new StorageContent
{
Cid = content.Cid
};
}
2024-03-26 11:39:59 +01:00
2025-01-16 11:31:50 +01:00
public CodexSpace Map(CodexOpenApi.Space space)
{
return new CodexSpace
{
QuotaMaxBytes = space.QuotaMaxBytes,
QuotaReservedBytes = space.QuotaReservedBytes,
QuotaUsedBytes = space.QuotaUsedBytes,
TotalBlocks = space.TotalBlocks
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoVersion Map(CodexOpenApi.CodexVersion obj)
2024-03-26 10:03:52 +01:00
{
return new DebugInfoVersion
{
2024-10-29 14:02:00 +01:00
Version = obj.Version,
Revision = obj.Revision,
Contracts = obj.Contracts
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTable Map(CodexOpenApi.PeersTable obj)
2024-03-26 10:03:52 +01:00
{
return new DebugInfoTable
{
2024-10-29 14:02:00 +01:00
LocalNode = Map(obj.LocalNode),
Nodes = Map(obj.Nodes)
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTableNode Map(CodexOpenApi.Node? token)
2024-03-26 10:03:52 +01:00
{
2024-10-29 14:02:00 +01:00
if (token == null) return new DebugInfoTableNode();
2024-03-26 10:03:52 +01:00
return new DebugInfoTableNode
{
2024-10-29 14:02:00 +01:00
Address = token.Address,
NodeId = token.NodeId,
PeerId = token.PeerId,
Record = token.Record,
Seen = token.Seen
2024-03-26 10:03:52 +01:00
};
}
2025-01-16 11:31:50 +01:00
private DebugInfoTableNode[] Map(ICollection<CodexOpenApi.Node> nodes)
{
if (nodes == null || nodes.Count == 0)
{
return new DebugInfoTableNode[0];
}
2024-10-29 14:02:00 +01:00
return nodes.Select(Map).ToArray();
}
2024-03-26 14:07:06 +01:00
private Manifest MapManifest(CodexOpenApi.ManifestItem manifest)
{
return new Manifest
{
BlockSize = new ByteSize(Convert.ToInt64(manifest.BlockSize)),
2024-11-21 09:39:28 +01:00
OriginalBytes = new ByteSize(Convert.ToInt64(manifest.DatasetSize)),
RootHash = manifest.TreeCid,
2024-03-26 14:07:06 +01:00
Protected = manifest.Protected
};
}
2024-03-26 10:03:52 +01:00
private JArray JArray(IDictionary<string, object> map, string name)
{
return (JArray)map[name];
}
private JObject JObject(IDictionary<string, object> map, string name)
{
return (JObject)map[name];
}
private string StringOrEmpty(JObject obj, string name)
{
if (obj.TryGetValue(name, out var token))
{
var str = (string?)token;
if (!string.IsNullOrEmpty(str)) return str;
}
return string.Empty;
}
private bool Bool(JObject obj, string name)
{
if (obj.TryGetValue(name, out var token))
{
return (bool)token;
}
return false;
}
2024-03-26 11:39:59 +01:00
private string ToDecInt(double d)
{
var i = new BigInteger(d);
return i.ToString("D");
}
private string ToDecInt(TestToken t)
{
return t.TstWei.ToString("D");
2024-03-26 11:39:59 +01:00
}
2024-09-23 10:52:12 +02:00
2025-04-03 09:15:39 +02:00
private TestToken ToTestToken(string s)
{
return new TestToken(ToBigInt(s));
}
private long ToLong(double value)
{
return Convert.ToInt64(value);
}
private BigInteger ToBigInt(string tokens)
2024-09-23 10:52:12 +02:00
{
return BigInteger.Parse(tokens);
}
2025-04-03 09:15:39 +02:00
private TimeSpan ToTimespan(long duration)
2024-09-23 10:52:12 +02:00
{
2025-04-03 09:15:39 +02:00
return TimeSpan.FromSeconds(duration);
2024-09-23 10:52:12 +02:00
}
2025-04-03 09:15:39 +02:00
private ByteSize ToByteSize(long size)
2024-09-23 10:52:12 +02:00
{
2025-04-03 09:15:39 +02:00
return new ByteSize(size);
2024-09-23 10:52:12 +02:00
}
2024-03-26 08:58:16 +01:00
}
}