-
Notifications
You must be signed in to change notification settings - Fork 461
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 exceptions-flow-control
- Loading branch information
Showing
31 changed files
with
563 additions
and
78 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ namespace Nethermind.Db; | |
public enum BlobTxsColumns | ||
{ | ||
FullBlobTxs, | ||
LightBlobTxs | ||
LightBlobTxs, | ||
ProcessedTxs | ||
} |
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
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
61 changes: 61 additions & 0 deletions
61
src/Nethermind/Nethermind.Merge.Plugin.Test/ProcessedTransactionsDbCleanerTests.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,61 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Nethermind.Blockchain; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Specs; | ||
using Nethermind.Core.Test.Builders; | ||
using Nethermind.Crypto; | ||
using Nethermind.Db; | ||
using Nethermind.Int256; | ||
using Nethermind.Logging; | ||
using Nethermind.Specs; | ||
using Nethermind.TxPool; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Merge.Plugin.Test; | ||
|
||
[TestFixture] | ||
[Parallelizable(ParallelScope.All)] | ||
public class ProcessedTransactionsDbCleanerTests | ||
{ | ||
private readonly ILogManager _logManager = LimboLogs.Instance; | ||
private readonly ISpecProvider _specProvider = MainnetSpecProvider.Instance; | ||
|
||
[Test] | ||
public async Task should_remove_processed_txs_from_db_after_finalization([Values(0, 1, 42, 358)] long blockOfTxs, [Values(1, 42, 358)] long finalizedBlock) | ||
{ | ||
Transaction GetTx(PrivateKey sender) | ||
{ | ||
return Build.A.Transaction | ||
.WithShardBlobTxTypeAndFields() | ||
.WithMaxFeePerGas(UInt256.One) | ||
.WithMaxPriorityFeePerGas(UInt256.One) | ||
.WithNonce(UInt256.Zero) | ||
.SignedAndResolved(new EthereumEcdsa(_specProvider.ChainId, _logManager), sender).TestObject; | ||
} | ||
|
||
IColumnsDb<BlobTxsColumns> columnsDb = new MemColumnsDb<BlobTxsColumns>(BlobTxsColumns.ProcessedTxs); | ||
BlobTxStorage blobTxStorage = new(columnsDb); | ||
Transaction[] txs = { GetTx(TestItem.PrivateKeyA), GetTx(TestItem.PrivateKeyB) }; | ||
|
||
blobTxStorage.AddBlobTransactionsFromBlock(blockOfTxs, txs); | ||
|
||
blobTxStorage.TryGetBlobTransactionsFromBlock(blockOfTxs, out Transaction[]? returnedTxs).Should().BeTrue(); | ||
returnedTxs!.Length.Should().Be(2); | ||
|
||
IBlockFinalizationManager finalizationManager = Substitute.For<IBlockFinalizationManager>(); | ||
ProcessedTransactionsDbCleaner dbCleaner = new(finalizationManager, columnsDb.GetColumnDb(BlobTxsColumns.ProcessedTxs), _logManager); | ||
|
||
finalizationManager.BlocksFinalized += Raise.EventWith( | ||
new FinalizeEventArgs(Build.A.BlockHeader.TestObject, | ||
Build.A.BlockHeader.WithNumber(finalizedBlock).TestObject)); | ||
|
||
await dbCleaner.CleaningTask; | ||
|
||
blobTxStorage.TryGetBlobTransactionsFromBlock(blockOfTxs, out returnedTxs).Should().Be(blockOfTxs > finalizedBlock); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/Nethermind/Nethermind.Merge.Plugin/ProcessedTransactionsDbCleaner.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,70 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Nethermind.Blockchain; | ||
using Nethermind.Core; | ||
using Nethermind.Core.Extensions; | ||
using Nethermind.Db; | ||
using Nethermind.Logging; | ||
|
||
namespace Nethermind.Merge.Plugin; | ||
|
||
public class ProcessedTransactionsDbCleaner : IDisposable | ||
{ | ||
private readonly IBlockFinalizationManager _finalizationManager; | ||
private readonly IDb _processedTxsDb; | ||
private readonly ILogger _logger; | ||
private long _lastFinalizedBlock = 0; | ||
public Task CleaningTask { get; private set; } = Task.CompletedTask; | ||
|
||
public ProcessedTransactionsDbCleaner(IBlockFinalizationManager finalizationManager, IDb processedTxsDb, ILogManager logManager) | ||
{ | ||
_finalizationManager = finalizationManager ?? throw new ArgumentNullException(nameof(finalizationManager)); | ||
_processedTxsDb = processedTxsDb ?? throw new ArgumentNullException(nameof(processedTxsDb)); | ||
_logger = logManager.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager)); | ||
|
||
_finalizationManager.BlocksFinalized += OnBlocksFinalized; | ||
} | ||
|
||
private void OnBlocksFinalized(object? sender, FinalizeEventArgs e) | ||
{ | ||
if (e.FinalizedBlocks.Count > 0 && e.FinalizedBlocks[0].Number > _lastFinalizedBlock) | ||
{ | ||
CleaningTask = Task.Run(() => CleanProcessedTransactionsDb(e.FinalizedBlocks[0].Number)); | ||
} | ||
} | ||
|
||
private void CleanProcessedTransactionsDb(long newlyFinalizedBlockNumber) | ||
{ | ||
try | ||
{ | ||
using (IWriteBatch writeBatch = _processedTxsDb.StartWriteBatch()) | ||
{ | ||
foreach (byte[] key in _processedTxsDb.GetAllKeys()) | ||
{ | ||
long blockNumber = key.ToLongFromBigEndianByteArrayWithoutLeadingZeros(); | ||
if (newlyFinalizedBlockNumber >= blockNumber) | ||
{ | ||
if (_logger.IsTrace) _logger.Trace($"Cleaning processed blob txs from block {blockNumber}"); | ||
writeBatch.Delete(blockNumber); | ||
} | ||
} | ||
} | ||
|
||
if (_logger.IsDebug) _logger.Debug($"Cleaned processed blob txs from block {_lastFinalizedBlock} to block {newlyFinalizedBlockNumber}"); | ||
|
||
_lastFinalizedBlock = newlyFinalizedBlockNumber; | ||
} | ||
catch (Exception exception) | ||
{ | ||
if (_logger.IsError) _logger.Error($"Couldn't correctly clean db with processed transactions. Newly finalized block {newlyFinalizedBlockNumber}, last finalized block: {_lastFinalizedBlock}", exception); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_finalizationManager.BlocksFinalized -= OnBlocksFinalized; | ||
} | ||
} |
Oops, something went wrong.