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

fix Storage.Find #200

Merged
merged 6 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions neo/Core/StorageKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public class StorageKey : IEquatable<StorageKey>, ISerializable
public UInt160 ScriptHash;
public byte[] Key;

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

void ISerializable.Deserialize(BinaryReader reader)
{
ScriptHash = reader.ReadSerializable<UInt160>();
Key = reader.ReadVarBytes();
Key = reader.ReadBytesWithGrouping();
}

public bool Equals(StorageKey other)
Expand All @@ -43,7 +43,7 @@ public override int GetHashCode()
void ISerializable.Serialize(BinaryWriter writer)
{
writer.Write(ScriptHash);
writer.WriteVarBytes(Key);
writer.WriteBytesWithGrouping(Key);
}
}
}
40 changes: 40 additions & 0 deletions neo/IO/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ internal static int GetVarSize(this string value)
return GetVarSize(size) + size;
}

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

public static string ReadFixedString(this BinaryReader reader, int length)
{
byte[] data = reader.ReadBytes(length);
Expand Down Expand Up @@ -171,6 +191,26 @@ public static void Write<T>(this BinaryWriter writer, T[] value) where T : ISeri
}
}

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

public static void WriteFixedString(this BinaryWriter writer, string value, int length)
{
if (value == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public LevelDBBlockchain(string path)
Version version;
Slice value;
db = DB.Open(path, new Options { CreateIfMissing = true });
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.SYS_Version), out value) && Version.TryParse(value.ToString(), out version) && version >= Version.Parse("2.6.0"))
if (db.TryGet(ReadOptions.Default, SliceBuilder.Begin(DataEntryPrefix.SYS_Version), out value) && Version.TryParse(value.ToString(), out version) && version >= Version.Parse("2.7.4"))
{
ReadOptions options = new ReadOptions { FillCache = false };
value = db.Get(options, SliceBuilder.Begin(DataEntryPrefix.SYS_CurrentBlock));
Expand Down
19 changes: 17 additions & 2 deletions neo/SmartContract/StateReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,23 @@ protected virtual bool Storage_Find(ExecutionEngine engine)
StorageContext context = _interface.GetInterface<StorageContext>();
if (!CheckStorageContext(context)) return false;
byte[] prefix = engine.EvaluationStack.Pop().GetByteArray();
prefix = context.ScriptHash.ToArray().Concat(prefix).ToArray();
StorageIterator iterator = new StorageIterator(Storages.Find(prefix).GetEnumerator());
byte[] prefix_key;
using (MemoryStream ms = new MemoryStream())
{
int index = 0;
int remain = prefix.Length;
while (remain >= 16)
{
ms.Write(prefix, index, 16);
ms.WriteByte(0);
index += 16;
remain -= 16;
}
if (remain > 0)
ms.Write(prefix, index, remain);
prefix_key = context.ScriptHash.ToArray().Concat(ms.ToArray()).ToArray();
}
StorageIterator iterator = new StorageIterator(Storages.Find(prefix_key).Where(p => p.Key.Key.Take(prefix.Length).SequenceEqual(prefix)).GetEnumerator());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Where part necessary? Since the prefix does not write any additional padding now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's necessary. Think of this, if the key is 0x112233, the padded key is 0x11223300000000000000000000000000. Then we find with the prefix 0x11223300. This should find nothing. But if we don't have the Where part, it will match the padded key and return wrong results.

engine.EvaluationStack.Push(StackItem.FromInterface(iterator));
disposables.Add(iterator);
return true;
Expand Down
2 changes: 1 addition & 1 deletion neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Copyright>2015-2017 The Neo Project</Copyright>
<AssemblyTitle>Neo</AssemblyTitle>
<Version>2.7.3</Version>
<Version>2.7.4</Version>
<Authors>The Neo Project</Authors>
<TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down