Skip to content

Commit 979b265

Browse files
authored
Isolate Tracer in Proof Module and remove IVisitingWorldState (#8981)
* Scope the tracer * Use state reader in Tracer * Inline dump state * Remove accept * Delete IVisitingWorldState * Move dump state to state reader * Whitespace
1 parent 72debe8 commit 979b265

32 files changed

Lines changed: 180 additions & 212 deletions

src/Nethermind/Nethermind.Api/IMainProcessingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using System.Threading.Tasks;
55
using Nethermind.Consensus.Processing;
66
using Nethermind.Core.ServiceStopper;
7+
using Nethermind.Evm.State;
78
using Nethermind.Evm.TransactionProcessing;
8-
using Nethermind.State;
99

1010
namespace Nethermind.Api;
1111

@@ -21,7 +21,7 @@ public interface IMainProcessingContext : IStoppableService
2121
ITransactionProcessor TransactionProcessor { get; }
2222
IBlockProcessor BlockProcessor { get; }
2323
IBlockchainProcessor BlockchainProcessor { get; }
24-
IVisitingWorldState WorldState { get; }
24+
IWorldState WorldState { get; }
2525

2626
Task IStoppableService.StopAsync() => BlockchainProcessor.StopAsync();
2727
string IStoppableService.Description => "blockchain processor";

src/Nethermind/Nethermind.Api/MainProcessingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using Nethermind.Consensus.Processing;
5+
using Nethermind.Evm.State;
56
using Nethermind.Evm.TransactionProcessing;
6-
using Nethermind.State;
77

88
namespace Nethermind.Api;
99

1010
public record MainProcessingContext(
1111
ITransactionProcessor TransactionProcessor,
1212
IBlockProcessor BlockProcessor,
1313
IBlockchainProcessor BlockchainProcessor,
14-
IVisitingWorldState WorldState
14+
IWorldState WorldState
1515
) : IMainProcessingContext
1616
{
1717
}

src/Nethermind/Nethermind.AuRa.Test/Contract/TestContractBlockchain.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ protected override Block GetGenesisBlock(IWorldState worldState) =>
4949
new GenesisLoader(
5050
ChainSpec,
5151
SpecProvider,
52+
StateReader,
5253
worldState,
53-
TxProcessor)
54+
TxProcessor,
55+
LogManager)
5456
.Load();
5557
}
5658
}

src/Nethermind/Nethermind.AuRa.Test/Contract/ValidatorContractTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class ValidatorContractTests
3030
private readonly Address _contractAddress = Address.FromNumber(long.MaxValue);
3131
private ITransactionProcessor _transactionProcessor;
3232
private IReadOnlyTxProcessorSource _readOnlyTxProcessorSource;
33-
private IVisitingWorldState _stateProvider;
33+
private IWorldState _stateProvider;
3434

3535
[SetUp]
3636
public void SetUp()
3737
{
3838
_block = new Block(Build.A.BlockHeader.WithStateRoot(TestItem.KeccakA).TestObject, new BlockBody());
3939
_transactionProcessor = Substitute.For<ITransactionProcessor>();
40-
_stateProvider = Substitute.For<IVisitingWorldState>();
40+
_stateProvider = Substitute.For<IWorldState>();
4141
_readOnlyTxProcessorSource = Substitute.For<IReadOnlyTxProcessorSource>();
4242
_readOnlyTxProcessorSource.Build(_block.Header).Returns(new ReadOnlyTxProcessingScope(_transactionProcessor, _stateProvider));
4343
}

src/Nethermind/Nethermind.AuRa.Test/Validators/ContractBasedValidatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace Nethermind.AuRa.Test.Validators;
3838

3939
public class ContractBasedValidatorTests
4040
{
41-
private IVisitingWorldState _stateProvider;
41+
private IWorldState _stateProvider;
4242
private IAbiEncoder _abiEncoder;
4343
private ILogManager _logManager;
4444
private AuRaParameters.Validator _validator;
@@ -62,7 +62,7 @@ public void SetUp()
6262
{
6363
_validatorStore = new ValidatorStore(new MemDb());
6464
_validSealerStrategy = new ValidSealerStrategy();
65-
_stateProvider = Substitute.For<IVisitingWorldState>();
65+
_stateProvider = Substitute.For<IWorldState>();
6666
_abiEncoder = Substitute.For<IAbiEncoder>();
6767
_logManager = LimboLogs.Instance;
6868
_blockTree = Substitute.For<IBlockTree>();

src/Nethermind/Nethermind.Blockchain.Test/GenesisLoaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private Block GetGenesisBlock(string chainspecPath)
6060
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
6161
specProvider.GetSpec(Arg.Any<ForkActivation>()).Returns(Berlin.Instance);
6262
ITransactionProcessor transactionProcessor = Substitute.For<ITransactionProcessor>();
63-
GenesisLoader genesisLoader = new(chainSpec, specProvider, stateProvider, transactionProcessor);
63+
GenesisLoader genesisLoader = new(chainSpec, specProvider, worldStateManager.GlobalStateReader, stateProvider, transactionProcessor, LimboLogs.Instance);
6464
return genesisLoader.Load();
6565
}
6666

src/Nethermind/Nethermind.Blockchain/GenesisLoader.cs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,94 @@
66
using System.Linq;
77
using Nethermind.Blockchain.Tracing;
88
using Nethermind.Core;
9+
using Nethermind.Core.Crypto;
910
using Nethermind.Core.Extensions;
1011
using Nethermind.Core.Specs;
1112
using Nethermind.Crypto;
1213
using Nethermind.Int256;
1314
using Nethermind.Evm;
1415
using Nethermind.Evm.State;
1516
using Nethermind.Evm.TransactionProcessing;
17+
using Nethermind.Logging;
1618
using Nethermind.Specs.ChainSpecStyle;
19+
using Nethermind.State;
20+
using Nethermind.Trie;
1721

1822
namespace Nethermind.Blockchain
1923
{
2024
public class GenesisLoader(
2125
ChainSpec chainSpec,
2226
ISpecProvider specProvider,
27+
IStateReader stateReader,
2328
IWorldState stateProvider,
24-
ITransactionProcessor transactionProcessor)
29+
ITransactionProcessor transactionProcessor,
30+
ILogManager logManager,
31+
Hash256? expectedGenesisHash = null
32+
)
2533
{
26-
private readonly ChainSpec _chainSpec = chainSpec ?? throw new ArgumentNullException(nameof(chainSpec));
27-
private readonly ISpecProvider _specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
28-
private readonly IWorldState _stateProvider = stateProvider ?? throw new ArgumentNullException(nameof(stateProvider));
29-
private readonly ITransactionProcessor _transactionProcessor = transactionProcessor ?? throw new ArgumentNullException(nameof(transactionProcessor));
34+
ILogger _logger = logManager.GetClassLogger<GenesisLoader>();
3035

3136
public Block Load()
3237
{
33-
Block genesis = _chainSpec.Genesis;
38+
Block genesis = chainSpec.Genesis;
3439
Preallocate(genesis);
3540

3641
// we no longer need the allocations - 0.5MB RAM, 9000 objects for mainnet
37-
_chainSpec.Allocations = null;
42+
chainSpec.Allocations = null;
3843

39-
if (!_chainSpec.GenesisStateUnavailable)
44+
if (!chainSpec.GenesisStateUnavailable)
4045
{
41-
_stateProvider.Commit(_specProvider.GenesisSpec, true);
46+
stateProvider.Commit(specProvider.GenesisSpec, true);
4247

43-
_stateProvider.CommitTree(0);
48+
stateProvider.CommitTree(0);
4449

45-
genesis.Header.StateRoot = _stateProvider.StateRoot;
50+
genesis.Header.StateRoot = stateProvider.StateRoot;
4651
}
4752

4853
genesis.Header.Hash = genesis.Header.CalculateHash();
4954

55+
ValidateGenesisHash(expectedGenesisHash, genesis.Header);
56+
5057
return genesis;
5158
}
5259

60+
/// <summary>
61+
/// If <paramref name="expectedGenesisHash"/> is <value>null</value> then it means that we do not care about the genesis hash (e.g. in some quick testing of private chains)/>
62+
/// </summary>
63+
/// <param name="expectedGenesisHash"></param>
64+
private void ValidateGenesisHash(Hash256? expectedGenesisHash, BlockHeader genesis)
65+
{
66+
if (expectedGenesisHash is not null && genesis.Hash != expectedGenesisHash)
67+
{
68+
if (_logger.IsTrace) _logger.Trace(stateReader.DumpState(genesis.StateRoot!));
69+
if (_logger.IsWarn) _logger.Warn(genesis.ToString(BlockHeader.Format.Full));
70+
if (_logger.IsError) _logger.Error($"Unexpected genesis hash, expected {expectedGenesisHash}, but was {genesis.Hash}");
71+
}
72+
else
73+
{
74+
if (_logger.IsDebug) _logger.Info($"Genesis hash : {genesis.Hash}");
75+
}
76+
77+
ThisNodeInfo.AddInfo("Genesis hash :", $"{genesis.Hash}");
78+
}
79+
5380
private void Preallocate(Block genesis)
5481
{
55-
_transactionProcessor.SetBlockExecutionContext(new BlockExecutionContext(genesis.Header, specProvider.GetSpec(genesis.Header)));
56-
foreach ((Address address, ChainSpecAllocation allocation) in _chainSpec.Allocations.OrderBy(static a => a.Key))
82+
transactionProcessor.SetBlockExecutionContext(new BlockExecutionContext(genesis.Header, specProvider.GetSpec(genesis.Header)));
83+
foreach ((Address address, ChainSpecAllocation allocation) in chainSpec.Allocations.OrderBy(static a => a.Key))
5784
{
58-
_stateProvider.CreateAccount(address, allocation.Balance, allocation.Nonce);
85+
stateProvider.CreateAccount(address, allocation.Balance, allocation.Nonce);
5986

6087
if (allocation.Code is not null)
6188
{
62-
_stateProvider.InsertCode(address, allocation.Code, _specProvider.GenesisSpec, true);
89+
stateProvider.InsertCode(address, allocation.Code, specProvider.GenesisSpec, true);
6390
}
6491

6592
if (allocation.Storage is not null)
6693
{
6794
foreach (KeyValuePair<UInt256, byte[]> storage in allocation.Storage)
6895
{
69-
_stateProvider.Set(new StorageCell(address, storage.Key),
96+
stateProvider.Set(new StorageCell(address, storage.Key),
7097
storage.Value.WithoutLeadingZeros().ToArray());
7198
}
7299
}
@@ -81,7 +108,7 @@ private void Preallocate(Block genesis)
81108
};
82109

83110
CallOutputTracer outputTracer = new();
84-
_transactionProcessor.Execute(constructorTransaction, outputTracer);
111+
transactionProcessor.Execute(constructorTransaction, outputTracer);
85112

86113
if (outputTracer.StatusCode != StatusCode.Success)
87114
{

src/Nethermind/Nethermind.Blockchain/IReadOnlyTxProcessingScope.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System;
5+
using Nethermind.Evm.State;
56
using Nethermind.Evm.TransactionProcessing;
67
using Nethermind.State;
78

@@ -10,6 +11,6 @@ namespace Nethermind.Blockchain;
1011
public interface IReadOnlyTxProcessingScope : IDisposable
1112
{
1213
ITransactionProcessor TransactionProcessor { get; }
13-
IVisitingWorldState WorldState { get; }
14+
IWorldState WorldState { get; }
1415
void Reset() => Dispose();
1516
}

src/Nethermind/Nethermind.Consensus.Test/ReadOnlyTxProcessingScopeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void Test_WhenDispose_ThenStateRootWillReset()
1818
{
1919
ReadOnlyTxProcessingScope env = new ReadOnlyTxProcessingScope(
2020
Substitute.For<ITransactionProcessor>(),
21-
Substitute.For<IVisitingWorldState>());
21+
Substitute.For<IWorldState>());
2222

2323
env.Dispose();
2424

src/Nethermind/Nethermind.Consensus/Processing/AutoReadOnlyTxProcessingEnvFactory.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public class AutoReadOnlyTxProcessingEnvFactory(ILifetimeScope parentLifetime, I
1616
{
1717
public IReadOnlyTxProcessorSource Create()
1818
{
19-
IVisitingWorldState worldState = worldStateManager.CreateResettableWorldState();
19+
IWorldState worldState = worldStateManager.CreateResettableWorldState();
2020
ILifetimeScope childScope = parentLifetime.BeginLifetimeScope((builder) =>
2121
{
2222
builder
23-
.AddSingleton<IVisitingWorldState>(worldState).AddSingleton<IWorldState>(worldState)
23+
.AddSingleton<IWorldState>(worldState)
2424
.AddSingleton<AutoReadOnlyTxProcessingEnv>();
2525
});
2626

@@ -29,18 +29,18 @@ public IReadOnlyTxProcessorSource Create()
2929

3030
public IReadOnlyTxProcessorSource CreateForWarmingUp(IWorldState worldStateToWarmUp)
3131
{
32-
IVisitingWorldState worldState = worldStateManager.CreateWorldStateForWarmingUp(worldStateToWarmUp);
32+
IWorldState worldState = worldStateManager.CreateWorldStateForWarmingUp(worldStateToWarmUp);
3333
ILifetimeScope childScope = parentLifetime.BeginLifetimeScope((builder) =>
3434
{
3535
builder
36-
.AddSingleton<IVisitingWorldState>(worldState).AddSingleton<IWorldState>(worldState)
36+
.AddSingleton<IWorldState>(worldState)
3737
.AddSingleton<AutoReadOnlyTxProcessingEnv>();
3838
});
3939

4040
return childScope.Resolve<AutoReadOnlyTxProcessingEnv>();
4141
}
4242

43-
private class AutoReadOnlyTxProcessingEnv(ITransactionProcessor transactionProcessor, IVisitingWorldState worldState, ILifetimeScope lifetimeScope) : IReadOnlyTxProcessorSource, IDisposable
43+
private class AutoReadOnlyTxProcessingEnv(ITransactionProcessor transactionProcessor, IWorldState worldState, ILifetimeScope lifetimeScope) : IReadOnlyTxProcessorSource, IDisposable
4444
{
4545
public IReadOnlyTxProcessingScope Build(BlockHeader? header)
4646
{

0 commit comments

Comments
 (0)