Skip to content

Commit

Permalink
Fix CallFlags for native contracts and syscalls (#2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Feb 17, 2021
1 parent 9f21f3c commit 79d462e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ partial class ApplicationEngine
/// </summary>
public static readonly InteropDescriptor System_Contract_CreateStandardAccount = Register("System.Contract.CreateStandardAccount", nameof(CreateStandardAccount), 1 << 8, CallFlags.None);
public static readonly InteropDescriptor System_Contract_CreateMultisigAccount = Register("System.Contract.CreateMultisigAccount", nameof(CreateMultisigAccount), 1 << 8, CallFlags.None);
public static readonly InteropDescriptor System_Contract_NativeOnPersist = Register("System.Contract.NativeOnPersist", nameof(NativeOnPersist), 0, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Contract_NativePostPersist = Register("System.Contract.NativePostPersist", nameof(NativePostPersist), 0, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Contract_NativeOnPersist = Register("System.Contract.NativeOnPersist", nameof(NativeOnPersist), 0, CallFlags.States);
public static readonly InteropDescriptor System_Contract_NativePostPersist = Register("System.Contract.NativePostPersist", nameof(NativePostPersist), 0, CallFlags.States);

protected internal void CallContract(UInt160 contractHash, string method, CallFlags callFlags, Array args)
{
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/ApplicationEngine.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Storage_AsReadOnly = Register("System.Storage.AsReadOnly", nameof(AsReadOnly), 1 << 4, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Get = Register("System.Storage.Get", nameof(Get), 1 << 15, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Find = Register("System.Storage.Find", nameof(Find), 1 << 15, CallFlags.ReadStates);
public static readonly InteropDescriptor System_Storage_Put = Register("System.Storage.Put", nameof(Put), 0, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Storage_PutEx = Register("System.Storage.PutEx", nameof(PutEx), 0, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Storage_Delete = Register("System.Storage.Delete", nameof(Delete), 0, CallFlags.WriteStates);
public static readonly InteropDescriptor System_Storage_Put = Register("System.Storage.Put", nameof(Put), 0, CallFlags.States);
public static readonly InteropDescriptor System_Storage_PutEx = Register("System.Storage.PutEx", nameof(PutEx), 0, CallFlags.States);
public static readonly InteropDescriptor System_Storage_Delete = Register("System.Storage.Delete", nameof(Delete), 0, CallFlags.States);

protected internal StorageContext GetStorageContext()
{
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private ExecutionContext CallContractInternal(ContractState contract, ContractMe
{
if (method.Safe)
{
flags &= ~CallFlags.WriteStates;
flags &= ~(CallFlags.WriteStates | CallFlags.AllowNotify);
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions src/neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private long GetMinimumDeploymentFee(DataCache snapshot)
return (long)(BigInteger)snapshot[CreateStorageKey(Prefix_MinimumDeploymentFee)];
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetMinimumDeploymentFee(ApplicationEngine engine, BigInteger value)
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value));
Expand All @@ -131,13 +131,13 @@ public IEnumerable<ContractState> ListContracts(DataCache snapshot)
return snapshot.Find(listContractsPrefix).Select(kvp => kvp.Value.GetInteroperable<ContractState>());
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] manifest)
{
return Deploy(engine, nefFile, manifest, StackItem.Null);
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] manifest, StackItem data)
{
if (engine.ScriptContainer is not Transaction tx)
Expand Down Expand Up @@ -183,13 +183,13 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma
return contract;
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest)
{
Update(engine, nefFile, manifest, StackItem.Null);
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest, StackItem data)
{
if (nefFile is null && manifest is null) throw new ArgumentException();
Expand Down Expand Up @@ -229,7 +229,7 @@ private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest, S
engine.SendNotification(Hash, "Update", new VM.Types.Array { contract.Hash.ToArray() });
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private void Destroy(ApplicationEngine engine)
{
UInt160 hash = engine.CallingScriptHash;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/FungibleToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public virtual BigInteger BalanceOf(DataCache snapshot, UInt160 account)
return storage.GetInteroperable<TState>().Balance;
}

[ContractMethod(CpuFee = 1 << 17, StorageFee = 50, RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowCall | CallFlags.AllowNotify)]
[ContractMethod(CpuFee = 1 << 17, StorageFee = 50, RequiredCallFlags = CallFlags.States | CallFlags.AllowCall | CallFlags.AllowNotify)]
protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160 to, BigInteger amount, StackItem data)
{
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
Expand Down
14 changes: 7 additions & 7 deletions src/neo/SmartContract/Native/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected override byte[] GetKey(byte[] tokenId)
return Crypto.Hash160(tokenId);
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void AddRoot(ApplicationEngine engine, string root)
{
if (!rootRegex.IsMatch(root)) throw new ArgumentException(null, nameof(root));
Expand All @@ -81,7 +81,7 @@ public IEnumerable<string> GetRoots(DataCache snapshot)
return snapshot[CreateStorageKey(Prefix_Roots)].GetInteroperable<StringList>();
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetPrice(ApplicationEngine engine, long price)
{
if (price <= 0 || price > 10000_00000000) throw new ArgumentOutOfRangeException(nameof(price));
Expand All @@ -108,7 +108,7 @@ public bool IsAvailable(DataCache snapshot, string name)
return true;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private bool Register(ApplicationEngine engine, string name, UInt160 owner)
{
if (!nameRegex.IsMatch(name)) throw new ArgumentException(null, nameof(name));
Expand All @@ -131,7 +131,7 @@ private bool Register(ApplicationEngine engine, string name, UInt160 owner)
return true;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private uint Renew(ApplicationEngine engine, string name)
{
if (!nameRegex.IsMatch(name)) throw new ArgumentException(null, nameof(name));
Expand All @@ -146,7 +146,7 @@ private uint Renew(ApplicationEngine engine, string name)
return state.Expiration;
}

[ContractMethod(CpuFee = 1 << 15, StorageFee = 20, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, StorageFee = 20, RequiredCallFlags = CallFlags.States)]
private void SetAdmin(ApplicationEngine engine, string name, UInt160 admin)
{
if (!nameRegex.IsMatch(name)) throw new ArgumentException(null, nameof(name));
Expand All @@ -165,7 +165,7 @@ private static bool CheckAdmin(ApplicationEngine engine, NameState state)
return engine.CheckWitnessInternal(state.Admin);
}

[ContractMethod(CpuFee = 1 << 15, StorageFee = 200, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, StorageFee = 200, RequiredCallFlags = CallFlags.States)]
private void SetRecord(ApplicationEngine engine, string name, RecordType type, string data)
{
if (!nameRegex.IsMatch(name)) throw new ArgumentException(null, nameof(name));
Expand Down Expand Up @@ -218,7 +218,7 @@ public string GetRecord(DataCache snapshot, string name, RecordType type)
yield return ((RecordType)key.Key[^1], Utility.StrictUTF8.GetString(value.Value));
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void DeleteRecord(ApplicationEngine engine, string name, RecordType type)
{
if (!nameRegex.IsMatch(name)) throw new ArgumentException(null, nameof(name));
Expand Down
10 changes: 5 additions & 5 deletions src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ internal override void PostPersist(ApplicationEngine engine)
}
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetGasPerBlock(ApplicationEngine engine, BigInteger gasPerBlock)
{
if (gasPerBlock < 0 || gasPerBlock > 10 * GAS.Factor)
Expand All @@ -190,7 +190,7 @@ public BigInteger GetGasPerBlock(DataCache snapshot)
return GetSortedGasRecords(snapshot, Ledger.CurrentIndex(snapshot) + 1).First().GasPerBlock;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetRegisterPrice(ApplicationEngine engine, long registerPrice)
{
if (registerPrice <= 0)
Expand Down Expand Up @@ -222,7 +222,7 @@ public BigInteger UnclaimedGas(DataCache snapshot, UInt160 account, uint end)
return CalculateBonus(snapshot, state.VoteTo, state.Balance, state.BalanceHeight, end);
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(RequiredCallFlags = CallFlags.States)]
private bool RegisterCandidate(ApplicationEngine engine, ECPoint pubkey)
{
if (!engine.CheckWitnessInternal(Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash()))
Expand All @@ -235,7 +235,7 @@ private bool RegisterCandidate(ApplicationEngine engine, ECPoint pubkey)
return true;
}

[ContractMethod(CpuFee = 1 << 16, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 16, RequiredCallFlags = CallFlags.States)]
private bool UnregisterCandidate(ApplicationEngine engine, ECPoint pubkey)
{
if (!engine.CheckWitnessInternal(Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash()))
Expand All @@ -249,7 +249,7 @@ private bool UnregisterCandidate(ApplicationEngine engine, ECPoint pubkey)
return true;
}

[ContractMethod(CpuFee = 1 << 16, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 16, RequiredCallFlags = CallFlags.States)]
private bool Vote(ApplicationEngine engine, UInt160 account, ECPoint voteTo)
{
if (!engine.CheckWitnessInternal(account)) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/NonfungibleToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected IIterator TokensOf(DataCache snapshot, UInt160 owner)
return new ArrayWrapper(tokens.Select(p => (StackItem)p).ToArray());
}

[ContractMethod(CpuFee = 1 << 17, StorageFee = 50, RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(CpuFee = 1 << 17, StorageFee = 50, RequiredCallFlags = CallFlags.States | CallFlags.AllowCall | CallFlags.AllowNotify)]
protected bool Transfer(ApplicationEngine engine, UInt160 to, byte[] tokenId)
{
if (to is null) throw new ArgumentNullException(nameof(to));
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal OracleContract()
Manifest.Abi.Events = events.ToArray();
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetPrice(ApplicationEngine engine, long price)
{
if (price <= 0)
Expand All @@ -95,7 +95,7 @@ public long GetPrice(DataCache snapshot)
return (long)(BigInteger)snapshot[CreateStorageKey(Prefix_Price)];
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowCall | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowCall | CallFlags.AllowNotify)]
private void Finish(ApplicationEngine engine)
{
Transaction tx = (Transaction)engine.ScriptContainer;
Expand Down Expand Up @@ -184,7 +184,7 @@ internal override void PostPersist(ApplicationEngine engine)
}
}

[ContractMethod(RequiredCallFlags = CallFlags.WriteStates | CallFlags.AllowNotify)]
[ContractMethod(RequiredCallFlags = CallFlags.States | CallFlags.AllowNotify)]
private void Request(ApplicationEngine engine, string url, string filter, string callback, StackItem userData, long gasForResponse)
{
//Check the arguments
Expand Down
10 changes: 5 additions & 5 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,31 @@ public bool IsBlocked(DataCache snapshot, UInt160 account)
return snapshot.Contains(CreateStorageKey(Prefix_BlockedAccount).Add(account));
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetFeePerByte(ApplicationEngine engine, long value)
{
if (value < 0 || value > 1_00000000) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_FeePerByte)).Set(value);
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetExecFeeFactor(ApplicationEngine engine, uint value)
{
if (value == 0 || value > MaxExecFeeFactor) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_ExecFeeFactor)).Set(value);
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void SetStoragePrice(ApplicationEngine engine, uint value)
{
if (value == 0 || value > MaxStoragePrice) throw new ArgumentOutOfRangeException(nameof(value));
if (!CheckCommittee(engine)) throw new InvalidOperationException();
engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_StoragePrice)).Set(value);
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private bool BlockAccount(ApplicationEngine engine, UInt160 account)
{
if (!CheckCommittee(engine)) throw new InvalidOperationException();
Expand All @@ -89,7 +89,7 @@ private bool BlockAccount(ApplicationEngine engine, UInt160 account)
return true;
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private bool UnblockAccount(ApplicationEngine engine, UInt160 account)
{
if (!CheckCommittee(engine)) throw new InvalidOperationException();
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/RoleManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ECPoint[] GetDesignatedByRole(DataCache snapshot, Role role, uint index)
.FirstOrDefault() ?? System.Array.Empty<ECPoint>();
}

[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.WriteStates)]
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.States)]
private void DesignateAsRole(ApplicationEngine engine, Role role, ECPoint[] nodes)
{
if (nodes.Length == 0 || nodes.Length > 32)
Expand Down

0 comments on commit 79d462e

Please sign in to comment.