Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow method overloads #2237

Merged
merged 7 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected internal void CallContract(UInt160 contractHash, string method, CallFl

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}");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method, args.Count);
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
if (md is null) throw new InvalidOperationException($"Method \"{method}\" with {args.Count} parameter(s) doesn't exist in the contract {contractHash}.");
bool hasReturnValue = md.ReturnType != ContractParameterType.Void;

if (!hasReturnValue) CurrentContext.EvaluationStack.Push(StackItem.Null);
Expand Down
17 changes: 7 additions & 10 deletions src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private ExecutionContext CallContractInternal(UInt160 contractHash, string metho
{
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}");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method, args.Length);
if (md is null) throw new InvalidOperationException($"Method \"{method}\" with {args.Length} parameter(s) doesn't exist in the contract {contractHash}.");
return CallContractInternal(contract, md, flags, hasReturnValue, args);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ private ExecutionContext CallContractInternal(ContractState contract, ContractMe

if (args.Count != method.Parameters.Length) throw new InvalidOperationException($"Method {method} Expects {method.Parameters.Length} Arguments But Receives {args.Count} Arguments");
if (hasReturnValue ^ (method.ReturnType != ContractParameterType.Void)) throw new InvalidOperationException("The return value type does not match.");
ExecutionContext context_new = LoadContract(contract, method.Name, flags & callingFlags, hasReturnValue);
ExecutionContext context_new = LoadContract(contract, method, flags & callingFlags);
state = context_new.GetState<ExecutionContextState>();
state.CallingScriptHash = callingScriptHash;

Expand Down Expand Up @@ -163,14 +163,11 @@ protected override void LoadContext(ExecutionContext context)
base.LoadContext(context);
}

public ExecutionContext LoadContract(ContractState contract, string method, CallFlags callFlags, bool hasReturnValue)
public ExecutionContext LoadContract(ContractState contract, ContractMethodDescriptor method, CallFlags callFlags)
{
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) return null;

ExecutionContext context = LoadScript(contract.Script,
rvcount: hasReturnValue ? 1 : 0,
initialPosition: md.Offset,
rvcount: method.ReturnType == ContractParameterType.Void ? 0 : 1,
initialPosition: method.Offset,
configureState: p =>
{
p.CallFlags = callFlags;
Expand All @@ -179,7 +176,7 @@ public ExecutionContext LoadContract(ContractState contract, string method, Call
});

// Call initialization
var init = contract.Manifest.Abi.GetMethod("_initialize");
var init = contract.Manifest.Abi.GetMethod("_initialize", 0);
if (init != null)
{
LoadContext(context.Clone(init.Offset));
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/DeployedContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public DeployedContract(ContractState contract)

Script = null;
ScriptHash = contract.Hash;
ContractMethodDescriptor descriptor = contract.Manifest.Abi.GetMethod("verify");
ContractMethodDescriptor descriptor = contract.Manifest.Abi.GetMethod("verify", -1);
if (descriptor is null) throw new NotSupportedException("The smart contract haven't got verify method.");

ParameterList = descriptor.Parameters.Select(u => u.Type).ToArray();
Expand Down
6 changes: 4 additions & 2 deletions src/neo/SmartContract/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
Expand Down Expand Up @@ -187,8 +188,9 @@ internal static bool VerifyWitness(this IVerifiable verifiable, StoreView snapsh
{
ContractState cs = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (cs is null) return false;
if (engine.LoadContract(cs, "verify", callFlags, true) is null)
return false;
ContractMethodDescriptor md = cs.Manifest.Abi.GetMethod("verify", -1);
if (md?.ReturnType != ContractParameterType.Boolean) return false;
engine.LoadContract(cs, md, callFlags);
}
else
{
Expand Down
21 changes: 15 additions & 6 deletions src/neo/SmartContract/Manifest/ContractAbi.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Neo.IO.Json;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.SmartContract.Manifest
{
/// <summary>
/// For technical details of ABI, please refer to NEP-3: NeoContract ABI. (https://github.com/neo-project/proposals/blob/master/nep-3.mediawiki)
/// NeoContract ABI
/// </summary>
public class ContractAbi
{
private IReadOnlyDictionary<string, ContractMethodDescriptor> methodDictionary;
private IReadOnlyDictionary<(string, int), ContractMethodDescriptor> methodDictionary;

/// <summary>
/// Methods is an array of Method objects which describe the details of each method in the contract.
Expand Down Expand Up @@ -44,11 +45,19 @@ public static ContractAbi FromJson(JObject json)
};
}

public ContractMethodDescriptor GetMethod(string name)
public ContractMethodDescriptor GetMethod(string name, int pcount)
{
methodDictionary ??= Methods.ToDictionary(p => p.Name);
methodDictionary.TryGetValue(name, out var method);
return method;
if (pcount < -1 || pcount > ushort.MaxValue) throw new ArgumentOutOfRangeException(nameof(pcount));
if (pcount >= 0)
{
methodDictionary ??= Methods.ToDictionary(p => (p.Name, p.Parameters.Length));
methodDictionary.TryGetValue((name, pcount), out var method);
shargon marked this conversation as resolved.
Show resolved Hide resolved
return method;
}
else
{
return Methods.FirstOrDefault(p => p.Name == name);
}
}

public JObject ToJson()
Expand Down
4 changes: 2 additions & 2 deletions src/neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private ContractState Deploy(ApplicationEngine engine, byte[] nefFile, byte[] ma

// Execute _deploy

ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod("_deploy");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod("_deploy", 2);
if (md != null)
engine.CallFromNativeContract(Hash, hash, md.Name, data, false);

Expand Down Expand Up @@ -196,7 +196,7 @@ private void Update(ApplicationEngine engine, byte[] nefFile, byte[] manifest, S
contract.UpdateCounter++; // Increase update counter
if (nefFile != null)
{
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod("_deploy");
ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod("_deploy", 2);
if (md != null)
engine.CallFromNativeContract(Hash, contract.Hash, md.Name, data, true);
}
Expand Down
8 changes: 6 additions & 2 deletions src/neo/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,18 @@ public long CalculateNetworkFee(StoreView snapshot, Transaction tx)
{
var contract = NativeContract.ContractManagement.GetContract(snapshot, hash);
if (contract is null) continue;
var md = contract.Manifest.Abi.GetMethod("verify", 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var md = contract.Manifest.Abi.GetMethod("verify", 0);
var md = contract.Manifest.Abi.GetMethod("verify", -1);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the wallet support 0 parameter only. Because no InvocationScript is provided.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a side note, but we use verify with parameter (a signature) in the Notary contract (#1573). But this can be accounted for when/if implementing support for it.

if (md is null)
throw new ArgumentException($"The smart contract {contract.Hash} haven't got verify method");
shargon marked this conversation as resolved.
Show resolved Hide resolved
if (md.ReturnType != ContractParameterType.Boolean)
throw new ArgumentException("The verify method doesn't return boolean value.");

// Empty invocation and verification scripts
size += Array.Empty<byte>().GetVarSize() * 2;

// Check verify cost
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
if (engine.LoadContract(contract, "verify", CallFlags.None, true) is null)
throw new ArgumentException($"The smart contract {contract.Hash} haven't got verify method");
engine.LoadContract(contract, md, CallFlags.None);
if (NativeContract.IsNative(hash)) engine.Push("verify");
if (engine.Execute() == VMState.FAULT) throw new ArgumentException($"Smart contract {contract.Hash} verification fault.");
if (!engine.ResultStack.Pop().GetBoolean()) throw new ArgumentException($"Smart contract {contract.Hash} returns false.");
Expand Down
3 changes: 2 additions & 1 deletion tests/neo.UnitTests/Extensions/NativeContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ public static StackItem Call(this NativeContract contract, StoreView snapshot, I
var engine = ApplicationEngine.Create(TriggerType.Application, container, snapshot, persistingBlock);
var contractState = NativeContract.ContractManagement.GetContract(snapshot, contract.Hash);
if (contractState == null) throw new InvalidOperationException();
var md = contract.Manifest.Abi.GetMethod(method, args.Length);

var script = new ScriptBuilder();

for (var i = args.Length - 1; i >= 0; i--)
script.EmitPush(args[i]);

script.EmitPush(method);
engine.LoadContract(contractState, method, CallFlags.All, contract.Manifest.Abi.GetMethod(method).ReturnType != ContractParameterType.Void);
engine.LoadContract(contractState, md, CallFlags.All);
engine.LoadScript(script.ToArray());

if (engine.Execute() != VMState.HALT)
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void VMTypes(
public void TestToParameter()
{
var manifest = new DummyNative().Manifest;
var netTypes = manifest.Abi.GetMethod("netTypes");
var netTypes = manifest.Abi.GetMethod("netTypes", 17);

Assert.AreEqual(netTypes.ReturnType, ContractParameterType.Void);
Assert.AreEqual(netTypes.Parameters[0].Type, ContractParameterType.Boolean);
Expand All @@ -72,7 +72,7 @@ public void TestToParameter()
Assert.AreEqual(netTypes.Parameters[15].Type, ContractParameterType.Integer);
Assert.AreEqual(netTypes.Parameters[16].Type, ContractParameterType.Any);

var vmTypes = manifest.Abi.GetMethod("vMTypes");
var vmTypes = manifest.Abi.GetMethod("vMTypes", 8);

Assert.AreEqual(vmTypes.ReturnType, ContractParameterType.Void);
Assert.AreEqual(vmTypes.Parameters[0].Type, ContractParameterType.Boolean);
Expand Down