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 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
12 changes: 11 additions & 1 deletion src/neo/Network/P2P/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ public static Message Create(MessageCommand command, ISerializable payload = nul
_payload_compressed = payload?.ToArray() ?? Array.Empty<byte>()
};

bool tryCompression =
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
command == MessageCommand.Block ||
command == MessageCommand.Consensus ||
command == MessageCommand.Transaction ||
command == MessageCommand.Headers ||
command == MessageCommand.Addr ||
command == MessageCommand.MerkleBlock ||
command == MessageCommand.FilterLoad ||
command == MessageCommand.FilterAdd;

// 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
37 changes: 16 additions & 21 deletions tests/neo.UnitTests/Network/P2P/UT_Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,34 @@ public void MultipleSizes()
[TestMethod]
public void Compression()
{
var payload = new VersionPayload()
var payload = new Transaction()
{
UserAgent = "".PadLeft(1024, '0'),
Nonce = 1,
Magic = 2,
Timestamp = 5,
Version = 6,
Capabilities = new NodeCapability[]
{
new ServerCapability(NodeCapabilityType.TcpServer, 25)
}
Version = 0,
Attributes = new TransactionAttribute[0],
Script = new byte[75],
Signers = new Signer[] { new Signer() { Account = UInt160.Zero } },
Witnesses = new Witness[0],
};

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

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

payload.Script = new byte[payload.Script.Length + 10];
msg = Message.Create(MessageCommand.Transaction, payload);
buffer = msg.ToArray();

buffer.Length.Should().Be(33);

var copy = buffer.AsSerializable<Message>();
var payloadCopy = (VersionPayload)copy.Payload;
var payloadCopy = (Transaction)copy.Payload;

copy.Command.Should().Be(msg.Command);
copy.Flags.Should().HaveFlag(MessageFlags.Compressed);

payloadCopy.UserAgent.Should().Be(payload.UserAgent);
payloadCopy.Nonce.Should().Be(payload.Nonce);
payloadCopy.Magic.Should().Be(payload.Magic);
payloadCopy.Timestamp.Should().Be(payload.Timestamp);
payloadCopy.Version.Should().Be(payload.Version);

payloadCopy.Capabilities.Length.Should().Be(1);
((ServerCapability)payloadCopy.Capabilities[0]).Type.Should().Be(NodeCapabilityType.TcpServer);
((ServerCapability)payloadCopy.Capabilities[0]).Port.Should().Be(25);
payloadCopy.ToArray().ToHexString().Should().Be(payload.ToArray().ToHexString());
}
}
}