cs-codex-dist-tests/ProjectPlugins/CodexPlugin/Mapper.cs

247 lines
7.6 KiB
C#
Raw Normal View History

2024-03-26 10:39:59 +00:00
using CodexContractsPlugin;
using CodexOpenApi;
2024-03-26 10:39:59 +00:00
using Newtonsoft.Json.Linq;
using System.Numerics;
2024-03-26 13:07:06 +00:00
using Utils;
2024-03-26 07:58:16 +00:00
namespace CodexPlugin
{
public class Mapper
{
public DebugInfo Map(CodexOpenApi.DebugInfo debugInfo)
{
return new DebugInfo
{
Id = debugInfo.Id,
Spr = debugInfo.Spr,
Addrs = debugInfo.Addrs.ToArray(),
2024-03-26 09:03:52 +00:00
AnnounceAddresses = JArray(debugInfo.AdditionalProperties, "announceAddresses").Select(x => x.ToString()).ToArray(),
2024-10-29 13:02:00 +00:00
Version = Map(debugInfo.Codex),
Table = Map(debugInfo.Table)
2024-03-26 07:58:16 +00:00
};
}
2024-03-26 09:03:52 +00:00
2024-03-26 13:07:06 +00:00
public LocalDatasetList Map(CodexOpenApi.DataList dataList)
2024-03-26 10:39:59 +00:00
{
2024-03-26 13:07:06 +00: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 10:39:59 +00:00
}
public CodexOpenApi.SalesAvailabilityCREATE Map(StorageAvailability availability)
{
return new CodexOpenApi.SalesAvailabilityCREATE
{
Duration = ToDecInt(availability.MaxDuration.TotalSeconds),
MinPrice = ToDecInt(availability.MinPriceForTotalSpace),
MaxCollateral = ToDecInt(availability.MaxCollateral),
TotalSize = ToDecInt(availability.TotalSpace.SizeInBytes)
};
}
public CodexOpenApi.StorageRequestCreation Map(StoragePurchaseRequest purchase)
{
return new CodexOpenApi.StorageRequestCreation
{
Duration = ToDecInt(purchase.Duration.TotalSeconds),
ProofProbability = ToDecInt(purchase.ProofProbability),
Reward = ToDecInt(purchase.PricePerSlotPerSecond),
Collateral = ToDecInt(purchase.RequiredCollateral),
2024-05-07 08:21:05 +00:00
Expiry = ToDecInt(purchase.Expiry.TotalSeconds),
Nodes = Convert.ToInt32(purchase.MinRequiredNumberOfNodes),
Tolerance = Convert.ToInt32(purchase.NodeFailureTolerance)
2024-03-26 10:39:59 +00:00
};
}
2024-09-24 11:06:00 +00:00
public StorageAvailability[] Map(ICollection<SalesAvailabilityREAD> availabilities)
2024-09-23 08:52:12 +00:00
{
return availabilities.Select(a => Map(a)).ToArray();
}
2024-09-24 11:06:00 +00:00
public StorageAvailability Map(SalesAvailabilityREAD availability)
2024-09-23 08:52:12 +00:00
{
return new StorageAvailability
(
ToByteSize(availability.TotalSize),
ToTimespan(availability.Duration),
new TestToken(ToBigIng(availability.MinPrice)),
new TestToken(ToBigIng(availability.MaxCollateral))
)
{
Id = availability.Id,
2024-09-24 13:21:33 +00:00
FreeSpace = ToByteSize(availability.FreeSize),
2024-09-23 08:52:12 +00:00
};
}
// TODO: Fix openapi spec for this call.
//public StoragePurchase Map(CodexOpenApi.Purchase purchase)
//{
// return new StoragePurchase(Map(purchase.Request))
// {
// State = purchase.State,
// Error = purchase.Error
// };
//}
//public StorageRequest Map(CodexOpenApi.StorageRequest request)
//{
// return new StorageRequest(Map(request.Ask), Map(request.Content))
// {
// Id = request.Id,
// Client = request.Client,
// Expiry = TimeSpan.FromSeconds(Convert.ToInt64(request.Expiry)),
// Nonce = request.Nonce
// };
//}
//public StorageAsk Map(CodexOpenApi.StorageAsk ask)
//{
// return new StorageAsk
// {
// Duration = TimeSpan.FromSeconds(Convert.ToInt64(ask.Duration)),
// MaxSlotLoss = ask.MaxSlotLoss,
// ProofProbability = ask.ProofProbability,
// Reward = Convert.ToDecimal(ask.Reward).TstWei(),
// Slots = ask.Slots,
// SlotSize = new ByteSize(Convert.ToInt64(ask.SlotSize))
// };
//}
//public StorageContent Map(CodexOpenApi.Content content)
//{
// return new StorageContent
// {
// Cid = content.Cid
// };
//}
2024-03-26 10:39:59 +00:00
public CodexSpace Map(Space space)
{
return new CodexSpace
{
QuotaMaxBytes = space.QuotaMaxBytes,
QuotaReservedBytes = space.QuotaReservedBytes,
QuotaUsedBytes = space.QuotaUsedBytes,
TotalBlocks = space.TotalBlocks
};
}
2024-10-29 13:02:00 +00:00
private DebugInfoVersion Map(CodexVersion obj)
2024-03-26 09:03:52 +00:00
{
return new DebugInfoVersion
{
2024-10-29 13:02:00 +00:00
Version = obj.Version,
Revision = obj.Revision
2024-03-26 09:03:52 +00:00
};
}
2024-10-29 13:02:00 +00:00
private DebugInfoTable Map(PeersTable obj)
2024-03-26 09:03:52 +00:00
{
return new DebugInfoTable
{
2024-10-29 13:02:00 +00:00
LocalNode = Map(obj.LocalNode),
Nodes = Map(obj.Nodes)
2024-03-26 09:03:52 +00:00
};
}
2024-10-29 13:02:00 +00:00
private DebugInfoTableNode Map(Node? token)
2024-03-26 09:03:52 +00:00
{
2024-10-29 13:02:00 +00:00
if (token == null) return new DebugInfoTableNode();
2024-03-26 09:03:52 +00:00
return new DebugInfoTableNode
{
2024-10-29 13:02:00 +00:00
Address = token.Address,
NodeId = token.NodeId,
PeerId = token.PeerId,
Record = token.Record,
Seen = token.Seen
2024-03-26 09:03:52 +00:00
};
}
2024-10-29 13:02:00 +00:00
private DebugInfoTableNode[] Map(ICollection<Node> nodes)
{
if (nodes == null || nodes.Count == 0)
{
return new DebugInfoTableNode[0];
}
2024-10-29 13:02:00 +00:00
return nodes.Select(Map).ToArray();
}
2024-03-26 13:07:06 +00:00
private Manifest MapManifest(CodexOpenApi.ManifestItem manifest)
{
return new Manifest
{
BlockSize = new ByteSize(Convert.ToInt64(manifest.BlockSize)),
OriginalBytes = new ByteSize(Convert.ToInt64(manifest.OriginalBytes)),
RootHash = manifest.RootHash,
Protected = manifest.Protected
};
}
2024-03-26 09:03:52 +00: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 10:39:59 +00: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 10:39:59 +00:00
}
2024-09-23 08:52:12 +00:00
private BigInteger ToBigIng(string tokens)
{
return BigInteger.Parse(tokens);
}
private TimeSpan ToTimespan(string duration)
{
return TimeSpan.FromSeconds(Convert.ToInt32(duration));
}
private ByteSize ToByteSize(string size)
{
return new ByteSize(Convert.ToInt64(size));
}
2024-03-26 07:58:16 +00:00
}
}