Skip to content

Commit

Permalink
Update Neo.SmartContract.Framework according to last neo3 changes (#106)
Browse files Browse the repository at this point in the history
* Update syscalls for neo3

* Update block

* Fix

* UT for test all syscalls

* Clean ut

* Update values

* Update opcodes

* More Syscalls

* All ut

* Remove CRLF

* Test Account

* Json Test

* Update

* More tests

* Ensure checks

* Runtime done

* Native contracts

* Format

* Reorder UT

* Native contract tests

* Remove unused class

* Blockchain UT

Except GetContract

* Blockchain UT 100%

* Crypto

* Clean

* ReadOnly Storages

* Enumerator

* Iterator

* Contract Unit test

* Format

* Format

* Revert dotnet upgrade in this PR
  • Loading branch information
shargon committed Oct 24, 2019
1 parent 05ad219 commit 502c0ab
Show file tree
Hide file tree
Showing 61 changed files with 2,806 additions and 571 deletions.
6 changes: 4 additions & 2 deletions src/Neo.Compiler.MSIL/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static uint ToInteropMethodHash(this byte[] method)
return BitConverter.ToUInt32(sha.ComputeHash(method), 0);
}
}
public static byte[] HexString2Bytes(string str)

public static byte[] HexString2Bytes(this string str)
{
if (str.IndexOf("0x") == 0)
str = str.Substring(2);
Expand All @@ -46,6 +47,7 @@ public static byte[] HexString2Bytes(string str)
}
return outd;
}

public static byte[] OpDataToBytes(string opdata)
{
try // convert hex string to byte[]
Expand All @@ -54,7 +56,7 @@ public static byte[] OpDataToBytes(string opdata)
}
catch
{
return System.Text.Encoding.UTF8.GetBytes(opdata);
return Encoding.UTF8.GetBytes(opdata);
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/Neo.Compiler.MSIL/MSIL/CctorSubVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public static object Dup(object src)
}
}

public static byte[] HexString2Bytes(string str)
{
byte[] outd = new byte[str.Length / 2];
for (var i = 0; i < str.Length / 2; i++)
{
outd[i] = byte.Parse(str.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
return outd;
}

public static void Parse(ILMethod from, NeoModule to)
{
calcStack = new Stack<object>();
Expand Down Expand Up @@ -193,8 +183,7 @@ public static void Parse(ILMethod from, NeoModule to)
}
else if (attrname == "HexToBytes")//HexString2Bytes to bytes[]
{
if (text.IndexOf("0x") == 0) text = text.Substring(2);
var hex = HexString2Bytes(text);
var hex = text.HexString2Bytes();
calcStack.Push(hex);
}
else if (attrname == "ToBigInteger")
Expand Down
11 changes: 4 additions & 7 deletions src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,10 @@ public bool IsAppCall(Mono.Cecil.MethodDefinition defs, out byte[] hash)

try
{
hash = new byte[20];
if (hashstr.Length < 40)
throw new Exception("hash too short:" + hashstr);
for (var i = 0; i < 20; i++)
{
hash[i] = byte.Parse(hashstr.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
hash = hashstr.HexString2Bytes();
if (hash.Length != 20)
throw new Exception("Wrong hash:" + hashstr);

//string hexhash 需要反序
hash = hash.Reverse().ToArray();
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2015-2019 The Neo Project</Copyright>
Expand All @@ -23,7 +23,7 @@

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.0" />
<PackageReference Include="Neo" Version="3.0.0-CI00190" />
<PackageReference Include="Neo" Version="3.0.0-CI00191" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Neo.SmartContract.Framework/AppcallAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public AppcallAttribute(byte[] scriptHash)
public AppcallAttribute(string scriptHash)
{
if (scriptHash == null) throw new ArgumentNullException();

if (scriptHash.StartsWith("0x"))
{
scriptHash = scriptHash.Remove(0, 2);
}

if (scriptHash.Length != 40) throw new ArgumentException();
this.ScriptHash = new byte[scriptHash.Length / 2];
for (int i = 0; i < this.ScriptHash.Length; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ public static byte[] Reverse(this byte[] source)
[NonemitWithConvert(ConvertMethod.ToBigInteger)]
public extern static BigInteger ToBigInteger(this string text);

[Syscall("Neo.Runtime.Serialize")]
[Syscall("System.Runtime.Serialize")]
public extern static byte[] Serialize(this object source);

[Syscall("Neo.Runtime.Deserialize")]
[Syscall("System.Runtime.Deserialize")]
public extern static object Deserialize(this byte[] source);
}
}
18 changes: 9 additions & 9 deletions src/Neo.SmartContract.Framework/Services/Neo/Block.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Neo.SmartContract.Framework.Services.Neo
{
public class Block : Header
public class Block
{
[Syscall("Neo.Block.GetTransactionCount")]
public extern int GetTransactionCount();

[Syscall("Neo.Block.GetTransactions")]
public extern Transaction[] GetTransactions();

[Syscall("Neo.Block.GetTransaction")]
public extern Transaction GetTransaction(int index);
public readonly byte[] Hash;
public readonly uint Version;
public readonly byte[] PrevHash;
public readonly byte[] MerkleRoot;
public readonly ulong Timestamp;
public readonly uint Index;
public readonly byte[] NextConsensus;
public readonly int TransactionsCount;
}
}
27 changes: 16 additions & 11 deletions src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
using System.Numerics;

namespace Neo.SmartContract.Framework.Services.Neo
{
public static class Blockchain
{
[Syscall("Neo.Blockchain.GetHeight")]
[Syscall("System.Blockchain.GetHeight")]
public static extern uint GetHeight();

[Syscall("Neo.Blockchain.GetHeader")]
public static extern Header GetHeader(uint height);

[Syscall("Neo.Blockchain.GetHeader")]
public static extern Header GetHeader(byte[] hash);

[Syscall("Neo.Blockchain.GetBlock")]
[Syscall("System.Blockchain.GetBlock")]
public static extern Block GetBlock(uint height);

[Syscall("Neo.Blockchain.GetBlock")]
[Syscall("System.Blockchain.GetBlock")]
public static extern Block GetBlock(byte[] hash);

[Syscall("Neo.Blockchain.GetTransaction")]
[Syscall("System.Blockchain.GetTransaction")]
public static extern Transaction GetTransaction(byte[] hash);

[Syscall("Neo.Blockchain.GetContract")]
[Syscall("System.Blockchain.GetTransactionFromBlock")]
public static extern Transaction GetTransactionFromBlock(byte[] blockHash, int txIndex);

[Syscall("System.Blockchain.GetTransactionFromBlock")]
public static extern Transaction GetTransactionFromBlock(uint blockIndex, int txIndex);

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

[Syscall("System.Blockchain.GetContract")]
public static extern Contract GetContract(byte[] script_hash);
}
}
32 changes: 19 additions & 13 deletions src/Neo.SmartContract.Framework/Services/Neo/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ namespace Neo.SmartContract.Framework.Services.Neo
{
public class Contract
{
public extern byte[] Script
{
[Syscall("Neo.Contract.GetScript")]
get;
}
/// <summary>
/// Script
/// </summary>
public readonly byte[] Script;

public extern bool IsPayable
{
[Syscall("Neo.Contract.IsPayable")]
get;
}
/// <summary>
/// Has storage
/// </summary>
public readonly bool HasStorage;

/// <summary>
/// Is payable
/// </summary>
public readonly bool IsPayable;

[Syscall("System.Contract.Call")]
public static extern object Call(byte[] scriptHash, string method, object[] arguments);

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

[Syscall("Neo.Contract.Migrate")]
public static extern Contract Migrate(byte[] script, ContractPropertyState contract_property_state);
[Syscall("Neo.Contract.Update")]
public static extern void Update(byte[] script, string manifest);

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

This file was deleted.

14 changes: 14 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Crypto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Neo.SmartContract.Framework.Services.Neo
{
public static class Crypto
{
[Syscall("Neo.Crypto.CheckSig")]
public extern static bool VerifySignature(byte[] pubkey, byte[] signature);

[Syscall("System.Crypto.Verify")]
public extern static bool VerifySignature(byte[] message, byte[] pubkey, byte[] signature);

[Syscall("Neo.Crypto.CheckMultiSig")]
public extern static bool VerifySignatures(byte[][] pubkey, byte[][] signature);
}
}
22 changes: 22 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Enumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace Neo.SmartContract.Framework.Services.Neo
{
public class Enumerator<TValue>
{
[Syscall("Neo.Enumerator.Create")]
public static extern Enumerator<TValue> Create(IEnumerable<TValue> entry);

[Syscall("Neo.Enumerator.Concat")]
public extern Enumerator<TValue> Concat(Enumerator<TValue> value);

[Syscall("Neo.Enumerator.Next")]
public extern bool Next();

public extern TValue Value
{
[Syscall("Neo.Enumerator.Value")]
get;
}
}
}
47 changes: 0 additions & 47 deletions src/Neo.SmartContract.Framework/Services/Neo/Header.cs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Iterator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
using System.Collections.Generic;

namespace Neo.SmartContract.Framework.Services.Neo
{
public class Iterator<TKey, TValue>
{
[Syscall("Neo.Iterator.Create")]
public static extern Iterator<TKey, TValue> Create(Map<TKey, TValue> entry);

[Syscall("Neo.Iterator.Create")]
public static extern Iterator<TKey, TValue> Create(IEnumerable<TValue> entry);

[Syscall("Neo.Iterator.Concat")]
public extern Iterator<TKey, TValue> Concat(Iterator<TKey, TValue> value);

[Syscall("Neo.Enumerator.Next")]
public extern bool Next();

Expand All @@ -16,5 +27,17 @@ public class Iterator<TKey, TValue>
[Syscall("Neo.Enumerator.Value")]
get;
}

public extern Enumerator<TKey> Keys
{
[Syscall("Neo.Iterator.Keys")]
get;
}

public extern Enumerator<TValue> Values
{
[Syscall("Neo.Iterator.Values")]
get;
}
}
}
11 changes: 11 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Json.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Neo.SmartContract.Framework.Services.Neo
{
public static class Json
{
[Syscall("Neo.Json.Serialize")]
public extern static string Serialize(object obj);

[Syscall("Neo.Json.Deserialize")]
public extern static object Deserialize(string json);
}
}
14 changes: 14 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Native.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Neo.SmartContract.Framework.Services.Neo
{
public class Native
{
[Appcall("0x43cf98eddbe047e198a3e5d57006311442a0ca15")]
public static extern object NEO(string method, object[] arguments);

[Appcall("0xa1760976db5fcdfab2a9930e8f6ce875b2d18225")]
public static extern object GAS(string method, object[] arguments);

[Appcall("0x9c5699b260bd468e2160dd5d45dfd2686bba8b77")]
public static extern object Policy(string method, object[] arguments);
}
}
8 changes: 7 additions & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ public static class Runtime
get;
}

public static extern uint Time
public static extern string Platform
{
[Syscall("System.Runtime.Platform")]
get;
}

public static extern ulong Time
{
[Syscall("System.Runtime.GetTime")]
get;
Expand Down
Loading

0 comments on commit 502c0ab

Please sign in to comment.