Skip to content

Commit

Permalink
Fix/snap is serving with hash in test (#8116)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Jan 28, 2025
1 parent 4f7e387 commit c0dd15e
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.State;

namespace Nethermind.Blockchain.Utils;

Expand Down
4 changes: 3 additions & 1 deletion src/Nethermind/Nethermind.Init/PruningTrieStateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.FullPruning;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Blockchain.Utils;
using Nethermind.Config;
using Nethermind.Core;
using Nethermind.Core.Crypto;
Expand Down Expand Up @@ -177,7 +178,8 @@ ILogManager logManager
worldState,
trieStore,
dbProvider,
logManager);
logManager,
new LastNStateRootTracker(blockTree, 128));

// NOTE: Don't forget this! Very important!
TrieStoreBoundaryWatcher trieStoreBoundaryWatcher = new(stateManager, blockTree!, logManager);
Expand Down
10 changes: 1 addition & 9 deletions src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,6 @@ private async Task InitPeer()
ProtocolValidator protocolValidator = new(_api.NodeStatsManager!, _api.BlockTree, forkInfo, _api.LogManager);
PooledTxsRequestor pooledTxsRequestor = new(_api.TxPool!, _api.Config<ITxPoolConfig>(), _api.SpecProvider);

ISnapServer? snapServer = null;
if (_syncConfig.SnapServingEnabled == true)
{
// TODO: Add a proper config for the state persistence depth.
snapServer = new LastNRootSnapServer(_api.WorldStateManager!.SnapServer!, new LastNStateRootTracker(_api.BlockTree, 128));

}

_api.ProtocolsManager = new ProtocolsManager(
_api.SyncPeerPool!,
syncServer,
Expand All @@ -383,7 +375,7 @@ private async Task InitPeer()
forkInfo,
_api.GossipPolicy,
_networkConfig,
snapServer,
_api.WorldStateManager!,
_api.LogManager,
_api.TxGossipPolicy);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using Nethermind.Network.P2P.Subprotocols.Eth.V62.Messages;
using Nethermind.Network.Rlpx;
using Nethermind.Specs;
using Nethermind.State;
using Nethermind.State.SnapServer;
using Nethermind.Stats;
using Nethermind.Stats.Model;
Expand Down Expand Up @@ -116,7 +117,7 @@ public Context()
forkInfo,
_gossipPolicy,
new NetworkConfig(),
Substitute.For<ISnapServer>(),
Substitute.For<IWorldStateManager>(),
LimboLogs.Instance);

_serializer.Register(new HelloMessageSerializer());
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Network/ProtocolsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
using Nethermind.Network.P2P.Subprotocols.NodeData;
using Nethermind.Network.P2P.Subprotocols.Snap;
using Nethermind.Network.Rlpx;
using Nethermind.State;
using Nethermind.State.SnapServer;
using Nethermind.Stats;
using Nethermind.Stats.Model;
using Nethermind.Synchronization;
using Nethermind.Synchronization.Peers;
using Nethermind.Synchronization.SnapSync;
using Nethermind.TxPool;
using ShouldGossip = Nethermind.TxPool.ShouldGossip;

Expand Down Expand Up @@ -89,7 +89,7 @@ public ProtocolsManager(
ForkInfo forkInfo,
IGossipPolicy gossipPolicy,
INetworkConfig networkConfig,
ISnapServer? snapServer,
IWorldStateManager worldStateManager,
ILogManager logManager,
ITxGossipPolicy? transactionsGossipPolicy = null)
{
Expand All @@ -108,7 +108,7 @@ public ProtocolsManager(
_gossipPolicy = gossipPolicy ?? throw new ArgumentNullException(nameof(gossipPolicy));
_txGossipPolicy = transactionsGossipPolicy ?? ShouldGossip.Instance;
_logManager = logManager ?? throw new ArgumentNullException(nameof(logManager));
_snapServer = snapServer;
_snapServer = worldStateManager.SnapServer;
_logger = _logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));

if (networkConfig.ClientIdMatcher is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Nethermind.Core.Crypto;

namespace Nethermind.Blockchain.Utils;
namespace Nethermind.State;

public interface ILastNStateRootTracker
{
Expand Down
6 changes: 4 additions & 2 deletions src/Nethermind/Nethermind.State/SnapServer/SnapServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ public class SnapServer : ISnapServer
private readonly ReadFlags _optimizedReadFlags = ReadFlags.HintCacheMiss;

private readonly AccountDecoder _decoder = new AccountDecoder();
private readonly ILastNStateRootTracker? _lastNStateRootTracker;

private const long HardResponseByteLimit = 2000000;
private const int HardResponseNodeLimit = 100000;

public SnapServer(IReadOnlyTrieStore trieStore, IReadOnlyKeyValueStore codeDb, IStateReader stateReader, ILogManager logManager)
public SnapServer(IReadOnlyTrieStore trieStore, IReadOnlyKeyValueStore codeDb, IStateReader stateReader, ILogManager logManager, ILastNStateRootTracker? lastNStateRootTracker = null)
{
_store = trieStore ?? throw new ArgumentNullException(nameof(trieStore));
_codeDb = codeDb ?? throw new ArgumentNullException(nameof(codeDb));
_stateReader = stateReader;
_lastNStateRootTracker = lastNStateRootTracker;
_logManager = logManager ?? throw new ArgumentNullException(nameof(logManager));
_logger = logManager.GetClassLogger();

Expand All @@ -66,7 +68,7 @@ public SnapServer(IReadOnlyTrieStore trieStore, IReadOnlyKeyValueStore codeDb, I

private bool IsRootMissing(in ValueHash256 stateRoot)
{
return !_stateReader.HasStateForRoot(stateRoot.ToCommitment());
return !_stateReader.HasStateForRoot(stateRoot.ToCommitment()) || (_lastNStateRootTracker?.HasStateRoot(stateRoot) == false);
}

public IOwnedReadOnlyList<byte[]>? GetTrieNodes(IReadOnlyList<PathGroup> pathSet, in ValueHash256 rootHash, CancellationToken cancellationToken)
Expand Down
7 changes: 5 additions & 2 deletions src/Nethermind/Nethermind.State/WorldStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public class WorldStateManager : IWorldStateManager
private readonly ReadOnlyDb _readaOnlyCodeCb;
private readonly IDbProvider _dbProvider;
private readonly BlockingVerifyTrie? _blockingVerifyTrie;
private readonly ILastNStateRootTracker _lastNStateRootTracker;

public WorldStateManager(
IWorldState worldState,
ITrieStore trieStore,
IDbProvider dbProvider,
ILogManager logManager
ILogManager logManager,
ILastNStateRootTracker lastNStateRootTracker = null
)
{
_dbProvider = dbProvider;
Expand All @@ -43,6 +45,7 @@ ILogManager logManager
_readaOnlyCodeCb = readOnlyDbProvider.GetDb<IDb>(DbNames.Code).AsReadOnly(true);
GlobalStateReader = new StateReader(_readOnlyTrieStore, _readaOnlyCodeCb, _logManager);
_blockingVerifyTrie = new BlockingVerifyTrie(trieStore, GlobalStateReader, _readaOnlyCodeCb!, logManager);
_lastNStateRootTracker = lastNStateRootTracker;
}

public static WorldStateManager CreateForTest(IDbProvider dbProvider, ILogManager logManager)
Expand Down Expand Up @@ -78,7 +81,7 @@ public void InitializeNetwork(ITrieNodeRecovery<IReadOnlyList<Hash256>> hashReco

public IStateReader GlobalStateReader { get; }

public ISnapServer? SnapServer => new SnapServer.SnapServer(_readOnlyTrieStore, _readaOnlyCodeCb, GlobalStateReader, _logManager);
public ISnapServer? SnapServer => _trieStore.Scheme == INodeStorage.KeyScheme.Hash ? null : new SnapServer.SnapServer(_readOnlyTrieStore, _readaOnlyCodeCb, GlobalStateReader, _logManager, _lastNStateRootTracker);

public IWorldState CreateResettableWorldState(IWorldState? forWarmup = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ private class Context
internal MemDb ClientStateDb { get; init; } = null!;
}

private Context CreateContext(IStateReader? stateRootTracker = null)
private Context CreateContext(IStateReader? stateRootTracker = null, ILastNStateRootTracker? lastNStateRootTracker = null)
{
MemDb stateDbServer = new();
MemDb codeDbServer = new();
TrieStore store = new(stateDbServer, LimboLogs.Instance);
StateTree tree = new(store, LimboLogs.Instance);
SnapServer server = new(store.AsReadOnly(), codeDbServer, stateRootTracker ?? CreateConstantStateRootTracker(true), LimboLogs.Instance);
SnapServer server = new(store.AsReadOnly(), codeDbServer, stateRootTracker ?? CreateConstantStateRootTracker(true), LimboLogs.Instance, lastNStateRootTracker);

MemDb clientStateDb = new();
using ProgressTracker progressTracker = new(clientStateDb, new TestSyncConfig(), new StateSyncPivot(null!, new TestSyncConfig(), LimboLogs.Instance), LimboLogs.Instance);
Expand Down Expand Up @@ -119,10 +119,18 @@ public void TestGetTrieNode_Storage_Root()
result.Count.Should().Be(1);
}

[Test]
public void TestNoState()
[TestCase(true)]
[TestCase(false)]
public void TestNoState(bool withLastNStateTracker)
{
Context context = CreateContext(stateRootTracker: CreateConstantStateRootTracker(false));
ILastNStateRootTracker? lastNStateTracker = null;
if (withLastNStateTracker)
{
lastNStateTracker = Substitute.For<ILastNStateRootTracker>();
lastNStateTracker.HasStateRoot(Arg.Any<Hash256>()).Returns(false);
}

Context context = CreateContext(stateRootTracker: CreateConstantStateRootTracker(withLastNStateTracker), lastNStateRootTracker: lastNStateTracker);

(IOwnedReadOnlyList<PathWithAccount> accounts, IOwnedReadOnlyList<byte[]> accountProofs) =
context.Server.GetAccountRanges(context.Tree.RootHash, Keccak.Zero, Keccak.MaxValue, 4000, CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ IContainer CreateNode()
Hash256 FillStorage(IContainer server)
{
IWorldState mainWorldState = server.Resolve<MainBlockProcessingContext>().WorldState;
IBlockTree blockTree = server.Resolve<IBlockTree>();
mainWorldState.StateRoot = Keccak.EmptyTreeHash;

for (int i = 0; i < 100; i++)
Expand All @@ -180,6 +181,12 @@ Hash256 FillStorage(IContainer server)
mainWorldState.Commit(Cancun.Instance);
mainWorldState.CommitTree(1);

// Snap server check for the past 128 block in blocktree explicitly to pass hive test.
// So need to simulate block processing..
Block block = Build.A.Block.WithStateRoot(mainWorldState.StateRoot).WithParent(blockTree.Head!).TestObject;
blockTree.SuggestBlock(block).Should().Be(AddBlockResult.Added);
blockTree.UpdateMainChain([block], true);

return mainWorldState.StateRoot;
}

Expand Down

This file was deleted.

0 comments on commit c0dd15e

Please sign in to comment.