Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed May 30, 2022
1 parent 7b961df commit 399f5ac
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 405 deletions.
27 changes: 7 additions & 20 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,9 @@ public bool IsKnownBlock(long number, Keccak blockHash)
if (level is null)
return false;
int? index = FindIndex(blockHash, level);
return index.HasValue && level.HasNonBeaconBlocks;
return index.HasValue &&
!level.BlockInfos[index.Value].IsBeaconHeader &&
!level.BlockInfos[index.Value].IsBeaconHeader;
}

public bool IsKnownBeaconBlock(long number, Keccak blockHash)
Expand All @@ -1653,25 +1655,10 @@ public bool IsKnownBeaconBlock(long number, Keccak blockHash)
if (level is null)
return false;
int? index = FindIndex(blockHash, level);
return index.HasValue && level.HasBeaconBlocks;
return index.HasValue &&
(level.BlockInfos[index.Value].IsBeaconHeader || level.BlockInfos[index.Value].IsBeaconBody);
}

public bool IsKnownBeaconBlock(long number, Keccak blockHash)
{
if (number > BestKnownBeaconNumber)
{
return false;
}

if (_headerCache.Get(blockHash) is not null)
{
return true;
}

ChainLevelInfo level = LoadLevel(number);
return level is not null && FindIndex(blockHash, level).HasValue;
}


private void UpdateDeletePointer(Keccak? hash)
{
if (hash is null)
Expand Down Expand Up @@ -2030,7 +2017,7 @@ void SetTotalDifficultyDeep(BlockHeader current)
else
{
UInt256 parentTotalDifficulty;
if (header.Number - 1 == _syncConfig.PivotNumberParsed)
if (_syncConfig.FastSync && header.Number - 1 == _syncConfig.PivotNumberParsed)
{
parentTotalDifficulty = _syncConfig.PivotTotalDifficultyParsed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public BlockValidator(
_headerValidator = headerValidator ?? throw new ArgumentNullException(nameof(headerValidator));
}

public bool ValidateHash(BlockHeader header)
{
return _headerValidator.ValidateHash(header);
}

public bool Validate(BlockHeader header, BlockHeader? parent, bool isUncle)
{
return _headerValidator.Validate(header, parent, isUncle);
Expand Down
32 changes: 32 additions & 0 deletions src/Nethermind/Nethermind.Core/BlockInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ public bool IsFinalized
}
}
}

public bool IsBeaconHeader
{
get => (Metadata & BlockMetadata.BeaconHeader) != 0;
set
{
if (value)
{
Metadata |= BlockMetadata.BeaconHeader;
}
else
{
Metadata &= ~BlockMetadata.BeaconHeader;
}
}
}

public bool IsBeaconBody
{
get => (Metadata & BlockMetadata.BeaconBody) == BlockMetadata.BeaconBody;
set
{
if (value)
{
Metadata |= BlockMetadata.BeaconBody;
}
else
{
Metadata &= ~BlockMetadata.BeaconBody;
}
}
}

public BlockMetadata Metadata { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Can_sync_using_chain_levels()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.SuggestBlocksUsingChainLevels()
.AssertBestKnownNumber(9)
.AssertBestSuggestedHeader(9)
Expand All @@ -42,7 +42,7 @@ public void Can_sync_using_chain_levels_with_restart()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.Restart()
.SuggestBlocksUsingChainLevels()
.AssertBestKnownNumber(9)
Expand All @@ -57,7 +57,7 @@ public void Correct_levels_after_chain_level_sync()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.SuggestBlocksUsingChainLevels()
.AssertBestKnownNumber(9)
.AssertBestSuggestedHeader(9)
Expand All @@ -72,7 +72,7 @@ public void Correct_levels_after_chain_level_sync_with_nullable_td()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Null)
.InsertBeaconBlocks(7, 9, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Null)
.InsertBeaconBlocks(8, 9, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Null)
.SuggestBlocksUsingChainLevels()
.AssertBestKnownNumber(9)
.AssertBestSuggestedHeader(9)
Expand All @@ -87,7 +87,7 @@ public void Correct_levels_after_chain_level_sync_with_zero_td()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Zero)
.InsertBeaconBlocks(7, 9, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Zero)
.InsertBeaconBlocks(8, 9, BlockTreeTestScenario.ScenarioBuilder.TotalDifficultyMode.Zero)
.SuggestBlocksUsingChainLevels()
.AssertBestKnownNumber(9)
.AssertBestSuggestedHeader(9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Should_set_correct_metadata()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(4, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.AssertMetadata(0, 4, BlockMetadata.None)
.AssertMetadata(5, 6, BlockMetadata.BeaconHeader | BlockMetadata.BeaconMainChain)
.AssertMetadata(7, 9, BlockMetadata.BeaconBody | BlockMetadata.BeaconHeader | BlockMetadata.BeaconMainChain);
Expand All @@ -41,8 +41,8 @@ public void Should_set_correct_metadata_after_suggest_blocks_using_chain_levels(
BlockTreeTestScenario.GoesLikeThis()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(3, 6)
.InsertBeaconBlocks(7, 9)
.InsertHeaders(4, 6)
.InsertBeaconBlocks(8, 9)
.SuggestBlocksUsingChainLevels()
.AssertMetadata(0, 9, BlockMetadata.None);
}
Expand All @@ -54,7 +54,7 @@ public void Should_fill_beacon_block_metadata_when_not_moved_to_main_chain()
.WithBlockTrees(4, 10, false)
.InsertBeaconPivot(7)
.InsertHeaders(3, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.SuggestBlocksUsingChainLevels()
.AssertMetadata(0, 3, BlockMetadata.None)
.AssertMetadata(4, 6, BlockMetadata.None)
Expand Down
13 changes: 7 additions & 6 deletions src/Nethermind/Nethermind.Merge.Plugin.Test/BlockTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public partial class BlockTreeTests
new SyncConfig(),
LimboLogs.Instance);

BlockTreeBuilder syncedTreeBuilder = Build.A.BlockTree().OfChainLength(notSyncedTreeSize);
BlockTree syncedTree = new(
treeBuilder.BlocksDb,
treeBuilder.HeadersDb,
treeBuilder.BlockInfoDb,
treeBuilder.MetadataDb,
treeBuilder.ChainLevelInfoRepository,
syncedTreeBuilder.BlocksDb,
syncedTreeBuilder.HeadersDb,
syncedTreeBuilder.BlockInfoDb,
syncedTreeBuilder.MetadataDb,
syncedTreeBuilder.ChainLevelInfoRepository,
MainnetSpecProvider.Instance,
NullBloomStorage.Instance,
new SyncConfig(),
Expand Down Expand Up @@ -488,7 +489,7 @@ public void Best_pointers_should_not_move_if_sync_is_not_finished()
.WithBlockTrees(4, 10)
.InsertBeaconPivot(7)
.InsertHeaders(5, 6)
.InsertBeaconBlocks(7, 9)
.InsertBeaconBlocks(8, 9)
.Restart()
.AssertBestBeaconBody(9)
.AssertBestBeaconHeader(9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public async Task Finishes_when_all_downloaded()
HeadersSyncBatch? result = await feed.PrepareRequest();
result.Should().BeNull();
feed.CurrentState.Should().Be(SyncFeedState.Dormant);
measuredProgress.CurrentValue.Should().Be(2000);
measuredProgress.CurrentValue.Should().Be(1000);
}

[Test]
Expand Down Expand Up @@ -196,8 +196,7 @@ public async Task Feed_able_to_sync_when_new_pivot_is_set()
blockTree.Insert(highestBlock, true);

pivot.EnsurePivot(syncedBlockTree.FindHeader(999, BlockTreeLookupOptions.None));
// TODO: beaconsync lowest inserted beacon header should be known header + 1
BuildAndProcessHeaderSyncBatches(ctx, blockTree, syncedBlockTree, pivot, 700, 501);
BuildAndProcessHeaderSyncBatches(ctx, blockTree, syncedBlockTree, pivot, 700, 701);
}

private async void BuildAndProcessHeaderSyncBatches(
Expand Down Expand Up @@ -232,7 +231,7 @@ private async void BuildAndProcessHeaderSyncBatches(

HeadersSyncBatch result = await ctx.Feed.PrepareRequest();
result.Should().BeNull();
blockTree.LowestInsertedBeaconHeader.Should().BeEquivalentTo(syncedBlockTree.FindHeader(endLowestBeaconHeader, BlockTreeLookupOptions.None));
blockTree.LowestInsertedBeaconHeader?.Hash.Should().BeEquivalentTo(syncedBlockTree.FindHeader(endLowestBeaconHeader, BlockTreeLookupOptions.None)?.Hash);
blockTree.BestKnownNumber.Should().Be(bestPointer);
blockTree.BestSuggestedHeader.Should().BeEquivalentTo(startBestHeader);
ctx.Feed.CurrentState.Should().Be(SyncFeedState.Dormant);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ protected override void PostFinishCleanUp()

protected override AddBlockResult InsertToBlockTree(BlockHeader header)
{
if (_chainMerged)
{
if (_logger.IsTrace)
_logger.Trace(
"Chain already merged, skipping header insert");
return AddBlockResult.AlreadyKnown;
}

if (_logger.IsTrace)
_logger.Trace(
$"Adding new header in beacon headers sync {header.ToString(BlockHeader.Format.FullHashAndNumber)}");
Expand All @@ -123,13 +131,16 @@ protected override AddBlockResult InsertToBlockTree(BlockHeader header)
{
options |= BlockTreeInsertOptions.TotalDifficultyNotNeeded;
}

// Found existing block in the block tree
if (_blockTree.IsKnownBlock(header.Number, header.Hash))
{
if (_blockTree.LowestInsertedBeaconHeader?.ParentHash == header.Hash || _chainMerged)
if (_blockTree.LowestInsertedBeaconHeader?.ParentHash == header.Hash)
{
_chainMerged = true;
if (_logger.IsTrace)
_logger.Trace(
$"Found header to join dangling beacon chain {header.ToString(BlockHeader.Format.FullHashAndNumber)}");
return AddBlockResult.AlreadyKnown;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Crypto;
using Nethermind.Db;
using Nethermind.Logging;
Expand Down Expand Up @@ -85,10 +86,14 @@ public bool ShouldBeInBeaconHeaders()

public bool IsBeaconSyncHeadersFinished()
{
long lowestInsertedBeaconHeader = _blockTree.LowestInsertedBeaconHeader?.Number ?? 0;
bool finished = _blockTree.LowestInsertedBeaconHeader == null
|| lowestInsertedBeaconHeader <= _beaconPivot.PivotDestinationNumber;
// || lowestedBeaconHeaderNumber <= (_blockTree.BestSuggestedHeader?.Number ?? long.MaxValue); ToDo Sarah
BlockHeader? lowestInsertedBeaconHeader = _blockTree.LowestInsertedBeaconHeader;
bool chainMerged =
((lowestInsertedBeaconHeader?.Number ?? 0) - 1) <= (_blockTree.BestSuggestedHeader?.Number ?? long.MaxValue) &&
lowestInsertedBeaconHeader != null &&
_blockTree.IsKnownBlock(lowestInsertedBeaconHeader.Number - 1, lowestInsertedBeaconHeader.ParentHash);
bool finished = lowestInsertedBeaconHeader == null
|| lowestInsertedBeaconHeader.Number <= _syncConfig.PivotNumberParsed + 1
|| chainMerged;

if (_logger.IsTrace) _logger.Trace($"IsBeaconSyncHeadersFinished: {finished}, BeaconPivotExists: {_beaconPivot.BeaconPivotExists()}, LowestInsertedBeaconHeaderNumber: {_blockTree.LowestInsertedBeaconHeader?.Number}, BeaconPivot: {_beaconPivot.PivotNumber}, BeaconPivotDestinationNumber: {_beaconPivot.PivotDestinationNumber}");
return finished;
Expand Down
Loading

0 comments on commit 399f5ac

Please sign in to comment.