Skip to content

Commit

Permalink
Optimize Ping message (neo-project#1903)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored and cloud8little committed Jan 24, 2021
1 parent e7f44fc commit c84d00c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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);
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;
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)
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

0 comments on commit c84d00c

Please sign in to comment.