Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Nethermind.Blockchain.Filters;
using Nethermind.Blockchain.Test.Builders;
using Nethermind.Consensus.Processing;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.Core.Timers;
using Nethermind.Facade.Filters;
using Nethermind.Logging;
using Nethermind.TxPool;
Expand All @@ -21,7 +23,7 @@ namespace Nethermind.Blockchain.Test.Filters;

public class FilterManagerTests
{
private IFilterStore _filterStore = null!;
private FilterStore _filterStore = null!;
private IBranchProcessor _branchProcessor = null!;
private IMainProcessingContext _mainProcessingContext = null!;
private ITxPool _txPool = null!;
Expand All @@ -34,7 +36,7 @@ public class FilterManagerTests
public void Setup()
{
_currentFilterId = 0;
_filterStore = Substitute.For<IFilterStore>();
_filterStore = new FilterStore(new TimerFactory(), 20, 10);
_branchProcessor = Substitute.For<IBranchProcessor>();
_mainProcessingContext = Substitute.For<IMainProcessingContext>();
_mainProcessingContext.BranchProcessor.Returns(_branchProcessor);
Expand All @@ -49,11 +51,11 @@ public void TearDown()
}

[Test, MaxTime(Timeout.MaxTestTime)]
public void removing_filter_removes_data()
public async Task removing_filter_removes_data()
{
LogsShouldNotBeEmpty(static _ => { }, static _ => { });
_filterManager.GetLogs(0).Should().NotBeEmpty();
_filterStore.FilterRemoved += Raise.EventWith(new FilterEventArgs(0));
await Task.Delay(60);
Comment thread
benaadams marked this conversation as resolved.
_filterManager.GetLogs(0).Should().BeEmpty();
}

Expand Down Expand Up @@ -324,8 +326,8 @@ private void Assert(IEnumerable<Action<FilterBuilder>> filterBuilders,
BlockFilter blockFilter = new(_currentFilterId++);
filters.Add(blockFilter);

_filterStore.GetFilters<LogFilter>().Returns(filters.OfType<LogFilter>().ToArray());
_filterStore.GetFilters<BlockFilter>().Returns(filters.OfType<BlockFilter>().ToArray());
_filterStore.SaveFilters(filters.OfType<LogFilter>());
_filterStore.SaveFilters(filters.OfType<BlockFilter>());
_filterManager = new FilterManager(_filterStore, _mainProcessingContext, _txPool, _logManager);

_branchProcessor.BlockProcessed += Raise.EventWith(_branchProcessor, new BlockProcessedEventArgs(block, []));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public async Task CleanUps_filters()
Assert.That(() => store.FilterExists(1), Is.False.After(30, 5), "filter 1 doesn't exist");
Assert.That(() => store.FilterExists(2), Is.False.After(30, 5), "filter 2 doesn't exist");
Assert.That(() => store.FilterExists(3), Is.False.After(30, 5), "filter 3 doesn't exist");
store.RefreshFilter(0);
Comment thread
benaadams marked this conversation as resolved.
Assert.That(() => removedFilterIds, Is.EquivalentTo([1, 2, 3]).After(30, 5));
}
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class BlockchainBridge(
IStateReader stateReader,
ITxPool txPool,
IReceiptFinder receiptStorage,
IFilterStore filterStore,
IFilterManager filterManager,
FilterStore filterStore,
FilterManager filterManager,
IEthereumEcdsa ecdsa,
ITimestamper timestamper,
ILogFinder logFinder,
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Facade/Filters/FilterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Nethermind.Blockchain.Filters
{
public class FilterManager : IFilterManager
public sealed class FilterManager
{
private readonly ConcurrentDictionary<int, List<FilterLog>> _logs =
new();
Expand All @@ -27,12 +27,12 @@ public class FilterManager : IFilterManager
new();

private Hash256? _lastBlockHash;
private readonly IFilterStore _filterStore;
private readonly FilterStore _filterStore;
private readonly ILogger _logger;
private long _logIndex;

public FilterManager(
IFilterStore filterStore,
FilterStore filterStore,
IMainProcessingContext mainProcessingContext,
ITxPool txPool,
ILogManager logManager)
Expand Down
23 changes: 20 additions & 3 deletions src/Nethermind/Nethermind.Facade/Filters/FilterStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Nethermind.Blockchain.Filters
{
public class FilterStore : IFilterStore
public sealed class FilterStore : IDisposable
{
private readonly TimeSpan _timeout;
private int _currentFilterId = -1;
Expand Down Expand Up @@ -84,6 +84,16 @@ private void CleanupStaleFilters()
}

public IEnumerable<T> GetFilters<T>() where T : FilterBase
{
// Return Array.Empty<T>() to avoid allocating enumerator
// and which has a non-allocating fast-path for
// foreach via IEnumerable<T>
if (_filters.IsEmpty) return Array.Empty<T>();

return GetFiltersEnumerate<T>();
}

private IEnumerable<T> GetFiltersEnumerate<T>() where T : FilterBase
{
// Reuse the enumerator
var enumerator = Interlocked.Exchange(ref _enumerator, null) ?? _filters.GetEnumerator();
Expand Down Expand Up @@ -131,7 +141,7 @@ public void RemoveFilter(int filterId)

public void SaveFilter(FilterBase filter)
{
if (_filters.ContainsKey(filter.Id))
if (!_filters.TryAdd(filter.Id, filter))
{
throw new InvalidOperationException($"Filter with ID {filter.Id} already exists");
}
Expand All @@ -140,8 +150,15 @@ public void SaveFilter(FilterBase filter)
{
_currentFilterId = Math.Max(filter.Id, _currentFilterId);
}
}

_filters[filter.Id] = filter;
// Used by tests
public void SaveFilters(IEnumerable<FilterBase> filters)
{
foreach (FilterBase filter in filters)
{
SaveFilter(filter);
}
Comment thread
benaadams marked this conversation as resolved.
Outdated
}

private int GetFilterId(bool generateId)
Expand Down
15 changes: 0 additions & 15 deletions src/Nethermind/Nethermind.Facade/Filters/IFilterManager.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/Nethermind/Nethermind.Facade/Filters/IFilterStore.cs

This file was deleted.

42 changes: 0 additions & 42 deletions src/Nethermind/Nethermind.Facade/Filters/NullFilterManager.cs

This file was deleted.

76 changes: 0 additions & 76 deletions src/Nethermind/Nethermind.Facade/Filters/NullFilterStore.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Init/Modules/RpcModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ protected override void Load(ContainerBuilder builder)
.AddSingleton<IBlockchainBridgeFactory, BlockchainBridgeFactory>()
.AddScoped<IBlockchainBridge>((ctx) => ctx.Resolve<IBlockchainBridgeFactory>().CreateBlockchainBridge())
.AddSingleton<IFeeHistoryOracle, FeeHistoryOracle>()
.AddSingleton<IFilterStore, ITimerFactory, IJsonRpcConfig>((timerFactory, rpcConfig) => new FilterStore(timerFactory, rpcConfig.FiltersTimeout))
.AddSingleton<IFilterManager, FilterManager>()
.AddSingleton<FilterStore, ITimerFactory, IJsonRpcConfig>((timerFactory, rpcConfig) => new FilterStore(timerFactory, rpcConfig.FiltersTimeout))
.AddSingleton<FilterManager>()
.AddSingleton<ISimulateReadOnlyBlocksProcessingEnvFactory, SimulateReadOnlyBlocksProcessingEnvFactory>()

// Proof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RegisterRpcModules(
IBlockTree blockTree,
ISpecProvider specProvider,
IReceiptMonitor receiptMonitor,
IFilterStore filterStore,
FilterStore filterStore,
ITxPool txPool,
IEthSyncingInfo ethSyncingInfo,
IPeerPool peerPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SubscribeModuleTests
private IBlockTree _blockTree = null!;
private ITxPool _txPool = null!;
private IReceiptStorage _receiptStorage = null!;
private IFilterStore _filterStore = null!;
private FilterStore _filterStore = null!;
private ISubscriptionManager _subscriptionManager = null!;
private IJsonRpcDuplexClient _jsonRpcDuplexClient = null!;
private IJsonSerializer _jsonSerializer = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class LogsSubscription : Subscription
public LogsSubscription(
IJsonRpcDuplexClient jsonRpcDuplexClient,
IReceiptMonitor receiptCanonicalityMonitor,
IFilterStore? store,
FilterStore? store,
IBlockTree? blockTree,
ILogManager? logManager,
Filter? filter = null)
Expand All @@ -33,7 +33,7 @@ public LogsSubscription(
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
_receiptCanonicalityMonitor = receiptCanonicalityMonitor ?? throw new ArgumentNullException(nameof(receiptCanonicalityMonitor));
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
IFilterStore filterStore = store ?? throw new ArgumentNullException(nameof(store));
FilterStore filterStore = store ?? throw new ArgumentNullException(nameof(store));

if (filter is not null)
{
Expand Down
Loading