Skip to content

Commit

Permalink
Merge branch 'add-nep11' of https://github.com/Ashuaidehao/neo-modules
Browse files Browse the repository at this point in the history
…into add-nep11
  • Loading branch information
Ashuaidehao committed Oct 19, 2021
2 parents b57db78 + 2ebd447 commit 66b5657
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/StateService/MPT/MPTTrie.Find.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,22 @@ private ReadOnlySpan<byte> Seek(ref MPTNode node, ReadOnlySpan<byte> path, out M
if (path.Length > MPTNode.MaxKeyLength || from.Length > MPTNode.MaxKeyLength)
throw new ArgumentException("exceeds limit");
path = Seek(ref root, path, out MPTNode start).ToArray();
offset = path.Length;
if (from.Length > 0)
{
for (int i = 0; i < from.Length && i < path.Length; i++)
{
if (path[i] < from[i]) return Enumerable.Empty<(TKey Key, TValue Value)>();
if (path[i] > from[i])
{
offset = from.Length;
break;
}
}
if (offset == 0)
{
offset = Math.Min(path.Length, from.Length);
}
}
return Travers(start, path, from, offset)
.Select(p => (FromNibbles(p.Key).AsSerializable<TKey>(), p.Value.AsSerializable<TValue>()));
}
Expand Down
17 changes: 16 additions & 1 deletion tests/Neo.Plugins.StateService.Tests/MPT/UT_MPTTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,22 @@ public void TestFindWithFrom()
r = mpt.Find("aa".HexToBytes(), "aa60".HexToBytes()).ToList();
Assert.AreEqual(0, r.Count);
r = mpt.Find("aa".HexToBytes(), "aa10".HexToBytes()).ToList();
Assert.AreEqual(1, r.Count);//without from key
Assert.AreEqual(1, r.Count);
}

[TestMethod]
public void TestFindStatesIssue652()
{
var snapshot = new TestSnapshot();
var mpt = new MPTTrie<TestKey, TestValue>(snapshot, null);
mpt.Put("abc1".HexToBytes(), "01".HexToBytes());
mpt.Put("abc3".HexToBytes(), "02".HexToBytes());
var r = mpt.Find("ab".HexToBytes(), "abd2".HexToBytes()).ToList();
Assert.AreEqual(0, r.Count);
r = mpt.Find("ab".HexToBytes(), "abb2".HexToBytes()).ToList();
Assert.AreEqual(2, r.Count);
r = mpt.Find("ab".HexToBytes(), "abc2".HexToBytes()).ToList();
Assert.AreEqual(1, r.Count);
}
}
}

0 comments on commit 66b5657

Please sign in to comment.