Skip to content

Commit

Permalink
Simplify null checks (#6355)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Dec 12, 2023
1 parent 6744d21 commit 9df511b
Show file tree
Hide file tree
Showing 39 changed files with 73 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private CompareReplacedUserOperationByFee() { }
public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

y.MaxFeePerGas.Divide(PartOfFeeRequiredToIncrease, out UInt256 bumpMaxFeePerGas);
if (y.MaxFeePerGas + bumpMaxFeePerGas > x.MaxFeePerGas) return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ private CompareUserOperationByNonce() { }
public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;
return x.Nonce.CompareTo(y.Nonce);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CompareUserOperationsByDecreasingGasPrice : IComparer<UserOperation
public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

//TODO Implement effective gas price sorting
return y.MaxPriorityFeePerGas.CompareTo(x.MaxPriorityFeePerGas);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ private CompareUserOperationsByHash() { }
public int Compare(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

return x.RequestId!.CompareTo(y.RequestId!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ private CompetingUserOperationEqualityComparer() { }
public bool Equals(UserOperation? x, UserOperation? y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x is null) return false;
if (y is null) return false;
if (x.GetType() != y.GetType()) return false;
return x.Sender.Equals(y.Sender) && x.Nonce.Equals(y.Nonce);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Blockchain/Find/BlockParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public BlockParameter(Hash256 blockHash, bool requireCanonical = false)

public bool Equals(BlockParameter? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Type == other.Type && BlockNumber == other.BlockNumber && BlockHash == other.BlockHash && other.RequireCanonical == RequireCanonical;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((BlockParameter)obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ private void CheckReloadSendersWhitelist()
public int Compare(Transaction x, Transaction y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

// we already have nonce ordered by previous code, we don't deal with it here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ public PersistentReport(Address maliciousValidator, in UInt256 blockNumber, byte

public bool Equals(PersistentReport other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(MaliciousValidator, other.MaliciousValidator) && BlockNumber == other.BlockNumber;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PersistentReport)obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public GasPriceTxComparer(IBlockFinder blockFinder, ISpecProvider specProvider)
public int Compare(Transaction? x, Transaction? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

// if gas bottleneck was calculated, it's highest priority for sorting
// if not, different method of sorting by gas price is needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public static class GasPriceTxComparerHelper
public static int Compare(Transaction? x, Transaction? y, in UInt256 baseFee, bool isEip1559Enabled)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

// EIP1559 changed the way we're sorting transactions. The transaction with a higher miner tip should go first
if (isEip1559Enabled)
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Address(byte[] bytes)

public bool Equals(Address? other)
{
if (ReferenceEquals(null, other))
if (other is null)
{
return false;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public static Address FromNumber(in UInt256 number)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down Expand Up @@ -293,13 +293,13 @@ public static AddressStructRef FromNumber(in UInt256 number)
/// <returns></returns>
public string ToString(bool withZeroX, bool withEip55Checksum) => Bytes.ToHexString(withZeroX, false, withEip55Checksum);

public bool Equals(Address? other) => !ReferenceEquals(null, other) && Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);
public bool Equals(Address? other) => other is not null && Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);

public bool Equals(AddressStructRef other) => Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Nethermind/Nethermind.Core/Bloom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ public override string ToString()

public bool Equals(Bloom? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;

return Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Bloom)obj);
Expand Down Expand Up @@ -284,7 +284,7 @@ public override string ToString()

public bool Equals(Bloom? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
return Nethermind.Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes);
}

Expand All @@ -296,7 +296,7 @@ public bool Equals(BloomStructRef other)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (obj.GetType() != typeof(BloomStructRef)) return false;
return Equals((Bloom)obj);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/Bytes32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Bytes32 Xor(Bytes32 other)

public bool Equals(Bytes32? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return _bytes.SequenceEqual(other._bytes);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Core/Crypto/PublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ public string ToShortString()

public static bool operator ==(PublicKey? a, PublicKey? b)
{
if (ReferenceEquals(a, null))
if (a is null)
{
return ReferenceEquals(b, null);
return b is null;
}

if (ReferenceEquals(b, null))
if (b is null)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/Crypto/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public override string ToString()

public bool Equals(Root? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Bytes.SequenceEqual(other.Bytes);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Core/Crypto/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ public override string ToString()

public bool Equals(Signature? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Core.Extensions.Bytes.AreEqual(Bytes, other.Bytes) && V == other.V;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Signature)obj);
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/StorageCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public StorageCell(Address address, ValueHash256 hash)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Crypto/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private bool Equals(PrivateKey other)

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override bool Matches(ref BloomStructRef bloom)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return Equals(obj as OrExpression);
}
Expand All @@ -89,7 +89,7 @@ public override int GetHashCode()

public bool Equals(OrExpression? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return _subexpressions.SequenceEqual(other._subexpressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override bool Matches(ref BloomStructRef bloom)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((SequenceTopicsFilter)obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private ref readonly Bloom.BloomExtract BloomExtract

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((SpecificTopic)obj);
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Mev/Data/MevBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public MevBundle(long blockNumber, IReadOnlyList<BundleTransaction> transactions

public bool Equals(MevBundle? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(Hash, other.Hash);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((MevBundle)obj);
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Mev/Data/MevMegabundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public MevMegabundle(long blockNumber, IReadOnlyList<BundleTransaction> transact

public bool Equals(MevMegabundle? other)
{
if (ReferenceEquals(null, other)) return false;
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(Hash, other.Hash)
&& Equals(RelaySignature, other.RelaySignature);
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((MevMegabundle)obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CompareMevBundleByBlock : IComparer<MevBundle>
public int Compare(MevBundle? x, MevBundle? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

return x.BlockNumber.CompareTo(y.BlockNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CompareMevBundleByHash : IComparer<MevBundle>
public int Compare(MevBundle? x, MevBundle? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

return x.Hash.CompareTo(y.Hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CompareMevBundleByMinTimestamp : IComparer<MevBundle>
public int Compare(MevBundle? x, MevBundle? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

return x.MinTimestamp.CompareTo(y.MinTimestamp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class CompareMevBundleBySequenceNumber : IComparer<MevBundle>
public int Compare(MevBundle? x, MevBundle? y)
{
if (ReferenceEquals(x, y)) return 0;
if (ReferenceEquals(null, y)) return 1;
if (ReferenceEquals(null, x)) return -1;
if (y is null) return 1;
if (x is null) return -1;

return y.SequenceNumber.CompareTo(x.SequenceNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public bool Equals(MessageTypeKey other)

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj is null) return false;
return obj is MessageTypeKey key && Equals(key);
}

Expand Down
Loading

0 comments on commit 9df511b

Please sign in to comment.