Skip to content

Commit

Permalink
Fix CallFlags (#2292)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Feb 5, 2021
1 parent c3c395c commit 7f799b8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Neo.SmartContract
{
partial class ApplicationEngine
{
public static readonly InteropDescriptor System_Contract_Call = Register("System.Contract.Call", nameof(CallContract), 1 << 15, CallFlags.AllowCall);
public static readonly InteropDescriptor System_Contract_Call = Register("System.Contract.Call", nameof(CallContract), 1 << 15, CallFlags.ReadStates | CallFlags.AllowCall);
public static readonly InteropDescriptor System_Contract_CallNative = Register("System.Contract.CallNative", nameof(CallNativeContract), 0, CallFlags.None);
public static readonly InteropDescriptor System_Contract_IsStandard = Register("System.Contract.IsStandard", nameof(IsStandardContract), 1 << 10, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Contract_GetCallFlags = Register("System.Contract.GetCallFlags", nameof(GetCallFlags), 1 << 10, CallFlags.None);
Expand Down
6 changes: 2 additions & 4 deletions src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected internal bool CheckWitnessInternal(UInt160 hash)
{
// Check allow state callflag

if (!CurrentContext.GetState<ExecutionContextState>().CallFlags.HasFlag(CallFlags.ReadStates))
throw new InvalidOperationException($"Cannot call this SYSCALL without the flag AllowStates.");
ValidateCallFlags(CallFlags.ReadStates);

var contract = NativeContract.ContractManagement.GetContract(Snapshot, CallingScriptHash);
// check if current group is the required one
Expand All @@ -102,8 +101,7 @@ protected internal bool CheckWitnessInternal(UInt160 hash)

// Check allow state callflag

if (!CurrentContext.GetState<ExecutionContextState>().CallFlags.HasFlag(CallFlags.ReadStates))
throw new InvalidOperationException($"Cannot call this SYSCALL without the flag AllowStates.");
ValidateCallFlags(CallFlags.ReadStates);

// only for non-Transaction types (Block, etc)

Expand Down
7 changes: 4 additions & 3 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public ExecutionContext LoadScript(Script script, int rvcount = -1, int initialP

protected override ExecutionContext LoadToken(ushort tokenId)
{
ValidateCallFlags(CallFlags.ReadStates | CallFlags.AllowCall);
ContractState contract = CurrentContext.GetState<ExecutionContextState>().Contract;
if (contract is null || tokenId >= contract.Nef.Tokens.Length)
throw new InvalidOperationException();
Expand Down Expand Up @@ -281,17 +282,17 @@ public override void Dispose()
base.Dispose();
}

protected void ValidateCallFlags(InteropDescriptor descriptor)
protected void ValidateCallFlags(CallFlags requiredCallFlags)
{
ExecutionContextState state = CurrentContext.GetState<ExecutionContextState>();
if (!state.CallFlags.HasFlag(descriptor.RequiredCallFlags))
if (!state.CallFlags.HasFlag(requiredCallFlags))
throw new InvalidOperationException($"Cannot call this SYSCALL with the flag {state.CallFlags}.");
}

protected override void OnSysCall(uint method)
{
InteropDescriptor descriptor = services[method];
ValidateCallFlags(descriptor);
ValidateCallFlags(descriptor.RequiredCallFlags);
AddGas(descriptor.FixedPrice * exec_fee_factor);
List<object> parameters = descriptor.Parameters.Count > 0
? new List<object>()
Expand Down

0 comments on commit 7f799b8

Please sign in to comment.