Skip to content

Commit

Permalink
Move some logic from blocktree into chain level info (#5705)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored May 17, 2023
1 parent 0169378 commit 742948e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
63 changes: 9 additions & 54 deletions src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ public bool WasProcessed(long number, Keccak blockHash)
throw new InvalidOperationException($"Not able to find block {blockHash} from an unknown level {number}");
}

int? index = FindIndex(blockHash, levelInfo);
int? index = levelInfo.FindIndex(blockHash);
if (index is null)
{
throw new InvalidOperationException($"Not able to find block {blockHash} index on the chain level");
Expand Down Expand Up @@ -1277,7 +1277,7 @@ public void MarkChainAsProcessed(IReadOnlyList<Block> blocks)
}

ChainLevelInfo? level = LoadLevel(block.Number);
int? index = level is null ? null : FindIndex(block.Hash, level);
int? index = level?.FindIndex(block.Hash);
if (index is null)
{
throw new InvalidOperationException($"Cannot mark unknown block {block.ToString(Block.Format.FullHashAndNumber)} as processed");
Expand Down Expand Up @@ -1454,11 +1454,7 @@ private void MoveToMain(Block block, BatchWrite batch, bool wasProcessed, bool f
}

ChainLevelInfo? level = LoadLevel(block.Number);
if (level.BlockInfos.Length > 1)
{
}

int? index = level is null ? null : FindIndex(block.Hash, level);
int? index = level?.FindIndex(block.Hash);
if (index is null)
{
throw new InvalidOperationException($"Cannot move unknown block {block.ToString(Block.Format.FullHashAndNumber)} to main");
Expand All @@ -1471,8 +1467,7 @@ private void MoveToMain(Block block, BatchWrite batch, bool wasProcessed, bool f
info.WasProcessed = wasProcessed;
if (index.Value != 0)
{
(level.BlockInfos[index.Value], level.BlockInfos[0]) =
(level.BlockInfos[0], level.BlockInfos[index.Value]);
level.SwapToMain(index.Value);
}

_bloomStorage.Store(block.Number, block.Bloom);
Expand Down Expand Up @@ -1581,7 +1576,7 @@ private void SetHeadBlock(Keccak headHash)
}

ChainLevelInfo? level = LoadLevel(headBlock.Number);
int? index = level is null ? null : FindIndex(headHash, level);
int? index = level?.FindIndex(headHash);
if (!index.HasValue)
{
throw new InvalidDataException("Head block data missing from chain info");
Expand Down Expand Up @@ -1673,41 +1668,16 @@ private ChainLevelInfo UpdateOrCreateLevel(long number, Keccak hash, BlockInfo b
{
using (BatchWrite? batch = _chainLevelInfoRepository.StartBatch())
{
ChainLevelInfo level = LoadLevel(number, false);

if (!blockInfo.IsBeaconInfo && number > BestKnownNumber)
{
BestKnownNumber = number;
}

ChainLevelInfo level = LoadLevel(number, false);

if (level is not null)
{
BlockInfo[] blockInfos = level.BlockInfos;

int? foundIndex = FindIndex(hash, level);
if (!foundIndex.HasValue)
{
Array.Resize(ref blockInfos, blockInfos.Length + 1);
}
else
{
if (blockInfo.IsBeaconInfo && blockInfos[foundIndex.Value].IsBeaconMainChain)
blockInfo.Metadata |= BlockMetadata.BeaconMainChain;
}

int index = foundIndex ?? blockInfos.Length - 1;

if (setAsMain)
{
blockInfos[index] = blockInfos[0];
blockInfos[0] = blockInfo;
}
else
{
blockInfos[index] = blockInfo;
}

level.BlockInfos = blockInfos;
level.InsertBlockInfo(hash, blockInfo, setAsMain);
}
else
{
Expand Down Expand Up @@ -1735,22 +1705,7 @@ private ChainLevelInfo UpdateOrCreateLevel(long number, Keccak hash, BlockInfo b
return (null, null);
}

int? index = FindIndex(blockHash, chainLevelInfo);
return index.HasValue ? (chainLevelInfo.BlockInfos[index.Value], chainLevelInfo) : (null, chainLevelInfo);
}

private static int? FindIndex(Keccak blockHash, ChainLevelInfo level)
{
for (int i = 0; i < level.BlockInfos.Length; i++)
{
Keccak hashAtIndex = level.BlockInfos[i].BlockHash;
if (hashAtIndex.Equals(blockHash))
{
return i;
}
}

return null;
return (chainLevelInfo.FindBlockInfo(blockHash), chainLevelInfo);
}

private ChainLevelInfo? LoadLevel(long number, bool forceLoad = true)
Expand Down
56 changes: 56 additions & 0 deletions src/Nethermind/Nethermind.Core/ChainLevelInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Diagnostics;
using System.Linq;
using Nethermind.Core.Crypto;
Expand Down Expand Up @@ -56,5 +57,60 @@ public BlockInfo? BeaconMainChainBlock

return null;
}

public int? FindIndex(Keccak blockHash)
{
for (int i = 0; i < BlockInfos.Length; i++)
{
Keccak hashAtIndex = BlockInfos[i].BlockHash;
if (hashAtIndex.Equals(blockHash))
{
return i;
}
}

return null;
}

public BlockInfo? FindBlockInfo(Keccak blockHash)
{
int? index = FindIndex(blockHash);
return index.HasValue ? BlockInfos[index.Value] : null;
}

public void InsertBlockInfo(Keccak hash, BlockInfo blockInfo, bool setAsMain)
{
BlockInfo[] blockInfos = BlockInfos;

int? foundIndex = FindIndex(hash);
if (!foundIndex.HasValue)
{
Array.Resize(ref blockInfos, blockInfos.Length + 1);
}
else
{
if (blockInfo.IsBeaconInfo && blockInfos[foundIndex.Value].IsBeaconMainChain)
blockInfo.Metadata |= BlockMetadata.BeaconMainChain;
}

int index = foundIndex ?? blockInfos.Length - 1;

if (setAsMain)
{
blockInfos[index] = blockInfos[0];
blockInfos[0] = blockInfo;
}
else
{
blockInfos[index] = blockInfo;
}

BlockInfos = blockInfos;
}

public void SwapToMain(int index)
{
(BlockInfos[index], BlockInfos[0]) = (BlockInfos[0], BlockInfos[index]);
}
}
}

0 comments on commit 742948e

Please sign in to comment.