-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add column for processed txs * add feature of returning keys only * adjust BlobTxStorage * cosmetics * add logic for storing processed blob txs and removing finalized * rename * fix * add tests * cosmetics * add sender recovery for reorganized blob txs * requested change * one more place * postmerge fix * fix saving of processed blob txs * optimize saving processed txs * readd blob support config * fix whitespace * fix test * fix tests parallelism? * add more logs * fix blob txs metric * refactor BlobTxStorage * optimize collecting blob txs from processed block * Create IBlobTxStorage and separate blob specific methods from ITxStorage * use batch when cleaning processed txs db * fix * fix file encoding * fix init when blob support or reorgs disabled * fix signing test txs * add regression test * fix tests * one more regression test * post-merge fix * add test for readding reorganized blob txs * improve test * one more check in test * Remove unnecessary list Sleep -> Task in test * cosmetic * compress 3 bool flags to 1 enum * fix file encoding * fix whitespaces --------- Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
- Loading branch information
1 parent
f3ed968
commit 8fa3263
Showing
30 changed files
with
562 additions
and
77 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
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; | ||
} | ||
} |
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.