Skip to content

Commit e62a72f

Browse files
asdacapMarchhill
andauthored
Feature/Worldstate backend (#9089)
* WorldState backend Fix check failure Add small test Whitespace Different setter Slight cleanup Whitespace Rename to scope provider Add code to `IWorldStateScopeProvider`. (#9291) * Add codedb * Add test * Whitespace * use span on set Fix TestworldStateFactory build Rename to update state root Refactor/Nicer IWorldStateScopeProvider interface (#9375) * Reduce interfacae surface area * Fix build * Fix random * Fix randomly failing pyspec test * Whitespace Refactor/Slight cleanup to world state write batch API. (#9400) Remove on account updated callback Fix build Feature/add back `OnAccountUpdated` (#9436) * Add back on account updated * Hint get * Update src/Nethermind/Nethermind.Evm/State/IWorldStateScopeProvider.cs Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> * Fix build --------- Co-authored-by: Lukasz Rozmej <lukasz.rozmej@gmail.com> Feature/worldstate scope provider manager (#9306) * World state manager to return scope provider * Fix build * Fix build Fix selfdestruct Fix selfdestruct not called in some case Fix build Whitespace Fix build Slight per on small contract * Fix build * Whitespace * Update src/Nethermind/Nethermind.State/TrieStoreScopeProvider.cs Co-authored-by: Marc <Marchhill@users.noreply.github.com> * Address comment * Make test more consistent * Extra comment * Fix precompile cache config * Remove missed comment * Address comment --------- Co-authored-by: Marc <Marchhill@users.noreply.github.com>
1 parent e2d0072 commit e62a72f

File tree

65 files changed

+1543
-675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1543
-675
lines changed

src/Nethermind/Nethermind.Benchmark/Store/WorldStateBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Autofac;
77
using BenchmarkDotNet.Attributes;
88
using DotNetty.Common.Utilities;
9+
using Nethermind.Consensus.Processing;
910
using Nethermind.Core;
1011
using Nethermind.Core.Specs;
1112
using Nethermind.Core.Test.Builders;
@@ -44,7 +45,7 @@ public void Setup()
4445
.AddModule(new TestNethermindModule())
4546
.Build();
4647

47-
IWorldState worldState = _globalWorldState = _container.Resolve<IWorldStateManager>().GlobalWorldState;
48+
IWorldState worldState = _globalWorldState = _container.Resolve<IMainProcessingContext>().WorldState;
4849
using var _ = worldState.BeginScope(IWorldState.PreGenesis);
4950

5051
Random rand = new Random(0);

src/Nethermind/Nethermind.Blockchain.Test/Producers/BlockProducerBaseTests.IsProducingBlocks.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Nethermind.Specs;
2626
using Nethermind.Specs.Forks;
2727
using Nethermind.Evm.State;
28+
using Nethermind.State;
2829
using NSubstitute;
2930
using NUnit.Framework;
3031

@@ -40,7 +41,7 @@ public async Task DevBlockProducer_IsProducingBlocks_returns_expected_results()
4041
DevBlockProducer blockProducer = new(
4142
Substitute.For<ITxSource>(),
4243
testRpc.BlockchainProcessor,
43-
testRpc.WorldStateManager.GlobalWorldState,
44+
testRpc.MainWorldState,
4445
testRpc.BlockTree,
4546
testRpc.Timestamper,
4647
testRpc.SpecProvider,
@@ -59,7 +60,7 @@ public async Task TestBlockProducer_IsProducingBlocks_returns_expected_results()
5960
TestBlockProducer blockProducer = new(
6061
Substitute.For<ITxSource>(),
6162
testRpc.BlockchainProcessor,
62-
testRpc.WorldStateManager.GlobalWorldState,
63+
testRpc.MainWorldState,
6364
Substitute.For<ISealer>(),
6465
testRpc.BlockTree,
6566
testRpc.Timestamper,
@@ -81,7 +82,7 @@ public async Task MinedBlockProducer_IsProducingBlocks_returns_expected_results(
8182
testRpc.BlockchainProcessor,
8283
Substitute.For<ISealer>(),
8384
testRpc.BlockTree,
84-
testRpc.WorldStateManager.GlobalWorldState,
85+
testRpc.MainWorldState,
8586
Substitute.For<IGasLimitCalculator>(),
8687
testRpc.Timestamper,
8788
testRpc.SpecProvider,
@@ -123,7 +124,7 @@ public async Task CliqueBlockProducer_IsProducingBlocks_returns_expected_results
123124
CliqueBlockProducer blockProducer = new(
124125
Substitute.For<ITxSource>(),
125126
testRpc.BlockchainProcessor,
126-
testRpc.WorldStateManager.GlobalWorldState,
127+
testRpc.MainWorldState,
127128
testRpc.Timestamper,
128129
Substitute.For<ICryptoRandom>(),
129130
Substitute.For<ISnapshotManager>(),
@@ -152,7 +153,7 @@ public async Task DevBlockProducer_OnGlobalWorldState_IsProducingAndSuggestingBl
152153
DevBlockProducer blockProducer = new(
153154
Substitute.For<ITxSource>(),
154155
testRpc.BlockchainProcessor,
155-
testRpc.WorldStateManager.GlobalWorldState,
156+
new WorldState(testRpc.WorldStateManager.GlobalWorldState, testRpc.LogManager),
156157
testRpc.BlockTree,
157158
testRpc.Timestamper,
158159
testRpc.SpecProvider,

src/Nethermind/Nethermind.Blockchain.Test/TransactionsExecutorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public void BlockProductionTransactionsExecutor_calculates_block_size_using_prop
364364
}
365365
}
366366

367-
public class WorldStateStab() : WorldState(Substitute.For<ITrieStore>(), Substitute.For<IKeyValueStoreWithBatching>(), LimboLogs.Instance), IWorldState
367+
public class WorldStateStab() : WorldState(Substitute.For<IWorldStateScopeProvider>(), LimboLogs.Instance), IWorldState
368368
{
369369
// we cannot mock ref methods
370370
ref readonly UInt256 IWorldState.GetBalance(Address address) => ref UInt256.MaxValue;

src/Nethermind/Nethermind.Clique.Test/CliqueBlockProducerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public On CreateNode(PrivateKey privateKey, bool withGenesisAlreadyProcessed = f
105105
SepoliaSpecProvider testnetSpecProvider = SepoliaSpecProvider.Instance;
106106
IReleaseSpec finalSpec = testnetSpecProvider.GetFinalSpec();
107107

108-
IWorldState stateProvider = container.Resolve<IWorldStateManager>().GlobalWorldState;
108+
IWorldState stateProvider = container.Resolve<IMainProcessingContext>().WorldState;
109109
using (stateProvider.BeginScope(IWorldState.PreGenesis))
110110
{
111111
stateProvider.CreateAccount(TestItem.PrivateKeyD.Address, 100.Ether());
@@ -280,7 +280,7 @@ public On ProcessBadGenesis()
280280

281281
public On ProcessGenesis(PrivateKey nodeKey)
282282
{
283-
using var _ = _containers[nodeKey].Resolve<IWorldStateManager>().GlobalWorldState.BeginScope(IWorldState.PreGenesis);
283+
using var _ = _containers[nodeKey].Resolve<IMainProcessingContext>().WorldState.BeginScope(IWorldState.PreGenesis);
284284
if (_logger.IsInfo) _logger.Info($"SUGGESTING GENESIS ON {nodeKey.Address}");
285285
_blockTrees[nodeKey].SuggestBlock(_genesis).Should().Be(AddBlockResult.Added);
286286
_blockEvents[nodeKey].WaitOne(_timeout);
@@ -289,7 +289,7 @@ public On ProcessGenesis(PrivateKey nodeKey)
289289

290290
public On ProcessGenesis3Validators(PrivateKey nodeKey)
291291
{
292-
using var _ = _containers[nodeKey].Resolve<IWorldStateManager>().GlobalWorldState.BeginScope(IWorldState.PreGenesis);
292+
using var _ = _containers[nodeKey].Resolve<IMainProcessingContext>().WorldState.BeginScope(IWorldState.PreGenesis);
293293
_blockTrees[nodeKey].SuggestBlock(_genesis3Validators);
294294
_blockEvents[nodeKey].WaitOne(_timeout);
295295
return this;

src/Nethermind/Nethermind.Consensus.AuRa/InitializationSteps/StartBlockProducerAuRa.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,16 @@ BlockProducerEnv Create()
259259
{
260260
ReadOnlyBlockTree readOnlyBlockTree = blockTree.AsReadOnly();
261261

262-
IWorldState worldState = worldStateManager.CreateResettableWorldState();
262+
IWorldStateScopeProvider worldStateScopeProvider = worldStateManager.CreateResettableWorldState();
263263
ILifetimeScope innerLifetime = lifetimeScope.BeginLifetimeScope((builder) => builder
264-
.AddSingleton<IWorldState>(worldState)
264+
.AddSingleton<IWorldStateScopeProvider>(worldStateScopeProvider)
265265
.AddSingleton<BlockchainProcessor.Options>(BlockchainProcessor.Options.NoReceipts)
266266
.AddSingleton<IBlockProcessor, ITransactionProcessor, IWorldState>(CreateBlockProcessor)
267267
.AddDecorator<IBlockchainProcessor, OneTimeChainProcessor>());
268268
lifetimeScope.Disposer.AddInstanceForAsyncDisposal(innerLifetime);
269269

270270
IBlockchainProcessor chainProcessor = innerLifetime.Resolve<IBlockchainProcessor>();
271+
IWorldState worldState = innerLifetime.Resolve<IWorldState>();
271272

272273
return new BlockProducerEnv(readOnlyBlockTree, chainProcessor, worldState, CreateTxSourceForProducer());
273274
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,11 @@ public class AutoReadOnlyTxProcessingEnvFactory(ILifetimeScope parentLifetime, I
1515
{
1616
public IReadOnlyTxProcessorSource Create()
1717
{
18-
IWorldState worldState = worldStateManager.CreateResettableWorldState();
18+
IWorldStateScopeProvider worldState = worldStateManager.CreateResettableWorldState();
1919
ILifetimeScope childScope = parentLifetime.BeginLifetimeScope((builder) =>
2020
{
2121
builder
22-
.AddSingleton<IWorldState>(worldState)
23-
.AddSingleton<AutoReadOnlyTxProcessingEnv>();
24-
});
25-
26-
return childScope.Resolve<AutoReadOnlyTxProcessingEnv>();
27-
}
28-
29-
public IReadOnlyTxProcessorSource CreateForWarmingUp(IWorldState worldStateToWarmUp)
30-
{
31-
IWorldState worldState = worldStateManager.CreateWorldStateForWarmingUp(worldStateToWarmUp);
32-
ILifetimeScope childScope = parentLifetime.BeginLifetimeScope((builder) =>
33-
{
34-
builder
35-
.AddSingleton<IWorldState>(worldState)
22+
.AddSingleton<IWorldStateScopeProvider>(worldState)
3623
.AddSingleton<AutoReadOnlyTxProcessingEnv>();
3724
});
3825

src/Nethermind/Nethermind.Consensus/Processing/BlockCachePreWarmer.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,30 @@
2525
namespace Nethermind.Consensus.Processing;
2626

2727
public sealed class BlockCachePreWarmer(
28-
IReadOnlyTxProcessingEnvFactory envFactory,
29-
IWorldState worldStateToWarmup,
28+
PrewarmerEnvFactory envFactory,
3029
int concurrency,
31-
ILogManager logManager,
32-
PreBlockCaches? preBlockCaches = null
30+
NodeStorageCache nodeStorageCache,
31+
PreBlockCaches preBlockCaches,
32+
ILogManager logManager
3333
) : IBlockCachePreWarmer
3434
{
3535
private int _concurrencyLevel = (concurrency == 0 ? Math.Min(Environment.ProcessorCount - 1, 16) : concurrency);
36-
private readonly ObjectPool<IReadOnlyTxProcessorSource> _envPool = new DefaultObjectPool<IReadOnlyTxProcessorSource>(new ReadOnlyTxProcessingEnvPooledObjectPolicy(envFactory, worldStateToWarmup), Environment.ProcessorCount * 2);
36+
private readonly ObjectPool<IReadOnlyTxProcessorSource> _envPool = new DefaultObjectPool<IReadOnlyTxProcessorSource>(new ReadOnlyTxProcessingEnvPooledObjectPolicy(envFactory, preBlockCaches), Environment.ProcessorCount * 2);
3737
private readonly ILogger _logger = logManager.GetClassLogger<BlockCachePreWarmer>();
3838
private BlockStateSource? _currentBlockState = null;
3939

4040
public BlockCachePreWarmer(
41-
IReadOnlyTxProcessingEnvFactory envFactory,
42-
IWorldState worldStateToWarmup,
41+
PrewarmerEnvFactory envFactory,
4342
IBlocksConfig blocksConfig,
44-
ILogManager logManager,
45-
PreBlockCaches? preBlockCaches = null
43+
NodeStorageCache nodeStorageCache,
44+
PreBlockCaches preBlockCaches,
45+
ILogManager logManager
4646
) : this(
4747
envFactory,
48-
worldStateToWarmup,
4948
blocksConfig.PreWarmStateConcurrency,
50-
logManager,
51-
preBlockCaches)
49+
nodeStorageCache,
50+
preBlockCaches,
51+
logManager)
5252
{
5353
}
5454

@@ -58,6 +58,8 @@ public Task PreWarmCaches(Block suggestedBlock, BlockHeader? parent, IReleaseSpe
5858
{
5959
_currentBlockState = new(this, suggestedBlock, parent, spec);
6060
CacheType result = preBlockCaches.ClearCaches();
61+
result |= nodeStorageCache.ClearCaches() ? CacheType.Rlp : CacheType.None;
62+
nodeStorageCache.Enabled = true;
6163
if (result != default)
6264
{
6365
if (_logger.IsWarn) _logger.Warn($"Caches {result} are not empty. Clearing them.");
@@ -82,8 +84,10 @@ public CacheType ClearCaches()
8284
{
8385
if (_logger.IsDebug) _logger.Debug("Clearing caches");
8486
CacheType cachesCleared = preBlockCaches?.ClearCaches() ?? default;
85-
if (_logger.IsDebug) _logger.Debug($"Cleared caches: {cachesCleared}");
8687

88+
nodeStorageCache.Enabled = false;
89+
cachesCleared |= nodeStorageCache.ClearCaches() ? CacheType.Rlp : CacheType.None;
90+
if (_logger.IsDebug) _logger.Debug($"Cleared caches: {cachesCleared}");
8791
return cachesCleared;
8892
}
8993

@@ -373,9 +377,9 @@ public void Dispose()
373377
private static void DisposeThreadState(AddressWarmingState state) => state.Dispose();
374378
}
375379

376-
private class ReadOnlyTxProcessingEnvPooledObjectPolicy(IReadOnlyTxProcessingEnvFactory envFactory, IWorldState worldStateToWarmUp) : IPooledObjectPolicy<IReadOnlyTxProcessorSource>
380+
private class ReadOnlyTxProcessingEnvPooledObjectPolicy(PrewarmerEnvFactory envFactory, PreBlockCaches preBlockCaches) : IPooledObjectPolicy<IReadOnlyTxProcessorSource>
377381
{
378-
public IReadOnlyTxProcessorSource Create() => envFactory.CreateForWarmingUp(worldStateToWarmUp);
382+
public IReadOnlyTxProcessorSource Create() => envFactory.Create(preBlockCaches);
379383
public bool Return(IReadOnlyTxProcessorSource obj) => true;
380384
}
381385

src/Nethermind/Nethermind.Consensus/Processing/IReadOnlyTxProcessingEnvFactory.cs

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

44
using Nethermind.Blockchain;
5-
using Nethermind.Evm.State;
65

76
namespace Nethermind.Consensus.Processing;
87

98
public interface IReadOnlyTxProcessingEnvFactory
109
{
1110
public IReadOnlyTxProcessorSource Create();
12-
public IReadOnlyTxProcessorSource CreateForWarmingUp(IWorldState worldState);
1311
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
using Autofac;
5+
using Nethermind.Blockchain;
6+
using Nethermind.Core;
7+
using Nethermind.Evm.State;
8+
using Nethermind.State;
9+
10+
namespace Nethermind.Consensus.Processing;
11+
12+
public class PrewarmerEnvFactory(IWorldStateManager worldStateManager, ILifetimeScope parentLifetime)
13+
{
14+
public IReadOnlyTxProcessorSource Create(PreBlockCaches preBlockCaches)
15+
{
16+
var worldState = new PrewarmerScopeProvider(
17+
worldStateManager.CreateResettableWorldState(),
18+
preBlockCaches,
19+
populatePreBlockCache: true
20+
);
21+
22+
ILifetimeScope childScope = parentLifetime.BeginLifetimeScope((builder) =>
23+
{
24+
builder
25+
.AddSingleton<IWorldStateScopeProvider>(worldState)
26+
.AddSingleton<AutoReadOnlyTxProcessingEnvFactory.AutoReadOnlyTxProcessingEnv>();
27+
});
28+
29+
return childScope.Resolve<AutoReadOnlyTxProcessingEnvFactory.AutoReadOnlyTxProcessingEnv>();
30+
}
31+
}

src/Nethermind/Nethermind.Consensus/Producers/BlockProducerEnvFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected virtual ContainerBuilder ConfigureBuilder(ContainerBuilder builder) =>
3131

3232
public IBlockProducerEnv Create()
3333
{
34-
IWorldState worldState = worldStateManager.CreateResettableWorldState();
34+
IWorldStateScopeProvider worldState = worldStateManager.CreateResettableWorldState();
3535
ILifetimeScope lifetimeScope = rootLifetime.BeginLifetimeScope(builder =>
3636
ConfigureBuilder(builder)
3737
.AddScoped(worldState));

0 commit comments

Comments
 (0)