Skip to content

Commit

Permalink
Fix logIndex in filter changes (#7853)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexb5dh authored Dec 3, 2024
1 parent 9ff58a0 commit e2b4946
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,35 @@ public void logs_should_be_empty_for_existing_block_and_addresses_and_non_existi
.WithLogs(Build.A.LogEntry.WithAddress(TestItem.AddressA)
.WithTopics(TestItem.KeccakB, TestItem.KeccakC).TestObject));

[Test, MaxTime(Timeout.MaxTestTime)]
[TestCase(1, 1)]
[TestCase(5, 3)]
public void logs_should_have_correct_log_indexes(int filtersCount, int logsPerTx)
{
const int txCount = 10;

Assert(
filterBuilder: (builder, _) => builder
.FromBlock(1L)
.ToBlock(10L)
.WithAddresses(TestItem.AddressA, TestItem.AddressB)
.WithTopicExpressions(TestTopicExpressions.Or(
TestTopicExpressions.Specific(TestItem.KeccakB),
TestTopicExpressions.Specific(TestItem.KeccakD)
)),
filterCount: filtersCount,
receiptBuilder: (builder, _) => builder
.WithBlockNumber(6L)
.WithLogs(Enumerable.Range(0, logsPerTx).Select(_ =>
Build.A.LogEntry
.WithAddress(TestItem.AddressA)
.WithTopics(TestItem.KeccakB, TestItem.KeccakC).TestObject
).ToArray()),
receiptCount: txCount,
logsAssertion: logs => logs.Select(l => l.LogIndex).Should().BeEquivalentTo(Enumerable.Range(0, txCount * logsPerTx))
);
}


private void LogsShouldNotBeEmpty(Action<FilterBuilder> filterBuilder,
Action<ReceiptBuilder> receiptBuilder)
Expand All @@ -255,6 +284,15 @@ private void LogsShouldBeEmpty(IEnumerable<Action<FilterBuilder>> filterBuilders
IEnumerable<Action<ReceiptBuilder>> receiptBuilders)
=> Assert(filterBuilders, receiptBuilders, logs => logs.Should().BeEmpty());

private void Assert(Action<FilterBuilder, int> filterBuilder, int filterCount,
Action<ReceiptBuilder, int> receiptBuilder, int receiptCount,
Action<IEnumerable<FilterLog>> logsAssertion)
=> Assert(
Enumerable.Range(0, filterCount).Select<int, Action<FilterBuilder>>(i => builder => filterBuilder(builder, i)),
Enumerable.Range(0, receiptCount).Select<int, Action<ReceiptBuilder>>(i => builder => receiptBuilder(builder, i)),
logsAssertion
);

private void Assert(IEnumerable<Action<FilterBuilder>> filterBuilders,
IEnumerable<Action<ReceiptBuilder>> receiptBuilders,
Action<IEnumerable<FilterLog>> logsAssertion)
Expand Down
6 changes: 4 additions & 2 deletions src/Nethermind/Nethermind.Facade/Filters/FilterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ private void AddReceipts(TxReceipt txReceipt)
IEnumerable<LogFilter> filters = _filterStore.GetFilters<LogFilter>();
foreach (LogFilter filter in filters)
{
StoreLogs(filter, txReceipt, ref _logIndex);
StoreLogs(filter, txReceipt, _logIndex);
}

_logIndex += txReceipt.Logs?.Length ?? 0;
}

private void AddBlock(Block block)
Expand All @@ -191,7 +193,7 @@ private void StoreBlock(BlockFilter filter, Block block)
if (_logger.IsDebug) _logger.Debug($"Filter with id: {filter.Id} contains {blocks.Count} blocks.");
}

private void StoreLogs(LogFilter filter, TxReceipt txReceipt, ref long logIndex)
private void StoreLogs(LogFilter filter, TxReceipt txReceipt, long logIndex)
{
if (txReceipt.Logs is null || txReceipt.Logs.Length == 0)
{
Expand Down

0 comments on commit e2b4946

Please sign in to comment.