Skip to content

Commit

Permalink
Optimize SendPingMessage (#1896)
Browse files Browse the repository at this point in the history
* Optimize SendPing

* Optimize

* Update src/neo/Network/P2P/TaskManager.cs

Co-authored-by: Shargon <shargon@gmail.com>

Co-authored-by: erikzhang <erik@neo.org>
  • Loading branch information
shargon and erikzhang authored Sep 2, 2020
1 parent 9322675 commit 234772c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Neo.IO.Caching;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -299,14 +300,22 @@ private void RequestTasks()

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

TrimmedBlock block;
using (SnapshotView snapshot = Blockchain.Singleton.GetSnapshot())
{
block = snapshot.Blocks[snapshot.CurrentBlockHash];
}

foreach (KeyValuePair<IActorRef, TaskSession> item in sessions)
{
var node = item.Key;
var session = item.Value;

if (session.ExpireTime < TimeProvider.Current.UtcNow ||
(Blockchain.Singleton.Height >= session.LastBlockIndex
&& TimeProvider.Current.UtcNow.ToTimestampMS() - PingCoolingOffPeriod >= Blockchain.Singleton.GetBlock(Blockchain.Singleton.CurrentBlockHash)?.Timestamp))
(block.Index >= session.LastBlockIndex &&
TimeProvider.Current.UtcNow.ToTimestampMS() - PingCoolingOffPeriod >= block.Timestamp))
{
if (session.InvTasks.Remove(MemPoolTaskHash))
{
Expand Down

0 comments on commit 234772c

Please sign in to comment.