Skip to content

Commit

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

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

* Added `AggressiveInlining` and compacked `Equals`

* Fixed `==` and `!-`

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

---------

Co-authored-by: Shargon <[email protected]>
  • Loading branch information
cschuchardt88 and shargon authored Nov 13, 2024
1 parent cf2b559 commit e6fa071
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Neo/Network/P2P/Payloads/WitnessRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
using Neo.VM.Types;
using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace Neo.Network.P2P.Payloads
{
/// <summary>
/// The rule used to describe the scope of the witness.
/// </summary>
public class WitnessRule : IInteroperable, ISerializable
public class WitnessRule : IInteroperable, ISerializable, IEquatable<WitnessRule>
{
/// <summary>
/// Indicates the action to be taken if the current context meets with the rule.
Expand All @@ -37,6 +38,27 @@ public class WitnessRule : IInteroperable, ISerializable

int ISerializable.Size => sizeof(WitnessRuleAction) + Condition.Size;

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

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

public override int GetHashCode()
{
return HashCode.Combine(Action, Condition.GetHashCode());
}

void ISerializable.Deserialize(ref MemoryReader reader)
{
Action = (WitnessRuleAction)reader.ReadByte();
Expand Down Expand Up @@ -96,5 +118,23 @@ public StackItem ToStackItem(IReferenceCounter referenceCounter)
Condition.ToStackItem(referenceCounter)
});
}

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

return left.Equals(right);
}

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

return !left.Equals(right);
}
}
}
72 changes: 72 additions & 0 deletions tests/Neo.UnitTests/Network/P2P/Payloads/UT_WitnessRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_WitnessRule.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Network.P2P.Payloads;
using Neo.Network.P2P.Payloads.Conditions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo.UnitTests.Network.P2P.Payloads
{
[TestClass]
public class UT_WitnessRule
{
[TestMethod]
public void Test_IEquatable()
{
var expected = new WitnessRule
{
Action = WitnessRuleAction.Allow,
Condition = new BooleanCondition
{
Expression = true,
}
};

var actual = new WitnessRule
{
Action = WitnessRuleAction.Allow,
Condition = new BooleanCondition
{
Expression = true,
}
};

var notEqual = new WitnessRule
{
Action = WitnessRuleAction.Deny,
Condition = new BooleanCondition
{
Expression = false,
}
};

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));
}
}
}

0 comments on commit e6fa071

Please sign in to comment.