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

Commit

Permalink
Merge pull request #9 from Etherna/improve/BEET-18-upload-chunks-with…
Browse files Browse the repository at this point in the history
…-hash

add hash check in chunk bulk upload
  • Loading branch information
tmm360 authored Oct 16, 2024
2 parents 39eb833 + 980298f commit 4652cce
Showing 1 changed file with 18 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

0 comments on commit 4652cce

Please sign in to comment.