Skip to content

Commit

Permalink
Remove ContractFeatures (neo-project#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent 818665f commit bc6b3c4
Show file tree
Hide file tree
Showing 16 changed files with 13 additions and 102 deletions.
5 changes: 1 addition & 4 deletions src/neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public class ContractState : ICloneable<ContractState>, ISerializable, IInterope
public byte[] Script;
public ContractManifest Manifest;

public bool HasStorage => Manifest.Features.HasFlag(ContractFeatures.HasStorage);
public bool Payable => Manifest.Features.HasFlag(ContractFeatures.Payable);

private UInt160 _scriptHash;
public UInt160 ScriptHash
{
Expand Down Expand Up @@ -82,7 +79,7 @@ public JObject ToJson()

public StackItem ToStackItem(ReferenceCounter referenceCounter)
{
return new Array(referenceCounter, new StackItem[] { Script, Manifest.ToString(), HasStorage, Payable });
return new Array(referenceCounter, new StackItem[] { Script, Manifest.ToString() });
}
}
}
7 changes: 2 additions & 5 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ protected internal void UpdateContract(byte[] script, byte[] manifest)
contract.Manifest = ContractManifest.Parse(manifest);
if (!contract.Manifest.IsValid(contract.ScriptHash))
throw new InvalidOperationException($"Invalid Manifest Hash: {contract.ScriptHash}");
if (!contract.HasStorage && Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id)).Any())
throw new InvalidOperationException($"Contract Does Not Support Storage But Uses Storage");
}
if (script != null)
{
Expand All @@ -113,9 +111,8 @@ protected internal void DestroyContract()
ContractState contract = Snapshot.Contracts.TryGet(hash);
if (contract == null) return;
Snapshot.Contracts.Delete(hash);
if (contract.HasStorage)
foreach (var (key, _) in Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id)))
Snapshot.Storages.Delete(key);
foreach (var (key, _) in Snapshot.Storages.Find(BitConverter.GetBytes(contract.Id)))
Snapshot.Storages.Delete(key);
}

protected internal void CallContract(UInt160 contractHash, string method, Array args)
Expand Down
2 changes: 0 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ partial class ApplicationEngine
protected internal StorageContext GetStorageContext()
{
ContractState contract = Snapshot.Contracts.TryGet(CurrentScriptHash);
if (!contract.HasStorage) throw new InvalidOperationException();
return new StorageContext
{
Id = contract.Id,
Expand All @@ -34,7 +33,6 @@ protected internal StorageContext GetStorageContext()
protected internal StorageContext GetReadOnlyContext()
{
ContractState contract = Snapshot.Contracts.TryGet(CurrentScriptHash);
if (!contract.HasStorage) throw new InvalidOperationException();
return new StorageContext
{
Id = contract.Id,
Expand Down
13 changes: 0 additions & 13 deletions src/neo/SmartContract/Manifest/ContractFeatures.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/neo/SmartContract/Manifest/ContractManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public int Size
/// </summary>
public ContractGroup[] Groups { get; set; }

/// <summary>
/// The features field describes what features are available for the contract.
/// </summary>
public ContractFeatures Features { get; set; }

/// <summary>
/// NEP10 - SupportedStandards
/// </summary>
Expand Down Expand Up @@ -116,11 +111,6 @@ public JObject ToJson()
return new JObject
{
["groups"] = Groups.Select(u => u.ToJson()).ToArray(),
["features"] = new JObject
{
["storage"] = Features.HasFlag(ContractFeatures.HasStorage),
["payable"] = Features.HasFlag(ContractFeatures.Payable)
},
["supportedstandards"] = SupportedStandards.Select(u => new JString(u)).ToArray(),
["abi"] = Abi.ToJson(),
["permissions"] = Permissions.Select(p => p.ToJson()).ToArray(),
Expand All @@ -139,7 +129,6 @@ public ContractManifest Clone()
return new ContractManifest
{
Groups = Groups.Select(p => p.Clone()).ToArray(),
Features = Features,
SupportedStandards = SupportedStandards[..],
Abi = Abi.Clone(),
Permissions = Permissions.Select(p => p.Clone()).ToArray(),
Expand Down Expand Up @@ -168,9 +157,6 @@ public void Deserialize(BinaryReader reader)
private void DeserializeFromJson(JObject json)
{
Groups = ((JArray)json["groups"]).Select(u => ContractGroup.FromJson(u)).ToArray();
Features = ContractFeatures.NoProperty;
if (json["features"]["storage"].AsBoolean()) Features |= ContractFeatures.HasStorage;
if (json["features"]["payable"].AsBoolean()) Features |= ContractFeatures.Payable;
SupportedStandards = ((JArray)json["supportedstandards"]).Select(u => u.AsString()).ToArray();
Abi = ContractAbi.FromJson(json["abi"]);
Permissions = ((JArray)json["permissions"]).Select(u => ContractPermission.FromJson(u)).ToArray();
Expand Down
2 changes: 0 additions & 2 deletions src/neo/SmartContract/Native/Designate/DesignateContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.VM;
using Neo.VM.Types;
using System;
Expand All @@ -22,7 +21,6 @@ public sealed class DesignateContract : NativeContract

internal DesignateContract()
{
Manifest.Features = ContractFeatures.HasStorage;
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
Expand Down
1 change: 0 additions & 1 deletion src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ protected NativeContract()
this.Manifest = new ContractManifest
{
Groups = System.Array.Empty<ContractGroup>(),
Features = ContractFeatures.NoProperty,
SupportedStandards = new string[0],
Abi = new ContractAbi()
{
Expand Down
2 changes: 0 additions & 2 deletions src/neo/SmartContract/Native/Oracle/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public sealed class OracleContract : NativeContract

internal OracleContract()
{
Manifest.Features = ContractFeatures.HasStorage;

var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
{
new ContractEventDescriptor
Expand Down
4 changes: 1 addition & 3 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using System;
using System.Numerics;

Expand All @@ -21,9 +20,8 @@ public sealed class PolicyContract : NativeContract
private const byte Prefix_MaxBlockSize = 12;
private const byte Prefix_MaxBlockSystemFee = 17;

public PolicyContract()
internal PolicyContract()
{
Manifest.Features = ContractFeatures.HasStorage;
}

[ContractMethod(0_01000000, CallFlags.AllowStates)]
Expand Down
3 changes: 0 additions & 3 deletions src/neo/SmartContract/Native/Tokens/Nep5Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ protected Nep5Token()
{
this.Factor = BigInteger.Pow(10, Decimals);

Manifest.Features = ContractFeatures.HasStorage;
Manifest.SupportedStandards = new[] { "NEP-5" };

var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
Expand Down Expand Up @@ -111,8 +110,6 @@ protected virtual bool Transfer(ApplicationEngine engine, UInt160 from, UInt160
if (amount.Sign < 0) throw new ArgumentOutOfRangeException(nameof(amount));
if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
return false;
ContractState contract_to = engine.Snapshot.Contracts.TryGet(to);
if (contract_to?.Payable == false) return false;
StorageKey key_from = CreateStorageKey(Prefix_Account).Add(from);
StorageItem storage_from = engine.Snapshot.Storages.GetAndChange(key_from);
if (amount.IsZero)
Expand Down
14 changes: 1 addition & 13 deletions tests/neo.UnitTests/Ledger/UT_ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ public void TestSetup()
};
}

[TestMethod]
public void TestGetHasStorage()
{
contract.HasStorage.Should().BeFalse();
}

[TestMethod]
public void TestGetPayable()
{
contract.Payable.Should().BeFalse();
}

[TestMethod]
public void TestGetScriptHash()
{
Expand Down Expand Up @@ -84,7 +72,7 @@ public void TestDeserialize()
public void TestGetSize()
{
ISerializable newContract = contract;
newContract.Size.Should().Be(265);
newContract.Size.Should().Be(218);
}

[TestMethod]
Expand Down
26 changes: 7 additions & 19 deletions tests/neo.UnitTests/SmartContract/Manifest/UT_ContractManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,18 @@ public class UT_ContractManifest
[TestMethod]
public void ParseFromJson_Default()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);

Assert.AreEqual(manifest.ToString(), json);
Assert.AreEqual(manifest.ToString(), TestUtils.CreateDefaultManifest(UInt160.Zero).ToString());
Assert.IsTrue(manifest.IsValid(UInt160.Zero));
}

[TestMethod]
public void ParseFromJson_Features()
{
var json = @"{""groups"":[],""features"":{""storage"":true,""payable"":true},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToJson().ToString(), json);

var check = TestUtils.CreateDefaultManifest(UInt160.Zero);
check.Features = ContractFeatures.HasStorage | ContractFeatures.Payable;
Assert.AreEqual(manifest.ToString(), check.ToString());
}

[TestMethod]
public void ParseFromJson_Permissions()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -53,7 +41,7 @@ public void ParseFromJson_Permissions()
[TestMethod]
public void ParseFromJson_SafeMethods()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[""balanceOf""],""extra"":null}";
var json = @"{""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[""balanceOf""],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -65,7 +53,7 @@ public void ParseFromJson_SafeMethods()
[TestMethod]
public void ParseFromJson_Trust()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -77,7 +65,7 @@ public void ParseFromJson_Trust()
[TestMethod]
public void ParseFromJson_Groups()
{
var json = @"{""groups"":[{""pubkey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var json = @"{""groups"":[{""pubkey"":""03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"",""signature"":""QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQQ==""}],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":null}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(manifest.ToString(), json);

Expand All @@ -89,7 +77,7 @@ public void ParseFromJson_Groups()
[TestMethod]
public void ParseFromJson_Extra()
{
var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":{""key"":""value""}}";
var json = @"{""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":{""key"":""value""}}";
var manifest = ContractManifest.Parse(json);
Assert.AreEqual(json, json);
Assert.AreEqual("value", manifest.Extra["key"].AsString(), false);
Expand Down Expand Up @@ -122,7 +110,7 @@ public void TestGetHash()
public void TestGetSize()
{
var temp = TestUtils.CreateDefaultManifest(UInt160.Zero);
Assert.AreEqual(259, temp.Size);
Assert.AreEqual(212, temp.Size);
}

[TestMethod]
Expand Down
5 changes: 0 additions & 5 deletions tests/neo.UnitTests/SmartContract/UT_InteropPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Ledger;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;

namespace Neo.UnitTests.SmartContract
Expand Down Expand Up @@ -56,7 +55,6 @@ public void ApplicationEngineRegularPut()
byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(new byte[0] { });
Expand Down Expand Up @@ -90,7 +88,6 @@ public void ApplicationEngineReusedStorage_FullReuse()
byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(value);
Expand Down Expand Up @@ -126,7 +123,6 @@ public void ApplicationEngineReusedStorage_PartialReuse()
byte[] script = CreatePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);
Expand Down Expand Up @@ -163,7 +159,6 @@ public void ApplicationEngineReusedStorage_PartialReuseTwice()
byte[] script = CreateMultiplePutScript(key, value);

ContractState contractState = TestUtils.GetContract(script);
contractState.Manifest.Features = ContractFeatures.HasStorage;

StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);
Expand Down
3 changes: 0 additions & 3 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,8 @@ public void TestContract_Update()
Signature = signature
}
};
manifest.Features = ContractFeatures.HasStorage;
var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
state.Manifest.Features = ContractFeatures.HasStorage;
var storageItem = new StorageItem
{
Value = new byte[] { 0x01 },
Expand Down Expand Up @@ -215,7 +213,6 @@ public void TestStorage_Find()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
state.Manifest.Features = ContractFeatures.HasStorage;

var storageItem = new StorageItem
{
Expand Down
Loading

0 comments on commit bc6b3c4

Please sign in to comment.