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

Allow take snapshot in LocalNode #2311

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
25 changes: 11 additions & 14 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Neo.Network.P2P
{
public class LocalNode : Peer
{
public class GetSnapshot { }
public class Snapshot { public IReadOnlyDictionary<IActorRef, RemoteNode> RemoteNodes { get; init; } public IReadOnlyCollection<IPEndPoint> UnconnectedPeers { get; init; } }
public class RelayDirectly { public IInventory Inventory; }
public class SendDirectly { public IInventory Inventory; }

Expand All @@ -24,8 +26,6 @@ public class SendDirectly { public IInventory Inventory; }
private readonly NeoSystem system;
internal readonly ConcurrentDictionary<IActorRef, RemoteNode> RemoteNodes = new ConcurrentDictionary<IActorRef, RemoteNode>();

public int ConnectedCount => RemoteNodes.Count;
public int UnconnectedCount => UnconnectedPeers.Count;
public static readonly uint Nonce;
public static string UserAgent { get; set; }

Expand Down Expand Up @@ -116,7 +116,7 @@ internal static IPEndPoint GetIpEndPoint(string hostAndPort)
/// </summary>
/// <param name="actor">Remote node actor</param>
/// <param name="node">Remote node</param>
public bool AllowNewConnection(IActorRef actor, RemoteNode node)
internal bool AllowNewConnection(IActorRef actor, RemoteNode node)
{
if (node.Version.Magic != system.Settings.Magic) return false;
if (node.Version.Nonce == Nonce) return false;
Expand All @@ -132,16 +132,6 @@ public bool AllowNewConnection(IActorRef actor, RemoteNode node)
return true;
}

public IEnumerable<RemoteNode> GetRemoteNodes()
{
return RemoteNodes.Values;
}

public IEnumerable<IPEndPoint> GetUnconnectedPeers()
{
return UnconnectedPeers;
}

/// <summary>
/// Override of abstract class that is triggered when <see cref="UnconnectedPeers"/> is empty.
/// Performs a BroadcastMessage with the command `MessageCommand.GetAddr`, which, eventually, tells all known connections.
Expand All @@ -151,7 +141,7 @@ public IEnumerable<IPEndPoint> GetUnconnectedPeers()
protected override void NeedMorePeers(int count)
{
count = Math.Max(count, MaxCountFromSeedList);
if (ConnectedPeers.Count > 0)
if (!ConnectedPeers.IsEmpty)
{
BroadcastMessage(MessageCommand.GetAddr);
}
Expand All @@ -170,6 +160,13 @@ protected override void OnReceive(object message)
base.OnReceive(message);
switch (message)
{
case GetSnapshot _:
Sender.Tell(new Snapshot
{
RemoteNodes = RemoteNodes,
UnconnectedPeers = UnconnectedPeers
});
break;
case Message msg:
BroadcastMessage(msg);
break;
Expand Down