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

Optimize Ping message #1903

Merged
merged 24 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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/neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,6 @@ private VerifyResult OnNewBlock(Block block)
return VerifyResult.Invalid;
block_cache.TryAdd(block.Hash, block);
block_cache_unverified.Remove(block.Index);
// We can store the new block in block_cache and tell the new height to other nodes before Persist().
system.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(Singleton.Height + 1)));
Persist(block);
SaveHeaderHashList();
if (block_cache_unverified.TryGetValue(Height + 1, out var unverifiedBlocks))
Expand All @@ -359,6 +357,8 @@ private VerifyResult OnNewBlock(Block block)
Self.Tell(unverifiedBlock, ActorRefs.NoSender);
Copy link
Member

Choose a reason for hiding this comment

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

This is not parallel, right?
In this sense, we were telling heights that were cached?

Copy link
Member Author

Choose a reason for hiding this comment

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

previously was before persists, now after it

block_cache_unverified.Remove(Height + 1);
}
// We can store the new block in block_cache and tell the new height to other nodes after Persist().
system.LocalNode.Tell(Message.Create(MessageCommand.Ping, PingPayload.Create(Singleton.Height)));
}
return VerifyResult.Succeed;
}
Expand Down
8 changes: 8 additions & 0 deletions src/neo/Network/P2P/RemoteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class Relay { public IInventory Inventory; }
public int ListenerTcpPort { get; private set; } = 0;
public VersionPayload Version { get; private set; }
public uint LastBlockIndex { get; private set; } = 0;
public uint LastHeightSent { get; private set; } = 0;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
public bool IsFullNode { get; private set; } = false;

public RemoteNode(NeoSystem system, object connection, IPEndPoint remote, IPEndPoint local)
Expand Down Expand Up @@ -124,6 +125,13 @@ protected override void OnReceive(object message)
RefreshPendingKnownHashes();
break;
case Message msg:
if (msg.Payload is PingPayload payload)
{
if (payload.LastBlockIndex > LastHeightSent)
LastHeightSent = payload.LastBlockIndex;
else if (msg.Command == MessageCommand.Ping)
vncoelho marked this conversation as resolved.
Show resolved Hide resolved
break;
}
EnqueueMessage(msg);
break;
case IInventory inventory:
Expand Down
14 changes: 6 additions & 8 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void OnBlock(Block block)
if (session is null) return;
session.IndexTasks.Remove(block.Index);
receivedBlockIndex.TryAdd(block.Index, session);
RequestTasks();
RequestTasks(false);
}

private void OnInvalidBlock(Block invalidBlock)
Expand Down Expand Up @@ -167,7 +167,7 @@ private void OnRegister(VersionPayload version)
if (session.IsFullNode)
session.InvTasks.TryAdd(MemPoolTaskHash, TimeProvider.Current.UtcNow);
sessions.TryAdd(Sender, session);
RequestTasks();
RequestTasks(true);
}

private void OnUpdate(Update update)
Expand All @@ -176,7 +176,7 @@ private void OnUpdate(Update update)
return;
session.LastBlockIndex = update.LastBlockIndex;
session.ExpireTime = TimeProvider.Current.UtcNow.AddMilliseconds(PingCoolingOffPeriod);
if (update.RequestTasks) RequestTasks();
if (update.RequestTasks) RequestTasks(true);
}

private void OnRestartTasks(InvPayload payload)
Expand Down Expand Up @@ -258,7 +258,7 @@ private void OnTimer()
}
}
}
RequestTasks();
RequestTasks(true);
}

protected override void PostStop()
Expand All @@ -272,11 +272,11 @@ public static Props Props(NeoSystem system)
return Akka.Actor.Props.Create(() => new TaskManager(system)).WithMailbox("task-manager-mailbox");
}

private void RequestTasks()
private void RequestTasks(bool sendPing)
{
if (sessions.Count() == 0) return;

SendPingMessage();
if (sendPing) SendPingMessage();

while (failedSyncTasks.Count() > 0)
{
Expand All @@ -300,8 +300,6 @@ private void RequestTasks()

private void SendPingMessage()
{
if (sessions.Count == 0) return;

TrimmedBlock block;
using (SnapshotView snapshot = Blockchain.Singleton.GetSnapshot())
{
Expand Down