Skip to content

Commit

Permalink
Don't refresh sync peer when its head block is known (#3916)
Browse files Browse the repository at this point in the history
* Don't refresh sync peer when its head block is known

* refactor

* Always refresh total difficulty

* small fix

* revert

* PassBlockHint in hive

* fix

* fix

* suggest block

* Next attempt

* fix

* Revert "fix"

This reverts commit 6269d06.

* Revert "Next attempt"

This reverts commit 4ac1046.

* suggest block if body is empty, header if not

* fix?

* refactor & cleaning

* rename method for more accurate

* fix?

* do not process suggested block

* send whole block instead of hash

* do not notify twice

* Move logic to hive plugin

Co-authored-by: Marcin Sobczak <marcindsobczak@gmail.com>
  • Loading branch information
LukaszRozmej and marcindsobczak authored Apr 1, 2022
1 parent 1ad915a commit b3ce2cd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 12 deletions.
21 changes: 20 additions & 1 deletion src/Nethermind/Nethermind.Hive/HivePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
using Nethermind.Blockchain.Processing;
using Nethermind.Blockchain.Rewards;
using Nethermind.Blockchain.Tracing;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.Synchronization.Peers;

namespace Nethermind.Hive
{
Expand Down Expand Up @@ -43,7 +46,23 @@ public Task Init(INethermindApi api)
return Task.CompletedTask;
}

public Task InitNetworkProtocol() => Task.CompletedTask;
public Task InitNetworkProtocol()
{
if (_api.SyncPeerPool == null) throw new ArgumentNullException(nameof(_api.SyncPeerPool));

_api.SyncPeerPool.PeerRefreshed += OnPeerRefreshed;
return Task.CompletedTask;
}

private void OnPeerRefreshed(object? sender, PeerHeadRefreshedEventArgs e)
{
BlockHeader header = e.Header;
if (header.UnclesHash == Keccak.OfAnEmptySequenceRlp && header.TxRoot == Keccak.EmptyTreeHash)
{
Block block = new(header, new BlockBody());
_api.BlockTree!.SuggestBlock(block);
}
}

public async Task InitRpcModules()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public PeerInfo GetPeer(Node node)
}

public event EventHandler<PeerBlockNotificationEventArgs> NotifyPeerBlock;
public event EventHandler<PeerHeadRefreshedEventArgs> PeerRefreshed;
}

private class TestBatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,17 @@ public void Update()
Snapshot best = TakeSnapshot(peerDifficulty.Value, peerBlock.Value);
if (!FastSyncEnabled)
{

bool anyPeers = peerBlock.Value > 0 &&
peerDifficulty.Value >= _syncProgressResolver.ChainDifficulty;
newModes = anyPeers ? SyncMode.Full : SyncMode.Disconnected;
reason = "No Useful Peers";
if (anyPeers)
{
newModes = SyncMode.Full;
}
else
{
newModes = SyncMode.Disconnected;
reason = "No Useful Peers";
}
}
else
{
Expand Down Expand Up @@ -183,7 +189,6 @@ public void Update()
}
}


UpdateSyncModes(newModes, reason);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ public interface ISyncPeerPool : IDisposable
PeerInfo? GetPeer(Node node);

event EventHandler<PeerBlockNotificationEventArgs> NotifyPeerBlock;

event EventHandler<PeerHeadRefreshedEventArgs> PeerRefreshed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;

namespace Nethermind.Synchronization.Peers;

public class PeerHeadRefreshedEventArgs : EventArgs
{
public ISyncPeer SyncPeer { get; }
public BlockHeader Header { get; }

public PeerHeadRefreshedEventArgs(ISyncPeer syncPeer, BlockHeader blockHeader)
{
SyncPeer = syncPeer;
Header = blockHeader;
}
}
24 changes: 17 additions & 7 deletions src/Nethermind/Nethermind.Synchronization/Peers/SyncPeerPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public async Task StopAsync()

public PeerInfo? GetPeer(Node node) => _peers.TryGetValue(node.Id, out PeerInfo? peerInfo) ? peerInfo : null;
public event EventHandler<PeerBlockNotificationEventArgs>? NotifyPeerBlock;
public event EventHandler<PeerHeadRefreshedEventArgs>? PeerRefreshed;

public void WakeUpAll()
{
Expand Down Expand Up @@ -252,10 +252,18 @@ public void AddPeer(ISyncPeer syncPeer)
PeerInfo peerInfo = new(syncPeer);
_peers.TryAdd(syncPeer.Node.Id, peerInfo);
Metrics.SyncPeers = _peers.Count;

if (_logger.IsDebug) _logger.Debug($"Adding {syncPeer.Node:c} to refresh queue");
if (NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportInterestingEvent(syncPeer.Node.Address, "adding node to refresh queue");
_peerRefreshQueue.Add(new RefreshTotalDiffTask(syncPeer));

BlockHeader? header = _blockTree.FindHeader(syncPeer.HeadHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
if (header is not null)
{
syncPeer.HeadNumber = header.Number;
}
else
{
if (_logger.IsDebug) _logger.Debug($"Adding {syncPeer.Node:c} to refresh queue");
if (NetworkDiagTracer.IsEnabled) NetworkDiagTracer.ReportInterestingEvent(syncPeer.Node.Address, "adding node to refresh queue");
_peerRefreshQueue.Add(new RefreshTotalDiffTask(syncPeer));
}
}

public void RemovePeer(ISyncPeer syncPeer)
Expand Down Expand Up @@ -583,13 +591,15 @@ await firstToComplete.ContinueWith(
{
syncPeer.TotalDifficulty = newTotalDifficulty;
syncPeer.HeadNumber = header.Number;
syncPeer.HeadHash = header.Hash;
syncPeer.HeadHash = header.Hash!;
PeerRefreshed?.Invoke(this, new PeerHeadRefreshedEventArgs(syncPeer, header));
}
}
else if (header.Number > syncPeer.HeadNumber)
{
syncPeer.HeadNumber = header.Number;
syncPeer.HeadHash = header.Hash;
syncPeer.HeadHash = header.Hash!;
}
syncPeer.IsInitialized = true;
Expand Down

0 comments on commit b3ce2cd

Please sign in to comment.