Skip to content

Commit

Permalink
Optional p2p compression (neo-project#1906)
Browse files Browse the repository at this point in the history
* Optional p2p compression

* Add optional compression

* Fix UT

* Remove Inv compression

* Try according to the type

* Add Addr and MerkleBlock

Co-authored-by: Erik Zhang <erik@neo.org>
  • Loading branch information
2 people authored and cloud8little committed Jan 24, 2021
1 parent a1b001d commit ea7f38b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
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 =
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());
}
}
}

0 comments on commit ea7f38b

Please sign in to comment.