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

Sync update with neo pull 2119 #402

Merged
merged 41 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
cdc135f
Merge pull request #1 from neo-project/master
Sep 11, 2020
be2a614
Merge pull request #2 from neo-project/master
Oct 15, 2020
30893a3
Merge pull request #3 from neo-project/master
Nov 3, 2020
f293fb5
Merge pull request #4 from neo-project/master
Nov 23, 2020
6b0c49c
Upgrade target framework.
Nov 23, 2020
c36f9b2
Revert "Upgrade target framework."
Nov 25, 2020
0ce51ed
Safe Attribute
shargon Dec 5, 2020
782fa68
Update Contract1.cs (#398)
Nov 26, 2020
d13e363
Revert "publish neon to myget/github packages" (#395)
shargon Nov 28, 2020
85ca995
Remove Contract.Create、Contract.Update、Contract.Destroy; Add Contract…
Dec 7, 2020
1270f96
Merge branch 'master' into neo2119
Dec 7, 2020
b80c12f
Update
Dec 7, 2020
6f9aa5d
Merge branch 'neo2119' of https://github.com/chenzhitong/neo-devpack-…
Dec 7, 2020
25793fe
remove CallNative
Dec 7, 2020
6dc95ee
Update templates/Template.CSharp/Contract1.cs
Dec 8, 2020
ca0a550
Update templates/Template.CSharp/Contract1.cs
Dec 8, 2020
da4e1d8
Update tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contra…
Dec 8, 2020
e5d1685
public
Dec 9, 2020
d6f559c
Merge branch 'neo2119' of https://github.com/chenzhitong/neo-devpack-…
Dec 9, 2020
5658be1
Merge branch 'master' into safe-attribute
shargon Dec 10, 2020
21bef7c
Merge remote-tracking branch 'neo-project/master' into neo2119
shargon Dec 10, 2020
e2cf49b
Update
shargon Dec 10, 2020
479d5dd
Update call
shargon Dec 10, 2020
0bd6915
Fix more UT
shargon Dec 10, 2020
545143a
Update hashes
shargon Dec 10, 2020
c2f974b
Mock contract create
shargon Dec 10, 2020
f287e4d
Fix more UT
shargon Dec 10, 2020
1e02f34
Fix UT
shargon Dec 10, 2020
0298a2e
More fixes
shargon Dec 10, 2020
2d91cb3
Merge pull request #6 from shargon/safe-attribute
Dec 10, 2020
06c3b30
More fixes
shargon Dec 10, 2020
a7ce9fe
More UT
shargon Dec 10, 2020
3493312
Fix UT's
shargon Dec 10, 2020
a754a23
dotnet-format
shargon Dec 10, 2020
3e09e8d
Fix Last test
shargon Dec 10, 2020
431f482
Merge branch 'master' into neo2119
Dec 11, 2020
9439ccd
Update
Dec 14, 2020
c7d4ca8
Remove private properties
shargon Dec 14, 2020
493f225
Merge branch 'master' into neo2119
Dec 15, 2020
b0770d2
Fixed UT
Dec 15, 2020
aa997f3
disable warning CS0626
erikzhang Dec 16, 2020
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
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-CI01098" />
</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);
shargon marked this conversation as resolved.
Show resolved Hide resolved

[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("0x7ab39c37afd995f2f947a7ecbf40e91307058595")]
public class Designation
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
4 changes: 3 additions & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/GAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x36a019d836d964c438c573f78badf79b9e7eebdd")]
[Contract("0xb399c051778cf37a1e4ef88509b2e054d0420a32")]
public class GAS
{
public static extern UInt160 Hash { [ContractHash] get; }
public static extern int Id { get; }
public static extern uint ActiveBlockIndex { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this two methods, which weren't defined for contract.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove these fields, the fifth field will become the third one, which will be incorrect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use private

Copy link
Member

@shargon shargon Dec 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested without ActiveBlockIndex and it works well, I think that we should remove it. Please take a look @erikzhang

public static extern string Symbol { get; }
public static extern byte Decimals { get; }
public static extern BigInteger TotalSupply();
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 @@
namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0xdf18cb2476964c241558ed1e2e8881dcd2d50bde")]
public class ManagementContract
{
public static extern UInt160 Hash { [ContractHash] get; }
public static extern string Name { get; }
public static extern int Id { get; }
public static extern uint ActiveBlockIndex { 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("0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64")]
public class NEO
{
public static extern UInt160 Hash { [ContractHash] get; }
Expand Down
2 changes: 2 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class Oracle
{
public static extern UInt160 Hash { [ContractHash] get; }
public const uint MinimumResponseFee = 0_10000000;
public static extern int Id { get; }
public static extern uint ActiveBlockIndex { get; }
public static extern void Request(string url, string filter, string callback, object userData, long gasForResponse);
}
}
4 changes: 3 additions & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Neo.SmartContract.Framework.Services.Neo
{
[Contract("0x1ca594b36b6b6b3f05efce8b106c824053d18713")]
[Contract("0xe8ff1989c19526f4d8102f226e2c6c993b63efe9")]
public class Policy
{
public static extern UInt160 Hash { [ContractHash] get; }
public static extern int Id { get; }
public static extern uint ActiveBlockIndex { get; }
public static extern uint GetMaxTransactionsPerBlock();
public static extern uint GetMaxBlockSize();
public static extern long GetMaxBlockSystemFee();
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);
}
}
}
25 changes: 25 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.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;
}
}
}
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
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_ABIOSafe.cs", true, false);
var abi = buildScript.finalABI;

var methodsABI = abi["methods"] as JArray;
Assert.IsFalse(methodsABI[0]["safe"].AsBoolean());
Assert.IsTrue(methodsABI[1]["safe"].AsBoolean());
Assert.IsFalse(methodsABI[2]["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
25 changes: 8 additions & 17 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.Designate.Hash.ToString(), "0x7ab39c37afd995f2f947a7ecbf40e91307058595");
Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0x479f134275b36d64f31fdbdc557cd2aa2f32d8d1");
Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64");
Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0xb399c051778cf37a1e4ef88509b2e054d0420a32");
Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0xe8ff1989c19526f4d8102f226e2c6c993b63efe9");
}

[TestMethod]
Expand Down
Loading