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

Add System.Storage.FindKeys #2169

Merged
merged 3 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.Storage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.Ledger;
using Neo.SmartContract.Enumerators;
using Neo.SmartContract.Iterators;
using Neo.SmartContract.Native;
using System;
Expand All @@ -16,6 +17,7 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Storage_AsReadOnly = Register("System.Storage.AsReadOnly", nameof(AsReadOnly), 1 << 4, CallFlags.ReadStates, false);
public static readonly InteropDescriptor System_Storage_Get = Register("System.Storage.Get", nameof(Get), 1 << 15, CallFlags.ReadStates, false);
public static readonly InteropDescriptor System_Storage_Find = Register("System.Storage.Find", nameof(Find), 1 << 15, CallFlags.ReadStates, false);
public static readonly InteropDescriptor System_Storage_FindKeys = Register("System.Storage.FindKeys", nameof(FindKeys), 1 << 15, CallFlags.ReadStates, false);
public static readonly InteropDescriptor System_Storage_Put = Register("System.Storage.Put", nameof(Put), 0, CallFlags.WriteStates, false);
public static readonly InteropDescriptor System_Storage_PutEx = Register("System.Storage.PutEx", nameof(PutEx), 0, CallFlags.WriteStates, false);
public static readonly InteropDescriptor System_Storage_Delete = Register("System.Storage.Delete", nameof(Delete), 0, CallFlags.WriteStates, false);
Expand Down Expand Up @@ -63,11 +65,19 @@ protected internal byte[] Get(StorageContext context, byte[] key)
protected internal IIterator Find(StorageContext context, byte[] prefix)
{
byte[] prefix_key = StorageKey.CreateSearchPrefix(context.Id, prefix);
StorageIterator iterator = new StorageIterator(Snapshot.Storages.Find(prefix_key).Where(p => p.Key.Key.AsSpan().StartsWith(prefix)).GetEnumerator());
StorageIterator iterator = new StorageIterator(Snapshot.Storages.Find(prefix_key).GetEnumerator());
Disposables.Add(iterator);
return iterator;
}

protected internal IEnumerator FindKeys(StorageContext context, byte[] prefix, byte removePrefix)
{
byte[] prefix_key = StorageKey.CreateSearchPrefix(context.Id, prefix);
StorageKeyEnumerator enumerator = new StorageKeyEnumerator(Snapshot.Storages.Find(prefix_key).Select(p => p.Key).GetEnumerator(), removePrefix);
Disposables.Add(enumerator);
return enumerator;
}

protected internal void Put(StorageContext context, byte[] key, byte[] value)
{
PutExInternal(context, key, value, StorageFlags.None);
Expand Down
36 changes: 36 additions & 0 deletions src/neo/SmartContract/Enumerators/StorageKeyEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Neo.Ledger;
using Neo.VM.Types;
using System.Collections.Generic;

namespace Neo.SmartContract.Enumerators
{
internal class StorageKeyEnumerator : IEnumerator
{
private readonly IEnumerator<StorageKey> enumerator;
private readonly byte removePrefix;

public StorageKeyEnumerator(IEnumerator<StorageKey> enumerator, byte removePrefix)
{
this.enumerator = enumerator;
this.removePrefix = removePrefix;
}

public void Dispose()
{
enumerator.Dispose();
}

public bool Next()
{
return enumerator.MoveNext();
}

public StackItem Value()
{
byte[] key = enumerator.Current.Key;
if (removePrefix > 0)
key = key[removePrefix..];
return key;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Ledger;
using Neo.SmartContract.Enumerators;
using System;
using System.Collections.Generic;

namespace Neo.UnitTests.SmartContract.Enumerators
{
[TestClass]
public class UT_StorageKeyEnumerator
{
private class TestEnumeratorDispose : IEnumerator<StorageKey>
{
public bool IsDisposed { get; private set; }
public StorageKey Current => throw new NotImplementedException();
object System.Collections.IEnumerator.Current => throw new NotImplementedException();
public void Dispose()
{
IsDisposed = true;
}
public bool MoveNext() => throw new NotImplementedException();
public void Reset() => throw new NotImplementedException();
}

[TestMethod]
public void TestGeneratorAndDispose()
{
var enumerator = new TestEnumeratorDispose();
var iterator = new StorageKeyEnumerator(enumerator, 0);
Action action = () => iterator.Dispose();
enumerator.IsDisposed.Should().BeFalse();
action.Should().NotThrow<Exception>();
enumerator.IsDisposed.Should().BeTrue();
}

[TestMethod]
public void TestNextAndValue()
{
var list = new List<StorageKey>
{
new StorageKey() { Id = 1, Key = new byte[] { 1, 2, 3 } },
new StorageKey() { Id = 1, Key = new byte[] { 4, 5, 6 } }
};

// With prefix

var iterator = new StorageKeyEnumerator(list.GetEnumerator(), 0);
Action actionTrue = () => iterator.Next().Should().BeTrue();
actionTrue.Should().NotThrow<Exception>();
CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, iterator.Value().GetSpan().ToArray());
actionTrue.Should().NotThrow<Exception>();
CollectionAssert.AreEqual(new byte[] { 4, 5, 6 }, iterator.Value().GetSpan().ToArray());
Action actionFalse = () => iterator.Next().Should().BeFalse();
actionFalse.Should().NotThrow<Exception>();

// Without prefix

iterator = new StorageKeyEnumerator(list.GetEnumerator(), 2);
actionTrue = () => iterator.Next().Should().BeTrue();
actionTrue.Should().NotThrow<Exception>();
CollectionAssert.AreEqual(new byte[] { 3 }, iterator.Value().GetSpan().ToArray());
actionTrue.Should().NotThrow<Exception>();
CollectionAssert.AreEqual(new byte[] { 6 }, iterator.Value().GetSpan().ToArray());
actionFalse = () => iterator.Next().Should().BeFalse();
actionFalse.Should().NotThrow<Exception>();
}
}
}