Skip to content

Commit

Permalink
Rename native contracts (#2152)
Browse files Browse the repository at this point in the history
* Rename native contracts

* Fix

* RoleManagement

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
erikzhang and shargon authored Dec 17, 2020
1 parent e8d08a5 commit 13c626c
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/neo/Network/P2P/Payloads/OracleResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public override bool Verify(StoreView snapshot, Transaction tx)
OracleRequest request = NativeContract.Oracle.GetRequest(snapshot, Id);
if (request is null) return false;
if (tx.NetworkFee + tx.SystemFee != request.GasForResponse) return false;
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.Designation.GetDesignatedByRole(snapshot, Role.Oracle, snapshot.Height + 1));
UInt160 oracleAccount = Blockchain.GetConsensusAddress(NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.Oracle, snapshot.Height + 1));
return tx.Signers.Any(p => p.Account.Equals(oracleAccount));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected internal void CallContractEx(UInt160 contractHash, string method, Arra

private void CallContractInternal(UInt160 contractHash, string method, Array args, CallFlags flags, ReturnTypeConvention convention)
{
ContractState contract = NativeContract.Management.GetContract(Snapshot, contractHash);
ContractState contract = NativeContract.ContractManagement.GetContract(Snapshot, contractHash);
if (contract is null) throw new InvalidOperationException($"Called Contract Does Not Exist: {contractHash}");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) throw new InvalidOperationException($"Method {method} Does Not Exist In Contract {contractHash}");
Expand All @@ -49,7 +49,7 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg
}
else
{
ContractState currentContract = NativeContract.Management.GetContract(Snapshot, CurrentScriptHash);
ContractState currentContract = NativeContract.ContractManagement.GetContract(Snapshot, CurrentScriptHash);
if (currentContract?.CanCall(contract, method) == false)
throw new InvalidOperationException($"Cannot Call Method {method} Of Contract {contractHash} From Contract {CurrentScriptHash}");
}
Expand Down Expand Up @@ -101,7 +101,7 @@ protected internal void CallNativeContract(string name)

protected internal bool IsStandardContract(UInt160 hash)
{
ContractState contract = NativeContract.Management.GetContract(Snapshot, hash);
ContractState contract = NativeContract.ContractManagement.GetContract(Snapshot, hash);

// It's a stored contract

Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected internal bool CheckWitnessInternal(UInt160 hash)
if (!CurrentContext.GetState<ExecutionContextState>().CallFlags.HasFlag(CallFlags.ReadStates))
throw new InvalidOperationException($"Cannot call this SYSCALL without the flag AllowStates.");

var contract = NativeContract.Management.GetContract(Snapshot, CallingScriptHash);
var contract = NativeContract.ContractManagement.GetContract(Snapshot, CallingScriptHash);
// check if current group is the required one
if (contract.Manifest.Groups.Select(p => p.PubKey).Intersect(signer.AllowedGroups).Any())
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ partial class ApplicationEngine

protected internal StorageContext GetStorageContext()
{
ContractState contract = NativeContract.Management.GetContract(Snapshot, CurrentScriptHash);
ContractState contract = NativeContract.ContractManagement.GetContract(Snapshot, CurrentScriptHash);
return new StorageContext
{
Id = contract.Id,
Expand All @@ -32,7 +32,7 @@ protected internal StorageContext GetStorageContext()

protected internal StorageContext GetReadOnlyContext()
{
ContractState contract = NativeContract.Management.GetContract(Snapshot, CurrentScriptHash);
ContractState contract = NativeContract.ContractManagement.GetContract(Snapshot, CurrentScriptHash);
return new StorageContext
{
Id = contract.Id,
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Callbacks/MethodCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public MethodCallback(ApplicationEngine engine, UInt160 hash, string method)
: base(ApplicationEngine.System_Contract_Call, false)
{
if (method.StartsWith('_')) throw new ArgumentException();
this.contract = NativeContract.Management.GetContract(engine.Snapshot, hash);
ContractState currentContract = NativeContract.Management.GetContract(engine.Snapshot, engine.CurrentScriptHash);
this.contract = NativeContract.ContractManagement.GetContract(engine.Snapshot, hash);
ContractState currentContract = NativeContract.ContractManagement.GetContract(engine.Snapshot, engine.CurrentScriptHash);
if (currentContract?.CanCall(this.contract, method) == false)
throw new InvalidOperationException();
this.method = this.contract.Manifest.Abi.Methods.First(p => p.Name == method);
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapsh

if (verification.Length == 0)
{
ContractState cs = NativeContract.Management.GetContract(snapshot, hash);
ContractState cs = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (cs is null) return false;
if (engine.LoadContract(cs, "verify", callFlags, true) is null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

namespace Neo.SmartContract.Native
{
public sealed class ManagementContract : NativeContract
public sealed class ContractManagement : NativeContract
{
public override int Id => 0;

private const byte Prefix_MinimumDeploymentFee = 20;
private const byte Prefix_NextAvailableId = 15;
private const byte Prefix_Contract = 8;

internal ManagementContract()
internal ContractManagement()
{
var events = new List<ContractEventDescriptor>(Manifest.Abi.Events)
{
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public abstract class NativeContract
private readonly Dictionary<string, ContractMethodMetadata> methods = new Dictionary<string, ContractMethodMetadata>();

public static IReadOnlyCollection<NativeContract> Contracts { get; } = contractsList;
public static ManagementContract Management { get; } = new ManagementContract();
public static ContractManagement ContractManagement { get; } = new ContractManagement();
public static NeoToken NEO { get; } = new NeoToken();
public static GasToken GAS { get; } = new GasToken();
public static PolicyContract Policy { get; } = new PolicyContract();
public static OracleContract Oracle { get; } = new OracleContract();
public static DesignationContract Designation { get; } = new DesignationContract();
public static RoleManagement RoleManagement { get; } = new RoleManagement();

public string Name => GetType().Name;
public byte[] Script { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/Native/Nep17Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void PostTransfer(ApplicationEngine engine, UInt160 from, UInt160 to, Bi

// Check if it's a wallet or smart contract

if (!callOnPayment || to is null || Management.GetContract(engine.Snapshot, to) is null) return;
if (!callOnPayment || to is null || ContractManagement.GetContract(engine.Snapshot, to) is null) return;

// Call onPayment method (NEP-17)

Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/OracleContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal override void PostPersist(ApplicationEngine engine)
if (list.Count == 0) engine.Snapshot.Storages.Delete(key);

//Mint GAS for oracle nodes
nodes ??= Designation.GetDesignatedByRole(engine.Snapshot, Role.Oracle, engine.Snapshot.PersistingBlock.Index).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
nodes ??= RoleManagement.GetDesignatedByRole(engine.Snapshot, Role.Oracle, engine.Snapshot.PersistingBlock.Index).Select(p => (Contract.CreateSignatureRedeemScript(p).ToScriptHash(), BigInteger.Zero)).ToArray();
if (nodes.Length > 0)
{
int index = (int)(response.Id % (ulong)nodes.Length);
Expand Down Expand Up @@ -191,7 +191,7 @@ private void Request(ApplicationEngine engine, string url, string filter, string
item_id.Value = BitConverter.GetBytes(id);

//Put the request to storage
if (Management.GetContract(engine.Snapshot, engine.CallingScriptHash) is null)
if (ContractManagement.GetContract(engine.Snapshot, engine.CallingScriptHash) is null)
throw new InvalidOperationException();
engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_Request).Add(item_id.Value), new StorageItem(new OracleRequest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma warning disable IDE0051

using Neo.Cryptography;
using Neo.Cryptography.ECC;
using Neo.IO;
Expand All @@ -14,11 +12,11 @@

namespace Neo.SmartContract.Native
{
public sealed class DesignationContract : NativeContract
public sealed class RoleManagement : NativeContract
{
public override int Id => -5;

internal DesignationContract()
internal RoleManagement()
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/neo/Wallets/AssetDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AssetDescriptor
public AssetDescriptor(UInt160 asset_id)
{
using SnapshotView snapshot = Blockchain.Singleton.GetSnapshot();
var contract = NativeContract.Management.GetContract(snapshot, asset_id);
var contract = NativeContract.ContractManagement.GetContract(snapshot, asset_id);
if (contract is null) throw new ArgumentException();

byte[] script;
Expand Down
4 changes: 2 additions & 2 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public long CalculateNetworkFee(StoreView snapshot, Transaction tx)

if (witness_script is null)
{
var contract = NativeContract.Management.GetContract(snapshot, hash);
var contract = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (contract is null) continue;

// Empty invocation and verification scripts
Expand Down Expand Up @@ -471,7 +471,7 @@ public bool Sign(ContractParametersContext context)
// Try Smart contract verification

using var snapshot = Blockchain.Singleton.GetSnapshot();
var contract = NativeContract.Management.GetContract(snapshot, scriptHash);
var contract = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);

if (contract != null)
{
Expand Down
12 changes: 6 additions & 6 deletions tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class NativeContractExtensions
public static ContractState DeployContract(this StoreView snapshot, UInt160 sender, byte[] nefFile, byte[] manifest, long gas = 200_00000000)
{
var script = new ScriptBuilder();
script.EmitAppCall(NativeContract.Management.Hash, "deploy", nefFile, manifest);
script.EmitAppCall(NativeContract.ContractManagement.Hash, "deploy", nefFile, manifest);

var engine = ApplicationEngine.Create(TriggerType.Application,
sender != null ? new Transaction() { Signers = new Signer[] { new Signer() { Account = sender } } } : null, snapshot, gas);
Expand All @@ -34,7 +34,7 @@ public static ContractState DeployContract(this StoreView snapshot, UInt160 send
public static void UpdateContract(this StoreView snapshot, UInt160 callingScriptHash, byte[] nefFile, byte[] manifest)
{
var script = new ScriptBuilder();
script.EmitAppCall(NativeContract.Management.Hash, "update", nefFile, manifest);
script.EmitAppCall(NativeContract.ContractManagement.Hash, "update", nefFile, manifest);

var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(script.ToArray());
Expand All @@ -57,7 +57,7 @@ public static void UpdateContract(this StoreView snapshot, UInt160 callingScript
public static void DestroyContract(this StoreView snapshot, UInt160 callingScriptHash)
{
var script = new ScriptBuilder();
script.EmitAppCall(NativeContract.Management.Hash, "destroy");
script.EmitAppCall(NativeContract.ContractManagement.Hash, "destroy");

var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(script.ToArray());
Expand All @@ -79,13 +79,13 @@ public static void DestroyContract(this StoreView snapshot, UInt160 callingScrip

public static void AddContract(this StoreView snapshot, UInt160 hash, ContractState state)
{
var key = new KeyBuilder(NativeContract.Management.Id, 8).Add(hash);
var key = new KeyBuilder(NativeContract.ContractManagement.Id, 8).Add(hash);
snapshot.Storages.Add(key, new Neo.Ledger.StorageItem(state, false));
}

public static void DeleteContract(this StoreView snapshot, UInt160 hash)
{
var key = new KeyBuilder(NativeContract.Management.Id, 8).Add(hash);
var key = new KeyBuilder(NativeContract.ContractManagement.Id, 8).Add(hash);
snapshot.Storages.Delete(key);
}

Expand All @@ -97,7 +97,7 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, s
public static StackItem Call(this NativeContract contract, StoreView snapshot, IVerifiable container, string method, params ContractParameter[] args)
{
var engine = ApplicationEngine.Create(TriggerType.Application, container, snapshot);
var contractState = NativeContract.Management.GetContract(snapshot, contract.Hash);
var contractState = NativeContract.ContractManagement.GetContract(snapshot, contract.Hash);
if (contractState == null) throw new InvalidOperationException();

engine.LoadContract(contractState, method, CallFlags.All, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void TestSetup()
_snapshot.PersistingBlock = new Block() { Index = 0 };

ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, _snapshot.PersistingBlock, _snapshot, 0);
NativeContract.Management.OnPersist(engine);
NativeContract.ContractManagement.OnPersist(engine);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Neo.UnitTests.SmartContract.Native
{
[TestClass]
public class UT_DesignationContract
public class UT_RoleManagement
{
private StoreView _snapshot;

Expand All @@ -36,7 +36,7 @@ public void TestSetAndGet()
};
UInt160 committeeMultiSigAddr = NativeContract.NEO.GetCommitteeAddress(snapshot1);
ECPoint[] validators = NativeContract.NEO.ComputeNextBlockValidators(snapshot1);
var ret = NativeContract.Designation.Call(
var ret = NativeContract.RoleManagement.Call(
snapshot1,
new Nep17NativeContractExtensions.ManualWitness(committeeMultiSigAddr),
"designateAsRole",
Expand All @@ -45,7 +45,7 @@ public void TestSetAndGet()
);
snapshot1.Commit();
var snapshot2 = _snapshot.Clone();
ret = NativeContract.Designation.Call(
ret = NativeContract.RoleManagement.Call(
snapshot2,
"getDesignatedByRole",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
Expand All @@ -61,7 +61,7 @@ public void TestSetAndGet()
(ret as VM.Types.Array)[5].GetSpan().ToHexString().Should().Be(validators[5].ToArray().ToHexString());
(ret as VM.Types.Array)[6].GetSpan().ToHexString().Should().Be(validators[6].ToArray().ToHexString());

ret = NativeContract.Designation.Call(
ret = NativeContract.RoleManagement.Call(
snapshot2,
"getDesignatedByRole",
new ContractParameter(ContractParameterType.Integer) { Value = new BigInteger((int)Role.StateValidator) },
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void TestContract_Update()
snapshot.Storages.Add(storageKey, storageItem);
state.UpdateCounter.Should().Be(0);
snapshot.UpdateContract(state.Hash, nef.ToArray(), manifest.ToJson().ToByteArray(false));
var ret = NativeContract.Management.GetContract(snapshot, state.Hash);
var ret = NativeContract.ContractManagement.GetContract(snapshot, state.Hash);
snapshot.Storages.Find(BitConverter.GetBytes(state.Id)).ToList().Count().Should().Be(1);
ret.UpdateCounter.Should().Be(1);
ret.Id.Should().Be(state.Id);
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,14 @@ public void TestBlockchain_GetContract()
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01 };
Neo.SmartContract.Native.NativeContract.Management.GetContract(engine.Snapshot, new UInt160(data1)).Should().BeNull();
Neo.SmartContract.Native.NativeContract.ContractManagement.GetContract(engine.Snapshot, new UInt160(data1)).Should().BeNull();

var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract();
snapshot.AddContract(state.Hash, state);
engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(new byte[] { 0x01 });
Neo.SmartContract.Native.NativeContract.Management.GetContract(engine.Snapshot, state.Hash).Should().BeSameAs(state);
Neo.SmartContract.Native.NativeContract.ContractManagement.GetContract(engine.Snapshot, state.Hash).Should().BeSameAs(state);
}

[TestMethod]
Expand Down

0 comments on commit 13c626c

Please sign in to comment.