Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhitong committed Aug 21, 2024
1 parent c7e4456 commit ad494b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Neo/Network/P2P/Peer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static Peer()
/// Tries to add a set of peers to the immutable ImmutableHashSet of UnconnectedPeers.
/// </summary>
/// <param name="peers">Peers that the method will try to add (union) to (with) UnconnectedPeers.</param>
protected void AddPeers(IEnumerable<IPEndPoint> peers)
protected internal void AddPeers(IEnumerable<IPEndPoint> peers)
{
if (UnconnectedPeers.Count < UnconnectedMax)
{
Expand Down
15 changes: 13 additions & 2 deletions tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Akka.Util.Internal;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Json;
using Neo.Ledger;
Expand All @@ -23,6 +24,8 @@
using Neo.UnitTests.Extensions;
using System;
using System.Linq;
using System.Security.Policy;
using static Neo.SmartContract.Native.NeoToken;

namespace Neo.Plugins.RpcServer.Tests
{
Expand Down Expand Up @@ -188,12 +191,17 @@ public void TestGetRawTransaction()
var snapshot = _neoSystem.GetSnapshotCache();
var tx = TestUtils.CreateValidTx(snapshot, _wallet, _walletAccount);
_neoSystem.MemPool.TryAdd(tx, snapshot);
var parameters = new JArray(tx.Hash.ToString(), true);
snapshot.Commit();
var parameters = new JArray(tx.Hash.ToString(), true);
var result = _rpcServer.GetRawTransaction(parameters);

var json = Utility.TransactionToJson(tx, _neoSystem.Settings);
Assert.AreEqual(json.ToString(), result.ToString());

parameters = new JArray(tx.Hash.ToString(), false);
result = _rpcServer.GetRawTransaction(parameters);
var tx2 = Convert.FromBase64String(result.AsString()).AsSerializable<Transaction>();
tx2.ToJson(_neoSystem.Settings).ToString().Should().Be(tx.ToJson(_neoSystem.Settings).ToString());
}

[TestMethod]
Expand Down Expand Up @@ -279,11 +287,14 @@ public void TestGetNextBlockValidators()
public void TestGetCandidates()
{
var snapshot = _neoSystem.GetSnapshotCache();
var result = _rpcServer.GetCandidates(new JArray());
var json = new JArray();
var validators = NativeContract.NEO.GetNextBlockValidators(snapshot, _neoSystem.Settings.ValidatorsCount);

var key = new KeyBuilder(NativeContract.NEO.Id, 33).Add(ECPoint.Parse("02237309a0633ff930d51856db01d17c829a5b2e5cc2638e9c03b4cfa8e9c9f971", ECCurve.Secp256r1));
snapshot.Add(key, new StorageItem(new CandidateState() { Registered = true, Votes = 10000 }));
snapshot.Commit();
var candidates = NativeContract.NEO.GetCandidates(_neoSystem.GetSnapshotCache());
var result = _rpcServer.GetCandidates(new JArray());

foreach (var candidate in candidates)
{
Expand Down
14 changes: 13 additions & 1 deletion tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Akka.Actor;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.IO;
Expand All @@ -18,6 +19,8 @@
using Neo.SmartContract.Native;
using Neo.UnitTests;
using System;
using System.Collections.Generic;
using System.Net;

namespace Neo.Plugins.RpcServer.Tests
{
Expand All @@ -33,10 +36,19 @@ public void TestGetConnectionCount()
[TestMethod]
public void TestGetPeers()
{
var result = _rpcServer.GetPeers(new JArray());
var settings = TestProtocolSettings.SoleNode;
var neoSystem = new NeoSystem(settings, _memoryStoreProvider);
var localNode = neoSystem.LocalNode.Ask<LocalNode>(new LocalNode.GetInstance()).Result;
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 11332) });
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 12332) });
localNode.AddPeers(new List<IPEndPoint>() { new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 13332) });
var rpcServer = new RpcServer(neoSystem, RpcServerSettings.Default);

var result = rpcServer.GetPeers(new JArray());
Assert.IsInstanceOfType(result, typeof(JObject));
var json = (JObject)result;
json.ContainsProperty("unconnected").Should().BeTrue();
(json["unconnected"] as JArray).Count.Should().Be(3);
json.ContainsProperty("bad").Should().BeTrue();
json.ContainsProperty("connected").Should().BeTrue();
}
Expand Down

0 comments on commit ad494b3

Please sign in to comment.