Skip to content

Commit

Permalink
Simplify default Blockchain processor tracers, require tracers to be …
Browse files Browse the repository at this point in the history
…reusable (needed for branches), remove confusion with factories (NethermindEth#3266)
  • Loading branch information
LukaszRozmej authored Aug 2, 2021
1 parent 8bf4d6f commit 746099c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class BlockchainProcessor : IBlockchainProcessor, IBlockProcessingQueue
public int SoftMaxRecoveryQueueSizeInTx = 10000; // adjust based on tx or gas
public const int MaxProcessingQueueSize = 2000; // adjust based on tx or gas

public CompositeBlockTracerFactory BlockTracerFactory { get; } = new();
public ITracerBag Tracers => _compositeBlockTracer;

private readonly IBlockProcessor _blockProcessor;
private readonly IBlockPreprocessorStep _recoveryStep;
Expand All @@ -55,6 +55,8 @@ public class BlockchainProcessor : IBlockchainProcessor, IBlockProcessingQueue
private DateTime _lastProcessedBlock;

private int _currentRecoveryQueueSize;
private readonly CompositeBlockTracer _compositeBlockTracer = new();

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -238,7 +240,7 @@ private void RunProcessingLoop()

if (_logger.IsTrace) _logger.Trace($"Processing block {block.ToString(Block.Format.Short)}).");

Block processedBlock = Process(block, blockRef.ProcessingOptions, BlockTracerFactory.Create());
Block processedBlock = Process(block, blockRef.ProcessingOptions, _compositeBlockTracer.GetTracer());
if (processedBlock == null)
{
if (_logger.IsTrace) _logger.Trace($"Failed / skipped processing {block.ToString(Block.Format.Full)}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethermind.Core;
using Nethermind.Evm.Tracing;
Expand All @@ -23,7 +24,7 @@ namespace Nethermind.Blockchain.Processing
{
public interface IBlockchainProcessor : IDisposable
{
CompositeBlockTracerFactory BlockTracerFactory { get; }
ITracerBag Tracers { get; }

void Start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Nethermind.Blockchain.Processing
{
public class OneTimeChainProcessor : IBlockchainProcessor
{
public CompositeBlockTracerFactory BlockTracerFactory => _processor.BlockTracerFactory;
public ITracerBag Tracers => _processor.Tracers;

private readonly IBlockchainProcessor _processor;
private readonly IReadOnlyDbProvider _readOnlyDbProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void Should_create_tracer_correctly()
GethLikeBlockTracer gethLikeBlockTracer = new(txHash, GethTraceOptions.Default);
ParityLikeBlockTracer parityLikeBlockTracer = new(txHash, ParityTraceTypes.All);

CompositeBlockTracer compositeBlockTracer = new CompositeBlockTracer(
new IBlockTracer[]{gethLikeBlockTracer, parityLikeBlockTracer});
CompositeBlockTracer compositeBlockTracer = new CompositeBlockTracer();
compositeBlockTracer.AddRange(gethLikeBlockTracer, parityLikeBlockTracer);

compositeBlockTracer.IsTracingRewards.Should().Be(true);
}
Expand All @@ -63,9 +63,9 @@ public void Should_trace_properly()
NullBlockTracer nullBlockTracer = NullBlockTracer.Instance;
AlwaysCancelBlockTracer alwaysCancelBlockTracer = AlwaysCancelBlockTracer.Instance;

CompositeBlockTracer blockTracer = new CompositeBlockTracer(
new IBlockTracer[]{gethLikeBlockTracer, parityLikeBlockTracer, nullBlockTracer, alwaysCancelBlockTracer});
CompositeBlockTracer blockTracer = new CompositeBlockTracer();
blockTracer.AddRange(gethLikeBlockTracer, parityLikeBlockTracer, nullBlockTracer, alwaysCancelBlockTracer);

blockTracer.StartNewBlockTrace(block);

blockTracer.StartNewTxTrace(tx1);
Expand Down
48 changes: 37 additions & 11 deletions src/Nethermind/Nethermind.Evm/Tracing/CompositeBlockTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@

namespace Nethermind.Evm.Tracing
{
public class CompositeBlockTracer : IBlockTracer
public class CompositeBlockTracer : IBlockTracer, ITracerBag
{
private readonly IBlockTracer[] _childTracers;
public bool IsTracingRewards { get; }
private readonly List<IBlockTracer> _childTracers = new List<IBlockTracer>();
public bool IsTracingRewards { get; private set; }

public CompositeBlockTracer(IBlockTracer[] childTracers)
public CompositeBlockTracer()
{
_childTracers = childTracers;
IsTracingRewards = _childTracers.Any(childTracer => childTracer.IsTracingRewards);
}

Expand All @@ -44,7 +43,7 @@ public void EndTxTrace()

public void ReportReward(Address author, string rewardType, UInt256 rewardValue)
{
for (int index = 0; index < _childTracers.Length; index++)
for (int index = 0; index < _childTracers.Count; index++)
{
IBlockTracer childTracer = _childTracers[index];
if (childTracer.IsTracingRewards)
Expand All @@ -56,7 +55,7 @@ public void ReportReward(Address author, string rewardType, UInt256 rewardValue)

public void StartNewBlockTrace(Block block)
{
for (int index = 0; index < _childTracers.Length; index++)
for (int index = 0; index < _childTracers.Count; index++)
{
IBlockTracer childTracer = _childTracers[index];
childTracer.StartNewBlockTrace(block);
Expand All @@ -65,11 +64,11 @@ public void StartNewBlockTrace(Block block)

public ITxTracer StartNewTxTrace(Transaction? tx)
{
IBlockTracer[] childBlockTracers = _childTracers;
IList<IBlockTracer> childBlockTracers = _childTracers;

List<ITxTracer> tracers = new(_childTracers.Length);
List<ITxTracer> tracers = new(childBlockTracers.Count);

for (int i = 0; i < childBlockTracers.Length; i++)
for (int i = 0; i < childBlockTracers.Count; i++)
{
IBlockTracer childBlockTracer = childBlockTracers[i];
ITxTracer txTracer = childBlockTracer.StartNewTxTrace(tx);
Expand All @@ -84,11 +83,38 @@ public ITxTracer StartNewTxTrace(Transaction? tx)

public void EndBlockTrace()
{
for (int index = 0; index < _childTracers.Length; index++)
for (int index = 0; index < _childTracers.Count; index++)
{
IBlockTracer childTracer = _childTracers[index];
childTracer.EndBlockTrace();
}
}

public void Add(IBlockTracer tracer)
{
_childTracers.Add(tracer);
IsTracingRewards |= tracer.IsTracingRewards;
}

public void AddRange(params IBlockTracer[] tracers)
{
_childTracers.AddRange(tracers);
IsTracingRewards |= tracers.Any(t => t.IsTracingRewards);
}

public void Remove(IBlockTracer tracer)
{
_childTracers.Remove(tracer);
IsTracingRewards = _childTracers.Any(t => t.IsTracingRewards);

}

public IBlockTracer GetTracer() =>
_childTracers.Count switch
{
0 => NullBlockTracer.Instance,
1 => _childTracers[0],
_ => this
};
}
}

This file was deleted.

8 changes: 7 additions & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/IBlockTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@

namespace Nethermind.Evm.Tracing
{
/// <summary>
/// Tracer for blocks.
/// </summary>
/// <remarks>
/// This tracer should be reusable between blocks. <see cref="StartNewBlockTrace"/> call should reset inner tracer state.
/// </remarks>
public interface IBlockTracer
{
bool IsTracingRewards { get; }

void ReportReward(Address author, string rewardType, UInt256 rewardValue);

void StartNewBlockTrace(Block block);

ITxTracer StartNewTxTrace(Transaction? tx);
Expand Down
7 changes: 0 additions & 7 deletions src/Nethermind/Nethermind.Evm/Tracing/IBlockTracerFactory.cs

This file was deleted.

26 changes: 26 additions & 0 deletions src/Nethermind/Nethermind.Evm/Tracing/ITracerBag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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/>.
//

namespace Nethermind.Evm.Tracing
{
public interface ITracerBag
{
void Add(IBlockTracer tracer);
void AddRange(params IBlockTracer[] tracers);
void Remove(IBlockTracer tracer);
}
}

0 comments on commit 746099c

Please sign in to comment.