Skip to content

Commit

Permalink
Check the parameters count (neo-project#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 3f46aeb commit 64d67c9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private void CallContractInternal(UInt160 contractHash, string method, Array arg

ContractMethodDescriptor md = contract.Manifest.Abi.GetMethod(method);
if (md is null) throw new InvalidOperationException();
if (args.Count != md.Parameters.Length) throw new InvalidOperationException();
int rvcount = md.ReturnType == ContractParameterType.Void ? 0 : 1;
ExecutionContext context_new = LoadScript(contract.Script, rvcount);
state = context_new.GetState<ExecutionContextState>();
Expand Down
14 changes: 7 additions & 7 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Runtime_GetNotifications_Test()
snapshot.Contracts.Add(scriptHash2, new ContractState()
{
Script = script.ToArray(),
Manifest = TestUtils.CreateDefaultManifest(scriptHash2, "test"),
Manifest = TestUtils.CreateManifest(scriptHash2, "test", ContractParameterType.Any, ContractParameterType.Integer, ContractParameterType.Integer),
});
}

Expand Down Expand Up @@ -223,7 +223,7 @@ public void TestExecutionEngine_GetCallingScriptHash()

var contract = new ContractState()
{
Manifest = TestUtils.CreateDefaultManifest(scriptA.ToArray().ToScriptHash(), "test"),
Manifest = TestUtils.CreateManifest(scriptA.ToArray().ToScriptHash(), "test", ContractParameterType.Any, ContractParameterType.Integer, ContractParameterType.Integer),
Script = scriptA.ToArray()
};
engine = GetEngine(true, true, false);
Expand Down Expand Up @@ -600,10 +600,10 @@ public void TestStorageContext_AsReadOnly()
public void TestContract_Call()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract("method");
state.Manifest.Features = ContractFeatures.HasStorage;
string method = "method";
var args = new VM.Types.Array { 0, 1 };
var state = TestUtils.GetContract(method, args.Count);
state.Manifest.Features = ContractFeatures.HasStorage;

snapshot.Contracts.Add(state.ScriptHash, state);
var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
Expand All @@ -627,12 +627,12 @@ public void TestContract_CallEx()
{
var snapshot = Blockchain.Singleton.GetSnapshot();

var state = TestUtils.GetContract("method");
string method = "method";
var args = new VM.Types.Array { 0, 1 };
var state = TestUtils.GetContract(method, args.Count);
state.Manifest.Features = ContractFeatures.HasStorage;
snapshot.Contracts.Add(state.ScriptHash, state);

string method = "method";
var args = new VM.Types.Array { 0, 1 };

foreach (var flags in new CallFlags[] { CallFlags.None, CallFlags.AllowCall, CallFlags.AllowModifyStates, CallFlags.All })
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void TestVerifyWitnesses()
Header header3 = new Header() { PrevHash = index3, Witness = new Witness { VerificationScript = new byte[0] } };
snapshot3.Contracts.Add(UInt160.Zero, new ContractState()
{
Manifest = TestUtils.CreateDefaultManifest(UInt160.Zero, "verify"),
Manifest = TestUtils.CreateManifest(UInt160.Zero, "verify", ContractParameterType.Boolean, ContractParameterType.Signature),
});
Assert.AreEqual(false, Neo.SmartContract.Helper.VerifyWitnesses(header3, snapshot3, 100));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ public void System_Runtime_GetInvocationCounter()
contracts.Delete(contractA.ScriptHash);
contracts.Delete(contractB.ScriptHash);
contracts.Delete(contractC.ScriptHash);
contractA.Manifest = TestUtils.CreateDefaultManifest(contractA.ScriptHash, "dummyMain");
contractB.Manifest = TestUtils.CreateDefaultManifest(contractA.ScriptHash, "dummyMain");
contractC.Manifest = TestUtils.CreateDefaultManifest(contractA.ScriptHash, "dummyMain");
contractA.Manifest = TestUtils.CreateManifest(contractA.ScriptHash, "dummyMain", ContractParameterType.Any, ContractParameterType.Integer, ContractParameterType.Integer);
contractB.Manifest = TestUtils.CreateManifest(contractA.ScriptHash, "dummyMain", ContractParameterType.Any, ContractParameterType.Integer, ContractParameterType.Integer);
contractC.Manifest = TestUtils.CreateManifest(contractA.ScriptHash, "dummyMain", ContractParameterType.Any, ContractParameterType.Integer, ContractParameterType.Integer);
contracts.Add(contractA.ScriptHash, contractA);
contracts.Add(contractB.ScriptHash, contractB);
contracts.Add(contractC.ScriptHash, contractC);
Expand Down
35 changes: 23 additions & 12 deletions tests/neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class TestUtils
{
public static readonly Random TestRandom = new Random(1337); // use fixed seed for guaranteed determinism

public static ContractManifest CreateDefaultManifest(UInt160 hash, string method = null)
public static ContractManifest CreateDefaultManifest(UInt160 hash)
{
return new ContractManifest()
{
Expand All @@ -26,15 +26,7 @@ public static ContractManifest CreateDefaultManifest(UInt160 hash, string method
{
Hash = hash,
Events = new ContractEventDescriptor[0],
Methods = method == null ? new ContractMethodDescriptor[0] : new ContractMethodDescriptor[]
{
new ContractMethodDescriptor()
{
Name = method,
Parameters = new ContractParameterDefinition[0],
ReturnType = ContractParameterType.Integer
}
}
Methods = new ContractMethodDescriptor[0]
},
Features = ContractFeatures.NoProperty,
Groups = new ContractGroup[0],
Expand All @@ -44,6 +36,25 @@ public static ContractManifest CreateDefaultManifest(UInt160 hash, string method
};
}

public static ContractManifest CreateManifest(UInt160 hash, string method, ContractParameterType returnType, params ContractParameterType[] parameterTypes)
{
ContractManifest manifest = CreateDefaultManifest(hash);
manifest.Abi.Methods = new ContractMethodDescriptor[]
{
new ContractMethodDescriptor()
{
Name = method,
Parameters = parameterTypes.Select((p, i) => new ContractParameterDefinition
{
Name = $"p{i}",
Type = p
}).ToArray(),
ReturnType = returnType
}
};
return manifest;
}

public static byte[] GetByteArray(int length, byte firstByte)
{
byte[] array = new byte[length];
Expand Down Expand Up @@ -82,13 +93,13 @@ public static Transaction GetTransaction()
};
}

internal static ContractState GetContract(string method = null)
internal static ContractState GetContract(string method = "test", int parametersCount = 0)
{
return new ContractState
{
Id = 0x43000000,
Script = new byte[] { 0x01, 0x01, 0x01, 0x01 },
Manifest = CreateDefaultManifest(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"), method)
Manifest = CreateManifest(UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01"), method, ContractParameterType.Any, Enumerable.Repeat(ContractParameterType.Any, parametersCount).ToArray())
};
}

Expand Down

0 comments on commit 64d67c9

Please sign in to comment.