Skip to content

Commit

Permalink
Remove System.Contract.Call (neo-project#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and cloud8little committed Jan 24, 2021
1 parent b608936 commit 6997bac
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 66 deletions.
6 changes: 0 additions & 6 deletions src/neo/SmartContract/ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Neo.SmartContract
{
partial class ApplicationEngine
{
public static readonly InteropDescriptor System_Contract_Call = Register("System.Contract.Call", nameof(CallContract), 1 << 15, CallFlags.AllowCall, false);
public static readonly InteropDescriptor System_Contract_CallEx = Register("System.Contract.CallEx", nameof(CallContractEx), 1 << 15, CallFlags.AllowCall, false);
public static readonly InteropDescriptor System_Contract_CallNative = Register("System.Contract.CallNative", nameof(CallNativeContract), 0, CallFlags.None, false);
public static readonly InteropDescriptor System_Contract_IsStandard = Register("System.Contract.IsStandard", nameof(IsStandardContract), 1 << 10, CallFlags.ReadStates, true);
Expand All @@ -23,11 +22,6 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Contract_NativeOnPersist = Register("System.Contract.NativeOnPersist", nameof(NativeOnPersist), 0, CallFlags.WriteStates, false);
public static readonly InteropDescriptor System_Contract_NativePostPersist = Register("System.Contract.NativePostPersist", nameof(NativePostPersist), 0, CallFlags.WriteStates, false);

protected internal void CallContract(UInt160 contractHash, string method, Array args)
{
CallContractEx(contractHash, method, args, CallFlags.All);
}

protected internal void CallContractEx(UInt160 contractHash, string method, Array args, CallFlags callFlags)
{
if (method.StartsWith('_')) throw new ArgumentException($"Invalid Method Name: {method}");
Expand Down
3 changes: 2 additions & 1 deletion src/neo/SmartContract/Callbacks/MethodCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MethodCallback : SyscallCallback
public override int ParametersCount => method.Parameters.Length;

public MethodCallback(ApplicationEngine engine, UInt160 hash, string method)
: base(ApplicationEngine.System_Contract_Call, false)
: base(ApplicationEngine.System_Contract_CallEx, false)
{
if (method.StartsWith('_')) throw new ArgumentException();
this.contract = NativeContract.ContractManagement.GetContract(engine.Snapshot, hash);
Expand All @@ -27,6 +27,7 @@ public MethodCallback(ApplicationEngine engine, UInt160 hash, string method)

public override void LoadContext(ApplicationEngine engine, Array args)
{
engine.Push((byte)CallFlags.All);
engine.Push(args);
engine.Push(method.Name);
engine.Push(contract.Hash.ToArray());
Expand Down
9 changes: 6 additions & 3 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,38 @@ public static ScriptBuilder Emit(this ScriptBuilder sb, params OpCode[] ops)

public static ScriptBuilder EmitAppCall(this ScriptBuilder sb, UInt160 scriptHash, string operation)
{
sb.EmitPush(CallFlags.All);
sb.EmitPush(0);
sb.Emit(OpCode.NEWARRAY);
sb.EmitPush(operation);
sb.EmitPush(scriptHash);
sb.EmitSysCall(ApplicationEngine.System_Contract_Call);
sb.EmitSysCall(ApplicationEngine.System_Contract_CallEx);
return sb;
}

public static ScriptBuilder EmitAppCall(this ScriptBuilder sb, UInt160 scriptHash, string operation, params ContractParameter[] args)
{
sb.EmitPush(CallFlags.All);
for (int i = args.Length - 1; i >= 0; i--)
sb.EmitPush(args[i]);
sb.EmitPush(args.Length);
sb.Emit(OpCode.PACK);
sb.EmitPush(operation);
sb.EmitPush(scriptHash);
sb.EmitSysCall(ApplicationEngine.System_Contract_Call);
sb.EmitSysCall(ApplicationEngine.System_Contract_CallEx);
return sb;
}

public static ScriptBuilder EmitAppCall(this ScriptBuilder sb, UInt160 scriptHash, string operation, params object[] args)
{
sb.EmitPush(CallFlags.All);
for (int i = args.Length - 1; i >= 0; i--)
sb.EmitPush(args[i]);
sb.EmitPush(args.Length);
sb.Emit(OpCode.PACK);
sb.EmitPush(operation);
sb.EmitPush(scriptHash);
sb.EmitSysCall(ApplicationEngine.System_Contract_Call);
sb.EmitSysCall(ApplicationEngine.System_Contract_CallEx);
return sb;
}

Expand Down
24 changes: 12 additions & 12 deletions tests/neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ public void FeeIsMultiSigContract()

var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
Assert.AreEqual(1967130, verificationGas);
Assert.AreEqual(348000, sizeGas);
Assert.AreEqual(2315130, tx.NetworkFee);
Assert.AreEqual(349000, sizeGas);
Assert.AreEqual(2316130, tx.NetworkFee);
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ public void FeeIsSignatureContractDetailed()
Assert.IsNull(tx.Witnesses);

// check pre-computed network fee (already guessing signature sizes)
tx.NetworkFee.Should().Be(1228550L);
tx.NetworkFee.Should().Be(1229550L);

// ----
// Sign
Expand Down Expand Up @@ -259,7 +259,7 @@ public void FeeIsSignatureContractDetailed()
// ------------------
// check tx_size cost
// ------------------
Assert.AreEqual(245, tx.Size);
Assert.AreEqual(246, tx.Size);

// will verify tx size, step by step

Expand All @@ -272,17 +272,17 @@ public void FeeIsSignatureContractDetailed()
// Note that Data size and Usage size are different (because of first byte on GetVarSize())
Assert.AreEqual(22, tx.Signers.GetVarSize());
// Part III
Assert.AreEqual(87, tx.Script.GetVarSize());
Assert.AreEqual(88, tx.Script.GetVarSize());
// Part IV
Assert.AreEqual(110, tx.Witnesses.GetVarSize());
// I + II + III + IV
Assert.AreEqual(25 + 22 + 1 + 87 + 110, tx.Size);
Assert.AreEqual(25 + 22 + 1 + 88 + 110, tx.Size);

Assert.AreEqual(1000, NativeContract.Policy.GetFeePerByte(snapshot));
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);

// final check: verification_cost and tx_size
Assert.AreEqual(245000, sizeGas);
Assert.AreEqual(246000, sizeGas);
Assert.AreEqual(983550, verificationGas);

// final assert
Expand Down Expand Up @@ -370,7 +370,7 @@ public void FeeIsSignatureContract_TestScope_Global()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(1228550, verificationGas + sizeGas);
Assert.AreEqual(1229550, verificationGas + sizeGas);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -457,7 +457,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(1249550, verificationGas + sizeGas);
Assert.AreEqual(1250550, verificationGas + sizeGas);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -547,7 +547,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(1249550, verificationGas + sizeGas);
Assert.AreEqual(1250550, verificationGas + sizeGas);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -689,7 +689,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(1269550, verificationGas + sizeGas);
Assert.AreEqual(1270550, verificationGas + sizeGas);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ public void FeeIsSignatureContract_TestScope_FeeOnly_Default()
// get sizeGas
var sizeGas = tx.Size * NativeContract.Policy.GetFeePerByte(snapshot);
// final check on sum: verification_cost + tx_size
Assert.AreEqual(1228550, verificationGas + sizeGas);
Assert.AreEqual(1229550, verificationGas + sizeGas);
// final assert
Assert.AreEqual(tx.NetworkFee, verificationGas + sizeGas);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void GetHashData()

data.LoadContext(engine, array);

Assert.AreEqual(3, engine.CurrentContext.EvaluationStack.Count);
Assert.AreEqual(4, engine.CurrentContext.EvaluationStack.Count);
Assert.AreEqual("9bc4860bb936abf262d7a51f74b4304833fee3b2", engine.Pop<VM.Types.ByteString>().GetSpan().ToHexString());
Assert.AreEqual("test", engine.Pop<VM.Types.ByteString>().GetString());
Assert.IsTrue(engine.Pop() == array);
Expand Down
8 changes: 4 additions & 4 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,17 @@ public void TestContract_Call()
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(new byte[] { 0x01 });

engine.CallContract(state.Hash, method, args);
engine.CallContractEx(state.Hash, method, args, CallFlags.All);
engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[0]);
engine.CurrentContext.EvaluationStack.Pop().Should().Be(args[1]);

state.Manifest.Permissions[0].Methods = WildcardContainer<string>.Create("a");
Assert.ThrowsException<InvalidOperationException>(() => engine.CallContract(state.Hash, method, args));
Assert.ThrowsException<InvalidOperationException>(() => engine.CallContractEx(state.Hash, method, args, CallFlags.All));

state.Manifest.Permissions[0].Methods = WildcardContainer<string>.CreateWildcard();
engine.CallContract(state.Hash, method, args);
engine.CallContractEx(state.Hash, method, args, CallFlags.All);

Assert.ThrowsException<InvalidOperationException>(() => engine.CallContract(UInt160.Zero, method, args));
Assert.ThrowsException<InvalidOperationException>(() => engine.CallContractEx(UInt160.Zero, method, args, CallFlags.All));
}

[TestMethod]
Expand Down
81 changes: 42 additions & 39 deletions tests/neo.UnitTests/VM/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ public void TestEmitAppCall1()
//format:(byte)0x10+(byte)OpCode.NEWARRAY+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call
ScriptBuilder sb = new ScriptBuilder();
sb.EmitAppCall(UInt160.Zero, "AAAAA");
byte[] tempArray = new byte[36];
tempArray[0] = (byte)OpCode.PUSH0;
tempArray[1] = (byte)OpCode.NEWARRAY;
tempArray[2] = (byte)OpCode.PUSHDATA1;
tempArray[3] = 5;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 4, 5);//operation.data
tempArray[9] = (byte)OpCode.PUSHDATA1;
tempArray[10] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 11, 20);//operation.data
uint api = ApplicationEngine.System_Contract_Call;
tempArray[31] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 32, 4);//api.data
byte[] tempArray = new byte[37];
tempArray[0] = (byte)OpCode.PUSH15;
tempArray[1] = (byte)OpCode.PUSH0;
tempArray[2] = (byte)OpCode.NEWARRAY;
tempArray[3] = (byte)OpCode.PUSHDATA1;
tempArray[4] = 5;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 5, 5);//operation.data
tempArray[10] = (byte)OpCode.PUSHDATA1;
tempArray[11] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 12, 20);//operation.data
uint api = ApplicationEngine.System_Contract_CallEx;
tempArray[32] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 33, 4);//api.data
CollectionAssert.AreEqual(tempArray, sb.ToArray());
}

Expand Down Expand Up @@ -116,19 +117,20 @@ public void TestEmitAppCall2()
//format:(ContractParameter[])ContractParameter+(byte)OpCode.PACK+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call
ScriptBuilder sb = new ScriptBuilder();
sb.EmitAppCall(UInt160.Zero, "AAAAA", new ContractParameter[] { new ContractParameter(ContractParameterType.Integer) });
byte[] tempArray = new byte[37];
tempArray[0] = (byte)OpCode.PUSH0;
tempArray[1] = (byte)OpCode.PUSH1;
tempArray[2] = (byte)OpCode.PACK;
tempArray[3] = (byte)OpCode.PUSHDATA1;
tempArray[4] = 0x05;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 5, 5);//operation.data
tempArray[10] = (byte)OpCode.PUSHDATA1;
tempArray[11] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 12, 20);//operation.data
uint api = ApplicationEngine.System_Contract_Call;
tempArray[32] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 33, 4);//api.data
byte[] tempArray = new byte[38];
tempArray[0] = (byte)OpCode.PUSH15;
tempArray[1] = (byte)OpCode.PUSH0;
tempArray[2] = (byte)OpCode.PUSH1;
tempArray[3] = (byte)OpCode.PACK;
tempArray[4] = (byte)OpCode.PUSHDATA1;
tempArray[5] = 0x05;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 6, 5);//operation.data
tempArray[11] = (byte)OpCode.PUSHDATA1;
tempArray[12] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 13, 20);//operation.data
uint api = ApplicationEngine.System_Contract_CallEx;
tempArray[33] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 34, 4);//api.data
CollectionAssert.AreEqual(tempArray, sb.ToArray());
}

Expand All @@ -138,19 +140,20 @@ public void TestEmitAppCall3()
//format:(object[])args+(byte)OpCode.PACK+(string)operation+(Uint160)scriptHash+(uint)InteropService.System_Contract_Call
ScriptBuilder sb = new ScriptBuilder();
sb.EmitAppCall(UInt160.Zero, "AAAAA", true);
byte[] tempArray = new byte[37];
tempArray[0] = (byte)OpCode.PUSH1;//arg
tempArray[1] = (byte)OpCode.PUSH1;//args.Length
tempArray[2] = (byte)OpCode.PACK;
tempArray[3] = (byte)OpCode.PUSHDATA1;
tempArray[4] = 0x05;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 5, 5);//operation.data
tempArray[10] = (byte)OpCode.PUSHDATA1;
tempArray[11] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 12, 20);//operation.data
uint api = ApplicationEngine.System_Contract_Call;
tempArray[32] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 33, 4);//api.data
byte[] tempArray = new byte[38];
tempArray[0] = (byte)OpCode.PUSH15;
tempArray[1] = (byte)OpCode.PUSH1;//arg
tempArray[2] = (byte)OpCode.PUSH1;//args.Length
tempArray[3] = (byte)OpCode.PACK;
tempArray[4] = (byte)OpCode.PUSHDATA1;
tempArray[5] = 0x05;//operation.Length
Array.Copy(Encoding.UTF8.GetBytes("AAAAA"), 0, tempArray, 6, 5);//operation.data
tempArray[11] = (byte)OpCode.PUSHDATA1;
tempArray[12] = 0x14;//scriptHash.Length
Array.Copy(UInt160.Zero.ToArray(), 0, tempArray, 13, 20);//operation.data
uint api = ApplicationEngine.System_Contract_CallEx;
tempArray[33] = (byte)OpCode.SYSCALL;
Array.Copy(BitConverter.GetBytes(api), 0, tempArray, 34, 4);//api.data
CollectionAssert.AreEqual(tempArray, sb.ToArray());
}

Expand All @@ -159,7 +162,7 @@ public void TestMakeScript()
{
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero);

Assert.AreEqual("0c14000000000000000000000000000000000000000011c00c0962616c616e63654f660c14fbedfe2ed2226592b648c4da97b9c9cd5dc1a6a641627d5b52",
Assert.AreEqual("1f0c14000000000000000000000000000000000000000011c00c0962616c616e63654f660c14fbedfe2ed2226592b648c4da97b9c9cd5dc1a6a641eef40cdb",
testScript.ToHexString());
}

Expand Down

0 comments on commit 6997bac

Please sign in to comment.