Skip to content

Commit

Permalink
[Add] IEquatable to Signer (neo-project#3571)
Browse files Browse the repository at this point in the history
* Added IEquatable to Signer

* Added `null` check to unit tests

* Added better testing of Signer

* Apply suggestions from code review

* Added WitnessRules checking to the unit tests

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

* Added `AggressiveInlining` and Compacked `Equals`

* Update src/Neo/Network/P2P/Payloads/Signer.cs

* Fixed null checking

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: Jimmy <[email protected]>
Co-authored-by: NGD Admin <[email protected]>
  • Loading branch information
4 people authored Nov 20, 2024
1 parent 80fc4fd commit 842fa52
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/Neo/Network/P2P/Payloads/Signer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads
{
/// <summary>
/// Represents a signer of a <see cref="Transaction"/>.
/// </summary>
public class Signer : IInteroperable, ISerializable
public class Signer : IInteroperable, ISerializable, IEquatable<Signer>
{
// This limits maximum number of AllowedContracts or AllowedGroups here
private const int MaxSubitems = 16;
Expand Down Expand Up @@ -66,6 +67,31 @@ public class Signer : IInteroperable, ISerializable
/*AllowedGroups*/ (Scopes.HasFlag(WitnessScope.CustomGroups) ? AllowedGroups.GetVarSize() : 0) +
/*Rules*/ (Scopes.HasFlag(WitnessScope.WitnessRules) ? Rules.GetVarSize() : 0);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Signer other)
{
if (ReferenceEquals(this, other))
return true;
if (other is null) return false;
return Account == other.Account &&
Scopes == other.Scopes &&
AllowedContracts.AsSpan().SequenceEqual(other.AllowedContracts.AsSpan()) &&
AllowedGroups.AsSpan().SequenceEqual(other.AllowedGroups.AsSpan()) &&
Rules.AsEnumerable().SequenceEqual(other.Rules.AsEnumerable());
}

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

public override int GetHashCode()
{
return HashCode.Combine(Account.GetHashCode(), Scopes);
}

public void Deserialize(ref MemoryReader reader)
{
Account = reader.ReadSerializable<UInt160>();
Expand Down Expand Up @@ -202,5 +228,23 @@ VM.Types.StackItem IInteroperable.ToStackItem(IReferenceCounter referenceCounter
Scopes.HasFlag(WitnessScope.WitnessRules) ? new VM.Types.Array(referenceCounter, Rules.Select(u => u.ToStackItem(referenceCounter))) : new VM.Types.Array(referenceCounter)
]);
}

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

return left.Equals(right);
}

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

return !left.Equals(right);
}
}
}
79 changes: 79 additions & 0 deletions tests/Neo.UnitTests/Network/P2P/Payloads/UT_Signers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,85 @@ namespace Neo.UnitTests.Network.P2P.Payloads
[TestClass]
public class UT_Signers
{
[TestMethod]
public void Test_IEquatable()
{
var ecPoint = ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", ECCurve.Secp256r1);
var expected = new Signer()
{
Account = UInt160.Zero,
Scopes = WitnessScope.Global,
AllowedContracts = [UInt160.Zero],
AllowedGroups = [ecPoint],
Rules = [
new WitnessRule
{
Condition = new BooleanCondition
{
Expression = true,
},
Action = WitnessRuleAction.Allow,
},
]
};

var actual = new Signer()
{
Account = UInt160.Zero,
Scopes = WitnessScope.Global,
AllowedContracts = [UInt160.Zero],
AllowedGroups = [ecPoint],
Rules = [
new WitnessRule
{
Condition = new BooleanCondition
{
Expression = true,
},
Action = WitnessRuleAction.Allow,
},
]
};

var notEqual = new Signer()
{
Account = UInt160.Zero,
Scopes = WitnessScope.WitnessRules,
AllowedContracts = [],
AllowedGroups = [],
Rules = []
};

var cnull = new Signer
{
Account = null,
Scopes = WitnessScope.Global,
AllowedContracts = null,
AllowedGroups = null,
Rules = null,
};

Assert.IsTrue(expected.Equals(expected));

Assert.AreEqual(expected, actual);
Assert.IsTrue(expected == actual);
Assert.IsTrue(expected.Equals(actual));

Assert.AreNotEqual(expected, notEqual);
Assert.IsTrue(expected != notEqual);
Assert.IsFalse(expected.Equals(notEqual));

Assert.IsFalse(expected == null);
Assert.IsFalse(null == expected);
Assert.AreNotEqual(expected, null);
Assert.IsFalse(expected.Equals(null));

//Check null
Assert.AreNotEqual(cnull, notEqual);
Assert.IsFalse(cnull.Equals(notEqual));
}


[TestMethod]
public void Serialize_Deserialize_Global()
{
Expand Down

0 comments on commit 842fa52

Please sign in to comment.