diff --git a/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeBucket.cs b/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeBucket.cs index e0a5445a4fa..6ce661d0546 100644 --- a/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeBucket.cs +++ b/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeBucket.cs @@ -51,9 +51,11 @@ public struct BondedItemsEnumerator : IEnumerator, IEnumerable this; diff --git a/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeTable.cs b/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeTable.cs index c0a5813e395..4dc4eae50d7 100644 --- a/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeTable.cs +++ b/src/Nethermind/Nethermind.Network.Discovery/RoutingTable/NodeTable.cs @@ -105,32 +105,25 @@ public ClosestNodesEnumerator(NodeBucket[] buckets, int bucketSize) public bool MoveNext() { - try + while (_count < _bucketSize) { - while (_count < _bucketSize) + if (!_enumeratorSet || !_itemEnumerator.MoveNext()) { - if (!_enumeratorSet || !_itemEnumerator.MoveNext()) + _itemEnumerator.Dispose(); + _bucketIndex++; + if (_bucketIndex >= _buckets.Length) { - _itemEnumerator.Dispose(); - _bucketIndex++; - if (_bucketIndex >= _buckets.Length) - { - return false; - } - - _itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator(); - _enumeratorSet = true; - continue; + return false; } - Current = _itemEnumerator.Current.Node!; - _count++; - return true; + _itemEnumerator = _buckets[_bucketIndex].BondedItems.GetEnumerator(); + _enumeratorSet = true; + continue; } - } - finally - { - _itemEnumerator.Dispose(); + + Current = _itemEnumerator.Current.Node!; + _count++; + return true; } return false; @@ -138,7 +131,7 @@ public bool MoveNext() void IEnumerator.Reset() => throw new NotSupportedException(); - public void Dispose() { } + public void Dispose() => _itemEnumerator.Dispose(); public ClosestNodesEnumerator GetEnumerator() => this;