Skip to content

Commit

Permalink
[Add] IEquatable to WitnessCondition (#3572)
Browse files Browse the repository at this point in the history
* Add `IEquatable` to WitnessCondition

* Added `null` check to `Equal(other)`

* Fixed `Equals` compacked method

* Added `AggressiveInlining`

* Fixed `==` and `!=`

* Remove size from Equals, fix NotCondition, remove ( )

---------

Co-authored-by: Fernando Diaz Toledano <[email protected]>
  • Loading branch information
cschuchardt88 and shargon authored Nov 12, 2024
1 parent 233d0b0 commit cf2b559
Show file tree
Hide file tree
Showing 11 changed files with 709 additions and 9 deletions.
44 changes: 43 additions & 1 deletion src/Neo/Network/P2P/Payloads/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads.Conditions
{
/// <summary>
/// Represents the condition that all conditions must be met.
/// </summary>
public class AndCondition : WitnessCondition
public class AndCondition : WitnessCondition, IEquatable<AndCondition>
{
/// <summary>
/// The expressions of the condition.
Expand All @@ -34,6 +35,29 @@ public class AndCondition : WitnessCondition
public override int Size => base.Size + Expressions.GetVarSize();
public override WitnessConditionType Type => WitnessConditionType.And;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(AndCondition other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return
Type == other.Type &&
Expressions.SequenceEqual(other.Expressions);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
if (obj == null) return false;
return obj is AndCondition ac && Equals(ac);
}

public override int GetHashCode()
{
return HashCode.Combine(Type, Expressions);
}

protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
if (maxNestDepth <= 0) throw new FormatException();
Expand Down Expand Up @@ -73,5 +97,23 @@ public override StackItem ToStackItem(IReferenceCounter referenceCounter)
result.Add(new VM.Types.Array(referenceCounter, Expressions.Select(p => p.ToStackItem(referenceCounter))));
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(AndCondition left, AndCondition right)
{
if (left is null || right is null)
return Equals(left, right);

return left.Equals(right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(AndCondition left, AndCondition right)
{
if (left is null || right is null)
return !Equals(left, right);

return !left.Equals(right);
}
}
}
45 changes: 44 additions & 1 deletion src/Neo/Network/P2P/Payloads/Conditions/BooleanCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class BooleanCondition : WitnessCondition
public class BooleanCondition : WitnessCondition, IEquatable<BooleanCondition>
{
/// <summary>
/// The expression of the <see cref="BooleanCondition"/>.
Expand All @@ -28,6 +30,29 @@ public class BooleanCondition : WitnessCondition
public override int Size => base.Size + sizeof(bool);
public override WitnessConditionType Type => WitnessConditionType.Boolean;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(BooleanCondition other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return
Type == other.Type &&
Expression == other.Expression;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
if (obj == null) return false;
return obj is BooleanCondition bc && Equals(bc);
}

public override int GetHashCode()
{
return HashCode.Combine(Type, Expression);
}

protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
Expression = reader.ReadBoolean();
Expand Down Expand Up @@ -61,5 +86,23 @@ public override StackItem ToStackItem(IReferenceCounter referenceCounter)
result.Add(Expression);
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(BooleanCondition left, BooleanCondition right)
{
if (left is null || right is null)
return Equals(left, right);

return left.Equals(right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(BooleanCondition left, BooleanCondition right)
{
if (left is null || right is null)
return !Equals(left, right);

return !left.Equals(right);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByContractCondition : WitnessCondition
public class CalledByContractCondition : WitnessCondition, IEquatable<CalledByContractCondition>
{
/// <summary>
/// The script hash to be checked.
Expand All @@ -28,6 +30,29 @@ public class CalledByContractCondition : WitnessCondition
public override int Size => base.Size + UInt160.Length;
public override WitnessConditionType Type => WitnessConditionType.CalledByContract;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(CalledByContractCondition other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return
Type == other.Type &&
Hash == other.Hash;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
if (obj == null) return false;
return obj is CalledByContractCondition cc && Equals(cc);
}

public override int GetHashCode()
{
return HashCode.Combine(Type, Hash.GetHashCode());
}

protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
Hash = reader.ReadSerializable<UInt160>();
Expand Down Expand Up @@ -61,5 +86,23 @@ public override StackItem ToStackItem(IReferenceCounter referenceCounter)
result.Add(Hash.ToArray());
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(CalledByContractCondition left, CalledByContractCondition right)
{
if (left is null || right is null)
return Equals(left, right);

return left.Equals(right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(CalledByContractCondition left, CalledByContractCondition right)
{
if (left is null || right is null)
return !Equals(left, right);

return !left.Equals(right);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@
using Neo.IO;
using Neo.Json;
using Neo.SmartContract;
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByEntryCondition : WitnessCondition
public class CalledByEntryCondition : WitnessCondition, IEquatable<CalledByEntryCondition>
{
public override WitnessConditionType Type => WitnessConditionType.CalledByEntry;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(CalledByEntryCondition other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return Type == other.Type;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
if (obj == null) return false;
return obj is CalledByEntryCondition cc && Equals(cc);
}

public override int GetHashCode()
{
return (byte)Type;
}

public override bool Match(ApplicationEngine engine)
{
var state = engine.CurrentContext.GetState<ExecutionContextState>();
Expand All @@ -33,5 +56,23 @@ protected override void DeserializeWithoutType(ref MemoryReader reader, int maxN
protected override void SerializeWithoutType(BinaryWriter writer) { }

private protected override void ParseJson(JObject json, int maxNestDepth) { }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(CalledByEntryCondition left, CalledByEntryCondition right)
{
if (left is null || right is null)
return Equals(left, right);

return left.Equals(right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(CalledByEntryCondition left, CalledByEntryCondition right)
{
if (left is null || right is null)
return !Equals(left, right);

return !left.Equals(right);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.VM.Types;
using System;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads.Conditions
{
public class CalledByGroupCondition : WitnessCondition
public class CalledByGroupCondition : WitnessCondition, IEquatable<CalledByGroupCondition>
{
/// <summary>
/// The group to be checked.
Expand All @@ -31,6 +33,29 @@ public class CalledByGroupCondition : WitnessCondition
public override int Size => base.Size + Group.Size;
public override WitnessConditionType Type => WitnessConditionType.CalledByGroup;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(CalledByGroupCondition other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return
Type == other.Type &&
Group == other.Group;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
if (obj == null) return false;
return obj is CalledByGroupCondition cc && Equals(cc);
}

public override int GetHashCode()
{
return HashCode.Combine(Type, Group);
}

protected override void DeserializeWithoutType(ref MemoryReader reader, int maxNestDepth)
{
Group = reader.ReadSerializable<ECPoint>();
Expand Down Expand Up @@ -66,5 +91,23 @@ public override StackItem ToStackItem(IReferenceCounter referenceCounter)
result.Add(Group.ToArray());
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(CalledByGroupCondition left, CalledByGroupCondition right)
{
if (left is null || right is null)
return Equals(left, right);

return left.Equals(right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(CalledByGroupCondition left, CalledByGroupCondition right)
{
if (left is null || right is null)
return !Equals(left, right);

return !left.Equals(right);
}
}
}
Loading

0 comments on commit cf2b559

Please sign in to comment.