Skip to content

Commit

Permalink
Some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap committed Dec 5, 2023
1 parent d726e0c commit 2aff001
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
10 changes: 4 additions & 6 deletions src/Nethermind/Nethermind.Core.Test/Blockchain/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TestBlockchain : IDisposable
public IReceiptStorage ReceiptStorage { get; set; } = null!;
public ITxPool TxPool { get; set; } = null!;
public IDb CodeDb => DbProvider.CodeDb;
public IWorldStateManager _readOnlyWorldStateManager = null!;
public IWorldStateManager WorldStateManager { get; set; } = null!;
public IBlockProcessor BlockProcessor { get; set; } = null!;
public IBeaconBlockRootHandler BeaconBlockRootHandler { get; set; } = null!;
public IBlockchainProcessor BlockchainProcessor { get; set; } = null!;
Expand Down Expand Up @@ -141,7 +141,7 @@ protected virtual async Task<TestBlockchain> Build(ISpecProvider? specProvider =
State.CommitTree(0);

ReadOnlyTrieStore = TrieStore.AsReadOnly(StateDb);
_readOnlyWorldStateManager = new ReadOnlyWorldStateManager(DbProvider, ReadOnlyTrieStore, LimboLogs.Instance);
WorldStateManager = new WorldStateManager(State, TrieStore, DbProvider, LimboLogs.Instance);
StateReader = new StateReader(ReadOnlyTrieStore, CodeDb, LogManager);

BlockTree = Builders.Build.A.BlockTree()
Expand All @@ -158,9 +158,7 @@ protected virtual async Task<TestBlockchain> Build(ISpecProvider? specProvider =

NonceManager = new NonceManager(chainHeadInfoProvider.AccountStateProvider);

WorldStateManager stateManager = new(State, TrieStore, DbProvider, ReadOnlyTrieStore, LimboLogs.Instance);

_trieStoreWatcher = new TrieStoreBoundaryWatcher(stateManager, BlockTree, LogManager);
_trieStoreWatcher = new TrieStoreBoundaryWatcher(WorldStateManager, BlockTree, LogManager);

ReceiptStorage = new InMemoryReceiptStorage();
VirtualMachine virtualMachine = new(new BlockhashProvider(BlockTree, LogManager), SpecProvider, LogManager);
Expand Down Expand Up @@ -252,7 +250,7 @@ protected virtual IBlockProducer CreateTestBlockProducer(TxPoolTxSource txPoolTx
BlocksConfig blocksConfig = new();

BlockProducerEnvFactory blockProducerEnvFactory = new(
_readOnlyWorldStateManager,
WorldStateManager,
BlockTree,
SpecProvider,
BlockValidator,
Expand Down
1 change: 0 additions & 1 deletion src/Nethermind/Nethermind.Init/InitializeStateDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public Task Execute(CancellationToken cancellationToken)
worldState,
trieStore,
getApi.DbProvider,
trieStore.AsReadOnly(cachedStateDb),
getApi.LogManager);

// TODO: Don't forget this
Expand Down
65 changes: 65 additions & 0 deletions src/Nethermind/Nethermind.State.Test/WorldStateManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using FluentAssertions;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.Specs;
using Nethermind.State;
using Nethermind.Trie.Pruning;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Store.Test;

public class WorldStateManagerTests
{
[Test]
public void ShouldProxyGlobalWorldState()
{
IWorldState worldState = Substitute.For<IWorldState>();
ITrieStore trieStore = Substitute.For<ITrieStore>();
IDbProvider dbProvider = TestMemDbProvider.Init();
WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance);

worldStateManager.GlobalWorldState.Should().Be(worldState);
}

[Test]
public void ShouldProxyReorgBoundaryEvent()
{
IWorldState worldState = Substitute.For<IWorldState>();
ITrieStore trieStore = Substitute.For<ITrieStore>();
IDbProvider dbProvider = TestMemDbProvider.Init();
WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance);

bool gotEvent = false;
worldStateManager.ReorgBoundaryReached += (sender, reached) => gotEvent = true;
trieStore.ReorgBoundaryReached += Raise.EventWith<ReorgBoundaryReached>(new ReorgBoundaryReached(1));

gotEvent.Should().BeTrue();
}

[Test]
public void ShouldCreateTemporaryWorldState_AndCanReset()
{
IWorldState worldState = Substitute.For<IWorldState>();
ITrieStore trieStore = Substitute.For<ITrieStore>();
IDbProvider dbProvider = TestMemDbProvider.Init();
WorldStateManager worldStateManager = new WorldStateManager(worldState, trieStore, dbProvider, LimboLogs.Instance);

(IWorldState tempWorldState, IStateReader stateReader, Action reset) = worldStateManager.CreateResettableWorldState();

byte[] code = new byte[] { 1 };
Hash256 codeHash = Keccak.Compute(code);
tempWorldState.CreateAccount(Address.Zero, 0, 0);
tempWorldState.InsertCode(Address.Zero, code, MainnetSpecProvider.Instance.GenesisSpec);

stateReader.GetCode(codeHash).Should().NotBeNull();
reset();
stateReader.GetCode(codeHash).Should().BeNull();
}
}
9 changes: 6 additions & 3 deletions src/Nethermind/Nethermind.State/ReadOnlyWorldStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ ILogManager logManager
public (IWorldState, IStateReader, Action) CreateResettableWorldState()
{
ReadOnlyDbProvider readOnlyDbProvider = _dbProvider.AsReadOnly(false);
IKeyValueStore codeDb = readOnlyDbProvider.GetDb<IDb>(DbNames.Code).AsReadOnly(true);
ReadOnlyDb codeDb = readOnlyDbProvider.GetDb<IDb>(DbNames.Code).AsReadOnly(true);
return (
new WorldState(_readOnlyTrieStore, codeDb, _logManager),
new StateReader(_readOnlyTrieStore, codeDb, _logManager),
readOnlyDbProvider.ClearTempChanges
);
() =>
{
readOnlyDbProvider.ClearTempChanges();
codeDb.ClearTempChanges();
});
}

public virtual event EventHandler<ReorgBoundaryReached>? ReorgBoundaryReached
Expand Down
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.State/WorldStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ public WorldStateManager(
IWorldState worldState,
ITrieStore trieStore,
IDbProvider dbProvider,
IReadOnlyTrieStore? readOnlyTrieStore,
ILogManager logManager
) : base(dbProvider, readOnlyTrieStore, logManager)
) : base(dbProvider, trieStore.AsReadOnly(), logManager)
{
_worldState = worldState;
_trieStore = trieStore;
Expand Down

0 comments on commit 2aff001

Please sign in to comment.