Skip to content

Commit

Permalink
Remove grouping (neo-project#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 6f63594 commit 53ea390
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 119 deletions.
39 changes: 0 additions & 39 deletions src/neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Neo.IO
{
public static class Helper
{
public const int GroupingSizeInBytes = 16;

public static T AsSerializable<T>(this byte[] value, int start = 0) where T : ISerializable, new()
{
using (MemoryStream ms = new MemoryStream(value, start, value.Length - start, false))
Expand Down Expand Up @@ -144,24 +142,6 @@ public static int GetVarSize(this string value)
return GetVarSize(size) + size;
}

public static byte[] ReadBytesWithGrouping(this BinaryReader reader)
{
using (MemoryStream ms = new MemoryStream())
{
int count;
do
{
byte[] group = reader.ReadFixedBytes(GroupingSizeInBytes);
count = reader.ReadByte();
if (count > GroupingSizeInBytes)
throw new FormatException();
if (count > 0)
ms.Write(group, 0, count);
} while (count == GroupingSizeInBytes);
return ms.ToArray();
}
}

public static byte[] ReadFixedBytes(this BinaryReader reader, int size)
{
var index = 0;
Expand Down Expand Up @@ -277,25 +257,6 @@ public static void Write<T>(this BinaryWriter writer, IReadOnlyCollection<T> val
}
}

public static void WriteBytesWithGrouping(this BinaryWriter writer, byte[] value)
{
int index = 0;
int remain = value.Length;
while (remain >= GroupingSizeInBytes)
{
writer.Write(value, index, GroupingSizeInBytes);
writer.Write((byte)GroupingSizeInBytes);
index += GroupingSizeInBytes;
remain -= GroupingSizeInBytes;
}
if (remain > 0)
writer.Write(value, index, remain);
int padding = GroupingSizeInBytes - remain;
for (int i = 0; i < padding; i++)
writer.Write((byte)0);
writer.Write((byte)remain);
}

public static void WriteFixedString(this BinaryWriter writer, string value, int length)
{
if (value == null)
Expand Down
30 changes: 11 additions & 19 deletions src/neo/Ledger/StorageKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Neo.Cryptography;
using Neo.IO;
using System;
using System.Buffers.Binary;
using System.IO;

namespace Neo.Ledger
Expand All @@ -10,31 +11,22 @@ public class StorageKey : IEquatable<StorageKey>, ISerializable
public int Id;
public byte[] Key;

int ISerializable.Size => sizeof(int) + (Key.Length / 16 + 1) * 17;
int ISerializable.Size => sizeof(int) + Key.Length;

internal static byte[] CreateSearchPrefix(int id, byte[] prefix)
internal static byte[] CreateSearchPrefix(int id, ReadOnlySpan<byte> prefix)
{
using (MemoryStream ms = new MemoryStream())
{
int index = 0;
int remain = prefix.Length;
while (remain >= 16)
{
ms.Write(prefix, index, 16);
ms.WriteByte(16);
index += 16;
remain -= 16;
}
if (remain > 0)
ms.Write(prefix, index, remain);
return Helper.Concat(BitConverter.GetBytes(id), ms.ToArray());
}
byte[] buffer = new byte[sizeof(int) + prefix.Length];
BinaryPrimitives.WriteInt32LittleEndian(buffer, id);
prefix.CopyTo(buffer.AsSpan(sizeof(int)));
return buffer;
}

//If the base stream of the reader doesn't support seeking, a NotSupportedException is thrown.
//But StorageKey never works with NetworkStream, so it doesn't matter.
void ISerializable.Deserialize(BinaryReader reader)
{
Id = reader.ReadInt32();
Key = reader.ReadBytesWithGrouping();
Key = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));
}

public bool Equals(StorageKey other)
Expand All @@ -60,7 +52,7 @@ public override int GetHashCode()
void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(Id);
writer.WriteBytesWithGrouping(Key);
writer.Write(Key);
}
}
}
61 changes: 0 additions & 61 deletions tests/neo.UnitTests/IO/UT_IOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,45 +285,6 @@ public void TestGetVarSizeString()
Assert.AreEqual(3, result);
}

[TestMethod]
public void TestReadBytesWithGrouping()
{
for (int i = 0; i < 2; i++)
{
if (i == 0)
{
byte[] caseArray = new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA};
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
Neo.IO.Helper.WriteBytesWithGrouping(writer, caseArray);
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
byte[] result = Neo.IO.Helper.ReadBytesWithGrouping(reader);
Assert.AreEqual(Encoding.Default.GetString(caseArray), Encoding.Default.GetString(result));
}
else
{
byte[] caseArray = new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10,
0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x11};
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(caseArray);
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
Action action = () => Neo.IO.Helper.ReadBytesWithGrouping(reader);
action.Should().Throw<FormatException>();
}
}
}

[TestMethod]
public void TestReadFixedString()
{
Expand Down Expand Up @@ -474,28 +435,6 @@ public void TestWriteGeneric()
0x00,0x00,0x00,0x00,0x00}), Encoding.Default.GetString(byteArray));
}


[TestMethod]
public void TestWriteBytesWithGrouping()
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
Neo.IO.Helper.WriteBytesWithGrouping(writer, new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA});
stream.Seek(0, SeekOrigin.Begin);
byte[] byteArray = new byte[stream.Length];
stream.Read(byteArray, 0, (int)stream.Length);
Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,0x10,
0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x04}), Encoding.Default.GetString(byteArray));
}

[TestMethod]
public void TestWriteFixedString()
{
Expand Down

0 comments on commit 53ea390

Please sign in to comment.