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

Read Fixed #1454

Merged
merged 13 commits into from
Mar 11, 2020
3 changes: 2 additions & 1 deletion src/neo/Consensus/Commit.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.IO;
using System.IO;

namespace Neo.Consensus
Expand All @@ -13,7 +14,7 @@ public class Commit : ConsensusMessage
public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
Signature = reader.ReadBytes(64);
Signature = reader.ReadFixedBytes(64);
}

public override void Serialize(BinaryWriter writer)
Expand Down
2 changes: 1 addition & 1 deletion src/neo/Consensus/RecoveryMessage.CommitPayloadCompact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ISerializable.Deserialize(BinaryReader reader)
{
ViewNumber = reader.ReadByte();
ValidatorIndex = reader.ReadUInt16();
Signature = reader.ReadBytes(64);
Signature = reader.ReadFixedBytes(64);
InvocationScript = reader.ReadVarBytes(1024);
}

Expand Down
2 changes: 1 addition & 1 deletion src/neo/Consensus/RecoveryMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void Deserialize(BinaryReader reader)
{
int preparationHashSize = UInt256.Zero.Size;
if (preparationHashSize == (int)reader.ReadVarInt((ulong)preparationHashSize))
PreparationHash = new UInt256(reader.ReadBytes(preparationHashSize));
PreparationHash = new UInt256(reader.ReadFixedBytes(preparationHashSize));
}

PreparationMessages = reader.ReadSerializableArray<PreparationPayloadCompact>(Blockchain.MaxValidators).ToDictionary(p => (int)p.ValidatorIndex);
Expand Down
17 changes: 14 additions & 3 deletions src/neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static byte[] ReadBytesWithGrouping(this BinaryReader reader)
int count;
do
{
byte[] group = reader.ReadBytes(GroupingSizeInBytes);
byte[] group = reader.ReadFixedBytes(GroupingSizeInBytes);
count = reader.ReadByte();
if (count > GroupingSizeInBytes)
throw new FormatException();
Expand All @@ -162,9 +162,20 @@ public static byte[] ReadBytesWithGrouping(this BinaryReader reader)
}
}

public static byte[] ReadFixedBytes(this BinaryReader reader, int size)
{
var data = new byte[size];
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (reader.Read(data, 0, size) != size)
{
throw new FormatException();
}

return data;
}

public static string ReadFixedString(this BinaryReader reader, int length)
{
byte[] data = reader.ReadBytes(length);
byte[] data = reader.ReadFixedBytes(length);
return Encoding.UTF8.GetString(data.TakeWhile(p => p != 0).ToArray());
}

Expand Down Expand Up @@ -196,7 +207,7 @@ public static T[] ReadSerializableArray<T>(this BinaryReader reader, int max = 0

public static byte[] ReadVarBytes(this BinaryReader reader, int max = 0x1000000)
{
return reader.ReadBytes((int)reader.ReadVarInt((ulong)max));
return reader.ReadFixedBytes((int)reader.ReadVarInt((ulong)max));
}

public static ulong ReadVarInt(this BinaryReader reader, ulong max = ulong.MaxValue)
Expand Down
3 changes: 1 addition & 2 deletions src/neo/Network/P2P/Payloads/NetworkAddressWithTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ void ISerializable.Deserialize(BinaryReader reader)
Timestamp = reader.ReadUInt32();

// Address
byte[] data = reader.ReadBytes(16);
if (data.Length != 16) throw new FormatException();
byte[] data = reader.ReadFixedBytes(16);
Address = new IPAddress(data).Unmap();

// Capabilities
Expand Down