Skip to content

Commit

Permalink
Read Fixed (neo-project#1454)
Browse files Browse the repository at this point in the history
* Read Fixed

* format

* Rename

* Fix Consensus UT

* Add ut, and allow buffered streams

* Add ut, and allow buffered streams

* Change name

* Update Helper.cs

Co-authored-by: erikzhang <erik@neo.org>
  • Loading branch information
2 people authored and Tommo-L committed Jun 22, 2020
1 parent d442b94 commit acef719
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 10 deletions.
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];

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

0 comments on commit acef719

Please sign in to comment.