Skip to content

Commit

Permalink
Fix/snap sync edge cases (#7708)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdacap authored Nov 5, 2024
1 parent 8bc8d8f commit 72b3ee7
Show file tree
Hide file tree
Showing 22 changed files with 443 additions and 627 deletions.
5 changes: 2 additions & 3 deletions src/Nethermind/Nethermind.Api/IBasicApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ public ContainerBuilder ConfigureContainerBuilderFromBasicApi(ContainerBuilder b
{
builder
.AddPropertiesFrom<IBasicApi>(this)
.AddSingleton(ConfigProvider.GetConfig<ISyncConfig>());

DbProvider!.ConfigureServiceCollection(builder);
.AddSingleton(ConfigProvider.GetConfig<ISyncConfig>())
.AddModule(new DbModule());

return builder;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Nethermind/Nethermind.Core/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ public static ContainerBuilder AddModule(this ContainerBuilder builder, IModule

return builder;
}

public static ContainerBuilder Map<TFrom, TTo>(this ContainerBuilder builder, Func<TFrom, TTo> mapper) where TFrom : notnull where TTo : notnull
{
builder.Register(mapper)
.As<TTo>()
.ExternallyOwned();

return builder;
}
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Nethermind/Nethermind.Core/Crypto/Hash256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json.Serialization;

using Nethermind.Core.Extensions;
using Nethermind.Int256;
using Nethermind.Serialization.Json;

namespace Nethermind.Core.Crypto
Expand Down Expand Up @@ -58,6 +59,10 @@ public ValueHash256(ReadOnlySpan<byte> bytes)
_bytes = Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(bytes));
}

public ValueHash256(UInt256 uint256, bool isBigEndian = true) : this(isBigEndian ? uint256.ToBigEndian() : uint256.ToLittleEndian())
{
}

public override bool Equals(object? obj) => obj is ValueHash256 keccak && Equals(keccak);

public bool Equals(ValueHash256 other) => _bytes.Equals(other._bytes);
Expand Down Expand Up @@ -112,6 +117,11 @@ public Hash256 ToCommitment()
public static bool operator >=(in ValueHash256 left, in ValueHash256 right) => left.CompareTo(in right) >= 0;
public static bool operator <=(in ValueHash256 left, in ValueHash256 right) => left.CompareTo(in right) <= 0;
public static implicit operator Hash256(in ValueHash256 keccak) => new(keccak);

public UInt256 ToUInt256(bool isBigEndian = true)
{
return new UInt256(Bytes, isBigEndian: isBigEndian);
}
}

public readonly struct Hash256AsKey(Hash256 key) : IEquatable<Hash256AsKey>
Expand Down
79 changes: 79 additions & 0 deletions src/Nethermind/Nethermind.Db/DbModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Autofac;
using Nethermind.Core;

namespace Nethermind.Db;

public class DbModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.AddSingleton(this);

// TODO: Have hooks that automatically get these
string[] dbNames = [
DbNames.State,
DbNames.Code,
DbNames.Metadata,
DbNames.Blocks,
DbNames.Headers,
DbNames.BlockInfos,
DbNames.BadBlocks,
DbNames.Bloom,
DbNames.Metadata,
];
foreach (string dbName in dbNames)
{
ConfigureDb(builder, dbName);
}

ConfigureColumnDb<ReceiptsColumns>(builder, DbNames.Receipts);
}

private static void ConfigureDb(ContainerBuilder builder, string dbName)
{
builder.Register((ctx) =>
{
IDbProvider dbProvider = ctx.Resolve<IDbProvider>();
IDb db = dbProvider.GetDb<IDb>(dbName);
return db;
})
.ExternallyOwned()
.Named<IDb>(dbName)
.Named<IDbMeta>(dbName);

builder.Register((ctx) =>
{
IDbProvider dbProvider = ctx.Resolve<IDbProvider>();
IDb db = dbProvider.GetDb<IDb>(dbName);
return db as ITunableDb ?? new NoopTunableDb();
})
.ExternallyOwned()
.Named<ITunableDb>(dbName);
}


private static void ConfigureColumnDb<TColumn>(ContainerBuilder builder, string dbName)
{
builder.Register((ctx) =>
{
IDbProvider dbProvider = ctx.Resolve<IDbProvider>();
IColumnsDb<TColumn> db = dbProvider.GetColumnDb<TColumn>(dbName);
return db;
})
.ExternallyOwned()
.As<IColumnsDb<TColumn>>();

builder.Register((ctx) =>
{
IDbProvider dbProvider = ctx.Resolve<IDbProvider>();
IColumnsDb<TColumn> db = dbProvider.GetColumnDb<TColumn>(dbName);
return db as ITunableDb ?? new NoopTunableDb();
})
.ExternallyOwned()
.Named<ITunableDb>(dbName);
}
}
29 changes: 0 additions & 29 deletions src/Nethermind/Nethermind.Db/IDbProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,5 @@ public interface IDbProvider : IDisposable
void RegisterDb<T>(string dbName, T db) where T : class, IDb;
void RegisterColumnDb<T>(string dbName, IColumnsDb<T> db);
IEnumerable<KeyValuePair<string, IDbMeta>> GetAllDbMeta();

void ConfigureServiceCollection(ContainerBuilder sc)
{
sc.AddSingleton(this);

// TODO: Have hooks that automatically get these
string[] dbNames = [
DbNames.State,
DbNames.Code,
DbNames.Metadata,
DbNames.Blocks,
DbNames.Headers,
DbNames.BlockInfos,
DbNames.BadBlocks,
DbNames.Bloom,
DbNames.Metadata,
];
foreach (string dbName in dbNames)
{
var db = GetDb<IDb>(dbName);
sc.AddKeyedSingleton<IDb>(dbName, db);
sc.AddKeyedSingleton<IDbMeta>(dbName, db);
sc.AddKeyedSingleton<ITunableDb>(dbName, db as ITunableDb ?? new NoopTunableDb());
}

IColumnsDb<ReceiptsColumns> receiptColumnDb = GetColumnDb<ReceiptsColumns>(DbNames.Receipts);
sc.AddSingleton<IColumnsDb<ReceiptsColumns>>(receiptColumnDb);
sc.AddKeyedSingleton<ITunableDb>(DbNames.Receipts, receiptColumnDb as ITunableDb ?? new NoopTunableDb());
}
}
}
Loading

0 comments on commit 72b3ee7

Please sign in to comment.