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

Move EnsureCanonical off main loop tx processing #7471

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 12 additions & 1 deletion src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ private void MoveToMain(Block block, BatchWrite batch, bool wasProcessed, bool f

if (_logger.IsTrace) _logger.Trace($"Block added to main {block}, block TD {block.TotalDifficulty}");

BlockAddedToMain?.Invoke(this, new BlockReplacementEventArgs(block, previous));
BlockAddedToMain?.Invoke(this, new BlockReplacementEventArgs(block, previous, IsSyncing()));

if (_logger.IsTrace) _logger.Trace($"Block {block.ToString(Block.Format.Short)}, TD: {block.TotalDifficulty} added to main chain");
}
Expand Down Expand Up @@ -1610,5 +1610,16 @@ public void ForkChoiceUpdated(Hash256? finalizedBlockHash, Hash256? safeBlockHas
_metadataDb.Set(MetadataDbKeys.SafeBlockHash, Rlp.Encode(SafeHash!).Bytes);
}
}

public bool IsSyncing()
{
long bestSuggestedNumber = FindBestSuggestedHeader()?.Number ?? 0;
if (bestSuggestedNumber == 0)
{
return true;
}
long headNumberOrZero = Head?.Number ?? 0;
return bestSuggestedNumber > headNumberOrZero;
}
}
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/BlockTreeOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,6 @@ public bool IsMainChain(Hash256 blockHash, bool throwOnMissingHash = true) =>

public BlockHeader FindBestSuggestedHeader() =>
_overlayTree.FindBestSuggestedHeader() ?? _baseTree.FindBestSuggestedHeader();

public bool IsSyncing() => _baseTree.IsSyncing() || _overlayTree.IsSyncing();
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Blockchain/IBlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,7 @@ AddBlockResult Insert(Block block, BlockTreeInsertBlockOptions insertBlockOption
void UpdateBeaconMainChain(BlockInfo[]? blockInfos, long clearBeaconMainChainStartPoint);

void RecalculateTreeLevels();

bool IsSyncing();
}
}
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Blockchain/ReadOnlyBlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,6 @@ public int DeleteChainSlice(in long startNumber, long? endNumber = null, bool fo
public void UpdateMainChain(IReadOnlyList<Block> blocks, bool wereProcessed, bool forceHeadBlock = false) => throw new InvalidOperationException($"{nameof(ReadOnlyBlockTree)} does not expect {nameof(UpdateMainChain)} calls");

public void ForkChoiceUpdated(Hash256? finalizedBlockHash, Hash256? safeBlockBlockHash) => throw new InvalidOperationException($"{nameof(ReadOnlyBlockTree)} does not expect {nameof(ForkChoiceUpdated)} calls");
public bool IsSyncing() => _wrapped.IsSyncing();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,24 @@ private void BlockTreeOnBlockAddedToMain(object? sender, BlockReplacementEventAr
{
RemoveBlockTx(e.PreviousBlock, e.Block);
}
EnsureCanonical(e.Block);
ReceiptsInserted?.Invoke(this, e);

// Dont block main loop
bool isSyncing = e.IsSyncing;
if (!isSyncing)
{
// Don't delay Canonical receipts if at head
EnsureCanonical(e.Block);
ReceiptsInserted?.Invoke(this, e);
}

// Don't block main loop
Task.Run(() =>
{
if (isSyncing)
{
// If syncing canonical receipts can lag
EnsureCanonical(e.Block);
ReceiptsInserted?.Invoke(this, e);
}

Block newMain = e.Block;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,10 @@ public CensorshipDetector(
_blockProcessor.BlockProcessing += OnBlockProcessing;
}

private bool IsSyncing()
{
long bestSuggestedNumber = _blockTree.FindBestSuggestedHeader()?.Number ?? 0;
if (bestSuggestedNumber == 0)
{
return true;
}
long headNumberOrZero = _blockTree.Head?.Number ?? 0;
return bestSuggestedNumber > headNumberOrZero;
}

private void OnBlockProcessing(object? sender, BlockEventArgs e)
{
// skip censorship detection if node is not synced yet
if (IsSyncing()) return;
if (_blockTree.IsSyncing()) return;

bool tracksPerAddressCensorship = _bestTxPerObservedAddresses is not null;
if (tracksPerAddressCensorship)
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Core/BlockReplacementEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace Nethermind.Core
{
public class BlockReplacementEventArgs(Block block, Block? previousBlock = null) : BlockEventArgs(block)
public class BlockReplacementEventArgs(Block block, Block? previousBlock = null, bool isSyncing = false) : BlockEventArgs(block)
{
public Block? PreviousBlock { get; } = previousBlock;
public bool IsSyncing { get; } = isSyncing;
}
}