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

Optional p2p compression #1906

Merged
merged 8 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/neo/Network/P2P/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Message : ISerializable

public int Size => sizeof(MessageFlags) + sizeof(MessageCommand) + _payload_compressed.GetVarSize();

public static Message Create(MessageCommand command, ISerializable payload = null)
public static Message Create(MessageCommand command, ISerializable payload = null, bool tryCompression = false)
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
Message message = new Message
{
Expand All @@ -37,7 +37,7 @@ public static Message Create(MessageCommand command, ISerializable payload = nul
};

// Try compression
if (message._payload_compressed.Length > CompressionMinSize)
if (tryCompression && message._payload_compressed.Length > CompressionMinSize)
{
var compressed = message._payload_compressed.CompressLz4();
if (compressed.Length < message._payload_compressed.Length - CompressionThreshold)
Expand Down
8 changes: 4 additions & 4 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void OnGetBlockByIndexMessageReceived(GetBlockByIndexPayload payload)

if (bloom_filter == null)
{
EnqueueMessage(Message.Create(MessageCommand.Block, block));
EnqueueMessage(Message.Create(MessageCommand.Block, block, true));
}
else
{
Expand All @@ -224,7 +224,7 @@ private void OnGetDataMessageReceived(InvPayload payload)
case InventoryType.TX:
Transaction tx = Blockchain.Singleton.GetTransaction(hash);
if (tx != null)
EnqueueMessage(Message.Create(MessageCommand.Transaction, tx));
EnqueueMessage(Message.Create(MessageCommand.Transaction, tx, true));
else
notFound.Add(hash);
break;
Expand All @@ -234,7 +234,7 @@ private void OnGetDataMessageReceived(InvPayload payload)
{
if (bloom_filter == null)
{
EnqueueMessage(Message.Create(MessageCommand.Block, block));
EnqueueMessage(Message.Create(MessageCommand.Block, block, true));
}
else
{
Expand All @@ -249,7 +249,7 @@ private void OnGetDataMessageReceived(InvPayload payload)
break;
default:
if (Blockchain.Singleton.RelayCache.TryGet(hash, out IInventory inventory))
EnqueueMessage(Message.Create((MessageCommand)payload.Type, inventory));
EnqueueMessage(Message.Create((MessageCommand)payload.Type, inventory, true));
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void CheckMessageQueue()

private void EnqueueMessage(MessageCommand command, ISerializable payload = null)
{
EnqueueMessage(Message.Create(command, payload));
EnqueueMessage(Message.Create(command, payload, true));
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion tests/neo.UnitTests/Network/P2P/UT_Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,14 @@ public void Compression()
}
};

var msg = Message.Create(MessageCommand.Version, payload);
var msg = Message.Create(MessageCommand.Version, payload, false);
var buffer = msg.ToArray();

buffer.Length.Should().BeGreaterThan(80);

msg = Message.Create(MessageCommand.Version, payload, true);
buffer = msg.ToArray();

buffer.Length.Should().BeLessThan(80);

var copy = buffer.AsSerializable<Message>();
Expand Down