-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Read only methods #1049
Closed
Closed
Read only methods #1049
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ea39390
Update VM to last changes
shargon 86aff3e
Remove hashes
shargon fa580d2
ReadOnly
shargon 1e0496a
Rename to ReadOnly
shargon 6759e5a
Fix unit tests
shargon be5d7a1
Refactor readOnly check
shargon aa3dce2
Reduce changes
shargon 8960dd8
Update VM to last changes (#1048)
shargon 6c6ab1a
Update VM to last changes
shargon 3bc6e80
ReadOnly
shargon cab7f01
Rename to ReadOnly
shargon fe0986a
Fix unit tests
shargon dd65378
Refactor readOnly check
shargon 288c9ff
Reduce changes
shargon a7e4485
Merge
shargon 6107a8d
Merge remote-tracking branch 'origin/read-only' into read-only
shargon 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 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 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 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,15 @@ | ||
namespace Neo.SmartContract | ||
{ | ||
public class ExecutionContextState | ||
{ | ||
/// <summary> | ||
/// Script hash | ||
/// </summary> | ||
public UInt160 ScriptHash { get; set; } | ||
|
||
/// <summary> | ||
/// Is read only | ||
/// </summary> | ||
public bool ReadOnly { get; set; } = false; | ||
} | ||
} |
This file contains 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 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -55,8 +55,8 @@ public static partial class InteropService | |||||
public static readonly uint System_Block_GetTransaction = Register("System.Block.GetTransaction", Block_GetTransaction, 0_00000400, TriggerType.Application); | ||||||
public static readonly uint System_Transaction_GetHash = Register("System.Transaction.GetHash", Transaction_GetHash, 0_00000400, TriggerType.All); | ||||||
public static readonly uint System_Contract_Call = Register("System.Contract.Call", Contract_Call, 0_01000000, TriggerType.System | TriggerType.Application); | ||||||
public static readonly uint System_Contract_Destroy = Register("System.Contract.Destroy", Contract_Destroy, 0_01000000, TriggerType.Application); | ||||||
public static readonly uint System_Storage_GetContext = Register("System.Storage.GetContext", Storage_GetContext, 0_00000400, TriggerType.Application); | ||||||
public static readonly uint System_Contract_Destroy = Register("System.Contract.Destroy", Contract_Destroy, 0_01000000, TriggerType.Application, true); | ||||||
public static readonly uint System_Storage_GetContext = Register("System.Storage.GetContext", Storage_GetContext, 0_00000400, TriggerType.Application, true); | ||||||
public static readonly uint System_Storage_GetReadOnlyContext = Register("System.Storage.GetReadOnlyContext", Storage_GetReadOnlyContext, 0_00000400, TriggerType.Application); | ||||||
public static readonly uint System_Storage_Get = Register("System.Storage.Get", Storage_Get, 0_01000000, TriggerType.Application); | ||||||
public static readonly uint System_Storage_Put = Register("System.Storage.Put", Storage_Put, GetStoragePrice, TriggerType.Application); | ||||||
|
@@ -88,19 +88,21 @@ internal static bool Invoke(ApplicationEngine engine, uint method) | |||||
return false; | ||||||
if (!descriptor.AllowedTriggers.HasFlag(engine.Trigger)) | ||||||
return false; | ||||||
if (descriptor.RequireWriteAccess && engine.CurrentContext.GetState<ExecutionContextState>().ReadOnly) | ||||||
return false; | ||||||
return descriptor.Handler(engine); | ||||||
} | ||||||
|
||||||
private static uint Register(string method, Func<ApplicationEngine, bool> handler, long price, TriggerType allowedTriggers) | ||||||
private static uint Register(string method, Func<ApplicationEngine, bool> handler, long price, TriggerType allowedTriggers, bool requireWriteAccess = false) | ||||||
{ | ||||||
InteropDescriptor descriptor = new InteropDescriptor(method, handler, price, allowedTriggers); | ||||||
InteropDescriptor descriptor = new InteropDescriptor(method, handler, price, allowedTriggers, requireWriteAccess); | ||||||
methods.Add(descriptor.Hash, descriptor); | ||||||
return descriptor.Hash; | ||||||
} | ||||||
|
||||||
private static uint Register(string method, Func<ApplicationEngine, bool> handler, Func<RandomAccessStack<StackItem>, long> priceCalculator, TriggerType allowedTriggers) | ||||||
private static uint Register(string method, Func<ApplicationEngine, bool> handler, Func<RandomAccessStack<StackItem>, long> priceCalculator, TriggerType allowedTriggers, bool requireWriteAccess = false) | ||||||
{ | ||||||
InteropDescriptor descriptor = new InteropDescriptor(method, handler, priceCalculator, allowedTriggers); | ||||||
InteropDescriptor descriptor = new InteropDescriptor(method, handler, priceCalculator, allowedTriggers, requireWriteAccess); | ||||||
methods.Add(descriptor.Hash, descriptor); | ||||||
return descriptor.Hash; | ||||||
} | ||||||
|
@@ -537,9 +539,10 @@ private static bool Contract_Call(ApplicationEngine engine) | |||||
|
||||||
StackItem method = engine.CurrentContext.EvaluationStack.Pop(); | ||||||
StackItem args = engine.CurrentContext.EvaluationStack.Pop(); | ||||||
string methodStr = method.GetString(); | ||||||
ContractManifest currentManifest = engine.Snapshot.Contracts.TryGet(engine.CurrentScriptHash)?.Manifest; | ||||||
|
||||||
if (currentManifest != null && !currentManifest.CanCall(contract.Manifest, method.GetString())) | ||||||
if (currentManifest != null && !currentManifest.CanCall(contract.Manifest, methodStr)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @neo-project/core @erikzhang current Manifest could be null? i think that it is a MUST
Suggested change
|
||||||
return false; | ||||||
|
||||||
if (engine.InvocationCounter.TryGetValue(contract.ScriptHash, out var counter)) | ||||||
|
@@ -552,6 +555,9 @@ private static bool Contract_Call(ApplicationEngine engine) | |||||
} | ||||||
|
||||||
ExecutionContext context_new = engine.LoadScript(contract.Script, 1); | ||||||
context_new.GetState<ExecutionContextState>().ReadOnly = currentManifest != null && | ||||||
(currentManifest.ReadOnlyMethods.IsWildcard || currentManifest.ReadOnlyMethods.Contains(methodStr)); | ||||||
|
||||||
context_new.EvaluationStack.Push(args); | ||||||
context_new.EvaluationStack.Push(method); | ||||||
return true; | ||||||
|
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.
@igormcoelho I increased the scope not only for the storages, for destroy, update, and create too