Skip to content

Commit

Permalink
modify the usage of txFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqjasoria committed Jun 30, 2022
1 parent ac3937c commit 9a853d8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 125 deletions.
76 changes: 0 additions & 76 deletions src/Nethermind/Nethermind.Evm.Test/Tracing/TxTraceFilterTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ParityLikeBlockTracer : BlockTracerBase<ParityLikeTxTrace, ParityLi
private bool _validateChainId;
private readonly ParityTraceTypes _types;
private readonly ISpecProvider _specProvider;
private readonly TxTraceFilter? _traceFilter = null;

public ParityLikeBlockTracer(Keccak txHash, ParityTraceTypes types)
: base(txHash)
Expand All @@ -44,10 +43,9 @@ public ParityLikeBlockTracer(ParityTraceTypes types)
IsTracingRewards = (types & ParityTraceTypes.Rewards) == ParityTraceTypes.Rewards;
}

public ParityLikeBlockTracer(ParityTraceTypes types, TxTraceFilter? traceFilter, ISpecProvider specProvider)
public ParityLikeBlockTracer(ParityTraceTypes types, ISpecProvider specProvider)
{
_types = types;
_traceFilter = traceFilter;
_specProvider = specProvider;
IsTracingRewards = (types & ParityTraceTypes.Rewards) == ParityTraceTypes.Rewards;
}
Expand Down Expand Up @@ -86,15 +84,5 @@ public override void StartNewBlockTrace(Block block)
public override void EndBlockTrace()
{
}

protected override bool ShouldTraceTx(Transaction? tx)
{
if (base.ShouldTraceTx(tx))
{
return _traceFilter == null || _traceFilter.ShouldTraceTx(tx, _validateChainId);
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using Nethermind.Core.Test.Builders;
using Nethermind.Evm.Tracing;
using Nethermind.Evm.Tracing.ParityStyle;
using Nethermind.JsonRpc.Modules.Trace;
using Nethermind.Logging;
using Nethermind.Specs;
using NUnit.Framework;

namespace Nethermind.JsonRpc.Test.Modules.Trace;

[Parallelizable(ParallelScope.All)]
[TestFixture]
public class TxTraceFilterTests
{
[Test]
public void Trace_filter_should_filter_proper_traces()
{
ParityTraceAction action1 = new () {From = TestItem.AddressA, To = TestItem.AddressB};
ParityTraceAction action2 = new () {From = TestItem.AddressB, To = TestItem.AddressC};
ParityTraceAction action3 = new () {From = TestItem.AddressA, To = TestItem.AddressC};

TxTraceFilter filterForFrom = new(new []{ TestItem.AddressA }, null, 0, null);
Assert.AreEqual(true, filterForFrom.ShouldUseTxTrace(action1));
Assert.AreEqual(false, filterForFrom.ShouldUseTxTrace(action2));
Assert.AreEqual(true, filterForFrom.ShouldUseTxTrace(action3));

TxTraceFilter filterForTo = new(null, new []{ TestItem.AddressC }, 0, null);
Assert.AreEqual(false, filterForTo.ShouldUseTxTrace(action1));
Assert.AreEqual(true, filterForTo.ShouldUseTxTrace(action2));
Assert.AreEqual(true, filterForTo.ShouldUseTxTrace(action3));

TxTraceFilter filterForFromAndTo = new ( new []{ TestItem.AddressA }, new []{ TestItem.AddressC }, 0, null);
Assert.AreEqual(false, filterForFromAndTo.ShouldUseTxTrace(action1));
Assert.AreEqual(false, filterForFromAndTo.ShouldUseTxTrace(action2));
Assert.AreEqual(true, filterForFromAndTo.ShouldUseTxTrace(action3));
}

[Test]
public void Trace_filter_should_skip_expected_number_of_traces_()
{
TxTraceFilter traceFilter = new(new []{ TestItem.AddressA }, null, 2, 2);
ParityTraceAction action1 = new () {From = TestItem.AddressA};
ParityTraceAction action2 = new () {From = TestItem.AddressB};

Assert.AreEqual(false, traceFilter.ShouldUseTxTrace(action1));
Assert.AreEqual(false, traceFilter.ShouldUseTxTrace(action2));
Assert.AreEqual(false, traceFilter.ShouldUseTxTrace(action1));
Assert.AreEqual(false, traceFilter.ShouldUseTxTrace(action2));
Assert.AreEqual(true, traceFilter.ShouldUseTxTrace(action1));
Assert.AreEqual(true, traceFilter.ShouldUseTxTrace(action1));
Assert.AreEqual(false, traceFilter.ShouldUseTxTrace(action1));

}
}
17 changes: 5 additions & 12 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Trace/TraceRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public ResultWrapper<ParityTxTraceFromReplay[]> trace_replayBlockTransactions(Bl

public ResultWrapper<ParityTxTraceFromStore[]> trace_filter(TraceFilterForRpc traceFilterForRpc)
{
TxTraceFilter txTracerFilter = new(traceFilterForRpc.FromAddress, traceFilterForRpc.ToAddress, traceFilterForRpc.After, traceFilterForRpc.Count, _specProvider, _logManager);
List<ParityLikeTxTrace> txTraces = new();
IEnumerable<SearchResult<Block>> blocksSearch =
_blockFinder.SearchForBlocksOnMainChain(traceFilterForRpc.FromBlock ?? BlockParameter.Latest, traceFilterForRpc.ToBlock ?? BlockParameter.Latest);
Expand All @@ -177,20 +176,14 @@ public ResultWrapper<ParityTxTraceFromStore[]> trace_filter(TraceFilterForRpc tr
}
Block block = blockSearch.Object;
IReadOnlyCollection<ParityLikeTxTrace> txTracesFromOneBlock =
TraceBlock(block, ParityTraceTypes.Trace, txTracerFilter);
TraceBlock(block, ParityTraceTypes.Trace);
txTraces.AddRange(txTracesFromOneBlock);
}

ParityTxTraceFromStore[] txTracesResult = txTraces.SelectMany(ParityTxTraceFromStore.FromTxTrace).ToArray();
List<ParityTxTraceFromStore> filteredTxTracesResult = new();

foreach (ParityTxTraceFromStore? txTrace in txTracesResult)
{
if (txTracerFilter.IsValidTxTrace(txTrace.Action))
filteredTxTracesResult.Add(txTrace);
}

return ResultWrapper<ParityTxTraceFromStore[]>.Success(filteredTxTracesResult.ToArray());
TxTraceFilter txTracerFilter = new(traceFilterForRpc.FromAddress, traceFilterForRpc.ToAddress, traceFilterForRpc.After, traceFilterForRpc.Count);
return ResultWrapper<ParityTxTraceFromStore[]>.Success(txTracerFilter.FilterTxTraces(txTracesResult));
}

public ResultWrapper<ParityTxTraceFromStore[]> trace_block(BlockParameter blockParameter)
Expand Down Expand Up @@ -244,12 +237,12 @@ public ResultWrapper<ParityTxTraceFromStore[]> trace_transaction(Keccak txHash)
return ResultWrapper<ParityTxTraceFromStore[]>.Success(ParityTxTraceFromStore.FromTxTrace(txTrace));
}

private IReadOnlyCollection<ParityLikeTxTrace> TraceBlock(Block block, ParityTraceTypes traceTypes, TxTraceFilter? txTraceFilter = null)
private IReadOnlyCollection<ParityLikeTxTrace> TraceBlock(Block block, ParityTraceTypes traceTypes)
{
using CancellationTokenSource cancellationTokenSource = new(_cancellationTokenTimeout);
CancellationToken cancellationToken = cancellationTokenSource.Token;

ParityLikeBlockTracer listener = new(traceTypes, txTraceFilter, _specProvider);
ParityLikeBlockTracer listener = new(traceTypes, _specProvider);
_tracer.Trace(block, listener);

return listener.BuildResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,49 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using System.Linq;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Crypto;
using Nethermind.Evm.Tracing.ParityStyle;
using Nethermind.Logging;

namespace Nethermind.Evm.Tracing
namespace Nethermind.JsonRpc.Modules.Trace
{
public class TxTraceFilter
{
private readonly Address[]? _fromAddresses;
private readonly Address[]? _toAddresses;
private int _after;
private int? _count;
private readonly ISpecProvider _specProvider;
private readonly ILogger _logger;
private readonly EthereumEcdsa _ecdsa;

public TxTraceFilter(
Address[]? fromAddresses,
Address[]? toAddresses,
int after,
int? count,
ISpecProvider specProvider,
ILogManager logManager)
int? count)
{
_fromAddresses = fromAddresses;
_toAddresses = toAddresses;
_after = after;
_count = count;
_specProvider = specProvider;
_logger = logManager.GetClassLogger();
_ecdsa = new EthereumEcdsa(specProvider.ChainId, logManager);
}

public bool ShouldTraceTx(Transaction? tx, bool validateChainId)
public ParityTxTraceFromStore[] FilterTxTraces(ParityTxTraceFromStore[]? txTraces)
{
if (tx == null || (_count <= 0))
List<ParityTxTraceFromStore> filteredTxTracesResult = new();
foreach (ParityTxTraceFromStore? txTrace in txTraces)
{
return false;
if (ShouldUseTxTrace(txTrace.Action))
filteredTxTracesResult.Add(txTrace);
}
return true;

return filteredTxTracesResult.ToArray();
}
public bool IsValidTxTrace(ParityTraceAction? tx)

public bool ShouldUseTxTrace(ParityTraceAction? tx)
{
if (_logger.IsTrace) _logger.Trace($"Tracing transaction {tx}, from: {tx?.From}, to: {tx?.To}, fromAddresses: {_fromAddresses}, toAddresses {_toAddresses}, after {_after}, count {_count}");
if (tx == null || !MatchAddresses(tx.From, tx.To) ||
(_count <= 0))
{
Expand All @@ -79,13 +74,6 @@ public bool IsValidTxTrace(ParityTraceAction? tx)
return true;
}

public bool ShouldTraceBlock(Block? block)
{
if (block == null)
return false;
return true;
}

private bool MatchAddresses(Address? fromAddress, Address? toAddress)
{
return (_fromAddresses == null || _fromAddresses.Contains(fromAddress)) &&
Expand Down

0 comments on commit 9a853d8

Please sign in to comment.