Skip to content

Commit

Permalink
chore: Fix Equals for RewardBase
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Dec 10, 2024
1 parent 31e2d87 commit dd0592e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Lib9c/Delegation/RewardBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RewardBase : IBencodable, IEquatable<RewardBase>
private const string StateTypeName = "reward_base";
private const long StateVersion = 1;
public const int Margin = 2;
private readonly IComparer<Currency> _currencyComparer = new CurrencyComparer();
private static readonly IComparer<Currency> _currencyComparer = new CurrencyComparer();

public RewardBase(
Address address,
Expand Down Expand Up @@ -79,7 +79,7 @@ public RewardBase(
throw new ArgumentException("Duplicated currency in reward base.");
}

RewardPortion = rewardPortion.ToImmutableDictionary(f => f.Currency, f => f);
RewardPortion = rewardPortion.ToImmutableSortedDictionary(f => f.Currency, f => f, _currencyComparer);
SigFig = sigfig;
StartHeight = startHeight;
}
Expand All @@ -106,7 +106,7 @@ public RewardBase(Address address, List bencoded)
throw new ArgumentException("Duplicated currency in reward base.");
}

RewardPortion = rewardPortion.ToImmutableDictionary(f => f.Currency, f => f);
RewardPortion = rewardPortion.ToImmutableSortedDictionary(f => f.Currency, f => f, _currencyComparer);
SigFig = (Integer)bencoded[4];

try
Expand All @@ -122,7 +122,7 @@ public RewardBase(Address address, List bencoded)
private RewardBase(
Address address,
BigInteger totalShares,
ImmutableDictionary<Currency, FungibleAssetValue> rewardPortion,
ImmutableSortedDictionary<Currency, FungibleAssetValue> rewardPortion,
int sigfig,
long? startHeight = null)
{
Expand All @@ -141,7 +141,7 @@ private RewardBase(

public int SigFig { get; private set; }

public ImmutableDictionary<Currency, FungibleAssetValue> RewardPortion { get; }
public ImmutableSortedDictionary<Currency, FungibleAssetValue> RewardPortion { get; }

public List Bencoded
{
Expand Down Expand Up @@ -199,9 +199,10 @@ private static RewardBase UpdateTotalShares(RewardBase rewardBase, BigInteger to
{
var newSigFig = Math.Max(rewardBase.SigFig, RecommendedSigFig(totalShares));
var multiplier = Multiplier(newSigFig - rewardBase.SigFig);
var newPortion = rewardBase.RewardPortion.ToImmutableDictionary(
var newPortion = rewardBase.RewardPortion.ToImmutableSortedDictionary(
kvp => kvp.Key,
kvp => kvp.Value * multiplier);
kvp => kvp.Value * multiplier,
_currencyComparer);

return new RewardBase(
rewardBase.Address,
Expand Down Expand Up @@ -233,7 +234,7 @@ public bool Equals(RewardBase? other)
|| (other is RewardBase rewardBase
&& Address == rewardBase.Address
&& TotalShares == rewardBase.TotalShares
&& RewardPortion.Equals(rewardBase.RewardPortion)
&& RewardPortion.SequenceEqual(rewardBase.RewardPortion)
&& SigFig == rewardBase.SigFig);

public override int GetHashCode()
Expand Down

0 comments on commit dd0592e

Please sign in to comment.