-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into pectra_devnet_4
- Loading branch information
Showing
77 changed files
with
2,241 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Nethermind/Nethermind.Blockchain.Test/Blocks/BadBlockStoreTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Nethermind.Blockchain.Blocks; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Test; | ||
using Nethermind.Core.Test.Builders; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Blockchain.Test.Blocks; | ||
|
||
public class BadBlockStoreTests | ||
{ | ||
[Test] | ||
public void Test_CanInsert() | ||
{ | ||
BadBlockStore badBlockStore = new BadBlockStore(new TestMemDb(), 10); | ||
|
||
List<Block> toAdd = new() | ||
{ | ||
Build.A.Block.WithNumber(1).TestObject, | ||
Build.A.Block.WithNumber(2).TestObject, | ||
Build.A.Block.WithNumber(3).TestObject, | ||
}; | ||
|
||
foreach (Block block in toAdd) | ||
{ | ||
badBlockStore.Insert(block); | ||
} | ||
|
||
badBlockStore.GetAll().Should().BeEquivalentTo(toAdd); | ||
} | ||
|
||
[Test] | ||
public void Test_LimitStoredBlock() | ||
{ | ||
BadBlockStore badBlockStore = new BadBlockStore(new TestMemDb(), 2); | ||
|
||
List<Block> toAdd = new() | ||
{ | ||
Build.A.Block.WithNumber(1).TestObject, | ||
Build.A.Block.WithNumber(2).TestObject, | ||
Build.A.Block.WithNumber(3).TestObject, | ||
}; | ||
|
||
foreach (Block block in toAdd) | ||
{ | ||
badBlockStore.Insert(block); | ||
} | ||
|
||
badBlockStore.GetAll().Count().Should().Be(2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Nethermind/Nethermind.Blockchain/Blocks/BadBlockStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Crypto; | ||
using Nethermind.Db; | ||
using Nethermind.Serialization.Rlp; | ||
|
||
namespace Nethermind.Blockchain.Blocks; | ||
|
||
public class BadBlockStore(IDb blockDb, long maxSize) : IBadBlockStore | ||
{ | ||
private readonly BlockDecoder _blockDecoder = new(); | ||
|
||
public void Insert(Block block, WriteFlags writeFlags = WriteFlags.None) | ||
{ | ||
if (block.Hash is null) | ||
{ | ||
throw new InvalidOperationException("An attempt to store a block with a null hash."); | ||
} | ||
|
||
using NettyRlpStream newRlp = _blockDecoder.EncodeToNewNettyStream(block); | ||
blockDb.Set(block.Number, block.Hash, newRlp.AsSpan(), writeFlags); | ||
|
||
TruncateToMaxSize(); | ||
} | ||
|
||
public IEnumerable<Block> GetAll() | ||
{ | ||
return blockDb.GetAllValues(true).Select(bytes => _blockDecoder.Decode(ByteArrayExtensions.AsRlpStream((byte[]?)bytes))); | ||
} | ||
|
||
private void TruncateToMaxSize() | ||
{ | ||
int toDelete = (int)(blockDb.GatherMetric().Size - maxSize!); | ||
if (toDelete > 0) | ||
{ | ||
foreach (var blockToDelete in GetAll().Take(toDelete)) | ||
{ | ||
Delete(blockToDelete.Number, blockToDelete.Hash); | ||
} | ||
} | ||
} | ||
|
||
private void Delete(long blockNumber, Hash256 blockHash) | ||
{ | ||
blockDb.Delete(blockNumber, blockHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Nethermind/Nethermind.Blockchain/Blocks/IBadBlockStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Collections.Generic; | ||
using Nethermind.Core; | ||
|
||
namespace Nethermind.Blockchain.Blocks; | ||
|
||
public interface IBadBlockStore | ||
{ | ||
void Insert(Block block, WriteFlags writeFlags = WriteFlags.None); | ||
IEnumerable<Block> GetAll(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.