Skip to content

Commit

Permalink
Add GetFullBlocks P2P logic (enable fixing #522) (#1138)
Browse files Browse the repository at this point in the history
* add GetFullBlocks P2P logic

* add missing new line

* allow request genesis block

* Optimization

* Update MessageCommand.cs

* - rename command
- fix protocol handler cast
- fix payload deserialization for default value

* change to ushort per review

* remove default count, rename class

* typo + failed refactor coverage ¯\_(ツ)_/¯
  • Loading branch information
ixje authored and vncoelho committed Nov 7, 2019
1 parent 4aff0d3 commit 87aed99
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions neo/Network/P2P/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private void DecompressPayload()
case MessageCommand.GetData:
Payload = decompressed.AsSerializable<InvPayload>();
break;
case MessageCommand.GetBlockData:
Payload = decompressed.AsSerializable<GetBlockDataPayload>();
break;
case MessageCommand.Transaction:
Payload = decompressed.AsSerializable<Transaction>();
break;
Expand Down
1 change: 1 addition & 0 deletions neo/Network/P2P/MessageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum MessageCommand : byte
Mempool = 0x25,
Inv = 0x27,
GetData = 0x28,
GetBlockData = 0x29,
NotFound = 0x2a,
Transaction = 0x2b,
Block = 0x2c,
Expand Down
37 changes: 37 additions & 0 deletions neo/Network/P2P/Payloads/GetBlockDataPayload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Neo.IO;
using System;
using System.IO;

namespace Neo.Network.P2P.Payloads
{
public class GetBlockDataPayload : ISerializable
{
private const ushort MaxBlocksCount = 500;
public uint IndexStart;
public ushort Count;

public int Size => sizeof(uint) + sizeof(ushort);

public static GetBlockDataPayload Create(uint index_start, ushort count)
{
return new GetBlockDataPayload
{
IndexStart = index_start,
Count = count
};
}

void ISerializable.Deserialize(BinaryReader reader)
{
IndexStart = reader.ReadUInt32();
Count = reader.ReadUInt16();
if (Count == 0 || Count > MaxBlocksCount) throw new FormatException();
}

void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(IndexStart);
writer.Write(Count);
}
}
}
23 changes: 23 additions & 0 deletions neo/Network/P2P/ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ protected override void OnReceive(object message)
case MessageCommand.GetBlocks:
OnGetBlocksMessageReceived((GetBlocksPayload)msg.Payload);
break;
case MessageCommand.GetBlockData:
OnGetBlockDataMessageReceived((GetBlockDataPayload)msg.Payload);
break;
case MessageCommand.GetData:
OnGetDataMessageReceived((InvPayload)msg.Payload);
break;
Expand Down Expand Up @@ -175,6 +178,26 @@ private void OnGetBlocksMessageReceived(GetBlocksPayload payload)
Context.Parent.Tell(Message.Create(MessageCommand.Inv, InvPayload.Create(InventoryType.Block, hashes.ToArray())));
}

private void OnGetBlockDataMessageReceived(GetBlockDataPayload payload)
{
for (uint i = payload.IndexStart, max = payload.IndexStart + payload.Count; i < max; i++)
{
Block block = Blockchain.Singleton.Store.GetBlock(i);
if (block == null)
break;

if (bloom_filter == null)
{
Context.Parent.Tell(Message.Create(MessageCommand.Block, block));
}
else
{
BitArray flags = new BitArray(block.Transactions.Select(p => bloom_filter.Test(p)).ToArray());
Context.Parent.Tell(Message.Create(MessageCommand.MerkleBlock, MerkleBlockPayload.Create(block, flags)));
}
}
}

private void OnGetDataMessageReceived(InvPayload payload)
{
UInt256[] hashes = payload.Hashes.Where(p => sentHashes.Add(p)).ToArray();
Expand Down

0 comments on commit 87aed99

Please sign in to comment.