Skip to content

Commit

Permalink
Add StateLess to CallFlags (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Apr 26, 2020
1 parent 55f2d7e commit 735bd07
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 47 deletions.
11 changes: 6 additions & 5 deletions src/neo/SmartContract/CallFlags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public enum CallFlags : byte
{
None = 0,

AllowModifyStates = 0b00000001,
AllowCall = 0b00000010,
AllowNotify = 0b00000100,
AllowStates = 0b00000001,
AllowModifyStates = 0b00000010,
AllowCall = 0b00000100,
AllowNotify = 0b00001000,

ReadOnly = AllowCall | AllowNotify,
All = AllowModifyStates | AllowCall | AllowNotify
ReadOnly = AllowStates | AllowCall | AllowNotify,
All = AllowStates | AllowModifyStates | AllowCall | AllowNotify
}
}
12 changes: 6 additions & 6 deletions src/neo/SmartContract/InteropService.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public static class Blockchain
{
public const uint MaxTraceableBlocks = Transaction.MaxValidUntilBlockIncrement;

public static readonly InteropDescriptor GetHeight = Register("System.Blockchain.GetHeight", Blockchain_GetHeight, 0_00000400, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetBlock = Register("System.Blockchain.GetBlock", Blockchain_GetBlock, 0_02500000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransaction = Register("System.Blockchain.GetTransaction", Blockchain_GetTransaction, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", Blockchain_GetTransactionFromBlock, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetContract = Register("System.Blockchain.GetContract", Blockchain_GetContract, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetHeight = Register("System.Blockchain.GetHeight", Blockchain_GetHeight, 0_00000400, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetBlock = Register("System.Blockchain.GetBlock", Blockchain_GetBlock, 0_02500000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetTransaction = Register("System.Blockchain.GetTransaction", Blockchain_GetTransaction, 0_01000000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetTransactionHeight = Register("System.Blockchain.GetTransactionHeight", Blockchain_GetTransactionHeight, 0_01000000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetTransactionFromBlock = Register("System.Blockchain.GetTransactionFromBlock", Blockchain_GetTransactionFromBlock, 0_01000000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetContract = Register("System.Blockchain.GetContract", Blockchain_GetContract, 0_01000000, TriggerType.Application, CallFlags.AllowStates);

private static bool Blockchain_GetHeight(ApplicationEngine engine)
{
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/InteropService.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public static class Runtime

public static readonly InteropDescriptor Platform = Register("System.Runtime.Platform", Runtime_Platform, 0_00000250, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GetTrigger = Register("System.Runtime.GetTrigger", Runtime_GetTrigger, 0_00000250, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GetTime = Register("System.Runtime.GetTime", Runtime_GetTime, 0_00000250, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetTime = Register("System.Runtime.GetTime", Runtime_GetTime, 0_00000250, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetScriptContainer = Register("System.Runtime.GetScriptContainer", Runtime_GetScriptContainer, 0_00000250, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GetExecutingScriptHash = Register("System.Runtime.GetExecutingScriptHash", Runtime_GetExecutingScriptHash, 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GetCallingScriptHash = Register("System.Runtime.GetCallingScriptHash", Runtime_GetCallingScriptHash, 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor GetEntryScriptHash = Register("System.Runtime.GetEntryScriptHash", Runtime_GetEntryScriptHash, 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor CheckWitness = Register("System.Runtime.CheckWitness", Runtime_CheckWitness, 0_00030000, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor CheckWitness = Register("System.Runtime.CheckWitness", Runtime_CheckWitness, 0_00030000, TriggerType.All, CallFlags.AllowStates);
public static readonly InteropDescriptor GetInvocationCounter = Register("System.Runtime.GetInvocationCounter", Runtime_GetInvocationCounter, 0_00000400, TriggerType.All, CallFlags.None);
public static readonly InteropDescriptor Log = Register("System.Runtime.Log", Runtime_Log, 0_01000000, TriggerType.All, CallFlags.AllowNotify);
public static readonly InteropDescriptor Notify = Register("System.Runtime.Notify", Runtime_Notify, 0_01000000, TriggerType.All, CallFlags.AllowNotify);
Expand Down
10 changes: 5 additions & 5 deletions src/neo/SmartContract/InteropService.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public static class Storage
public const int MaxKeySize = 64;
public const int MaxValueSize = ushort.MaxValue;

public static readonly InteropDescriptor GetContext = Register("System.Storage.GetContext", Storage_GetContext, 0_00000400, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetReadOnlyContext = Register("System.Storage.GetReadOnlyContext", Storage_GetReadOnlyContext, 0_00000400, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor AsReadOnly = Register("System.Storage.AsReadOnly", Storage_AsReadOnly, 0_00000400, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor Get = Register("System.Storage.Get", Storage_Get, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor Find = Register("System.Storage.Find", Storage_Find, 0_01000000, TriggerType.Application, CallFlags.None);
public static readonly InteropDescriptor GetContext = Register("System.Storage.GetContext", Storage_GetContext, 0_00000400, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor GetReadOnlyContext = Register("System.Storage.GetReadOnlyContext", Storage_GetReadOnlyContext, 0_00000400, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor AsReadOnly = Register("System.Storage.AsReadOnly", Storage_AsReadOnly, 0_00000400, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor Get = Register("System.Storage.Get", Storage_Get, 0_01000000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor Find = Register("System.Storage.Find", Storage_Find, 0_01000000, TriggerType.Application, CallFlags.AllowStates);
public static readonly InteropDescriptor Put = Register("System.Storage.Put", Storage_Put, GetStoragePrice, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor PutEx = Register("System.Storage.PutEx", Storage_PutEx, GetStoragePrice, TriggerType.Application, CallFlags.AllowModifyStates);
public static readonly InteropDescriptor Delete = Register("System.Storage.Delete", Storage_Delete, 1 * GasPerByte, TriggerType.Application, CallFlags.AllowModifyStates);
Expand Down
5 changes: 3 additions & 2 deletions src/neo/SmartContract/Native/ContractMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ internal class ContractMethodAttribute : Attribute
public ContractParameterType ReturnType { get; }
public ContractParameterType[] ParameterTypes { get; set; } = Array.Empty<ContractParameterType>();
public string[] ParameterNames { get; set; } = Array.Empty<string>();
public bool SafeMethod { get; set; } = false;
public CallFlags RequiredCallFlags { get; }

public ContractMethodAttribute(long price, ContractParameterType returnType)
public ContractMethodAttribute(long price, ContractParameterType returnType, CallFlags requiredCallFlags)
{
this.Price = price;
this.ReturnType = returnType;
this.RequiredCallFlags = requiredCallFlags;
}
}
}
8 changes: 4 additions & 4 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ protected NativeContract()
ReturnType = attribute.ReturnType,
Parameters = attribute.ParameterTypes.Zip(attribute.ParameterNames, (t, n) => new ContractParameterDefinition { Type = t, Name = n }).ToArray()
});
if (attribute.SafeMethod) safeMethods.Add(name);
if (!attribute.RequiredCallFlags.HasFlag(CallFlags.AllowModifyStates)) safeMethods.Add(name);
methods.Add(name, new ContractMethodMetadata
{
Delegate = (Func<ApplicationEngine, Array, StackItem>)method.CreateDelegate(typeof(Func<ApplicationEngine, Array, StackItem>), this),
Price = attribute.Price,
RequiredCallFlags = attribute.SafeMethod ? CallFlags.None : CallFlags.AllowModifyStates
RequiredCallFlags = attribute.RequiredCallFlags
});
}
this.Manifest = new ContractManifest
Expand Down Expand Up @@ -133,7 +133,7 @@ internal virtual bool Initialize(ApplicationEngine engine)
return true;
}

[ContractMethod(0, ContractParameterType.Boolean)]
[ContractMethod(0, ContractParameterType.Boolean, CallFlags.AllowModifyStates)]
protected StackItem OnPersist(ApplicationEngine engine, Array args)
{
if (engine.Trigger != TriggerType.System) return false;
Expand All @@ -145,7 +145,7 @@ protected virtual bool OnPersist(ApplicationEngine engine)
return true;
}

[ContractMethod(0, ContractParameterType.Array, Name = "supportedStandards", SafeMethod = true)]
[ContractMethod(0, ContractParameterType.Array, CallFlags.None, Name = "supportedStandards")]
protected StackItem SupportedStandardsMethod(ApplicationEngine engine, Array args)
{
return new Array(engine.ReferenceCounter, SupportedStandards.Select(p => (StackItem)p));
Expand Down
18 changes: 9 additions & 9 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal override bool Initialize(ApplicationEngine engine)
return true;
}

[ContractMethod(0_01000000, ContractParameterType.Integer, SafeMethod = true)]
[ContractMethod(0_01000000, ContractParameterType.Integer, CallFlags.AllowStates)]
private StackItem GetMaxTransactionsPerBlock(ApplicationEngine engine, Array args)
{
return GetMaxTransactionsPerBlock(engine.Snapshot);
Expand All @@ -78,7 +78,7 @@ public uint GetMaxTransactionsPerBlock(StoreView snapshot)
return BitConverter.ToUInt32(snapshot.Storages[CreateStorageKey(Prefix_MaxTransactionsPerBlock)].Value, 0);
}

[ContractMethod(0_01000000, ContractParameterType.Integer, SafeMethod = true)]
[ContractMethod(0_01000000, ContractParameterType.Integer, CallFlags.AllowStates)]
private StackItem GetMaxBlockSize(ApplicationEngine engine, Array args)
{
return GetMaxBlockSize(engine.Snapshot);
Expand All @@ -89,7 +89,7 @@ public uint GetMaxBlockSize(StoreView snapshot)
return BitConverter.ToUInt32(snapshot.Storages[CreateStorageKey(Prefix_MaxBlockSize)].Value, 0);
}

[ContractMethod(0_01000000, ContractParameterType.Integer, SafeMethod = true)]
[ContractMethod(0_01000000, ContractParameterType.Integer, CallFlags.AllowStates)]
private StackItem GetFeePerByte(ApplicationEngine engine, Array args)
{
return GetFeePerByte(engine.Snapshot);
Expand All @@ -100,7 +100,7 @@ public long GetFeePerByte(StoreView snapshot)
return BitConverter.ToInt64(snapshot.Storages[CreateStorageKey(Prefix_FeePerByte)].Value, 0);
}

[ContractMethod(0_01000000, ContractParameterType.Array, SafeMethod = true)]
[ContractMethod(0_01000000, ContractParameterType.Array, CallFlags.AllowStates)]
private StackItem GetBlockedAccounts(ApplicationEngine engine, Array args)
{
return new Array(engine.ReferenceCounter, GetBlockedAccounts(engine.Snapshot).Select(p => (StackItem)p.ToArray()));
Expand All @@ -111,7 +111,7 @@ public UInt160[] GetBlockedAccounts(StoreView snapshot)
return snapshot.Storages[CreateStorageKey(Prefix_BlockedAccounts)].Value.AsSerializableArray<UInt160>();
}

[ContractMethod(0_03000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
[ContractMethod(0_03000000, ContractParameterType.Boolean, CallFlags.AllowModifyStates, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
private StackItem SetMaxBlockSize(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
Expand All @@ -122,7 +122,7 @@ private StackItem SetMaxBlockSize(ApplicationEngine engine, Array args)
return true;
}

[ContractMethod(0_03000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
[ContractMethod(0_03000000, ContractParameterType.Boolean, CallFlags.AllowModifyStates, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
private StackItem SetMaxTransactionsPerBlock(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
Expand All @@ -132,7 +132,7 @@ private StackItem SetMaxTransactionsPerBlock(ApplicationEngine engine, Array arg
return true;
}

[ContractMethod(0_03000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
[ContractMethod(0_03000000, ContractParameterType.Boolean, CallFlags.AllowModifyStates, ParameterTypes = new[] { ContractParameterType.Integer }, ParameterNames = new[] { "value" })]
private StackItem SetFeePerByte(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
Expand All @@ -142,7 +142,7 @@ private StackItem SetFeePerByte(ApplicationEngine engine, Array args)
return true;
}

[ContractMethod(0_03000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Hash160 }, ParameterNames = new[] { "account" })]
[ContractMethod(0_03000000, ContractParameterType.Boolean, CallFlags.AllowModifyStates, ParameterTypes = new[] { ContractParameterType.Hash160 }, ParameterNames = new[] { "account" })]
private StackItem BlockAccount(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
Expand All @@ -156,7 +156,7 @@ private StackItem BlockAccount(ApplicationEngine engine, Array args)
return true;
}

[ContractMethod(0_03000000, ContractParameterType.Boolean, ParameterTypes = new[] { ContractParameterType.Hash160 }, ParameterNames = new[] { "account" })]
[ContractMethod(0_03000000, ContractParameterType.Boolean, CallFlags.AllowModifyStates, ParameterTypes = new[] { ContractParameterType.Hash160 }, ParameterNames = new[] { "account" })]
private StackItem UnblockAccount(ApplicationEngine engine, Array args)
{
if (!CheckValidators(engine)) return false;
Expand Down
Loading

0 comments on commit 735bd07

Please sign in to comment.