Skip to content

Commit

Permalink
Remove IMessageSerializer, use IZeroMessageSerializer for all messages (
Browse files Browse the repository at this point in the history
#4292)

* change to zero serializer

* fix get length

* temp change

* change to zero intializer

* update message pad

* add and use zero serializer

* use zero serializer

* update messgae serialization service

* rewrite functions to use byte buffer

* update P2P messages serializer

* update serializers

* update tests

* remove zero message serializer

* fix failing tests

* fix netty rlp has been read

* fix length calc

* fix serializers

* update

* optimize byte array allocations

* fix span creation

* remove redundant to array

* fix block body serializer

* fix receipts serializer

* Fix/buffer reallocation (#4306)

* serialization

* reuse buffer

* Fix reciepts decoder

* fix reciepts serializer

* fix zero packet splitter

* implement func to use stream encoder

* Clean up

* fix ChainLevelDecoder

* add comment

* add GetLength private to reduce length calculations

* convert all to inner serializer to use get length to allocate buffer

* Kill more allocations

* kill bit more allocations

* less allocs

* fix build

* use rlp stream

* remove rlp.encode from zeroPacketSplitter

* restore WriteAdaptivePacketType(in IByteBuffer buffer) and fix tests

* fix length calc

* Remove test console outputs

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
tanishqjasoria and LukaszRozmej authored Aug 4, 2022
1 parent 31be552 commit a0d0d68
Show file tree
Hide file tree
Showing 56 changed files with 1,237 additions and 846 deletions.
28 changes: 14 additions & 14 deletions src/Nethermind/Nethermind.Core.Test/Encoding/ReceiptDecoderTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -33,7 +33,7 @@ public void Can_do_roundtrip_storage([Values(true, false)] bool encodeWithTxHash
TxReceipt GetExpected()
{
ReceiptBuilder receiptBuilder = Build.A.Receipt.WithAllFieldsFilled;

if ((encodeBehaviors & RlpBehaviors.Eip658Receipts) != 0)
{
receiptBuilder.WithState(null);
Expand All @@ -47,7 +47,7 @@ TxReceipt GetExpected()
{
receiptBuilder.WithTransactionHash(null);
}

if (!withError)
{
receiptBuilder.WithError(string.Empty);
Expand All @@ -71,7 +71,7 @@ TxReceipt BuildReceipt()

ReceiptStorageDecoder encoder = new(encodeWithTxHash);
Rlp rlp = encoder.Encode(txReceipt, encodeBehaviors);

ReceiptStorageDecoder decoder = new();
TxReceipt deserialized;
if (valueDecoder)
Expand Down Expand Up @@ -184,7 +184,7 @@ public void Can_do_roundtrip_none_rlp_stream()

AssertMessageReceipt(txReceipt, deserialized);
}

[Test]
public void Can_do_roundtrip_with_receipt_message_and_tx_type_access_list()
{
Expand All @@ -197,9 +197,9 @@ public void Can_do_roundtrip_with_receipt_message_and_tx_type_access_list()

ReceiptMessageDecoder decoder = new();

byte[] rlpStreamResult = decoder.Encode(txReceipt).Bytes;
byte[] rlpStreamResult = decoder.EncodeNew(txReceipt, RlpBehaviors.None);
TxReceipt deserialized = decoder.Decode(new RlpStream(rlpStreamResult));

AssertMessageReceipt(txReceipt, deserialized);
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public void Can_do_roundtrip_with_storage_receipt_and_tx_type_access_list()

AssertStorageReceipt(txReceipt, deserialized);
}

public static IEnumerable<(TxReceipt, string)> TestCaseSource()
{
Bloom bloom = new();
Expand All @@ -237,7 +237,7 @@ public void Can_do_roundtrip_with_storage_receipt_and_tx_type_access_list()
yield return (Build.A.Receipt.WithBloom(bloom).WithGasUsedTotal(500).WithState(TestItem.KeccakA).WithTxType(TxType.AccessList).TestObject, "access list");
yield return (Build.A.Receipt.WithBloom(bloom).WithGasUsedTotal(100).WithState(TestItem.KeccakH).WithTxType(TxType.EIP1559).TestObject, "eip 1559");
}

[TestCaseSource(nameof(TestCaseSource))]
public void Can_do_roundtrip_with_storage_receipt((TxReceipt TxReceipt, string Description) testCase)
{
Expand All @@ -249,20 +249,20 @@ public void Can_do_roundtrip_with_storage_receipt((TxReceipt TxReceipt, string D

AssertStorageReceipt(txReceipt, deserialized);
}

[TestCaseSource(nameof(TestCaseSource))]
public void Can_do_roundtrip_with_receipt_message((TxReceipt TxReceipt, string Description) testCase)
{
TxReceipt txReceipt = testCase.TxReceipt;

ReceiptMessageDecoder decoder = new();

byte[] rlpStreamResult = decoder.Encode(txReceipt).Bytes;
byte[] rlpStreamResult = decoder.EncodeNew(txReceipt);
TxReceipt deserialized = decoder.Decode(new RlpStream(rlpStreamResult));

AssertMessageReceipt(txReceipt, deserialized);
}

private void AssertMessageReceipt(TxReceipt txReceipt, TxReceipt deserialized)
{
Assert.AreEqual(txReceipt.Bloom, deserialized.Bloom, "bloom");
Expand Down
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.Network.Benchmark/InFlowBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2018 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

Expand Down Expand Up @@ -53,7 +53,7 @@ public void Setup()
{
throw new Exception("decoder buffer");
}

SetupAll();
IterationSetup();
Current();
Expand All @@ -65,7 +65,7 @@ public void Setup()
SetupAll();
IterationSetup();
}

[IterationSetup]
public void IterationSetup()
{
Expand Down Expand Up @@ -149,7 +149,7 @@ public void Current()
IByteBuffer decoded = _zeroDecoder.Decode(_decoderBuffer);
IByteBuffer merged = _zeroMerger.Decode(decoded);
merged.ReadByte();
_outputMessage = _newBlockMessageSerializer.Deserialize(merged.ReadAllBytes());
_outputMessage = _newBlockMessageSerializer.Deserialize(merged.ReadAllBytesAsArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System.Net;
using DotNetty.Buffers;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Core.Test.Builders;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void PingMessageTest()
new(_privateKey.PublicKey, 60 + _timestamper.UnixTime.MillisecondsLong, _farAddress, _nearAddress,
new byte[32]) { FarAddress = _farAddress };

byte[] data = _messageSerializationService.Serialize(message);
IByteBuffer data = _messageSerializationService.ZeroSerialize(message);
PingMsg deserializedMessage = _messageSerializationService.Deserialize<PingMsg>(data);

Assert.AreEqual(message.MsgType, deserializedMessage.MsgType);
Expand All @@ -84,7 +85,7 @@ public void PongMessageTest()
FarAddress = _farAddress
};

byte[] data = _messageSerializationService.Serialize(message);
IByteBuffer data = _messageSerializationService.ZeroSerialize(message);
PongMsg deserializedMessage = _messageSerializationService.Deserialize<PongMsg>(data);

Assert.AreEqual(message.MsgType, deserializedMessage.MsgType);
Expand All @@ -99,21 +100,21 @@ public void Ping_with_enr_there_and_back()
{
PingMsg pingMsg = new (TestItem.PublicKeyA, long.MaxValue, TestItem.IPEndPointA, TestItem.IPEndPointB, new byte[32]);
pingMsg.EnrSequence = 3;
byte[] serialized = _messageSerializationService.Serialize(pingMsg);
IByteBuffer serialized = _messageSerializationService.ZeroSerialize(pingMsg);
pingMsg = _messageSerializationService.Deserialize<PingMsg>(serialized);
Assert.AreEqual(3, pingMsg.EnrSequence);
}

[Test]
public void Enr_request_there_and_back()
{
EnrRequestMsg msg = new(TestItem.PublicKeyA, long.MaxValue);
byte[] serialized = _messageSerializationService.Serialize(msg);
IByteBuffer serialized = _messageSerializationService.ZeroSerialize(msg);
EnrRequestMsg deserialized = _messageSerializationService.Deserialize<EnrRequestMsg>(serialized);
Assert.AreEqual(msg.ExpirationTime, deserialized.ExpirationTime);
Assert.AreEqual(deserialized.FarPublicKey, _privateKey.PublicKey);
}

[Test]
public void Enr_response_there_and_back()
{
Expand All @@ -124,7 +125,7 @@ public void Enr_response_there_and_back()
signer.Sign(nodeRecord);
EnrResponseMsg msg = new(TestItem.PublicKeyA, nodeRecord, TestItem.KeccakA);

byte[] serialized = _messageSerializationService.Serialize(msg);
IByteBuffer serialized = _messageSerializationService.ZeroSerialize(msg);
EnrResponseMsg deserialized = _messageSerializationService.Deserialize<EnrResponseMsg>(serialized);
Assert.AreEqual(msg.NodeRecord.EnrSequence, deserialized.NodeRecord.EnrSequence);
Assert.AreEqual(msg.RequestKeccak, deserialized.RequestKeccak);
Expand Down Expand Up @@ -161,7 +162,7 @@ public void FindNodeMessageTest()
FarAddress = _farAddress
};

byte[] data = _messageSerializationService.Serialize(message);
IByteBuffer data = _messageSerializationService.ZeroSerialize(message);
FindNodeMsg deserializedMessage = _messageSerializationService.Deserialize<FindNodeMsg>(data);

Assert.AreEqual(message.MsgType, deserializedMessage.MsgType);
Expand All @@ -186,7 +187,7 @@ public void NeighborsMessageTest()
FarAddress = _farAddress
};

byte[] data = _messageSerializationService.Serialize(message);
byte[] data = _messageSerializationService.ZeroSerialize(message).ReadAllBytesAsArray();
NeighborsMsg deserializedMessage = _messageSerializationService.Deserialize<NeighborsMsg>(data);

Assert.AreEqual(message.MsgType, deserializedMessage.MsgType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -20,5 +20,5 @@ namespace Nethermind.Network.Discovery.Messages;

public interface INodeIdResolver
{
PublicKey GetNodeId(byte[] signature, int recoveryId, Span<byte> typeAndData);
PublicKey GetNodeId(ReadOnlySpan<byte> signature, int recoveryId, Span<byte> typeAndData);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -28,8 +28,8 @@ public NodeIdResolver(IEcdsa ecdsa)
_ecdsa = ecdsa;
}

public PublicKey GetNodeId(byte[] signature, int recoveryId, Span<byte> typeAndData)
public PublicKey GetNodeId(ReadOnlySpan<byte> signature, int recoveryId, Span<byte> typeAndData)
{
return _ecdsa.RecoverPublicKey(new Signature(signature, recoveryId), Keccak.Compute(typeAndData));
return _ecdsa.RecoverPublicKey(new Signature(signature, recoveryId), Keccak.Compute(typeAndData));
}
}
Loading

0 comments on commit a0d0d68

Please sign in to comment.