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

[neo3] Add Ping expiration #1829

Merged
merged 6 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 7 additions & 6 deletions src/neo/Network/P2P/RemoteNode.ProtocolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private void OnInventoryReceived(IInventory inventory)
system.Blockchain.Tell(inventory, ActorRefs.NoSender);
pendingKnownHashes.Remove(inventory.Hash);
knownHashes.Add(inventory.Hash);
if (inventory is Block b) UpdateLastBlockIndex(b.Index, false);
}

private void OnInvMessageReceived(InvPayload payload)
Expand Down Expand Up @@ -323,13 +324,13 @@ private void OnMemPoolMessageReceived()

private void OnPingMessageReceived(PingPayload payload)
{
UpdateLastBlockIndex(payload);
UpdateLastBlockIndex(payload.LastBlockIndex, true);
EnqueueMessage(Message.Create(MessageCommand.Pong, PingPayload.Create(Blockchain.Singleton.Height, payload.Nonce)));
}

private void OnPongMessageReceived(PingPayload payload)
{
UpdateLastBlockIndex(payload);
UpdateLastBlockIndex(payload.LastBlockIndex, true);
}

private void OnVerackMessageReceived()
Expand Down Expand Up @@ -375,12 +376,12 @@ private void RefreshPendingKnownHashes()
}
}

private void UpdateLastBlockIndex(PingPayload payload)
private void UpdateLastBlockIndex(uint lastBlockIndex, bool requestTasks)
{
if (payload.LastBlockIndex > LastBlockIndex)
if (lastBlockIndex > LastBlockIndex)
{
LastBlockIndex = payload.LastBlockIndex;
system.TaskManager.Tell(new TaskManager.Update { LastBlockIndex = LastBlockIndex });
LastBlockIndex = lastBlockIndex;
system.TaskManager.Tell(new TaskManager.Update { LastBlockIndex = LastBlockIndex, RequestTasks = requestTasks });
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Neo.Network.P2P
internal class TaskManager : UntypedActor
{
public class Register { public VersionPayload Version; }
public class Update { public uint LastBlockIndex; }
public class Update { public uint LastBlockIndex; public bool RequestTasks; }
public class NewTasks { public InvPayload Payload; }
public class RestartTasks { public InvPayload Payload; }
private class Timer { }
Expand Down Expand Up @@ -129,7 +129,7 @@ protected override void OnReceive(object message)
OnRegister(register.Version);
break;
case Update update:
OnUpdate(update.LastBlockIndex);
OnUpdate(update);
break;
case NewTasks tasks:
OnNewTasks(tasks.Payload);
Expand Down Expand Up @@ -169,12 +169,13 @@ private void OnRegister(VersionPayload version)
RequestTasks();
}

private void OnUpdate(uint lastBlockIndex)
private void OnUpdate(Update update)
{
if (!sessions.TryGetValue(Sender, out TaskSession session))
return;
session.LastBlockIndex = lastBlockIndex;
RequestTasks();
session.LastBlockIndex = update.LastBlockIndex;
session.ExpireTime = TimeProvider.Current.UtcNow.AddMilliseconds(PingCoolingOffPeriod);
if (update.RequestTasks) RequestTasks();
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnRestartTasks(InvPayload payload)
Expand Down