Skip to content

Commit

Permalink
Use runtime throw helpers (#6348)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Dec 12, 2023
1 parent 1040052 commit 6744d21
Show file tree
Hide file tree
Showing 38 changed files with 80 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NewPendingUserOpsSubscription : Subscription
UserOperationSubscriptionParam? userOperationSubscriptionParam = null)
: base(jsonRpcDuplexClient)
{
if (userOperationPools is null) throw new ArgumentNullException(nameof(userOperationPools));
ArgumentNullException.ThrowIfNull(userOperationPools);
if (userOperationSubscriptionParam is not null)
{
if (userOperationSubscriptionParam.EntryPoints.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NewReceivedUserOpsSubscription : Subscription
UserOperationSubscriptionParam? userOperationSubscriptionParam = null)
: base(jsonRpcDuplexClient)
{
if (userOperationPools is null) throw new ArgumentNullException(nameof(userOperationPools));
ArgumentNullException.ThrowIfNull(userOperationPools);
if (userOperationSubscriptionParam is not null)
{
if (userOperationSubscriptionParam.EntryPoints.Length == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Blockchain/BlockTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public BlockHeader[] FindHeaders(Hash256? blockHash, int numberOfBlocks, int ski
{
static BlockHeader[] FindHeadersReversedFast(BlockTree tree, BlockHeader startHeader, int numberOfBlocks, bool reverse = false)
{
if (startHeader is null) throw new ArgumentNullException(nameof(startHeader));
ArgumentNullException.ThrowIfNull(startHeader);
if (numberOfBlocks == 1)
{
return new[] { startHeader };
Expand Down
5 changes: 1 addition & 4 deletions src/Nethermind/Nethermind.Cli/Modules/CliModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public CliModuleLoader(ICliEngine engine, IJsonRpcClient client, ICliConsole cli
/// <exception cref="ArgumentNullException"></exception>
private static Delegate CreateDelegate(MethodInfo methodInfo, CliModuleBase module)
{
if (methodInfo is null)
{
throw new ArgumentNullException(nameof(methodInfo));
}
ArgumentNullException.ThrowIfNull(methodInfo);

ParameterInfo[] parameterInfos = methodInfo.GetParameters();
Type[] types = new Type[parameterInfos.Length + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class AuRaRewardCalculator : IRewardCalculator

public AuRaRewardCalculator(AuRaParameters auRaParameters, IAbiEncoder abiEncoder, ITransactionProcessor transactionProcessor)
{
if (auRaParameters is null) throw new ArgumentNullException(nameof(auRaParameters));
if (abiEncoder is null) throw new ArgumentNullException(nameof(abiEncoder));
if (transactionProcessor is null) throw new ArgumentNullException(nameof(transactionProcessor));
ArgumentNullException.ThrowIfNull(auRaParameters);
ArgumentNullException.ThrowIfNull(abiEncoder);
ArgumentNullException.ThrowIfNull(transactionProcessor);

IList<IRewardContract> BuildTransitions()
{
Expand All @@ -48,7 +48,7 @@ IList<IRewardContract> BuildTransitions()
return contracts;
}

if (auRaParameters is null) throw new ArgumentNullException(nameof(AuRaParameters));
ArgumentNullException.ThrowIfNull(auRaParameters);
_contracts = BuildTransitions();
_blockRewardCalculator = new StaticRewardCalculator(auRaParameters.BlockReward);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class ListBasedValidator : AuRaValidatorBase
public ListBasedValidator(AuRaParameters.Validator validator, IValidSealerStrategy validSealerStrategy, IValidatorStore validatorStore, ILogManager logManager, long startBlockNumber, bool forSealing = false)
: base(validSealerStrategy, validatorStore, logManager, startBlockNumber, forSealing)
{
if (validator is null) throw new ArgumentNullException(nameof(validator));
ArgumentNullException.ThrowIfNull(validator);

Validators = validator.Addresses?.Length > 0
? validator.Addresses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MultiValidator : IAuRaValidator, IReportingValidator, ITxSource, ID
ILogManager logManager,
bool forSealing = false)
{
if (validator is null) throw new ArgumentNullException(nameof(validator));
ArgumentNullException.ThrowIfNull(validator);
if (validator.ValidatorType != AuRaParameters.ValidatorType.Multi) throw new ArgumentException("Wrong validator type.", nameof(validator));
_validatorFactory = validatorFactory ?? throw new ArgumentNullException(nameof(validatorFactory));
_blockTree = blockTree ?? throw new ArgumentNullException(nameof(blockTree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CompositeBlockPreprocessorStep : IBlockPreprocessorStep

public CompositeBlockPreprocessorStep(params IBlockPreprocessorStep[] recoverySteps)
{
if (recoverySteps is null) throw new ArgumentNullException(nameof(recoverySteps));
ArgumentNullException.ThrowIfNull(recoverySteps);

_recoverySteps = new LinkedList<IBlockPreprocessorStep>();
for (int i = 0; i < recoverySteps.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class ReadOnlyTxProcessingEnv : IReadOnlyTxProcessorSource
ISpecProvider? specProvider,
ILogManager? logManager)
{
if (specProvider is null) throw new ArgumentNullException(nameof(specProvider));
if (worldStateManager is null) throw new ArgumentNullException(nameof(worldStateManager));
ArgumentNullException.ThrowIfNull(specProvider);
ArgumentNullException.ThrowIfNull(worldStateManager);

StateReader = worldStateManager.GlobalStateReader;
StateProvider = worldStateManager.CreateResettableWorldState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ public static class BlockProductionTriggerExtensions
{
public static IBlockProductionTrigger IfPoolIsNotEmpty(this IBlockProductionTrigger? trigger, ITxPool? txPool)
{
if (trigger is null) throw new ArgumentNullException(nameof(trigger));
if (txPool is null) throw new ArgumentNullException(nameof(txPool));
ArgumentNullException.ThrowIfNull(trigger);
ArgumentNullException.ThrowIfNull(txPool);
return trigger.ButOnlyWhen(() => txPool.GetPendingTransactionsCount() > 0);
}

public static IBlockProductionTrigger ButOnlyWhen(this IBlockProductionTrigger? trigger, Func<bool> condition)
{
if (trigger is null) throw new ArgumentNullException(nameof(trigger));
ArgumentNullException.ThrowIfNull(trigger);
return new TriggerWithCondition(trigger, condition);
}

public static IBlockProductionTrigger Or(this IBlockProductionTrigger? trigger, IBlockProductionTrigger? alternative)
{
if (trigger is null) throw new ArgumentNullException(nameof(trigger));
if (alternative is null) throw new ArgumentNullException(nameof(alternative));
ArgumentNullException.ThrowIfNull(trigger);
ArgumentNullException.ThrowIfNull(alternative);

if (trigger is CompositeBlockProductionTrigger composite1)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Consensus/Tracing/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private void Process(Block block, IBlockTracer blockTracer, IBlockchainProcessor

public void Accept(ITreeVisitor visitor, Hash256 stateRoot)
{
if (visitor is null) throw new ArgumentNullException(nameof(visitor));
if (stateRoot is null) throw new ArgumentNullException(nameof(stateRoot));
ArgumentNullException.ThrowIfNull(visitor);
ArgumentNullException.ThrowIfNull(stateRoot);

_stateProvider.Accept(visitor, stateRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TxFilterPipelineBuilder
ISpecProvider? specProvider,
IBlocksConfig blocksConfig)
{
if (specProvider is null) throw new ArgumentNullException(nameof(specProvider));
ArgumentNullException.ThrowIfNull(specProvider);

return new TxFilterPipelineBuilder(logManager)
.WithMinGasPriceFilter(blocksConfig, specProvider)
Expand Down
5 changes: 1 addition & 4 deletions src/Nethermind/Nethermind.Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ public static bool TryParseVariableLength(string? value, out Address? address)

public Address(byte[] bytes)
{
if (bytes is null)
{
throw new ArgumentNullException(nameof(bytes));
}
ArgumentNullException.ThrowIfNull(bytes);

if (bytes.Length != Size)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Nethermind/Nethermind.Core/Caching/LruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public sealed class LruCache<TKey, TValue> : ICache<TKey, TValue> where TKey : n

public LruCache(int maxCapacity, int startCapacity, string name)
{
if (maxCapacity < 1)
{
throw new ArgumentOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfLessThan(maxCapacity, 1);

_maxCapacity = maxCapacity;
_cacheMap = typeof(TKey) == typeof(byte[])
Expand Down
5 changes: 1 addition & 4 deletions src/Nethermind/Nethermind.Core/Caching/SpanLruCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public sealed class SpanLruCache<TKey, TValue> : ISpanCache<TKey, TValue> where

public SpanLruCache(int maxCapacity, int startCapacity, string name, ISpanEqualityComparer<TKey> comparer)
{
if (maxCapacity < 1)
{
throw new ArgumentOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfLessThan(maxCapacity, 1);

_maxCapacity = maxCapacity;
_cacheMap = new SpanDictionary<TKey, LinkedListNode<LruCacheItem>>(startCapacity, comparer);
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Core/Collections/IListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public static void ForEach<T>(this IReadOnlyList<T> list, Action<T> action)
/// <returns></returns>
public static int BinarySearch<TItem, TSearch>(this IList<TItem> list, TSearch value, Func<TSearch, TItem, int> comparer)
{
if (list is null) throw new ArgumentNullException(nameof(list));
if (comparer is null) throw new ArgumentNullException(nameof(comparer));
ArgumentNullException.ThrowIfNull(list);
ArgumentNullException.ThrowIfNull(comparer);

int lower = 0;
int upper = list.Count - 1;
Expand Down
20 changes: 10 additions & 10 deletions src/Nethermind/Nethermind.Core/Collections/LinkedHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public bool Add(T item)

public void ExceptWith(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

foreach (T t in other)
{
Expand All @@ -67,7 +67,7 @@ public void ExceptWith(IEnumerable<T>? other)

public void IntersectWith(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

T[] ts = new T[Count];
CopyTo(ts, 0);
Expand All @@ -83,7 +83,7 @@ public void IntersectWith(IEnumerable<T>? other)

public bool IsProperSubsetOf(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

int contains = 0;
int noContains = 0;
Expand All @@ -104,7 +104,7 @@ public bool IsProperSubsetOf(IEnumerable<T>? other)

public bool IsProperSupersetOf(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

ISet<T> set = other.ToHashSet();
int otherCount = set.Count;
Expand Down Expand Up @@ -132,37 +132,37 @@ public bool IsProperSupersetOf(IEnumerable<T>? other)

public bool IsSubsetOf(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

ISet<T> set = other.ToHashSet();
return this.All(t => set.Contains(t));
}

public bool IsSupersetOf(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

return other.All(Contains);
}

public bool Overlaps(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

return other.Any(Contains);
}

public bool SetEquals(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

ISet<T> set = other.ToHashSet();
return Count == set.Count && IsSupersetOf(set);
}

public void SymmetricExceptWith(IEnumerable<T> other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

T[] ts = new T[Count];
CopyTo(ts, 0);
Expand All @@ -186,7 +186,7 @@ public void SymmetricExceptWith(IEnumerable<T> other)

public void UnionWith(IEnumerable<T>? other)
{
if (other is null) throw new ArgumentNullException(nameof(other));
ArgumentNullException.ThrowIfNull(other);

foreach (T t in other)
{
Expand Down
15 changes: 3 additions & 12 deletions src/Nethermind/Nethermind.Core/Collections/SpanDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ public class SpanDictionary<TKey, TValue> : IDictionary<TKey[], TValue>, IDictio

public SpanDictionary(int capacity, ISpanEqualityComparer<TKey> comparer)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
ArgumentOutOfRangeException.ThrowIfNegative(capacity);

if (capacity > 0)
{
Expand Down Expand Up @@ -946,10 +943,7 @@ void ICollection.CopyTo(Array array, int index)
/// </summary>
public int EnsureCapacity(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
ArgumentOutOfRangeException.ThrowIfNegative(capacity);

int currentCapacity = _entries == null ? 0 : _entries.Length;
if (currentCapacity >= capacity)
Expand Down Expand Up @@ -992,10 +986,7 @@ public int EnsureCapacity(int capacity)
/// </remarks>
public void TrimExcess(int capacity)
{
if (capacity < Count)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
ArgumentOutOfRangeException.ThrowIfLessThan(capacity, Count);

int newSize = HashHelpers.GetPrime(capacity);
Entry[]? oldEntries = _entries;
Expand Down
5 changes: 1 addition & 4 deletions src/Nethermind/Nethermind.Crypto/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public PrivateKey(string hexString)

public PrivateKey(byte[] keyBytes)
{
if (keyBytes is null)
{
throw new ArgumentNullException(nameof(keyBytes));
}
ArgumentNullException.ThrowIfNull(keyBytes);

if (!SecP256k1.VerifyPrivateKey(keyBytes))
{
Expand Down
10 changes: 2 additions & 8 deletions src/Nethermind/Nethermind.Crypto/SecureStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public static class SecureStringExtensions
{
public static byte[] ToByteArray(this SecureString secureString, System.Text.Encoding encoding = null)
{
if (secureString is null)
{
throw new ArgumentNullException(nameof(secureString));
}
ArgumentNullException.ThrowIfNull(secureString);

encoding ??= System.Text.Encoding.UTF8;

Expand All @@ -35,10 +32,7 @@ public static byte[] ToByteArray(this SecureString secureString, System.Text.Enc

public static string Unsecure(this SecureString secureString)
{
if (secureString is null)
{
throw new ArgumentNullException(nameof(secureString));
}
ArgumentNullException.ThrowIfNull(secureString);

IntPtr unmanagedString = IntPtr.Zero;
try
Expand Down

0 comments on commit 6744d21

Please sign in to comment.