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 all 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 @@ -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++)
ixje marked this conversation as resolved.
Show resolved Hide resolved
{
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