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

test RpcServer.Utilities, .SmartContract and .Wallet #3461

Merged
merged 21 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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/Plugins/RpcServer/RpcServer.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Neo.Plugins.RpcServer
partial class RpcServer
{
[RpcMethod]
protected virtual JToken ListPlugins(JArray _params)
protected internal virtual JToken ListPlugins(JArray _params)
{
return new JArray(Plugin.Plugins
.OrderBy(u => u.Name)
Expand All @@ -34,7 +34,7 @@ protected virtual JToken ListPlugins(JArray _params)
}

[RpcMethod]
protected virtual JToken ValidateAddress(JArray _params)
protected internal virtual JToken ValidateAddress(JArray _params)
{
string address = Result.Ok_Or(() => _params[0].AsString(), RpcError.InvalidParams.WithData($"Invlid address format: {_params[0]}"));
JObject json = new();
Expand Down
100 changes: 100 additions & 0 deletions tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_RpcServer.Wallet.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.UnitTests;
using Neo.UnitTests.Extensions;
using Neo.Wallets;
using System;
using System.IO;
using System.Linq;

namespace Neo.Plugins.RpcServer.Tests;

public partial class UT_RpcServer
{
static readonly string neoScriptHash = "0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5";
static readonly string NeoTotalSupplyScript = "wh8MC3RvdGFsU3VwcGx5DBT1Y\u002BpAvCg9TQ4FxI6jBbPyoHNA70FifVtS";
static readonly JArray signers = [new JObject()
Copy link
Contributor

Choose a reason for hiding this comment

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

can we do abnormal test with tons of signers, tons of opened sessions, invalid address

{
["account"] = "0xef4073a0f2b305a38ec4050e4d3d28bc40ea63f5",
["scopes"] = "CalledByEntry",
}];

[TestMethod]
public void TestInvokeFunction()
{
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(neoScriptHash, "totalSupply", new JArray([]), signers, true));
Assert.AreEqual(resp.Count, 7);
Assert.AreEqual(resp["script"], NeoTotalSupplyScript);
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], neoScriptHash);
Assert.AreEqual(resp["state"], "HALT");
Assert.AreEqual(resp["exception"], null);
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
Assert.AreEqual(resp["stack"][0]["type"], "Integer");
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
}

[TestMethod]
public void TestInvokeScript()
{
JObject resp = (JObject)_rpcServer.InvokeScript(new JArray(NeoTotalSupplyScript, signers, true));
Assert.AreEqual(resp.Count, 7);
Assert.IsTrue(resp.ContainsProperty("gasconsumed"));
Assert.IsTrue(resp.ContainsProperty("diagnostics"));
Assert.AreEqual(resp["diagnostics"]["invokedcontracts"]["call"][0]["hash"], neoScriptHash);
Assert.AreEqual(resp["state"], "HALT");
Assert.AreEqual(resp["exception"], null);
Assert.AreEqual(((JArray)resp["notifications"]).Count, 0);
Assert.AreEqual(resp["stack"][0]["type"], "Integer");
Assert.AreEqual(resp["stack"][0]["value"], "100000000");
}

[TestMethod]
public void TestTraverseIterator()
{
JObject resp = (JObject)_rpcServer.InvokeFunction(new JArray(neoScriptHash, "getAllCandidates", new JArray([]), signers, true));
string sessionId = resp["session"].AsString();
string iteratorId = resp["stack"][0]["id"].AsString();
JArray respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
Assert.AreEqual(respArray.Count, 0);
_rpcServer.TerminateSession([sessionId]);
try
{
respArray = (JArray)_rpcServer.TraverseIterator([sessionId, iteratorId, 100]);
}
catch (RpcException e)
{
Assert.AreEqual(e.Message, "Unknown session");
}
}

[TestMethod]
public void TestGetUnclaimedGas()
{
string address = Contract
.CreateSignatureRedeemScript(TestProtocolSettings.SoleNode.StandbyCommittee[0])
.ToScriptHash().ToAddress(ProtocolSettings.Default.AddressVersion);
JObject resp = (JObject)_rpcServer.GetUnclaimedGas([address]);
Assert.AreEqual(resp["unclaimed"], "0");
Assert.AreEqual(resp["address"], address);
}
}
54 changes: 54 additions & 0 deletions tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_RpcServer.Wallet.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.UnitTests;
using Neo.UnitTests.Extensions;
using System;
using System.IO;
using System.Linq;

namespace Neo.Plugins.RpcServer.Tests;

public partial class UT_RpcServer
{
[TestMethod]
public void TestListPlugins()
{
JArray resp = (JArray)_rpcServer.ListPlugins([]);
Assert.AreEqual(resp.Count, 0);
Plugins.Plugin.Plugins.Add(new RpcServerPlugin());
resp = (JArray)_rpcServer.ListPlugins([]);
Assert.AreEqual(resp.Count, 2);
foreach (JObject p in resp)
Assert.AreEqual(p["name"], "RpcServer");
}

[TestMethod]
public void TestValidateAddress()
{
string validAddr = "NM7Aky765FG8NhhwtxjXRx7jEL1cnw7PBP";
JObject resp = (JObject)_rpcServer.ValidateAddress([validAddr]);
Assert.AreEqual(resp["address"], validAddr);
Assert.AreEqual(resp["isvalid"], true);
string invalidAddr = "ANeo2toNeo3MigrationAddressxwPB2Hz";
resp = (JObject)_rpcServer.ValidateAddress([invalidAddr]);
Assert.AreEqual(resp["address"], invalidAddr);
Assert.AreEqual(resp["isvalid"], false);
}
}
24 changes: 23 additions & 1 deletion tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Neo.Wallets;
using Neo.Wallets.NEP6;
using System;
using System.Net;
using System.Text;

namespace Neo.Plugins.RpcServer.Tests
Expand All @@ -39,7 +40,28 @@ public void TestSetup()
_memoryStore = new MemoryStore();
_memoryStoreProvider = new TestMemoryStoreProvider(_memoryStore);
_neoSystem = new NeoSystem(TestProtocolSettings.SoleNode, _memoryStoreProvider);
_rpcServer = new RpcServer(_neoSystem, RpcServerSettings.Default);
_rpcServer = new RpcServer(_neoSystem, new RpcServerSettings
Copy link
Member

Choose a reason for hiding this comment

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

why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need RpcServer session enabled to traverse iterator

Copy link
Member

Choose a reason for hiding this comment

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

I think this can work.

_rpcServer = new RpcServer(_neoSystem, RpcServerSettings.Default with { SessionEnabled = true });

{
Network = 5195086u,
BindAddress = IPAddress.None,
SslCert = string.Empty,
SslCertPassword = string.Empty,
MaxGasInvoke = (long)new BigDecimal(10M, NativeContract.GAS.Decimals).Value,
MaxFee = (long)new BigDecimal(0.1M, NativeContract.GAS.Decimals).Value,
TrustedAuthorities = Array.Empty<string>(),
EnableCors = true,
AllowOrigins = Array.Empty<string>(),
KeepAliveTimeout = 60,
RequestHeadersTimeout = 15,
MaxIteratorResultItems = 100,
MaxStackSize = ushort.MaxValue,
DisabledMethods = Array.Empty<string>(),
MaxConcurrentConnections = 40,
MaxRequestBodySize = 5 * 1024 * 1024,
SessionEnabled = true,
SessionExpirationTime = TimeSpan.FromSeconds(60),
FindStoragePageSize = 50
});
_walletAccount = _wallet.Import("KxuRSsHgJMb3AMSN6B9P3JHNGMFtxmuimqgR9MmXPcv3CLLfusTd");
var key = new KeyBuilder(NativeContract.GAS.Id, 20).Add(_walletAccount.ScriptHash);
var snapshot = _neoSystem.GetSnapshotCache();
Expand Down
Loading