Skip to content

Commit

Permalink
Perf/dont redownload downloaded code (#6873)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Mar 28, 2024
1 parent 3b70f9d commit 7059b45
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Test.Builders;
using Nethermind.Db;
using Nethermind.Logging;
using Nethermind.State;
using Nethermind.State.Proofs;
using Nethermind.State.Snap;
using Nethermind.Synchronization.SnapSync;
using Nethermind.Trie;
using Nethermind.Trie.Pruning;
Expand Down Expand Up @@ -264,6 +267,55 @@ public void MissingAccountFromRange()
Assert.IsFalse(db.KeyExists(rootHash));
}

[Test]
public void Will_not_redownload_persisted_code()
{
MemDb db = new();
MemDb codeDb = new();
DbProvider dbProvider = new();
dbProvider.RegisterDb(DbNames.State, db);
dbProvider.RegisterDb(DbNames.Code, codeDb);

BlockTree tree = Build.A.BlockTree().OfChainLength(5).TestObject;
using ProgressTracker progressTracker = new(tree, dbProvider.GetDb<IDb>(DbNames.State), LimboLogs.Instance,
accountRangePartitionCount: 1);
SnapProvider snapProvider = CreateSnapProvider(progressTracker, dbProvider);

PathWithAccount[] accountsWithPath =
[
new PathWithAccount(new Hash256("0000000000000000000000000000000000000000000000000000000001112345"),
new Account(0, 0, Keccak.EmptyTreeHash, TestItem.Keccaks[0])),
new PathWithAccount(new Hash256("0000000000000000000000000000000000000000000000000000000001113456"),
new Account(0, 0, Keccak.EmptyTreeHash, TestItem.Keccaks[1])),
new PathWithAccount(new Hash256("0000000000000000000000000000000000000000000000000000000001114567"),
new Account(0, 0, Keccak.EmptyTreeHash, TestItem.Keccaks[2])),
new PathWithAccount(new Hash256("0000000000000000000000000000000000000000000000000000000001123456"),
new Account(0, 0, Keccak.EmptyTreeHash, TestItem.Keccaks[3])),
new PathWithAccount(new Hash256("0000000000000000000000000000000000000000000000000000000001123457"),
new Account(0, 0, Keccak.EmptyTreeHash, TestItem.Keccaks[4]))
];

codeDb[TestItem.Keccaks[1].Bytes] = [1];
codeDb[TestItem.Keccaks[2].Bytes] = [1];

StateTree stateTree = new StateTree();
foreach (PathWithAccount pathWithAccount in accountsWithPath)
{
stateTree.Set(pathWithAccount.Path, pathWithAccount.Account);
}

stateTree.UpdateRootHash();

snapProvider.AddAccountRange(1,
stateTree.RootHash,
accountsWithPath[0].Path,
accountsWithPath);

progressTracker.IsFinished(out SnapSyncBatch nextRequest).Should().BeFalse();
progressTracker.IsFinished(out nextRequest).Should().BeFalse();
nextRequest.CodesRequest.Count.Should().Be(3);
}

private SnapProvider CreateSnapProvider(ProgressTracker progressTracker, IDbProvider dbProvider)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private void ChangeState(SyncFeedState newState)

public void Activate() => ChangeState(SyncFeedState.Active);

public void Finish()
public virtual void Finish()
{
ChangeState(SyncFeedState.Finished);
GC.Collect(2, GCCollectionMode.Aggressive, true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public interface ISnapProvider

bool IsSnapGetRangesFinished();
void UpdatePivot();
void Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using Microsoft.Extensions.ObjectPool;
using Nethermind.Core;
using Nethermind.Core.Caching;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
Expand All @@ -29,6 +30,9 @@ public class SnapProvider : ISnapProvider

private readonly ProgressTracker _progressTracker;

// This is actually close to 97% effective.
private readonly LruKeyCache<ValueHash256> _codeExistKeyCache = new(1024 * 16, "");

public SnapProvider(ProgressTracker progressTracker, IDb codeDb, INodeStorage nodeStorage, ILogManager logManager)
{
_codeDb = codeDb ?? throw new ArgumentNullException(nameof(codeDb));
Expand Down Expand Up @@ -88,7 +92,18 @@ public AddRangeResult AddAccountRange(long blockNumber, in ValueHash256 expected
_progressTracker.EnqueueAccountStorage(item);
}

_progressTracker.EnqueueCodeHashes(CollectionsMarshal.AsSpan(codeHashes));

using ArrayPoolList<ValueHash256> filteredCodeHashes = codeHashes.AsParallel().Where((code) =>
{
if (_codeExistKeyCache.Get(code)) return false;
bool exist = _codeDb.KeyExists(code.Bytes);
if (exist) _codeExistKeyCache.Set(code);
return !exist;
}).ToPooledList(codeHashes.Count);

_progressTracker.EnqueueCodeHashes(filteredCodeHashes.AsSpan());

_progressTracker.UpdateAccountRangePartitionProgress(effectiveHashLimit, accounts[^1].Path, moreChildrenToRight);
}
else if (result == AddRangeResult.MissingRootHashInProofs)
Expand Down Expand Up @@ -326,6 +341,11 @@ public void UpdatePivot()
_progressTracker.UpdatePivot();
}

public void Dispose()
{
_codeExistKeyCache.Clear();
}

private class TrieStorePoolPolicy : IPooledObjectPolicy<ITrieStore>
{
private readonly INodeStorage _stateDb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ public override void SyncModeSelectorOnChanged(SyncMode current)
}
}

public override void Finish()
{
_snapProvider.Dispose();
base.Finish();
}

public override bool IsFinished => _snapProvider.IsSnapGetRangesFinished();
}
}

0 comments on commit 7059b45

Please sign in to comment.