Skip to content

Commit

Permalink
Sync update with neo pull 2119 (#402)
Browse files Browse the repository at this point in the history
* Upgrade target framework.

* Revert "Upgrade target framework."

This reverts commit 6b0c49c.

* Safe Attribute

* Update Contract1.cs (#398)

* Revert "publish neon to myget/github packages" (#395)

* Remove Contract.Create、Contract.Update、Contract.Destroy; Add Contract.CallNative.

* Update

* remove CallNative

* Update templates/Template.CSharp/Contract1.cs

Co-authored-by: Owen Zhang <[email protected]>

* Update templates/Template.CSharp/Contract1.cs

Co-authored-by: Owen Zhang <[email protected]>

* Update tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs

Co-authored-by: Owen Zhang <[email protected]>

* public

* Update

* Update call

* Fix more UT

* Update hashes

* Mock contract create

* Fix more UT

* Fix UT

* More fixes

* More fixes

* More UT

* Fix UT's

* dotnet-format

* Fix Last test

* Update

* Remove private properties

* Fixed UT

* disable warning CS0626

Co-authored-by: Shargon <[email protected]>
Co-authored-by: Owen Zhang <[email protected]>
Co-authored-by: Erik Zhang <[email protected]>
  • Loading branch information
4 people authored Dec 16, 2020
1 parent b83ec54 commit fac2847
Show file tree
Hide file tree
Showing 38 changed files with 243 additions and 122 deletions.
4 changes: 2 additions & 2 deletions src/Neo.Compiler.MSIL/FuncExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static JObject GenerateAbi(NeoModule module, Dictionary<int, int> addrCon
funcsign["name"] = function.Value.displayName;
var offset = addrConvTable?[function.Value.funcaddr] ?? function.Value.funcaddr;
funcsign["offset"] = offset.ToString();
funcsign["safe"] = function.Value.method?.method.CustomAttributes.Any(u => u.AttributeType.FullName == "Neo.SmartContract.Framework.SafeAttribute") == true;
JArray funcparams = new JArray();
funcsign["parameters"] = funcparams;
if (mm.paramtypes != null)
Expand Down Expand Up @@ -206,8 +207,7 @@ public static string GenerateManifest(JObject abi, NeoModule module)
return
@"{""groups"":[],""abi"":" +
sbABI +
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""name"":""" + name +
@""",""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}";
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""name"":""" + name + @""",""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}";
}
}
}
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 @@ -43,7 +43,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-CI01089" />
<PackageReference Include="Neo" Version="3.0.0-CI01105" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/Neo.SmartContract.Framework/SafeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Neo.SmartContract.Framework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SafeAttribute : Attribute
{
}
}
3 changes: 0 additions & 3 deletions src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ public static class Blockchain

[Syscall("System.Blockchain.GetTransactionHeight")]
public static extern BigInteger GetTransactionHeight(UInt256 hash);

[Syscall("System.Blockchain.GetContract")]
public static extern Contract GetContract(UInt160 script_hash);
}
}
9 changes: 0 additions & 9 deletions src/Neo.SmartContract.Framework/Services/Neo/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ public class Contract
[Syscall("System.Contract.CallEx")]
public static extern object CallEx(UInt160 scriptHash, string method, object[] arguments, CallFlags flag);

[Syscall("System.Contract.Create")]
public static extern Contract Create(byte[] script, string manifest);

[Syscall("System.Contract.Update")]
public static extern void Update(byte[] script, string manifest);

[Syscall("System.Contract.Destroy")]
public static extern void Destroy();

[Syscall("System.Contract.GetCallFlags")]
public static extern byte GetCallFlags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x7062149f9377e3a110a343f811b9e406f8ef7824")]
[Contract("0xc0073f4c7069bf38995780c9da065f9b3949ea7a")]
public class Designation
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/GAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x36a019d836d964c438c573f78badf79b9e7eebdd")]
[Contract("0xa6a6c15dcdc9b997dac448b6926522d22efeedfb")]
public class GAS
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
15 changes: 15 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma warning disable CS0626

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0xcd97b70d82d69adfcd9165374109419fade8d6ab")]
public class ManagementContract
{
public static extern UInt160 Hash { [ContractHash] get; }
public static extern string Name { get; }
public static extern Contract GetContract(UInt160 hash);
public static extern Contract Deploy(byte[] nefFile, string manifest);
public static extern void Update(byte[] nefFile, string manifest);
public static extern void Destroy();
}
}
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0xe22f9134cef8b03e53f71b3f960a20a65cddc972")]
[Contract("0x0a46e2e37c9987f570b4af253fb77e7eef0f72b6")]
public class NEO
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x35e4fc2e69a4d04d1db4d755c4150c50aff2e9a9")]
[Contract("0xb1c37d5847c2ae36bdde31d0cc833a7ad9667f8f")]
public class Oracle
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x1ca594b36b6b6b3f05efce8b106c824053d18713")]
[Contract("0xdde31084c0fdbebc7f5ed5f53a38905305ccee14")]
public class Policy
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public enum SyscallCallback : uint
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,
Expand Down
8 changes: 4 additions & 4 deletions templates/Template.CSharp/Contract1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public static byte[] MyMethod()
public static void _deploy(bool update)
{
if (update) return;

// It will be executed during deploy
Storage.Put("Hello", "World");
}

public static void Update(byte[] script, string manifest)
public static void Update(byte[] nefFile, string manifest)
{
if (!IsOwner()) throw new Exception("No authorization.");
Contract.Update(script, manifest);
ManagementContract.Update(nefFile, manifest);
}

public static void Destroy()
{
if (!IsOwner()) throw new Exception("No authorization.");
Contract.Destroy();
ManagementContract.Destroy();
}
}
}
2 changes: 1 addition & 1 deletion templates/Template.NEP17.CSharp/NEP17.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Template.NEP17.CSharp
public partial class NEP17 : SmartContract
{
private static bool ValidateAddress(UInt160 address) => address.IsValid && !address.IsZero;
private static bool IsDeployed(UInt160 address) => Blockchain.GetContract(address) != null;
private static bool IsDeployed(UInt160 address) => ManagementContract.GetContract(address) != null;
}
}
4 changes: 2 additions & 2 deletions templates/Template.NEP17.CSharp/NEP17.Owner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public static bool Deploy()
public static void Update(byte[] nefFile, string manifest)
{
if (!IsOwner()) throw new Exception("No authorization.");
Contract.Update(nefFile, manifest);
ManagementContract.Update(nefFile, manifest);
}

public static void Destroy()
{
if (!IsOwner()) throw new Exception("No authorization.");
Contract.Destroy();
ManagementContract.Destroy();
}

public static void EnablePayment()
Expand Down
33 changes: 33 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;

namespace Neo.Compiler.MSIL.Extensions
{
public static class TestExtensions
{
public static void ContractAdd(this StoreView snapshot, ContractState contract)
{
var key = new KeyBuilder(0, 8).Add(contract.Hash);
snapshot.Storages.Add(key, new StorageItem(contract));
}

public static void DeployNativeContracts(this StoreView snapshot)
{
var method = typeof(SmartContract.Native.ManagementContract).GetMethod("OnPersist", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var engine = new TestEngine(TriggerType.OnPersist, null, snapshot);
method.Invoke(SmartContract.Native.NativeContract.Management, new object[] { engine });
}

/// <summary>
/// Set Persisting block for unit test
/// </summary>
/// <param name="block">Block</param>
public static void SetPersistingBlock(this StoreView snapshot, Block block)
{
snapshot.GetType().GetProperty("PersistingBlock").SetValue(snapshot, block);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static int UnitTest_002()
}
return a;
}

public static int UnitTest_003()
{
int c = UnitTest_002() + s;
Expand Down
25 changes: 25 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABISafe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Neo.SmartContract.Framework;

namespace Neo.Compiler.MSIL.UnitTests.TestClasses
{
public class Contract_ABISafe : SmartContract.Framework.SmartContract
{
static int s = 1;

public static int UnitTest_001()
{
return 1;
}

[Safe]
public static int UnitTest_002()
{
return 2;
}

public static int UnitTest_003()
{
return 3;
}
}
}
22 changes: 22 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.IO.Json;

namespace Neo.Compiler.MSIL.UnitTests
{
[TestClass]
public class UnitTest_ABI_Safe
{
[TestMethod]
public void UnitTest_TestSafe()
{
var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABISafe.cs", true, false);
var abi = buildScript.finalABI;

var methodsABI = abi["methods"] as JArray;
Assert.IsFalse(methodsABI[1]["safe"].AsBoolean());
Assert.IsTrue(methodsABI[2]["safe"].AsBoolean());
Assert.IsFalse(methodsABI[3]["safe"].AsBoolean());
}
}
}
6 changes: 4 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.Extensions;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using Neo.VM.Types;
Expand All @@ -17,9 +19,9 @@ public void Init()
{
var hash = UInt160.Parse("0102030405060708090A0102030405060708090A");
_engine = new TestEngine();
_engine.Snapshot.Contracts.Add(hash, new Ledger.ContractState()
_engine.Snapshot.ContractAdd(new ContractState()
{
Hash = UInt160.Zero,
Hash = hash,
Script = _engine.Build("./TestClasses/Contract1.cs").finalNEFScript,
Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)),
});
Expand Down
8 changes: 6 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.Extensions;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM.Types;

Expand Down Expand Up @@ -108,8 +110,9 @@ public void NullCollationAndCollation()
{
var _testengine = new TestEngine();
_testengine.AddEntryScript("./TestClasses/Contract_NULL.cs");
_testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState()
_testengine.Snapshot.ContractAdd(new ContractState()
{
Hash = testengine.EntryScriptHash,
Script = testengine.EntryContext.Script,
Manifest = new ContractManifest()
});
Expand All @@ -124,8 +127,9 @@ public void NullCollationAndCollation2()
{
var _testengine = new TestEngine();
_testengine.AddEntryScript("./TestClasses/Contract_NULL.cs");
_testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState()
_testengine.Snapshot.ContractAdd(new ContractState()
{
Hash = testengine.EntryScriptHash,
Script = testengine.EntryContext.Script,
Manifest = new ContractManifest()
});
Expand Down
29 changes: 10 additions & 19 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Compiler.MSIL.Extensions;
using Neo.Compiler.MSIL.UnitTests.Utils;
using Neo.SmartContract;
using Neo.SmartContract.Native;
Expand All @@ -14,30 +15,20 @@ public class Contract_NativeContracts
[TestInitialize]
public void Test_Init()
{
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy);
script = sb.ToArray();
}

// Fake native deploy

typeof(TestSnapshot).GetProperty("PersistingBlock").SetValue(snapshot, new Network.P2P.Payloads.Block() { Index = 0 });
var testengine = new TestEngine(TriggerType.System, null, snapshot);
testengine.LoadScript(script);
Assert.AreEqual(VMState.HALT, testengine.Execute());
snapshot.SetPersistingBlock(new Network.P2P.Payloads.Block() { Index = 0 });
snapshot.DeployNativeContracts();
}

[TestMethod]
public void TestHashes()
{
// var attr = typeof(Oracle).GetCustomAttribute<ContractAttribute>();
Assert.AreEqual(NativeContract.Designate.Hash.ToString(), "0x7062149f9377e3a110a343f811b9e406f8ef7824");
Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0x35e4fc2e69a4d04d1db4d755c4150c50aff2e9a9");
Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0xe22f9134cef8b03e53f71b3f960a20a65cddc972");
Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0x36a019d836d964c438c573f78badf79b9e7eebdd");
Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0x1ca594b36b6b6b3f05efce8b106c824053d18713");
Assert.AreEqual(NativeContract.Designation.Hash.ToString(), "0xc0073f4c7069bf38995780c9da065f9b3949ea7a");
Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0xb1c37d5847c2ae36bdde31d0cc833a7ad9667f8f");
Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0x0a46e2e37c9987f570b4af253fb77e7eef0f72b6");
Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0xa6a6c15dcdc9b997dac448b6926522d22efeedfb");
Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0xdde31084c0fdbebc7f5ed5f53a38905305ccee14");
}

[TestMethod]
Expand Down Expand Up @@ -90,7 +81,7 @@ public void Test_NEO()
Assert.AreEqual(1, result.Count);

var entry = result.Pop();
Assert.AreEqual("neo", entry.GetString());
Assert.AreEqual("NEO", entry.GetString());

// NeoHash

Expand Down Expand Up @@ -119,7 +110,7 @@ public void Test_GAS()

var entry = result.Pop();

Assert.AreEqual("gas", entry.GetString());
Assert.AreEqual("GAS", entry.GetString());
}
}
}
Loading

0 comments on commit fac2847

Please sign in to comment.