Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
merge hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 16, 2024
2 parents b8e7ebb + 4652cce commit 16d657d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/BeeTurbo/Handlers/ChunksBulkUploadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public async Task HandleAsync(HttpContext httpContext)
chunkPayload[SwarmChunk.SpanSize..].ToArray(),
hasher);
var chunkRef = new UploadedChunkRef(hash, batchId);

//read check hash
var checkHash = ReadSwarmHash(payload.AsSpan()[i..(i + SwarmHash.HashSize)]);
i += SwarmHash.HashSize;
if (checkHash != hash)
throw new InvalidDataException("Invalid hash with provided data");

await dbContext.ChunksBucket.UploadFromBytesAsync(hash.ToString(), chunkPayload);
await dbContext.ChunkPushQueue.CreateAsync(chunkRef);
Expand All @@ -78,13 +84,24 @@ public async Task HandleAsync(HttpContext httpContext)
}

// Helpers.
private static SwarmHash ReadSwarmHash(Span<byte> payload)
{
if (payload.Length != SwarmHash.HashSize)
throw new ArgumentOutOfRangeException(nameof(payload));

var valueByteArray = new byte[SwarmHash.HashSize];
for (int i = 0; i < valueByteArray.Length; i++)
valueByteArray[i] = payload[i];
return SwarmHash.FromByteArray(valueByteArray);
}

private static ushort ReadUshort(Span<byte> payload)
{
if (payload.Length != sizeof(ushort))
throw new ArgumentOutOfRangeException(nameof(payload));

var valueByteArray = new byte[sizeof(ushort)];
for (int i = 0; i < sizeof(ushort); i++)
for (int i = 0; i < valueByteArray.Length; i++)
valueByteArray[i] = payload[i];
return BitConverter.ToUInt16(valueByteArray);
}
Expand Down
22 changes: 22 additions & 0 deletions src/BeeTurbo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Exceptions;
using Serilog.Sinks.Elasticsearch;
Expand Down Expand Up @@ -115,6 +116,7 @@ private static void ConfigureServices(WebApplicationBuilder builder, string beeU
var config = builder.Configuration;

// Add services.
services.AddCors();
services.AddHttpForwarder();

// Add request handlers.
Expand Down Expand Up @@ -157,6 +159,26 @@ private static void ConfigureServices(WebApplicationBuilder builder, string beeU
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
private static void ConfigureApplication(WebApplication app, string beeUrl)
{
var env = app.Environment;

app.UseCors(builder =>
{
if (env.IsDevelopment())
{
builder.SetIsOriginAllowed(_ => true)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}
else
{
builder.WithOrigins("https://etherna.io")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
}
});

app.UseRouting();
app.UseWebSockets(new WebSocketOptions
{
Expand Down

0 comments on commit 16d657d

Please sign in to comment.