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

Add event name, callback support and base64 syscalls #302

Merged
merged 23 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,10 @@ private int ConvertCall(OpCode src, NeoMethod to)
}
else if (calltype == 5)
{
//var callp = Encoding.UTF8.GetBytes(callname);
ConvertPushString(callname, src, to);

// package the arguments into an array
ConvertPushNumber(pcount + 1, null, to);
ConvertPushNumber(pcount, null, to);
Convert1by1(VM.OpCode.PACK, null, to);
ConvertPushString(callname, src, to);

//a syscall
{
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.4.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
<PackageReference Include="Neo" Version="3.0.0-CI00938" />
<PackageReference Include="Neo" Version="3.0.0-CI00953" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ public class Notification : IApiInterface
/// </summary>
public readonly byte[] ScriptHash;

/// <summary>
/// Notification's name
/// </summary>
public readonly string EventName;

/// <summary>
/// Notification's state
/// </summary>
public readonly object State;
public readonly object[] State;
}
}
3 changes: 0 additions & 3 deletions src/Neo.SmartContract.Framework/Services/Neo/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public static extern long GasLeft
[Syscall("System.Runtime.CheckWitness")]
public static extern bool CheckWitness(byte[] hashOrPubkey);

[Syscall("System.Runtime.Notify")]
public static extern void Notify(params object[] state);

[Syscall("System.Runtime.Log")]
public static extern void Log(string message);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Neo.SmartContract.Framework/Services/System/Binary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Neo.SmartContract.Framework.Services.System
{
public class Binary
{
[Syscall("System.Binary.Base64Decode")]
public static extern byte[] Base64Decode(string input);

[Syscall("System.Binary.Base64Encode")]
public static extern string Base64Encode(byte[] input);
}
}
103 changes: 103 additions & 0 deletions src/Neo.SmartContract.Framework/Services/System/Callback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;

namespace Neo.SmartContract.Framework.Services.System
{
public class Callback
{
[Syscall("System.Callback.Invoke")]
public extern object Invoke(object[] args);

// Static

[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<TResult>(Func<TResult> func);

[OpCode(OpCode.PUSH1)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T, TResult>(Func<T, TResult> func);

[OpCode(OpCode.PUSH2)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, TResult>(Func<T1, T2, TResult> func);

[OpCode(OpCode.PUSH3)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func);

[OpCode(OpCode.PUSH4)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> func);

[OpCode(OpCode.PUSH5)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, TResult>(Func<T1, T2, T3, T4, T5, TResult> func);

[OpCode(OpCode.PUSH6)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, TResult>(Func<T1, T2, T3, T4, T5, T6, TResult> func);

[OpCode(OpCode.PUSH7)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, TResult> func);

[OpCode(OpCode.PUSH8)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> func);

[OpCode(OpCode.PUSH9)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> func);

[OpCode(OpCode.PUSH10)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> func);

[OpCode(OpCode.PUSH11)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> func);

[OpCode(OpCode.PUSH12)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> func);

[OpCode(OpCode.PUSH13)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> func);

[OpCode(OpCode.PUSH14)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> func);

[OpCode(OpCode.PUSH15)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> func);

[OpCode(OpCode.PUSH16)]
[OpCode(OpCode.SWAP)]
[Syscall("System.Callback.Create")]
public static extern Callback Create<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult>(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> func);

[Syscall("System.Callback.CreateFromMethod")]
public static extern Callback CreateFromMethod(byte[] hash, string method);

[Syscall("System.Callback.CreateFromSyscall")]
public static extern Callback CreateFromSyscall(SyscallCallback method);
}
}
37 changes: 37 additions & 0 deletions src/Neo.SmartContract.Framework/Services/System/SyscallCallback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Neo.SmartContract.Framework.Services.System
{
public enum SyscallCallback : uint
{
System_Binary_Serialize = 0x24011c3f,
System_Binary_Deserialize = 0xdfd07c52,
System_Binary_Base64Encode = 0x7653bfac,
System_Binary_Base64Decode = 0xc384a3db,
System_Blockchain_GetHeight = 0x1f72f57e,
System_Blockchain_GetBlock = 0x2d924783,
System_Blockchain_GetTransaction = 0x488d55e6,
System_Blockchain_GetTransactionHeight = 0xb188324a,
System_Blockchain_GetTransactionFromBlock = 0x69fd567e,
System_Blockchain_GetContract = 0x414bc5a9,
System_Contract_IsStandard = 0x859d6bd7,
System_Contract_CreateStandardAccount = 0x28799cf,
Neo_Crypto_RIPEMD160 = 0xd2d6d126,
Neo_Crypto_SHA256 = 0x1174acd7,
Neo_Crypto_VerifyWithECDsaSecp256r1 = 0x780d4495,
Neo_Crypto_VerifyWithECDsaSecp256k1 = 0xb7533c7e,
Neo_Crypto_CheckMultisigWithECDsaSecp256r1 = 0xafef8d13,
Neo_Crypto_CheckMultisigWithECDsaSecp256k1 = 0xb2efc657,
System_Json_Serialize = 0x4b268d24,
System_Json_Deserialize = 0xe479ca7,
System_Runtime_Platform = 0xf6fc79b2,
System_Runtime_GetTrigger = 0xa0387de9,
System_Runtime_GetTime = 0x388c3b7,
System_Runtime_GetScriptContainer = 0x3008512d,
System_Runtime_GetExecutingScriptHash = 0x74a8fedb,
System_Runtime_GetCallingScriptHash = 0x3c6e5339,
System_Runtime_GetEntryScriptHash = 0x38e2b4f9,
System_Runtime_CheckWitness = 0x8cec27f8,
System_Runtime_GetInvocationCounter = 0x43112784,
System_Runtime_GetNotifications = 0xf1354327,
System_Runtime_GasLeft = 0xced88814
}
}
10 changes: 8 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract2.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System;
using System.ComponentModel;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
public class Contract2 : SmartContract.Framework.SmartContract
{
[DisplayName("event")]
public static event Action<object> notify;

public static byte UnitTest_002(object arg1, object arg2)
{
Neo.SmartContract.Framework.Services.Neo.Runtime.Notify(arg1);
Neo.SmartContract.Framework.Services.Neo.Runtime.Notify(arg2);
notify(arg1);
notify(arg2);
var nb = new byte[] { 1, 2, 3, 4 };
return nb[2];
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_NULL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static string NullCollation(string code)

public static bool IfNull(object obj)
{
if ((bool)obj)
{
return true;
if ((bool)obj)
{
return true;
}

return false;
}
}

public static object NullCollationAndCollation(string code)
{
return Storage.Get(code)?.ToBigInteger() ?? 123;
Expand Down
14 changes: 10 additions & 4 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_shift.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System;
using System.ComponentModel;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
class Contract_shift : SmartContract.Framework.SmartContract
{
[DisplayName("event")]
public static event Action<int> notify;

public static object Main()
{
int v = 8;
var v1 = v << 1;
var v2 = v << -1;
var v3 = v >> 1;
var v4 = v >> -1;
SmartContract.Framework.Services.Neo.Runtime.Notify(v1);
SmartContract.Framework.Services.Neo.Runtime.Notify(v2);
SmartContract.Framework.Services.Neo.Runtime.Notify(v3);
SmartContract.Framework.Services.Neo.Runtime.Notify(v4);
notify(v1);
notify(v2);
notify(v3);
notify(v4);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
using System;
using System.ComponentModel;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
class Contract_shift_bigint : SmartContract.Framework.SmartContract
{
[DisplayName("event")]
public static event Action<System.Numerics.BigInteger> notify;

public static object Main()
{
System.Numerics.BigInteger v = 8;
var v1 = v << 0;
var v2 = v << 1;
var v3 = v >> 1;
var v4 = v >> 2;
SmartContract.Framework.Services.Neo.Runtime.Notify(v1);
SmartContract.Framework.Services.Neo.Runtime.Notify(v2);
SmartContract.Framework.Services.Neo.Runtime.Notify(v3);
SmartContract.Framework.Services.Neo.Runtime.Notify(v4);
notify(v1);
notify(v2);
notify(v3);
notify(v4);
return false;
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.VM;
using Neo.VM.Types;
using System;

Expand Down Expand Up @@ -70,10 +71,8 @@ public void Test_ByteArrayPick()
testengine.AddEntryScript("./TestClasses/Contract2.cs");

var result = testengine.GetMethod("unitTest_002").Run("hello", 1);
StackItem wantresult = 3;

var bequal = wantresult.Equals(result);
Assert.IsTrue(bequal);
Assert.AreEqual(3, result.GetBigInteger());
}
}
}
4 changes: 2 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Test_IntArray()
var result = testengine.ExecuteTestCaseStandard("testIntArray");

//test 0,1,2
Assert.IsTrue(result.TryPop(out Array arr));
var arr = result.Pop<Array>();
CollectionAssert.AreEqual(new int[] { 0, 1, 2 }, arr.Cast<PrimitiveType>().Select(u => (int)u.ToBigInteger()).ToArray());
}

Expand All @@ -28,7 +28,7 @@ public void Test_IntArrayInit()
var result = testengine.ExecuteTestCaseStandard("testIntArrayInit");

//test 1,4,5
Assert.IsTrue(result.TryPop(out Array arr));
var arr = result.Pop<Array>();
CollectionAssert.AreEqual(new int[] { 1, 4, 5 }, arr.Cast<Integer>().Select(u => (int)u.ToBigInteger()).ToArray());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/UnitTest_Returns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Test_DoubleReturnB()

Assert.AreEqual(1, result.Count);

Assert.IsTrue(result.TryPop(out Integer r1));
var r1 = result.Pop<Integer>();
Assert.AreEqual(-3, r1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/UnitTest_TypeConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void UnitTest_TestTypeConvert()
var result = testengine.ExecuteTestCaseStandard("testType");

//test 0,1,2
Assert.IsTrue(result.TryPop(out Array arr));
var arr = result.Pop<Array>();
Assert.IsTrue(arr[0].Type == StackItemType.Integer);
Assert.IsTrue(arr[1].Type == StackItemType.Buffer);
Assert.IsTrue((arr[1].ConvertTo(StackItemType.ByteString) as PrimitiveType).ToBigInteger() == (arr[0] as PrimitiveType).ToBigInteger());
Expand Down
Loading