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
27 changes: 24 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,30 @@ public static byte[] ReadBytesWithGrouping(this BinaryReader reader)
}
}

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

while (size > 0)
{
var bytesRead = reader.Read(data, index, size);

if (bytesRead <= 0)
{
throw new FormatException();
}

size -= bytesRead;
index += bytesRead;
}

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 +217,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
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/Consensus/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ public void TestSerializeAndDeserializeConsensusContext()
consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length];
using (SHA256 sha256 = SHA256.Create())
{
consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()) }, 3, new[] { (byte)'3', (byte)'4' });
consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()) }, 3, new[] { (byte)'6', (byte)'7' });
consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx1.Hash.ToArray()).Concat(sha256.ComputeHash(testTx1.Hash.ToArray())).ToArray() }, 3, new[] { (byte)'3', (byte)'4' });
consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit { Signature = sha256.ComputeHash(testTx2.Hash.ToArray()).Concat(sha256.ComputeHash(testTx2.Hash.ToArray())).ToArray() }, 3, new[] { (byte)'6', (byte)'7' });
}

consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestampMS();
Expand Down
34 changes: 34 additions & 0 deletions tests/neo.UnitTests/IO/UT_IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@ public void TestAsSerializableGeneric()
Assert.AreEqual(UInt160.Zero, result);
}

[TestMethod]
public void TestReadFixedBytes()
{
byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };

// Less data

using (var reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8, false))
{
byte[] result = Neo.IO.Helper.ReadFixedBytes(reader, 3);

Assert.AreEqual("010203", result.ToHexString());
Assert.AreEqual(3, reader.BaseStream.Position);
}

// Same data

using (var reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8, false))
{
byte[] result = Neo.IO.Helper.ReadFixedBytes(reader, 4);

Assert.AreEqual("01020304", result.ToHexString());
Assert.AreEqual(4, reader.BaseStream.Position);
}

// More data

using (var reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8, false))
{
Assert.ThrowsException<FormatException>(() => Neo.IO.Helper.ReadFixedBytes(reader, 5));
Assert.AreEqual(4, reader.BaseStream.Position);
}
}

[TestMethod]
public void TestNullableArray()
{
Expand Down