Skip to content

Commit 9ce9c6f

Browse files
authored
Update UT RpcServer (neo-project#3460)
* Update UT_RpcServer.Blockchain.cs * Update UT_RpcServer.Blockchain.cs * update * fixed bug * format * update * Update NativeContractExtensions.cs * update * Remove conflicting files * update * format
1 parent dc926f7 commit 9ce9c6f

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

src/Neo/Network/P2P/Peer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static Peer()
146146
/// Tries to add a set of peers to the immutable ImmutableHashSet of UnconnectedPeers.
147147
/// </summary>
148148
/// <param name="peers">Peers that the method will try to add (union) to (with) UnconnectedPeers.</param>
149-
protected void AddPeers(IEnumerable<IPEndPoint> peers)
149+
protected internal void AddPeers(IEnumerable<IPEndPoint> peers)
150150
{
151151
if (UnconnectedPeers.Count < UnconnectedMax)
152152
{

src/Plugins/RpcServer/RpcServer.Node.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ namespace Neo.Plugins.RpcServer
2424
partial class RpcServer
2525
{
2626
[RpcMethod]
27-
protected virtual JToken GetConnectionCount(JArray _params)
27+
protected internal virtual JToken GetConnectionCount(JArray _params)
2828
{
2929
return localNode.ConnectedCount;
3030
}
3131

3232
[RpcMethod]
33-
protected virtual JToken GetPeers(JArray _params)
33+
protected internal virtual JToken GetPeers(JArray _params)
3434
{
3535
JObject json = new();
3636
json["unconnected"] = new JArray(localNode.GetUnconnectedPeers().Select(p =>

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Blockchain.cs

+57-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
using Akka.Actor;
1313
using Akka.Util.Internal;
14+
using FluentAssertions;
1415
using Microsoft.VisualStudio.TestTools.UnitTesting;
16+
using Neo.Cryptography.ECC;
1517
using Neo.IO;
1618
using Neo.Json;
1719
using Neo.Ledger;
@@ -23,6 +25,8 @@
2325
using Neo.UnitTests.Extensions;
2426
using System;
2527
using System.Linq;
28+
using System.Security.Policy;
29+
using static Neo.SmartContract.Native.NeoToken;
2630

2731
namespace Neo.Plugins.RpcServer.Tests
2832
{
@@ -61,6 +65,11 @@ public void TestGetBlockByHash()
6165
{
6266
Assert.AreEqual(VerifyResult.Succeed, tx.VerifyStateIndependent(UnitTests.TestProtocolSettings.Default));
6367
});
68+
69+
result = _rpcServer.GetBlock(new BlockHashOrIndex(block.Hash), true);
70+
var block3 = block.ToJson(UnitTests.TestProtocolSettings.Default);
71+
block3["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
72+
result.ToString().Should().Be(block3.ToString());
6473
}
6574

6675
[TestMethod]
@@ -78,6 +87,11 @@ public void TestGetBlockByIndex()
7887
{
7988
Assert.AreEqual(VerifyResult.Succeed, tx.VerifyStateIndependent(UnitTests.TestProtocolSettings.Default));
8089
});
90+
91+
result = _rpcServer.GetBlock(new BlockHashOrIndex(block.Index), true);
92+
var block3 = block.ToJson(UnitTests.TestProtocolSettings.Default);
93+
block3["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
94+
result.ToString().Should().Be(block3.ToString());
8195
}
8296

8397
[TestMethod]
@@ -120,6 +134,11 @@ public void TestGetBlockHeader()
120134
var header = block.Header.ToJson(_neoSystem.Settings);
121135
header["confirmations"] = NativeContract.Ledger.CurrentIndex(snapshot) - block.Index + 1;
122136
Assert.AreEqual(header.ToString(), result.ToString());
137+
138+
result = _rpcServer.GetBlockHeader(new BlockHashOrIndex(block.Hash), false);
139+
var headerArr = Convert.FromBase64String(result.AsString());
140+
var header2 = headerArr.AsSerializable<Header>();
141+
header2.ToJson(_neoSystem.Settings).ToString().Should().Be(block.Header.ToJson(_neoSystem.Settings).ToString());
123142
}
124143

125144
[TestMethod]
@@ -129,9 +148,23 @@ public void TestGetContractState()
129148
var contractState = TestUtils.GetContract();
130149
snapshot.AddContract(contractState.Hash, contractState);
131150
snapshot.Commit();
151+
132152
var result = _rpcServer.GetContractState(new ContractNameOrHashOrId(contractState.Hash));
153+
Assert.AreEqual(contractState.ToJson().ToString(), result.ToString());
133154

155+
result = _rpcServer.GetContractState(new ContractNameOrHashOrId(contractState.Id));
134156
Assert.AreEqual(contractState.ToJson().ToString(), result.ToString());
157+
158+
var byId = _rpcServer.GetContractState(new ContractNameOrHashOrId(-1));
159+
var byName = _rpcServer.GetContractState(new ContractNameOrHashOrId("ContractManagement"));
160+
byId.ToString().Should().Be(byName.ToString());
161+
162+
snapshot.DeleteContract(contractState.Hash);
163+
snapshot.Commit();
164+
Action act = () => _rpcServer.GetContractState(new ContractNameOrHashOrId(contractState.Hash));
165+
act.Should().Throw<RpcException>().WithMessage(RpcError.UnknownContract.Message);
166+
act = () => _rpcServer.GetContractState(new ContractNameOrHashOrId(contractState.Id));
167+
act.Should().Throw<RpcException>().WithMessage(RpcError.UnknownContract.Message);
135168
}
136169

137170
[TestMethod]
@@ -143,8 +176,10 @@ public void TestGetRawMemPool()
143176
_neoSystem.MemPool.TryAdd(tx, snapshot);
144177

145178
var result = _rpcServer.GetRawMemPool();
146-
147179
Assert.IsTrue(((JArray)result).Any(p => p.AsString() == tx.Hash.ToString()));
180+
181+
result = _rpcServer.GetRawMemPool(true);
182+
Assert.IsTrue(((JArray)result["verified"]).Any(p => p.AsString() == tx.Hash.ToString()));
148183
}
149184

150185
[TestMethod]
@@ -154,10 +189,14 @@ public void TestGetRawTransaction()
154189
var tx = TestUtils.CreateValidTx(snapshot, _wallet, _walletAccount);
155190
_neoSystem.MemPool.TryAdd(tx, snapshot);
156191
snapshot.Commit();
157-
var result = _rpcServer.GetRawTransaction(tx.Hash, true);
158192

193+
var result = _rpcServer.GetRawTransaction(tx.Hash, true);
159194
var json = Utility.TransactionToJson(tx, _neoSystem.Settings);
160195
Assert.AreEqual(json.ToString(), result.ToString());
196+
197+
result = _rpcServer.GetRawTransaction(tx.Hash, false);
198+
var tx2 = Convert.FromBase64String(result.AsString()).AsSerializable<Transaction>();
199+
tx2.ToJson(_neoSystem.Settings).ToString().Should().Be(tx.ToJson(_neoSystem.Settings).ToString());
161200
}
162201

163202
[TestMethod]
@@ -197,6 +236,15 @@ public void TestFindStorage()
197236
json["next"] = 1;
198237
json["results"] = jarr;
199238
Assert.AreEqual(json.ToString(), result.ToString());
239+
240+
var result2 = _rpcServer.FindStorage(new ContractNameOrHashOrId(contractState.Hash), Convert.ToBase64String(key));
241+
result2.ToString().Should().Be(result.ToString());
242+
243+
Enumerable.Range(0, 51).ToList().ForEach(i => TestUtils.StorageItemAdd(snapshot, contractState.Id, new byte[] { 0x01, (byte)i }, new byte[] { 0x02 }));
244+
snapshot.Commit();
245+
var result4 = _rpcServer.FindStorage(new ContractNameOrHashOrId(contractState.Hash), Convert.ToBase64String(new byte[] { 0x01 }), 0);
246+
result4["next"].Should().Be(RpcServerSettings.Default.FindStoragePageSize);
247+
(result4["truncated"]).AsBoolean().Should().Be(true);
200248
}
201249

202250
[TestMethod]
@@ -232,12 +280,16 @@ public void TestGetNextBlockValidators()
232280
public void TestGetCandidates()
233281
{
234282
var snapshot = _neoSystem.GetSnapshotCache();
283+
235284
var result = _rpcServer.GetCandidates();
236285
var json = new JArray();
237286
var validators = NativeContract.NEO.GetNextBlockValidators(snapshot, _neoSystem.Settings.ValidatorsCount);
287+
288+
var key = new KeyBuilder(NativeContract.NEO.Id, 33).Add(ECPoint.Parse("02237309a0633ff930d51856db01d17c829a5b2e5cc2638e9c03b4cfa8e9c9f971", ECCurve.Secp256r1));
289+
snapshot.Add(key, new StorageItem(new CandidateState() { Registered = true, Votes = 10000 }));
238290
snapshot.Commit();
239291
var candidates = NativeContract.NEO.GetCandidates(_neoSystem.GetSnapshotCache());
240-
292+
result = _rpcServer.GetCandidates();
241293
foreach (var candidate in candidates)
242294
{
243295
var item = new JObject();
@@ -350,7 +402,8 @@ public void TestGetBlockHashInvalidIndex()
350402
var block = TestUtils.CreateBlockWithValidTransactions(snapshot, _wallet, _walletAccount, 3);
351403
TestUtils.BlocksAdd(snapshot, block.Hash, block);
352404
snapshot.Commit();
353-
Assert.ThrowsException<RpcException>(() => _rpcServer.GetBlockHash(block.Index + 1));
405+
Action act = () => _rpcServer.GetBlockHash(block.Index + 1);
406+
act.Should().Throw<RpcException>();
354407
}
355408

356409
[TestMethod]

tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs

+32
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,50 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using Akka.Actor;
13+
using FluentAssertions;
1214
using Microsoft.VisualStudio.TestTools.UnitTesting;
1315
using Neo.IO;
1416
using Neo.Json;
17+
using Neo.Network.P2P;
1518
using Neo.Network.P2P.Payloads;
1619
using Neo.SmartContract.Native;
1720
using Neo.UnitTests;
1821
using System;
22+
using System.Collections.Generic;
23+
using System.Net;
1924

2025
namespace Neo.Plugins.RpcServer.Tests
2126
{
2227
partial class UT_RpcServer
2328
{
29+
[TestMethod]
30+
public void TestGetConnectionCount()
31+
{
32+
var result = _rpcServer.GetConnectionCount(new JArray());
33+
result.GetType().Should().Be(typeof(JNumber));
34+
}
35+
36+
[TestMethod]
37+
public void TestGetPeers()
38+
{
39+
var settings = TestProtocolSettings.SoleNode;
40+
var neoSystem = new NeoSystem(settings, _memoryStoreProvider);
41+
var localNode = neoSystem.LocalNode.Ask<LocalNode>(new LocalNode.GetInstance()).Result;
42+
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 11332) });
43+
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 12332) });
44+
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 13332) });
45+
var rpcServer = new RpcServer(neoSystem, RpcServerSettings.Default);
46+
47+
var result = rpcServer.GetPeers(new JArray());
48+
Assert.IsInstanceOfType(result, typeof(JObject));
49+
var json = (JObject)result;
50+
json.ContainsProperty("unconnected").Should().BeTrue();
51+
(json["unconnected"] as JArray).Count.Should().Be(3);
52+
json.ContainsProperty("bad").Should().BeTrue();
53+
json.ContainsProperty("connected").Should().BeTrue();
54+
}
55+
2456
[TestMethod]
2557
public void TestGetVersion()
2658
{

tests/Neo.UnitTests/Extensions/NativeContractExtensions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// Redistribution and use in source and binary forms with or without
1010
// modifications are permitted.
1111

12+
using FluentAssertions;
13+
using Neo.IO;
1214
using Neo.Network.P2P.Payloads;
1315
using Neo.Persistence;
1416
using Neo.SmartContract;
@@ -100,14 +102,26 @@ public static void DestroyContract(this DataCache snapshot, UInt160 callingScrip
100102

101103
public static void AddContract(this DataCache snapshot, UInt160 hash, ContractState state)
102104
{
105+
//key: hash, value: ContractState
103106
var key = new KeyBuilder(NativeContract.ContractManagement.Id, 8).Add(hash);
104107
snapshot.Add(key, new StorageItem(state));
108+
//key: id, value: hash
109+
var key2 = new KeyBuilder(NativeContract.ContractManagement.Id, 12).AddBigEndian(state.Id);
110+
if (!snapshot.Contains(key2)) snapshot.Add(key2, new StorageItem(hash.ToArray()));
105111
}
106112

107113
public static void DeleteContract(this DataCache snapshot, UInt160 hash)
108114
{
115+
//key: hash, value: ContractState
109116
var key = new KeyBuilder(NativeContract.ContractManagement.Id, 8).Add(hash);
117+
var value = snapshot.TryGet(key)?.GetInteroperable<ContractState>();
110118
snapshot.Delete(key);
119+
if (value != null)
120+
{
121+
//key: id, value: hash
122+
var key2 = new KeyBuilder(NativeContract.ContractManagement.Id, 12).AddBigEndian(value.Id);
123+
snapshot.Delete(key2);
124+
}
111125
}
112126

113127
public static StackItem Call(this NativeContract contract, DataCache snapshot, string method, params ContractParameter[] args)

0 commit comments

Comments
 (0)