-
Notifications
You must be signed in to change notification settings - Fork 226
Fix snapshot point read cache options #1079
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
Open
Jim8y
wants to merge
3
commits into
master-n3
Choose a base branch
from
fix-snapshot-read-cache
base: master-n3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−16
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/Neo.Plugins.Storage.Tests/SnapshotReadOptionsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (C) 2015-2026 The Neo Project. | ||
| // | ||
| // SnapshotReadOptionsTest.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using System.Reflection; | ||
|
|
||
| namespace Neo.Plugins.Storage.Tests; | ||
|
|
||
| [TestClass] | ||
| public class SnapshotReadOptionsTest | ||
| { | ||
| [TestMethod] | ||
| public void LevelDbSnapshotUsesDedicatedReadOptionsForScanAndPointReads() | ||
| { | ||
| var snapshotType = GetSnapshotType(typeof(LevelDBStore)); | ||
| var scanReadOptions = GetField(snapshotType, "_scanReadOptions"); | ||
| var pointReadOptions = GetField(snapshotType, "_pointReadOptions"); | ||
|
|
||
| AssertMethodUsesOnly(snapshotType, "Find", scanReadOptions, pointReadOptions); | ||
| AssertMethodUsesOnly(snapshotType, "GetEnumerator", scanReadOptions, pointReadOptions); | ||
| AssertMethodUsesOnly(snapshotType, "Contains", pointReadOptions, scanReadOptions); | ||
| AssertMethodUsesOnly(snapshotType, "TryGet", pointReadOptions, scanReadOptions); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void RocksDbSnapshotUsesDedicatedReadOptionsForScanAndPointReads() | ||
| { | ||
| var snapshotType = GetSnapshotType(typeof(RocksDBStore)); | ||
| var scanOptions = GetField(snapshotType, "_scanOptions"); | ||
| var pointOptions = GetField(snapshotType, "_pointOptions"); | ||
|
|
||
| AssertMethodUsesOnly(snapshotType, "Find", scanOptions, pointOptions); | ||
| AssertMethodUsesOnly(snapshotType, "Contains", pointOptions, scanOptions); | ||
| AssertMethodUsesOnly(snapshotType, "TryGet", pointOptions, scanOptions); | ||
| } | ||
|
|
||
| private static Type GetSnapshotType(Type storeProviderType) | ||
| { | ||
| return storeProviderType.Assembly.GetType("Neo.Plugins.Storage.Snapshot", throwOnError: true)!; | ||
| } | ||
|
|
||
| private static FieldInfo GetField(Type type, string fieldName) | ||
| { | ||
| var field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); | ||
| Assert.IsNotNull(field, $"{type.FullName} should declare {fieldName}."); | ||
| return field; | ||
| } | ||
|
|
||
| private static void AssertMethodUsesOnly(Type type, string methodName, FieldInfo expectedField, FieldInfo unexpectedField) | ||
| { | ||
| var implementations = GetImplementations(type, methodName).ToArray(); | ||
| Assert.IsNotEmpty(implementations, $"{type.FullName}.{methodName} should exist."); | ||
| Assert.IsTrue( | ||
| implementations.Any(method => UsesField(method, expectedField)), | ||
| $"{type.FullName}.{methodName} should use {expectedField.Name}."); | ||
| Assert.IsFalse( | ||
| implementations.Any(method => UsesField(method, unexpectedField)), | ||
| $"{type.FullName}.{methodName} should not use {unexpectedField.Name}."); | ||
| } | ||
|
|
||
| private static IEnumerable<MethodBase> GetImplementations(Type type, string methodName) | ||
| { | ||
| const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; | ||
|
|
||
| foreach (var method in type.GetMethods(flags).Where(method => method.Name == methodName)) | ||
| yield return method; | ||
|
|
||
| foreach (var nestedType in type.GetNestedTypes(BindingFlags.NonPublic)) | ||
| { | ||
| if (!nestedType.Name.Contains($"<{methodName}>")) continue; | ||
|
|
||
| var moveNext = nestedType.GetMethod("MoveNext", flags); | ||
| if (moveNext != null) | ||
| yield return moveNext; | ||
| } | ||
| } | ||
|
|
||
| private static bool UsesField(MethodBase method, FieldInfo field) | ||
| { | ||
| var body = method.GetMethodBody()?.GetILAsByteArray(); | ||
| if (body is null) return false; | ||
|
|
||
| var token = BitConverter.GetBytes(field.MetadataToken); | ||
| for (var i = 0; i <= body.Length - token.Length; i++) | ||
| { | ||
| if (body[i] == token[0] | ||
| && body[i + 1] == token[1] | ||
| && body[i + 2] == token[2] | ||
| && body[i + 3] == token[3]) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion]
LevelDbSnapshotUsesDedicatedReadOptionsForScanAndPointReadsandRocksDbSnapshotUsesDedicatedReadOptionsForScanAndPointReadsnever construct a snapshot and never inspect fill-cache. They only scan method IL for the_scan*vs_point*field tokens. A constructor that setFillCache=falseon the point-read options (or forgotSetFillCache(false)on the scan options) would still pass, which is the behavior this PR is meant to lock in.UsesFieldalso matches a metadata token anywhere in the body (noldfld/ldfldacheck) andAssertMethodUsesOnlytreats “any overload/state-machine method references the expected field” as success, so oneTryGetoverload could silently miss the point-read options if the other hit.Suggestion: Also scan the constructors (or otherwise assert setup) so scan options are the only ones that disable fill-cache, and require every
TryGetoverload to load the point-read field. Prefer opcode-awareldfldmatching over a raw 4-byte token search.