-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Add] IEquatable
to Signer
#3571
base: master
Are you sure you want to change the base?
Changes from all commits
a6b5118
ce2d4a7
ee6a7e9
ae24f0a
b97534b
3a1ec39
115616c
4e1bc8c
c632d09
025033c
c7bb72f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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.SequenceEqual(other.AllowedContracts) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of these values are null depending of the Scope There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scope can not be null, its a enum, can not be null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, some of the values can be null, we should make unit tests with real signers with different scopes, depending of the scope, you will find nulls entries |
||
AllowedGroups.SequenceEqual(other.AllowedGroups) && | ||
Rules.SequenceEqual(other.Rules); | ||
} | ||
|
||
[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>(); | ||
|
@@ -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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a practical need for this API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those of being nulls will also be compared, i have checked, null == null return true. Not sure if this can be of any problem, looks right to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not bad to me also, == and != not required to me, but it's ok