Skip to content

Commit

Permalink
Add overloads on ContractManagement (neo-project#2242)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent dcb1bdd commit ea6909c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public IEnumerable<ContractState> ListContracts(StoreView snapshot)
return snapshot.Storages.Find(listContractsPrefix).Select(kvp => kvp.Value.GetInteroperable<ContractState>());
}

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

[ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)]
private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] manifest, StackItem data)
{
Expand Down Expand Up @@ -164,6 +170,12 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma
return contract;
}

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

[ContractMethod(0, CallFlags.WriteStates | CallFlags.AllowNotify)]
private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest, StackItem data)
{
Expand Down
6 changes: 3 additions & 3 deletions src/neo/SmartContract/Native/NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class NativeContract
private static readonly List<NativeContract> contractsList = new List<NativeContract>();
private static readonly Dictionary<int, NativeContract> contractsIdDictionary = new Dictionary<int, NativeContract>();
private static readonly Dictionary<UInt160, NativeContract> contractsHashDictionary = new Dictionary<UInt160, NativeContract>();
private readonly Dictionary<string, ContractMethodMetadata> methods = new Dictionary<string, ContractMethodMetadata>();
private readonly Dictionary<(string, int), ContractMethodMetadata> methods = new Dictionary<(string, int), ContractMethodMetadata>();
private static int id_counter = 0;

#region Named Native Contracts
Expand Down Expand Up @@ -69,7 +69,7 @@ protected NativeContract()
Parameters = metadata.Parameters.Select(p => new ContractParameterDefinition { Type = ToParameterType(p.Type), Name = p.Name }).ToArray(),
Safe = (attribute.RequiredCallFlags & ~CallFlags.ReadOnly) == 0
});
methods.Add(metadata.Name, metadata);
methods.Add((metadata.Name, metadata.Parameters.Length), metadata);
}
this.Manifest = new ContractManifest
{
Expand Down Expand Up @@ -121,7 +121,7 @@ internal void Invoke(ApplicationEngine engine)
throw new InvalidOperationException("It is not allowed to use Neo.Native.Call directly to call native contracts. System.Contract.Call should be used.");
ExecutionContext context = engine.CurrentContext;
string operation = context.EvaluationStack.Pop().GetString();
ContractMethodMetadata method = methods[operation];
ContractMethodMetadata method = methods[(operation, context.EvaluationStack.Count)];
ExecutionContextState state = context.GetState<ExecutionContextState>();
if (!state.CallFlags.HasFlag(method.RequiredCallFlags))
throw new InvalidOperationException($"Cannot call this method with the flag {state.CallFlags}.");
Expand Down
5 changes: 0 additions & 5 deletions tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using VMArray = Neo.VM.Types.Array;

namespace Neo.UnitTests.SmartContract.Native
{
Expand Down Expand Up @@ -100,14 +99,10 @@ public void TestInvoke()
engine.LoadScript(testNativeContract.Script, configureState: p => p.ScriptHash = testNativeContract.Hash);

ByteString method1 = new ByteString(System.Text.Encoding.Default.GetBytes("wrongMethod"));
VMArray args1 = new VMArray();
engine.CurrentContext.EvaluationStack.Push(args1);
engine.CurrentContext.EvaluationStack.Push(method1);
Assert.ThrowsException<KeyNotFoundException>(() => testNativeContract.Invoke(engine));

ByteString method2 = new ByteString(System.Text.Encoding.Default.GetBytes("helloWorld"));
VMArray args2 = new VMArray();
engine.CurrentContext.EvaluationStack.Push(args2);
engine.CurrentContext.EvaluationStack.Push(method2);
testNativeContract.Invoke(engine);
}
Expand Down

0 comments on commit ea6909c

Please sign in to comment.