Skip to content

Commit

Permalink
Merge pull request #3 from neo-project/master
Browse files Browse the repository at this point in the history
m
  • Loading branch information
陈志同 committed Nov 3, 2020
2 parents be2a614 + ff34e42 commit 30893a3
Show file tree
Hide file tree
Showing 25 changed files with 243 additions and 156 deletions.
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 @@ -42,7 +42,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-CI01036" />
<PackageReference Include="Neo" Version="3.0.0-CI01048" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 29 additions & 0 deletions src/Neo.SmartContract.Framework/ByteString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Neo.SmartContract.Framework
{
public abstract class ByteString
{
public extern byte this[int index]
{
[OpCode(OpCode.PICKITEM)]
get;
}

public extern int Length
{
[OpCode(OpCode.SIZE)]
get;
}

[Script]
public static extern implicit operator string(ByteString str);

[Script]
public static extern implicit operator ByteString(string str);

[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public static extern explicit operator byte[](ByteString str);

[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public static extern explicit operator ByteString(byte[] buffer);
}
}
12 changes: 4 additions & 8 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace Neo.SmartContract.Framework
{
public static class Helper
{
internal const string StackItemType_Integer = "0x21";
internal const string StackItemType_ByteString = "0x28";
internal const string StackItemType_Buffer = "0x30";

/// <summary>
/// Converts byte to byte[] considering the byte as a BigInteger (0x00 at the end)
/// </summary>
Expand All @@ -19,7 +15,7 @@ public static class Helper
/// <summary>
/// Converts sbyte to byte[].
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_Buffer)]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern static byte[] ToByteArray(this sbyte source);

/// <summary>
Expand All @@ -33,7 +29,7 @@ public static class Helper
[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.SWAP)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.CONVERT, StackItemType_Integer)]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern static BigInteger ToBigInteger(this byte[] source);
//{
// if (value == null) return 0;
Expand All @@ -43,13 +39,13 @@ public static class Helper
/// <summary>
/// Converts string to byte[]. Examples: "hello" -> [0x68656c6c6f]; "" -> []; "Neo" -> [0x4e656f]
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_Buffer)]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern static byte[] ToByteArray(this string source);

/// <summary>
/// Converts byte[] to string. Examples: [0x68656c6c6f] -> "hello"; [] -> ""; [0x4e656f] -> "Neo"
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_ByteString)]
[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public extern static string ToByteString(this byte[] source);

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/GAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class GAS
public static extern byte Decimals { get; }
public static extern BigInteger TotalSupply();
public static extern BigInteger BalanceOf(byte[] account);
public static extern bool Transfer(byte[] from, byte[] to, BigInteger amount);
}
}
4 changes: 3 additions & 1 deletion src/Neo.SmartContract.Framework/Services/Neo/NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class NEO
public static extern byte Decimals { get; }
public static extern BigInteger TotalSupply();
public static extern BigInteger BalanceOf(byte[] account);
public static extern bool Transfer(byte[] from, byte[] to, BigInteger amount);

public static extern bool SetGasPerBlock(BigInteger gasPerBlock);
public static extern BigInteger GetGasPerBlock();
public static extern BigInteger UnclaimedGas(byte[] account, uint end);

public static extern bool RegisterCandidate(byte[] pubkey);
public static extern bool UnRegisterCandidate(byte[] pubkey);
public static extern bool Vote(byte[] account, byte[] voteTo);
public static extern (string, BigInteger)[] GetCandidates();
public static extern string[] GetValidators();
public static extern string[] GetCommittee();
public static extern string[] GetNextBlockValidators();
}
Expand Down
1 change: 1 addition & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Neo.SmartContract.Framework.Services.Neo
[Contract("0x3c05b488bf4cf699d0631bf80190896ebbf38c3b")]
public class Oracle
{
public const uint MinimumResponseFee = 0_10000000;
public static extern string Name { get; }
public static extern void Request(string url, string filter, string callback, object userData, long gasForResponse);
}
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 @@ -13,7 +13,7 @@ public class Policy
public static extern uint GetMaxBlockSize();
public static extern long GetMaxBlockSystemFee();
public static extern BigInteger GetFeePerByte();
public static extern string[] GetBlockedAccounts();
public static extern string[] IsBlocked(byte[] account);
public static extern bool SetMaxBlockSize(uint value);
public static extern bool SetMaxTransactionsPerBlock(uint value);
public static extern bool SetMaxBlockSystemFee(long value);
Expand Down
9 changes: 9 additions & 0 deletions src/Neo.SmartContract.Framework/StackItemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Neo.SmartContract.Framework
{
internal static class StackItemType
{
public const string Integer = "0x21";
public const string ByteString = "0x28";
public const string Buffer = "0x30";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static string OracleName()
return Oracle.Name;
}

public static uint OracleMinimumResponseFee()
{
return Oracle.MinimumResponseFee;
}

public static string DesignationName()
{
return Designation.Name;
Expand Down
12 changes: 12 additions & 0 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public void Test_Oracle()
var entry = result.Pop();

Assert.AreEqual("Oracle", entry.GetString());

// Minimum Response Fee

testengine.Reset();
result = testengine.ExecuteTestCaseStandard("oracleMinimumResponseFee");

Assert.AreEqual(VMState.HALT, testengine.State);
Assert.AreEqual(1, result.Count);

entry = result.Pop();

Assert.AreEqual(0_10000000u, entry.GetInteger());
}

[TestMethod]
Expand Down
26 changes: 13 additions & 13 deletions tests/Neo.SmartContract.Framework.UnitTests/HelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void TestHexToBytes()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.AreEqual("0a0b0c0d0e0f", (item as ByteString).GetSpan().ToHexString());
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
Assert.AreEqual("0a0b0c0d0e0f", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -46,7 +46,7 @@ public void TestToBigInteger()
Assert.AreEqual(0, item.GetInteger());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[0]));
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new VM.Types.ByteString(new byte[0]));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand All @@ -57,7 +57,7 @@ public void TestToBigInteger()
// Value

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[] { 123 }));
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new VM.Types.ByteString(new byte[] { 123 }));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand Down Expand Up @@ -102,7 +102,7 @@ public void Test_ByteToByteArray()
var result = _engine.GetMethod("testByteToByteArray").Run();

StackItem wantResult = new byte[] { 0x01 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -111,7 +111,7 @@ public void Test_Reverse()
var result = _engine.GetMethod("testReverse").Run();

StackItem wantResult = new byte[] { 0x03, 0x02, 0x01 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -120,7 +120,7 @@ public void Test_SbyteToByteArray()
var result = _engine.GetMethod("testSbyteToByteArray").Run();

StackItem wantResult = new byte[] { 255 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -129,7 +129,7 @@ public void Test_StringToByteArray()
var result = _engine.GetMethod("testStringToByteArray").Run();

StackItem wantResult = new byte[] { 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -138,7 +138,7 @@ public void Test_Concat()
var result = _engine.GetMethod("testConcat").Run();

StackItem wantResult = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -147,7 +147,7 @@ public void Test_Range()
var result = _engine.GetMethod("testRange").Run();

StackItem wantResult = new byte[] { 0x02 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -156,7 +156,7 @@ public void Test_Take()
var result = _engine.GetMethod("testTake").Run();

StackItem wantResult = new byte[] { 0x01, 0x02 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -165,7 +165,7 @@ public void Test_Last()
var result = _engine.GetMethod("testLast").Run();

StackItem wantResult = new byte[] { 0x02, 0x03 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -174,7 +174,7 @@ public void Test_ToScriptHash()
var result = _engine.GetMethod("testToScriptHash").Run();

StackItem wantResult = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xaa, 0xbb, 0xcc, 0xdd, 0xee };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}
}
}
2 changes: 1 addition & 1 deletion tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void TestArrayConvert()

static JObject ParseJson(StackItem item)
{
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
var json = System.Text.Encoding.UTF8.GetString(item.GetSpan());
return JObject.Parse(json);
}
Expand Down
26 changes: 13 additions & 13 deletions tests/Neo.SmartContract.Framework.UnitTests/MapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void TestByteArray()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"a":"teststring2"}
Assert.AreEqual("7b2261223a2274657374737472696e6732227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b2261223a2274657374737472696e6732227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -61,9 +61,9 @@ public void TestClear()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {}
Assert.AreEqual("7b7d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b7d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}


Expand All @@ -76,9 +76,9 @@ public void TestByteArray2()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u0001\u0001":"\u0022\u0022"}
Assert.AreEqual("7b225c75303030315c7530303031223a225c75303032325c7530303232227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c75303030315c7530303031223a225c75303032325c7530303232227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -91,9 +91,9 @@ public void TestUnicode()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u4E2D":"129840test10022939"}
Assert.AreEqual("7b225c7534453244223a22313239383430746573743130303232393339227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c7534453244223a22313239383430746573743130303232393339227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -106,9 +106,9 @@ public void TestUnicodeValue()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"ab":"\u6587"}
Assert.AreEqual("7b226162223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b226162223a225c7536353837227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -122,9 +122,9 @@ public void TestUnicodeKeyValue()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u4E2D":"\u6587"}
Assert.AreEqual("7b225c7534453244223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c7534453244223a225c7536353837227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand Down Expand Up @@ -160,7 +160,7 @@ public void TestDeserialize()
var map = item as Map;
Assert.AreEqual(1, map.Count);
Assert.IsTrue(map.ContainsKey("a"));
Assert.AreEqual((ByteString)"testdeserialize", map["a"]);
Assert.AreEqual((VM.Types.ByteString)"testdeserialize", map["a"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public void Test_AccountIsStandard()

// Empty

var result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(new byte[0]));
var result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(new byte[0]));
Assert.AreEqual(VM.VMState.FAULT, _engine.State);
Assert.AreEqual(0, result.Count);

// No standard

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(new byte[20]));
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(new byte[20]));
Assert.AreEqual(VM.VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand All @@ -49,7 +49,7 @@ public void Test_AccountIsStandard()
Assert.AreEqual(false, item.GetBoolean());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(noStandard));
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(noStandard));
Assert.AreEqual(VM.VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand Down
Loading

0 comments on commit 30893a3

Please sign in to comment.