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

Check the parameters count #1695

Merged
merged 4 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
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
24 changes: 19 additions & 5 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ public void Runtime_GetNotifications_Test()
scriptHash2 = script.ToArray().ToScriptHash();

snapshot.Contracts.Delete(scriptHash2);
snapshot.Contracts.Add(scriptHash2, new ContractState()
var state = new ContractState()
{
Script = script.ToArray(),
Manifest = TestUtils.CreateDefaultManifest(scriptHash2, "test"),
});
};
state.Manifest.Abi.Methods[0].Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition(){ Name="a", Type= ContractParameterType.Integer},
new ContractParameterDefinition(){ Name="b", Type= ContractParameterType.Integer}
};
snapshot.Contracts.Add(scriptHash2, state);
}

// Wrong length
Expand Down Expand Up @@ -217,8 +223,6 @@ public void TestExecutionEngine_GetCallingScriptHash()
// Test real

using ScriptBuilder scriptA = new ScriptBuilder();
scriptA.Emit(OpCode.DROP); // Drop arguments
scriptA.Emit(OpCode.DROP); // Drop method
scriptA.EmitSysCall(ApplicationEngine.System_Runtime_GetCallingScriptHash);

var contract = new ContractState()
Expand All @@ -230,7 +234,7 @@ public void TestExecutionEngine_GetCallingScriptHash()
engine.Snapshot.Contracts.Add(contract.ScriptHash, contract);

using ScriptBuilder scriptB = new ScriptBuilder();
scriptB.EmitAppCall(contract.ScriptHash, "test", 0, 1);
scriptB.EmitAppCall(contract.ScriptHash, "test");
engine.LoadScript(scriptB.ToArray());

Assert.AreEqual(VMState.HALT, engine.Execute());
Expand Down Expand Up @@ -601,6 +605,11 @@ public void TestContract_Call()
{
var snapshot = Blockchain.Singleton.GetSnapshot();
var state = TestUtils.GetContract("method");
state.Manifest.Abi.Methods[0].Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition(){ Name="a", Type= ContractParameterType.Integer},
new ContractParameterDefinition(){ Name="b", Type= ContractParameterType.Integer}
};
state.Manifest.Features = ContractFeatures.HasStorage;
string method = "method";
var args = new VM.Types.Array { 0, 1 };
Expand Down Expand Up @@ -628,6 +637,11 @@ public void TestContract_CallEx()
var snapshot = Blockchain.Singleton.GetSnapshot();

var state = TestUtils.GetContract("method");
state.Manifest.Abi.Methods[0].Parameters = new ContractParameterDefinition[]
{
new ContractParameterDefinition(){ Name="a", Type= ContractParameterType.Integer},
new ContractParameterDefinition(){ Name="b", Type= ContractParameterType.Integer}
};
state.Manifest.Features = ContractFeatures.HasStorage;
snapshot.Contracts.Add(state.ScriptHash, state);

Expand Down
8 changes: 8 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ public void System_Runtime_GetInvocationCounter()
contracts.Add(contractA.ScriptHash, contractA);
contracts.Add(contractB.ScriptHash, contractB);
contracts.Add(contractC.ScriptHash, contractC);

contractA.Manifest.Abi.Methods[0].Parameters =
contractB.Manifest.Abi.Methods[0].Parameters =
contractC.Manifest.Abi.Methods[0].Parameters = new Neo.SmartContract.Manifest.ContractParameterDefinition[]
{
new Neo.SmartContract.Manifest.ContractParameterDefinition(){ Name="a", Type= ContractParameterType.Integer},
new Neo.SmartContract.Manifest.ContractParameterDefinition(){ Name="b", Type= ContractParameterType.Integer}
};
}

// Call A,B,B,C
Expand Down
8 changes: 4 additions & 4 deletions tests/neo.UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ public static ContractManifest CreateDefaultManifest(UInt160 hash, string method
Abi = new ContractAbi()
{
Hash = hash,
Events = new ContractEventDescriptor[0],
Methods = method == null ? new ContractMethodDescriptor[0] : new ContractMethodDescriptor[]
Events = Array.Empty<ContractEventDescriptor>(),
Methods = method == null ? Array.Empty<ContractMethodDescriptor>() : new ContractMethodDescriptor[]
{
new ContractMethodDescriptor()
{
Name = method,
Parameters = new ContractParameterDefinition[0],
Parameters = Array.Empty<ContractParameterDefinition>(),
ReturnType = ContractParameterType.Integer
}
}
},
Features = ContractFeatures.NoProperty,
Groups = new ContractGroup[0],
Groups = Array.Empty<ContractGroup>(),
SafeMethods = WildcardContainer<string>.Create(),
Trusts = WildcardContainer<UInt160>.Create(),
Extra = null,
Expand Down