Skip to content

Commit

Permalink
Add ChunkSize to GetBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
earlbread committed Jun 5, 2019
1 parent 36f7206 commit c9d9a82
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Libplanet/Net/Messages/GetBlocks.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
Expand All @@ -7,9 +8,19 @@ namespace Libplanet.Net.Messages
{
internal class GetBlocks : Message
{
public GetBlocks(IEnumerable<HashDigest<SHA256>> hashes)
public GetBlocks(
IEnumerable<HashDigest<SHA256>> hashes,
int chunkSize = 500)
{
if (chunkSize <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(chunkSize),
"Chunk size must be greater than 0.");
}

BlockHashes = hashes;
ChunkSize = chunkSize;
}

public GetBlocks(NetMQFrame[] frames)
Expand All @@ -19,10 +30,13 @@ public GetBlocks(NetMQFrame[] frames)
.Skip(1).Take(hashCount)
.Select(f => f.ConvertToHashDigest<SHA256>())
.ToList();
ChunkSize = frames[1 + hashCount].ConvertToInt32();
}

public IEnumerable<HashDigest<SHA256>> BlockHashes { get; }

public int ChunkSize { get; }

protected override MessageType Type => MessageType.GetBlocks;

protected override IEnumerable<NetMQFrame> DataFrames
Expand All @@ -36,6 +50,9 @@ protected override IEnumerable<NetMQFrame> DataFrames
{
yield return new NetMQFrame(hash.ToByteArray());
}

yield return new NetMQFrame(
NetworkOrderBitsConverter.GetBytes(ChunkSize));
}
}
}
Expand Down

0 comments on commit c9d9a82

Please sign in to comment.