Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Nethermind/Nethermind.Core/Collections/JournalCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Nethermind.Core.Collections
{
Expand All @@ -21,14 +24,17 @@ public void Restore(int snapshot)
{
if (snapshot >= Count)
{
throw new InvalidOperationException($"{nameof(JournalCollection<T>)} tried to restore snapshot {snapshot} beyond current position {Count}");
ThrowInvalidRestore(snapshot);
}

// Just remove excessive items after snapshot
int index = snapshot + 1;
_list.RemoveRange(index, Count - index);
CollectionsMarshal.SetCount(_list, snapshot + 1);
}

[DoesNotReturn]
[StackTraceHidden]
private void ThrowInvalidRestore(int snapshot)
=> throw new InvalidOperationException($"{nameof(JournalCollection<T>)} tried to restore snapshot {snapshot} beyond current position {Count}");

public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_list).GetEnumerator();
Expand Down
36 changes: 20 additions & 16 deletions src/Nethermind/Nethermind.Core/Collections/JournalSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,51 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Nethermind.Core.Collections
{
/// <summary>
/// <see cref="ISet{T}"/> of items <see cref="T"/> with ability to store and restore state snapshots.
/// <see cref="ICollection{T}"/> of items <see cref="T"/> with ability to store and restore state snapshots.
/// </summary>
/// <typeparam name="T">Item type.</typeparam>
/// <remarks>Due to snapshots <see cref="Remove"/> is not supported.</remarks>
public sealed class JournalSet<T> : IReadOnlySet<T>, ICollection<T>, IJournal<int>
public sealed class JournalSet<T> : IReadOnlyCollection<T>, ICollection<T>, IJournal<int>
{
private readonly List<T> _items = new();
private readonly HashSet<T> _set = new();
private readonly List<T> _items = [];
private readonly HashSet<T> _set = [];
public int TakeSnapshot() => Position;

private int Position => Count - 1;

[SkipLocalsInit]
public void Restore(int snapshot)
{
if (snapshot >= Count)
int count = _set.Count;
if (snapshot >= count)
{
throw new InvalidOperationException($"{nameof(JournalCollection<T>)} tried to restore snapshot {snapshot} beyond current position {Count}");
ThrowInvalidRestore(snapshot);
}

int current = Position;
if (count == 0) return;

// we use dictionary to remove items added after snapshot
for (int i = snapshot + 1; i <= current; i++)
foreach (T item in CollectionsMarshal.AsSpan(_items)[(snapshot + 1)..])
{
T item = _items[i];
_set.Remove(item);
}

_items.RemoveRange(snapshot + 1, current - snapshot);
CollectionsMarshal.SetCount(_items, snapshot + 1);
}

[DoesNotReturn]
[StackTraceHidden]
private void ThrowInvalidRestore(int snapshot)
=> throw new InvalidOperationException($"{nameof(JournalSet<T>)} tried to restore snapshot {snapshot} beyond current position {Count}");

public bool Add(T item)
{
if (_set.Add(item))
Expand All @@ -65,11 +75,5 @@ public void Clear()
void ICollection<T>.Add(T item) => Add(item);
public bool Contains(T item) => _set.Contains(item);
public void CopyTo(T[] array, int arrayIndex) => _set.CopyTo(array, arrayIndex);
public bool IsProperSubsetOf(IEnumerable<T> other) => _set.IsProperSubsetOf(other);
public bool IsProperSupersetOf(IEnumerable<T> other) => _set.IsProperSupersetOf(other);
public bool IsSubsetOf(IEnumerable<T> other) => _set.IsSubsetOf(other);
public bool IsSupersetOf(IEnumerable<T> other) => _set.IsSupersetOf(other);
public bool Overlaps(IEnumerable<T> other) => _set.Overlaps(other);
public bool SetEquals(IEnumerable<T> other) => _set.SetEquals(other);
}
}
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm.Test/EvmPooledMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void ReportExtraGasPressure(long extraGasPressure)
throw new NotImplementedException();
}

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/AccessTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void MarkAsFailed(Address recipient, GasConsumed gasSpent, byte[
GasSpent += gasSpent.SpentGas;
}

public override void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public override void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
{
Dictionary<Address, ISet<UInt256>> dictionary = new();
foreach (Address address in accessedAddresses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static AlwaysCancelTxTracer Instance
public void ReportGasUpdateForVmTrace(long refund, long gasAvailable) => throw new OperationCanceledException(ErrorMessage);
public void ReportRefund(long refund) => throw new OperationCanceledException(ErrorMessage);
public void ReportExtraGasPressure(long extraGasPressure) => throw new OperationCanceledException(ErrorMessage);
public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells) => throw new OperationCanceledException(ErrorMessage);
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells) => throw new OperationCanceledException(ErrorMessage);
public void ReportFees(UInt256 fees, UInt256 burntFees) => throw new OperationCanceledException(ErrorMessage);
public void Dispose() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void ReportRefund(long refund) =>
public void ReportExtraGasPressure(long extraGasPressure) =>
_currentTxTracer.ReportExtraGasPressure(extraGasPressure);

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells) =>
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells) =>
_currentTxTracer.ReportAccess(accessedAddresses, accessedStorageCells);

public void SetOperationStack(TraceStack stack) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public void ReportExtraGasPressure(long extraGasPressure)
}
}

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
{
token.ThrowIfCancellationRequested();
if (innerTracer.IsTracingAccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void ReportExtraGasPressure(long extraGasPressure)
}
}

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
{
for (int index = 0; index < _txTracers.Count; index++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void ReportRefund(long refund)
public void ReportExtraGasPressure(long extraGasPressure)
=> InnerTracer.ReportExtraGasPressure(extraGasPressure);

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
=> InnerTracer.ReportAccess(accessedAddresses, accessedStorageCells);

public void ReportFees(UInt256 fees, UInt256 burntFees)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/ITxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ void LoadOperationTransientStorage(Address storageCellAddress, UInt256 storageIn
/// <param name="accessedAddresses">address</param>
/// <param name="accessedStorageCells">cell</param>
/// <remarks>Depends on <see cref="IsTracingAccess"/></remarks>
void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells);
void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells);

/// <summary>
/// Reports fees of a transaction
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/NullTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public override void ReportRefund(long refund)
public override void ReportExtraGasPressure(long extraGasPressure)
=> ThrowInvalidOperationException();

public override void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public override void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
=> ThrowInvalidOperationException();

public override void ReportFees(UInt256 fees, UInt256 burntFees)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Evm/Tracing/TxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public virtual void ReportByteCode(ReadOnlyMemory<byte> byteCode) { }
public virtual void ReportGasUpdateForVmTrace(long refund, long gasAvailable) { }
public virtual void ReportRefund(long refund) { }
public virtual void ReportExtraGasPressure(long extraGasPressure) { }
public virtual void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells) { }
public virtual void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells) { }
public virtual void ReportFees(UInt256 fees, UInt256 burntFees) { }
public virtual void Dispose() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Logging;
using Nethermind.Synchronization.Blocks;

namespace Nethermind.Merge.Plugin.Synchronization;

Expand Down Expand Up @@ -136,7 +136,7 @@ private void OnMissingBeaconHeader(long blockNumber)
}
else
{
headers.RemoveRange(toTake, headers.Count - toTake);
CollectionsMarshal.SetCount(headers, toTake);
}

return headers.ToArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Test.Runner/StateTestTxTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void ReportExtraGasPressure(long extraGasPressure)
throw new NotImplementedException();
}

public void ReportAccess(IReadOnlySet<Address> accessedAddresses, IReadOnlySet<StorageCell> accessedStorageCells)
public void ReportAccess(IReadOnlyCollection<Address> accessedAddresses, IReadOnlyCollection<StorageCell> accessedStorageCells)
{
throw new NotImplementedException();
}
Expand Down