Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetFullBlocks P2P logic (enable fixing #522) #1138

Merged
merged 20 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions neo/Network/P2P/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private void DecompressPayload()
case MessageCommand.GetBlocks:
Payload = decompressed.AsSerializable<GetBlocksPayload>();
break;
case MessageCommand.GetFullBlocks:
Payload = decompressed.AsSerializable<GetFullBlocksPayload>();
break;
case MessageCommand.Headers:
Payload = decompressed.AsSerializable<HeadersPayload>();
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 @@ -17,6 +17,7 @@ public enum MessageCommand : byte
Headers = 0x21,
GetBlocks = 0x24,
Mempool = 0x25,
GetFullBlocks = 0x26,
Inv = 0x27,
GetData = 0x28,
NotFound = 0x2a,
Expand Down
37 changes: 37 additions & 0 deletions neo/Network/P2P/Payloads/GetFullBlocksPayload.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 GetFullBlocksPayload : ISerializable
ixje marked this conversation as resolved.
Show resolved Hide resolved
{
public const ushort MaxBlocksCount = 500;
shargon marked this conversation as resolved.
Show resolved Hide resolved
ixje marked this conversation as resolved.
Show resolved Hide resolved
public uint IndexStart;
shargon marked this conversation as resolved.
Show resolved Hide resolved
public short Count;
ixje marked this conversation as resolved.
Show resolved Hide resolved

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

public static GetFullBlocksPayload Create(uint index_start, short count = -1)
ixje marked this conversation as resolved.
Show resolved Hide resolved
{
return new GetFullBlocksPayload
{
IndexStart = index_start,
Count = count
};
}

void ISerializable.Deserialize(BinaryReader reader)
{
IndexStart = reader.ReadUInt32();
Count = reader.ReadInt16();
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.GetFullBlocks:
OnGetFullBlocksMessageReceived((GetFullBlocksPayload)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 OnGetFullBlocksMessageReceived(GetFullBlocksPayload payload)
{
for (uint i = payload.IndexStart; i < payload.IndexStart + payload.Count; 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)));
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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