Skip to content

Commit 0797b7b

Browse files
committed
FlatDB squashed commit
1 parent 008a926 commit 0797b7b

File tree

80 files changed

+9737
-561
lines changed

Some content is hidden

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

80 files changed

+9737
-561
lines changed

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<PackageVersion Include="Open.NAT.Core" Version="2.1.0.5" />
7373
<PackageVersion Include="PierTwo.Lantern.Discv5.WireProtocol" Version="1.0.0-preview.6" />
7474
<PackageVersion Include="Polly" Version="8.6.4" />
75+
<PackageVersion Include="prometheus-net" Version="8.2.1" />
7576
<PackageVersion Include="prometheus-net.AspNetCore" Version="8.2.1" />
7677
<PackageVersion Include="Prometheus.Client.MetricPusher" Version="3.3.0" />
7778
<PackageVersion Include="Pyroscope" Version="0.13.0" />
@@ -89,4 +90,4 @@
8990
<PackageVersion Include="Websocket.Client" Version="5.3.0" />
9091
<PackageVersion Include="ZstdSharp.Port" Version="0.8.6" />
9192
</ItemGroup>
92-
</Project>
93+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
using Nethermind.Core;
5+
using Nethermind.Core.Crypto;
6+
using Nethermind.State.Flat;
7+
8+
namespace Nethermind.Blockchain;
9+
10+
public class CanonicalStateRootFinder(IBlockTree blockTree): ICanonicalStateRootFinder
11+
{
12+
public Hash256? GetCanonicalStateRootAtBlock(long blockNumber)
13+
{
14+
BlockHeader? header = blockTree.FindHeader(blockNumber, BlockTreeLookupOptions.RequireCanonical);
15+
return header?.StateRoot;
16+
}
17+
}

src/Nethermind/Nethermind.Consensus/Processing/PrewarmerEnvFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4+
using System;
45
using Autofac;
56
using Nethermind.Blockchain;
67
using Nethermind.Core;

src/Nethermind/Nethermind.Core.Test/TestMemColumnDb.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
22
// SPDX-License-Identifier: LGPL-3.0-only
33

4+
using System;
45
using System.Collections.Generic;
56
using Nethermind.Db;
67

@@ -30,6 +31,12 @@ public IColumnsWriteBatch<TKey> StartWriteBatch()
3031
{
3132
return new InMemoryColumnWriteBatch<TKey>(this);
3233
}
34+
35+
public IColumnDbSnapshot<TKey> CreateSnapshot()
36+
{
37+
throw new Exception("Snapshot not implemented");
38+
}
39+
3340
public void Dispose() { }
3441
public void Flush(bool onlyWal = false) { }
3542
}

src/Nethermind/Nethermind.Core/Account.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public Account WithChangedCodeHash(Hash256 newCodeHash)
127127
}
128128

129129
public AccountStruct ToStruct() => new(Nonce, Balance, StorageRoot, CodeHash);
130+
131+
public override string ToString()
132+
{
133+
return $"N:{Nonce},B:{Balance},S{StorageRoot},C:{CodeHash}";
134+
}
130135
}
131136

132137
public readonly struct AccountStruct : IEquatable<AccountStruct>

src/Nethermind/Nethermind.Core/Address.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System;
5+
using System.Buffers.Binary;
56
using System.ComponentModel;
67
using System.Diagnostics;
78
using System.Globalization;

src/Nethermind/Nethermind.Core/Crypto/Hash256.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: LGPL-3.0-only
33

44
using System;
5+
using System.Buffers.Binary;
56
using System.Diagnostics;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
@@ -113,7 +114,12 @@ public readonly struct Hash256AsKey(Hash256 key) : IEquatable<Hash256AsKey>, ICo
113114
public bool Equals(Hash256AsKey other) => Equals(_key, other._key);
114115
public override int GetHashCode() => _key?.GetHashCode() ?? 0;
115116

116-
public int CompareTo(Hash256AsKey other) => _key.CompareTo(other._key);
117+
public int CompareTo(Hash256AsKey other)
118+
{
119+
if (_key is null && other._key is not null) return -1;
120+
if (_key is null && other._key is null) return 0;
121+
return _key!.CompareTo(other._key);
122+
}
117123
}
118124

119125
[DebuggerStepThrough]

src/Nethermind/Nethermind.Core/IKeyValueStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public interface IMergeableKeyValueStore : IWriteOnlyKeyValueStore
7474
void Merge(ReadOnlySpan<byte> key, ReadOnlySpan<byte> value, WriteFlags flags = WriteFlags.None);
7575
}
7676

77-
public interface ISortedKeyValueStore : IKeyValueStore
77+
public interface ISortedKeyValueStore : IReadOnlyKeyValueStore
7878
{
7979
byte[]? FirstKey { get; }
8080
byte[]? LastKey { get; }
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Runtime.InteropServices;
5+
using System.Threading;
6+
7+
namespace Nethermind.Core.Utils;
8+
9+
/// <summary>
10+
/// Provides a component that can be disposed multiple times and runs <see cref="CleanUp"/> only on the last dispose.
11+
/// </summary>
12+
public abstract class RefCountingDisposable : IDisposable
13+
{
14+
private const int Single = 1;
15+
private const int NoAccessors = 0;
16+
private const int Disposing = -1;
17+
18+
protected PaddedValue _leases;
19+
20+
protected RefCountingDisposable(int initialCount = Single)
21+
{
22+
_leases.Value = initialCount;
23+
}
24+
25+
public void AcquireLease()
26+
{
27+
if (TryAcquireLease() == false)
28+
{
29+
ThrowCouldNotAcquire();
30+
}
31+
32+
[DoesNotReturn]
33+
[StackTraceHidden]
34+
static void ThrowCouldNotAcquire()
35+
{
36+
throw new Exception("The lease cannot be acquired");
37+
}
38+
}
39+
40+
protected bool TryAcquireLease()
41+
{
42+
// Volatile read for starting value
43+
var current = Volatile.Read(ref _leases.Value);
44+
if (current == Disposing)
45+
{
46+
// Already disposed
47+
return false;
48+
}
49+
50+
while (true)
51+
{
52+
var prev = Interlocked.CompareExchange(ref _leases.Value, current + Single, current);
53+
if (prev == current)
54+
{
55+
// Successfully acquired
56+
return true;
57+
}
58+
if (prev == Disposing)
59+
{
60+
// Already disposed
61+
return false;
62+
}
63+
64+
// Try again with new starting value
65+
current = prev;
66+
// Add PAUSE instruction to reduce shared core contention
67+
Thread.SpinWait(1);
68+
}
69+
}
70+
71+
/// <summary>
72+
/// Disposes it once, decreasing the lease count by 1.
73+
/// </summary>
74+
public void Dispose() => ReleaseLeaseOnce();
75+
76+
private void ReleaseLeaseOnce()
77+
{
78+
// Volatile read for starting value
79+
var current = Volatile.Read(ref _leases.Value);
80+
if (current <= NoAccessors)
81+
{
82+
// Mismatched Acquire/Release
83+
ThrowOverDisposed();
84+
}
85+
86+
while (true)
87+
{
88+
var prev = Interlocked.CompareExchange(ref _leases.Value, current - Single, current);
89+
if (prev != current)
90+
{
91+
current = prev;
92+
// Add PAUSE instruction to reduce shared core contention
93+
Thread.SpinWait(1);
94+
continue;
95+
}
96+
if (prev == Single)
97+
{
98+
// Last use, try to dispose underlying
99+
break;
100+
}
101+
if (prev <= NoAccessors)
102+
{
103+
// Mismatched Acquire/Release
104+
ThrowOverDisposed();
105+
}
106+
107+
// Successfully released
108+
return;
109+
}
110+
111+
if (Interlocked.CompareExchange(ref _leases.Value, Disposing, NoAccessors) == NoAccessors)
112+
{
113+
// set to disposed by this Release
114+
CleanUp();
115+
}
116+
117+
[DoesNotReturn]
118+
[StackTraceHidden]
119+
static void ThrowOverDisposed()
120+
{
121+
throw new Exception("The lease has already been disposed");
122+
}
123+
}
124+
125+
protected abstract void CleanUp();
126+
127+
public override string ToString()
128+
{
129+
var leases = Volatile.Read(ref _leases.Value);
130+
return leases == Disposing ? "Disposed" : $"Leases: {leases}";
131+
}
132+
133+
[StructLayout(LayoutKind.Explicit, Size = 128)]
134+
protected struct PaddedValue
135+
{
136+
[FieldOffset(64)]
137+
public long Value;
138+
}
139+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: 2025 Demerzel Solutions Limited
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
using System;
5+
6+
namespace Nethermind.Core.Utils;
7+
8+
public class RefCountingDisposableBox<T>(T item): RefCountingDisposable
9+
where T : IDisposable
10+
{
11+
12+
public T Item => item;
13+
14+
public bool TryAcquire()
15+
{
16+
return TryAcquireLease();
17+
}
18+
19+
protected override void CleanUp()
20+
{
21+
Item.Dispose();
22+
}
23+
}

0 commit comments

Comments
 (0)