Skip to content

Commit

Permalink
Merge branch 'fix_1244_sysfee' of https://github.com/Tommo-L/neo into…
Browse files Browse the repository at this point in the history
… fix_1244_sysfee
  • Loading branch information
Luchuan committed Nov 18, 2019
2 parents c18c09d + 43985bd commit 22c87f2
Show file tree
Hide file tree
Showing 28 changed files with 696 additions and 99 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,4 @@ paket-files/
*.sln.iml

PublishProfiles
/.vscode
11 changes: 1 addition & 10 deletions neo.UnitTests/Network/RPC/UT_ContractClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ public void TestSetup()
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]);
}

[TestMethod]
public void TestMakeScript()
{
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero);

Assert.AreEqual("14000000000000000000000000000000000000000051c10962616c616e63654f66142582d1b275e86c8f0e93a9b2facd5fdb760976a168627d5b52",
testScript.ToHexString());
}

[TestMethod]
public void TestInvoke()
{
Expand Down Expand Up @@ -60,7 +51,7 @@ public void TestDeployContract()
UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter());

ContractClient contractClient = new ContractClient(rpcClientMock.Object);
var result = contractClient.DeployContract(new byte[1], manifest, keyPair1);
var result = contractClient.CreateDeployContractTx(new byte[1], manifest, keyPair1);

Assert.IsNotNull(result);
}
Expand Down
29 changes: 29 additions & 0 deletions neo.UnitTests/Network/RPC/UT_Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.RPC;
using System;
using System.Numerics;

namespace Neo.UnitTests.Network.RPC
{
[TestClass]
public class UT_Helper
{
[TestMethod]
public void TestToBigInteger()
{
decimal amount = 1.23456789m;
uint decimals = 9;
var result = amount.ToBigInteger(decimals);
Assert.AreEqual(1234567890, result);

amount = 1.23456789m;
decimals = 18;
result = amount.ToBigInteger(decimals);
Assert.AreEqual(BigInteger.Parse("1234567890000000000"), result);

amount = 1.23456789m;
decimals = 4;
Assert.ThrowsException<OverflowException>(() => result = amount.ToBigInteger(decimals));
}
}
}
25 changes: 24 additions & 1 deletion neo.UnitTests/Network/RPC/UT_Nep5API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System.Linq;
using System.Numerics;

namespace Neo.UnitTests.Network.RPC
Expand Down Expand Up @@ -76,13 +77,35 @@ public void TestGetTotalSupply()
Assert.AreEqual(1_00000000, (int)result);
}

[TestMethod]
public void TestGetTokenInfo()
{
UInt160 scriptHash = NativeContract.GAS.Hash;
byte[] testScript = scriptHash.MakeScript("name")
.Concat(scriptHash.MakeScript("symbol"))
.Concat(scriptHash.MakeScript("decimals"))
.Concat(scriptHash.MakeScript("totalSupply"))
.ToArray(); ;
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript,
new ContractParameter { Type = ContractParameterType.String, Value = NativeContract.GAS.Name },
new ContractParameter { Type = ContractParameterType.String, Value = NativeContract.GAS.Symbol },
new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(NativeContract.GAS.Decimals) },
new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) });

var result = nep5API.GetTokenInfo(NativeContract.GAS.Hash);
Assert.AreEqual(NativeContract.GAS.Name, result.Name);
Assert.AreEqual(NativeContract.GAS.Symbol, result.Symbol);
Assert.AreEqual(8, (int)result.Decimals);
Assert.AreEqual(1_00000000, (int)result.TotalSupply);
}

[TestMethod]
public void TestTransfer()
{
byte[] testScript = NativeContract.GAS.Hash.MakeScript("transfer", sender, UInt160.Zero, new BigInteger(1_00000000));
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter());

var result = nep5API.Transfer(NativeContract.GAS.Hash, keyPair1, UInt160.Zero, new BigInteger(1_00000000));
var result = nep5API.CreateTransferTx(NativeContract.GAS.Hash, keyPair1, UInt160.Zero, new BigInteger(1_00000000));
Assert.IsNotNull(result);
}
}
Expand Down
50 changes: 21 additions & 29 deletions neo.UnitTests/Network/RPC/UT_RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ public void TestGetBlockHex()
{
JObject response = CreateResponse(1);
response["result"] = "000000002deadfa82cbc4682f5800";

MockResponse(response.ToString());
var result = rpc.GetBlockHex("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual("000000002deadfa82cbc4682f5800", result);

MockResponse(response.ToString());
result = rpc.GetBlockHex("100");
var result = rpc.GetBlockHex("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual("000000002deadfa82cbc4682f5800", result);
}

Expand All @@ -134,20 +130,15 @@ public void TestGetBlock()
};

JObject json = block.ToJson();
json["confirmations"] = 20;
JObject response = CreateResponse(1);
response["result"] = json;

MockResponse(response.ToString());
var result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString());
Assert.IsNull(result.Confirmations);
Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length);
Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString());

MockResponse(response.ToString());
result = rpc.GetBlock("100");
var result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString());
Assert.IsNull(result.Confirmations);
Assert.IsNull(result.NextBlockHash);
Assert.AreEqual(20, result.Confirmations);
Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length);
Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString());

Expand All @@ -158,6 +149,7 @@ public void TestGetBlock()
result = rpc.GetBlock("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(block.Hash.ToString(), result.Block.Hash.ToString());
Assert.AreEqual(20, result.Confirmations);
Assert.AreEqual("0x773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e", result.NextBlockHash.ToString());
Assert.AreEqual(block.Transactions.Length, result.Block.Transactions.Length);
Assert.AreEqual(block.Transactions[0].Hash.ToString(), result.Block.Transactions[0].Hash.ToString());
}
Expand Down Expand Up @@ -189,13 +181,9 @@ public void TestGetBlockHeaderHex()
{
JObject response = CreateResponse(1);
response["result"] = "0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2";

MockResponse(response.ToString());
var result = rpc.GetBlockHeaderHex("100");
Assert.AreEqual("0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2", result);

MockResponse(response.ToString());
result = rpc.GetBlockHeaderHex("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
var result = rpc.GetBlockHeaderHex("100");
Assert.AreEqual("0x4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2", result);
}

Expand All @@ -206,18 +194,15 @@ public void TestGetBlockHeader()
TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 _, out UInt160 _, out ulong _, out uint _, out Witness _);

JObject json = header.ToJson();
json["confirmations"] = 20;
JObject response = CreateResponse(1);
response["result"] = json;
MockResponse(response.ToString());

var result = rpc.GetBlockHeader("100");
Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString());
Assert.IsNull(result.Confirmations);

MockResponse(response.ToString());
result = rpc.GetBlockHeader("773dd2dae4a9c9275290f89b56e67d7363ea4826dfd4fc13cc01cf73a44b0d0e");
Assert.AreEqual(header.Hash.ToString(), result.Header.Hash.ToString());
Assert.IsNull(result.Confirmations);
Assert.IsNull(result.NextBlockHash);
Assert.AreEqual(20, result.Confirmations);

json["confirmations"] = 20;
json["nextblockhash"] = "4c1e879872344349067c3b1a30781eeb4f9040d3795db7922f513f6f9660b9b2";
Expand Down Expand Up @@ -373,9 +358,18 @@ public void TestGetRawTransaction()
Assert.AreEqual(transaction.Hash, result.Transaction.Hash);
Assert.AreEqual(json.ToString(), result.ToJson().ToString());

// make the code compatible with the old version
json["blockhash"] = UInt256.Zero.ToString();
json["confirmations"] = 100;
json["blocktime"] = 10;
MockResponse(response.ToString());

result = rpc.GetRawTransaction("0x9786cce0dddb524c40ddbdd5e31a41ed1f6b5c8a683c122f627ca4a007a7cf4e");
Assert.AreEqual(transaction.Hash, result.Transaction.Hash);
Assert.AreEqual(100, result.Confirmations);
Assert.AreEqual(null, result.VMState);
Assert.AreEqual(json.ToString(), result.ToJson().ToString());

json["vmState"] = VMState.HALT;
MockResponse(response.ToString());

Expand Down Expand Up @@ -472,8 +466,7 @@ public void TestInvokeFunction()
""type"": ""ByteArray"",
""value"": ""262bec084432""
}
],
""tx"":""d101361426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf000000000000000000000000""
]
}");
JObject response = CreateResponse(1);
response["result"] = json;
Expand All @@ -496,8 +489,7 @@ public void TestInvokeScript()
""type"": ""ByteArray"",
""value"": ""262bec084432""
}
],
""tx"":""d101361426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf000000000000000000000000""
]
}");
JObject response = CreateResponse(1);
response["result"] = json;
Expand Down
5 changes: 2 additions & 3 deletions neo.UnitTests/Network/RPC/UT_TransactionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ public static void MockInvokeScript(Mock<RpcClient> mockClient, byte[] script, p
Stack = parameters,
GasConsumed = "100",
Script = script.ToHexString(),
State = "",
Tx = ""
State = ""
};

mockClient.Setup(p => p.RpcSend("invokescript", It.Is<JObject>(j => j.AsString() == script.ToHexString())))
mockClient.Setup(p => p.RpcSend("invokescript", It.Is<JObject[]>(j => j[0].AsString() == script.ToHexString())))
.Returns(result.ToJson())
.Verifiable();
}
Expand Down
116 changes: 116 additions & 0 deletions neo.UnitTests/Network/RPC/UT_WalletAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Neo.IO.Json;
using Neo.Network.P2P.Payloads;
using Neo.Network.RPC;
using Neo.Network.RPC.Models;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using System.Numerics;

namespace Neo.UnitTests.Network.RPC
{
[TestClass]
public class UT_WalletAPI
{
Mock<RpcClient> rpcClientMock;
KeyPair keyPair1;
string address1;
UInt160 sender;
WalletAPI walletAPI;

[TestInitialize]
public void TestSetup()
{
keyPair1 = new KeyPair(Wallet.GetPrivateKeyFromWIF("KyXwTh1hB76RRMquSvnxZrJzQx7h9nQP2PCRL38v6VDb5ip3nf1p"));
sender = Contract.CreateSignatureRedeemScript(keyPair1.PublicKey).ToScriptHash();
address1 = Neo.Wallets.Helper.ToAddress(sender);
rpcClientMock = UT_TransactionManager.MockRpcClient(sender, new byte[0]);
walletAPI = new WalletAPI(rpcClientMock.Object);
}

[TestMethod]
public void TestGetUnclaimedGas()
{
byte[] testScript = NativeContract.NEO.Hash.MakeScript("unclaimedGas", sender, 99);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetUnclaimedGas(address1);
Assert.AreEqual(1.1m, balance);
}

[TestMethod]
public void TestGetNeoBalance()
{
byte[] testScript = NativeContract.NEO.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) });

var balance = walletAPI.GetNeoBalance(address1);
Assert.AreEqual(1_00000000u, balance);
}

[TestMethod]
public void TestGetGasBalance()
{
byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetGasBalance(address1);
Assert.AreEqual(1.1m, balance);
}

[TestMethod]
public void TestGetTokenBalance()
{
byte[] testScript = UInt160.Zero.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

var balance = walletAPI.GetTokenBalance(UInt160.Zero.ToString(), address1);
Assert.AreEqual(1_10000000, balance);
}

[TestMethod]
public void TestClaimGas()
{
byte[] balanceScript = NativeContract.NEO.Hash.MakeScript("balanceOf", sender);
UT_TransactionManager.MockInvokeScript(rpcClientMock, balanceScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_00000000) });

byte[] testScript = NativeContract.NEO.Hash.MakeScript("transfer", sender, sender, new BigInteger(1_00000000));
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

rpcClientMock.Setup(p => p.RpcSend("sendrawtransaction", It.IsAny<JObject>())).Returns(true);

var tranaction = walletAPI.ClaimGas(keyPair1.Export());
Assert.AreEqual(testScript.ToHexString(), tranaction.Script.ToHexString());
}

[TestMethod]
public void TestTransfer()
{
byte[] decimalsScript = NativeContract.GAS.Hash.MakeScript("decimals");
UT_TransactionManager.MockInvokeScript(rpcClientMock, decimalsScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(8) });

byte[] testScript = NativeContract.GAS.Hash.MakeScript("transfer", sender, UInt160.Zero, NativeContract.GAS.Factor * 100);
UT_TransactionManager.MockInvokeScript(rpcClientMock, testScript, new ContractParameter { Type = ContractParameterType.Integer, Value = new BigInteger(1_10000000) });

rpcClientMock.Setup(p => p.RpcSend("sendrawtransaction", It.IsAny<JObject>())).Returns(true);

var tranaction = walletAPI.Transfer(NativeContract.GAS.Hash.ToString(), keyPair1.Export(), UInt160.Zero.ToAddress(), 100, 1.1m);
Assert.AreEqual(testScript.ToHexString(), tranaction.Script.ToHexString());
}

[TestMethod]
public void TestWaitTransaction()
{
Transaction transaction = TestUtils.GetTransaction();
rpcClientMock.Setup(p => p.RpcSend("getrawtransaction", It.Is<JObject[]>(j => j[0].AsString() == transaction.Hash.ToString())))
.Returns(new RpcTransaction { Transaction = transaction, VMState = VMState.HALT, BlockHash = UInt256.Zero, BlockTime = 100, Confirmations = 1 }.ToJson());

var tx = walletAPI.WaitTransaction(transaction).Result;
Assert.AreEqual(VMState.HALT, tx.VMState);
Assert.AreEqual(UInt256.Zero, tx.BlockHash);
}
}
}
15 changes: 3 additions & 12 deletions neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,13 @@ public void Check_BalanceOfTransferAndBurn()
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
NativeContract.GAS.Burn(engine, new UInt160(to), BigInteger.MinusOne));

// Burn more than expected

Assert.ThrowsException<InvalidOperationException>(() =>
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000600000000001)));

// Real burn

NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(1));
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(1)).Should().Be(1);

NativeContract.GAS.BalanceOf(snapshot, to).Should().Be(3000599999999999);

keyCount.Should().Be(snapshot.Storages.GetChangeSet().Count());

// Burn all

NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000599999999999));
// Burn more than expected
NativeContract.GAS.Burn(engine, new UInt160(to), new BigInteger(3000600000000000)).Should().Be(3000599999999999);

(keyCount - 1).Should().Be(snapshot.Storages.GetChangeSet().Count());

Expand Down
Loading

0 comments on commit 22c87f2

Please sign in to comment.