Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 30, 2024
1 parent a167055 commit 323328e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 121 deletions.
24 changes: 14 additions & 10 deletions Lib9c/Delegation/Bond.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,34 @@ public bool Equals(Bond? other)
public override int GetHashCode()
=> Address.GetHashCode();

internal Bond AddShare(BigInteger share)
internal Bond AddShare(BigInteger value)
{
if (share.Sign <= 0)
if (value.Sign <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(share),
share,
nameof(value),
value,
"share must be positive.");
}

return new Bond(Address, Share + share, LastDistributeHeight);
var share = Share + value;
var lastDistributeHeight = LastDistributeHeight;
return new Bond(Address, share, lastDistributeHeight);
}

internal Bond SubtractShare(BigInteger share)
internal Bond SubtractShare(BigInteger value)
{
if (share > Share)
if (value > Share)
{
throw new ArgumentOutOfRangeException(
nameof(share),
share,
nameof(value),
value,
"share must be less than or equal to the current share.");
}

return new Bond(Address, Share - share, LastDistributeHeight);
var share = Share - value;
var lastDistributeHeight = share.IsZero ? null : LastDistributeHeight;
return new Bond(Address, share, lastDistributeHeight);
}

internal Bond UpdateLastDistributeHeight(long height)
Expand Down
2 changes: 2 additions & 0 deletions Lib9c/Delegation/CurrencyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Nekoyume.Delegation
{
internal class CurrencyComparer : IComparer<Currency>
{
public static CurrencyComparer Default { get; } = new CurrencyComparer();

public int Compare(Currency x, Currency y)
=> ByteArrayCompare(x.Hash.ToByteArray(), y.Hash.ToByteArray());

Expand Down
21 changes: 8 additions & 13 deletions Lib9c/Delegation/Delegatee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ public Bond Bond(TDelegator delegator, FungibleAssetValue fav, long height)
throw new InvalidOperationException("Cannot bond to tombstoned delegatee.");
}

var bond = Repository.GetBond((TDelegatee)this, delegator.Address);
var oldBond = Repository.GetBond((TDelegatee)this, delegator.Address);
var share = ShareFromFAV(fav);
bond = bond.AddShare(share);
var newBond = oldBond.AddShare(share);
Metadata.AddShare(share);
Metadata.AddDelegatedFAV(fav);
Repository.SetBond(bond);
Repository.SetBond(newBond);
StartNewRewardPeriod(height);
Repository.SetDelegatee((TDelegatee)this);
DelegationChanged?.Invoke(this, height);

return bond;
return newBond;
}

public Bond Unbond(TDelegator delegator, BigInteger share, long height)
Expand All @@ -111,22 +111,17 @@ public Bond Unbond(TDelegator delegator, BigInteger share, long height)
"Cannot unbond without bonding.");
}

var bond = Repository.GetBond((TDelegatee)this, delegator.Address);
var oldBond = Repository.GetBond((TDelegatee)this, delegator.Address);
var fav = FAVFromShare(share);
bond = bond.SubtractShare(share);
if (bond.Share.IsZero)
{
bond = bond.ClearLastDistributeHeight();
}

var newBond = oldBond.SubtractShare(share);
Metadata.RemoveShare(share);
Metadata.RemoveDelegatedFAV(fav);
Repository.SetBond(bond);
Repository.SetBond(newBond);
StartNewRewardPeriod(height);
Repository.SetDelegatee((TDelegatee)this);
DelegationChanged?.Invoke(this, height);

return bond;
return newBond;
}

public void DistributeReward(TDelegator delegator, long height)
Expand Down
129 changes: 69 additions & 60 deletions Lib9c/Delegation/DelegateeMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@

namespace Nekoyume.Delegation
{
public class DelegateeMetadata : IBencodable, IEquatable<DelegateeMetadata>
public struct DelegateeMetadata : IBencodable
{
private const string StateTypeName = "delegatee_metadata";
private const long StateVersion = 1;

private Address? _address;
private readonly IComparer<Currency> _currencyComparer = new CurrencyComparer();

/// <summary>
/// Create a new instance of DelegateeMetadata.
/// </summary>
Expand Down Expand Up @@ -90,7 +87,7 @@ public DelegateeMetadata(
false,
-1L,
false,
ImmutableSortedSet<UnbondingRef>.Empty)
ImmutableArray<UnbondingRef>.Empty)
{
}

Expand Down Expand Up @@ -196,7 +193,7 @@ public DelegateeMetadata(
DelegateeAddress = delegateeAddress;
DelegateeAccountAddress = delegateeAccountAddress;
DelegationCurrency = delegationCurrency;
RewardCurrencies = rewardCurrencies.ToImmutableSortedSet(_currencyComparer);
RewardCurrencies = rewardCurrencies.ToImmutableArray();
DelegationPoolAddress = delegationPoolAddress;
RewardPoolAddress = rewardPoolAddress;
RewardRemainderPoolAddress = rewardRemainderPoolAddress;
Expand All @@ -209,7 +206,7 @@ public DelegateeMetadata(
Jailed = jailed;
JailedUntil = jailedUntil;
Tombstoned = tombstoned;
UnbondingRefs = unbondingRefs.ToImmutableSortedSet();
UnbondingRefs = unbondingRefs.ToImmutableArray();
}

private DelegateeMetadata(
Expand Down Expand Up @@ -255,7 +252,7 @@ private DelegateeMetadata(
DelegateeAddress = delegateeAddress;
DelegateeAccountAddress = delegateeAccountAddress;
DelegationCurrency = delegationCurrency;
RewardCurrencies = rewardCurrencies.ToImmutableSortedSet(_currencyComparer);
RewardCurrencies = rewardCurrencies.ToImmutableArray();
DelegationPoolAddress = delegationPoolAddress;
RewardPoolAddress = rewardPoolAddress;
RewardRemainderPoolAddress = rewardRemainderPoolAddress;
Expand All @@ -268,21 +265,21 @@ private DelegateeMetadata(
Jailed = jailed;
JailedUntil = jailedUntil;
Tombstoned = tombstoned;
UnbondingRefs = unbondingRefs.ToImmutableSortedSet();
UnbondingRefs = unbondingRefs.ToImmutableArray();
}

public Address DelegateeAddress { get; }

public Address DelegateeAccountAddress { get; }

public Address Address
=> _address ??= DelegationAddress.DelegateeMetadataAddress(
public readonly Address Address
=> DelegationAddress.DelegateeMetadataAddress(
DelegateeAddress,
DelegateeAccountAddress);

public Currency DelegationCurrency { get; }

public ImmutableSortedSet<Currency> RewardCurrencies { get; }
public ImmutableArray<Currency> RewardCurrencies { get; }

public Address DelegationPoolAddress { get; internal set; }

Expand All @@ -308,7 +305,7 @@ public Address Address

public bool Tombstoned { get; internal set; }

public ImmutableSortedSet<UnbondingRef> UnbondingRefs { get; private set; }
public ImmutableArray<UnbondingRef> UnbondingRefs { get; private set; }

// TODO : Better serialization
public List Bencoded => List.Empty
Expand All @@ -332,53 +329,65 @@ public Address Address

IValue IBencodable.Bencoded => Bencoded;

public BigInteger ShareFromFAV(FungibleAssetValue fav)
public readonly BigInteger ShareFromFAV(FungibleAssetValue fav)
=> TotalShares.IsZero
? fav.RawValue
: TotalShares * fav.RawValue / TotalDelegatedFAV.RawValue;

public FungibleAssetValue FAVFromShare(BigInteger share)
public readonly FungibleAssetValue FAVFromShare(BigInteger share)
=> TotalShares == share
? TotalDelegatedFAV
: (TotalDelegatedFAV * share).DivRem(TotalShares).Quotient;

public void AddDelegatedFAV(FungibleAssetValue fav)
public readonly DelegateeMetadata AddDelegatedFAV(FungibleAssetValue fav)
{
TotalDelegatedFAV += fav;
var metadata = this;
metadata.TotalDelegatedFAV += fav;
return metadata;
}

public void RemoveDelegatedFAV(FungibleAssetValue fav)
public readonly DelegateeMetadata RemoveDelegatedFAV(FungibleAssetValue fav)
{
TotalDelegatedFAV -= fav;
var metadata = this;
metadata.TotalDelegatedFAV -= fav;
return metadata;
}

public void AddShare(BigInteger share)
public readonly DelegateeMetadata AddShare(BigInteger share)
{
TotalShares += share;
var metadata = this;
metadata.TotalShares += share;
return metadata;
}

public void RemoveShare(BigInteger share)
public readonly DelegateeMetadata RemoveShare(BigInteger share)
{
TotalShares -= share;
var metadata = this;
metadata.TotalShares -= share;
return metadata;
}

public void AddUnbondingRef(UnbondingRef unbondingRef)
public readonly DelegateeMetadata AddUnbondingRef(UnbondingRef unbondingRef)
{
UnbondingRefs = UnbondingRefs.Add(unbondingRef);
var metadata = this;
metadata.UnbondingRefs = UnbondingRefs.Add(unbondingRef);
return metadata;
}

public void RemoveUnbondingRef(UnbondingRef unbondingRef)
public readonly DelegateeMetadata RemoveUnbondingRef(UnbondingRef unbondingRef)
{
UnbondingRefs = UnbondingRefs.Remove(unbondingRef);
var metadata = this;
metadata.UnbondingRefs = UnbondingRefs.Remove(unbondingRef);
return metadata;
}

public Address BondAddress(Address delegatorAddress)
public readonly Address BondAddress(Address delegatorAddress)
=> DelegationAddress.BondAddress(Address, delegatorAddress);

public Address UnbondLockInAddress(Address delegatorAddress)
public readonly Address UnbondLockInAddress(Address delegatorAddress)
=> DelegationAddress.UnbondLockInAddress(Address, delegatorAddress);

public virtual Address RebondGraceAddress(Address delegatorAddress)
public readonly Address RebondGraceAddress(Address delegatorAddress)
=> DelegationAddress.RebondGraceAddress(Address, delegatorAddress);

/// <summary>
Expand All @@ -388,7 +397,7 @@ public virtual Address RebondGraceAddress(Address delegatorAddress)
/// <returns>
/// <see cref="Address"/> of the distribution pool.
/// </returns>
public virtual Address DistributionPoolAddress()
public readonly Address DistributionPoolAddress()
=> DelegationAddress.DistributionPoolAddress(Address);

/// <summary>
Expand All @@ -397,7 +406,7 @@ public virtual Address DistributionPoolAddress()
/// <returns>
/// <see cref="Address"/> of the current <see cref="RewardBase"/>.
/// </returns>
public virtual Address CurrentRewardBaseAddress()
public readonly Address CurrentRewardBaseAddress()
=> DelegationAddress.CurrentRewardBaseAddress(Address);

/// <summary>
Expand All @@ -407,7 +416,7 @@ public virtual Address CurrentRewardBaseAddress()
/// <returns>
/// <see cref="Address"/> of the <see cref="RewardBase"/> at the given height.
/// </returns>
public virtual Address RewardBaseAddress(long height)
public readonly Address RewardBaseAddress(long height)
=> DelegationAddress.RewardBaseAddress(Address, height);

/// <summary>
Expand All @@ -417,7 +426,7 @@ public virtual Address RewardBaseAddress(long height)
/// <returns>
/// <see cref="Address"/> of the current lump sum rewards record.
/// </returns>
public virtual Address CurrentLumpSumRewardsRecordAddress()
public readonly Address CurrentLumpSumRewardsRecordAddress()
=> DelegationAddress.CurrentRewardBaseAddress(Address);

/// <summary>
Expand All @@ -430,37 +439,37 @@ public virtual Address CurrentLumpSumRewardsRecordAddress()
/// <returns>
/// <see cref="Address"/> of the lump sum rewards record at the given height.
/// </returns>
public virtual Address LumpSumRewardsRecordAddress(long height)
public readonly Address LumpSumRewardsRecordAddress(long height)
=> DelegationAddress.RewardBaseAddress(Address, height);

public override bool Equals(object? obj)
=> obj is DelegateeMetadata other && Equals(other);

public virtual bool Equals(DelegateeMetadata? other)
=> ReferenceEquals(this, other)
|| (other is DelegateeMetadata delegatee
&& (GetType() != delegatee.GetType())
&& DelegateeAddress.Equals(delegatee.DelegateeAddress)
&& DelegateeAccountAddress.Equals(delegatee.DelegateeAccountAddress)
&& DelegationCurrency.Equals(delegatee.DelegationCurrency)
&& RewardCurrencies.SequenceEqual(delegatee.RewardCurrencies)
&& DelegationPoolAddress.Equals(delegatee.DelegationPoolAddress)
&& RewardPoolAddress.Equals(delegatee.RewardPoolAddress)
&& RewardRemainderPoolAddress.Equals(delegatee.RewardRemainderPoolAddress)
&& SlashedPoolAddress.Equals(delegatee.SlashedPoolAddress)
&& UnbondingPeriod == delegatee.UnbondingPeriod
&& RewardPoolAddress.Equals(delegatee.RewardPoolAddress)
&& TotalDelegatedFAV.Equals(delegatee.TotalDelegatedFAV)
&& TotalShares.Equals(delegatee.TotalShares)
&& Jailed == delegatee.Jailed
&& UnbondingRefs.SequenceEqual(delegatee.UnbondingRefs));

public override int GetHashCode()
=> DelegateeAddress.GetHashCode();
// public override bool Equals(object? obj)
// => obj is DelegateeMetadata other && Equals(other);

// public bool Equals(DelegateeMetadata? other)
// => ReferenceEquals(this, other)
// || (other is DelegateeMetadata delegatee
// && (GetType() != delegatee.GetType())
// && DelegateeAddress.Equals(delegatee.DelegateeAddress)
// && DelegateeAccountAddress.Equals(delegatee.DelegateeAccountAddress)
// && DelegationCurrency.Equals(delegatee.DelegationCurrency)
// && RewardCurrencies.SequenceEqual(delegatee.RewardCurrencies)
// && DelegationPoolAddress.Equals(delegatee.DelegationPoolAddress)
// && RewardPoolAddress.Equals(delegatee.RewardPoolAddress)
// && RewardRemainderPoolAddress.Equals(delegatee.RewardRemainderPoolAddress)
// && SlashedPoolAddress.Equals(delegatee.SlashedPoolAddress)
// && UnbondingPeriod == delegatee.UnbondingPeriod
// && RewardPoolAddress.Equals(delegatee.RewardPoolAddress)
// && TotalDelegatedFAV.Equals(delegatee.TotalDelegatedFAV)
// && TotalShares.Equals(delegatee.TotalShares)
// && Jailed == delegatee.Jailed
// && UnbondingRefs.SequenceEqual(delegatee.UnbondingRefs));

// public override int GetHashCode()
// => DelegateeAddress.GetHashCode();

// TODO: [GuildMigration] Remove this method when the migration is done.
// Remove private setter for UnbondingPeriod.
public void UpdateUnbondingPeriod(long unbondingPeriod)
public readonly DelegateeMetadata UpdateUnbondingPeriod(long unbondingPeriod)

Check failure on line 472 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

'DelegateeMetadata.UpdateUnbondingPeriod(long)': not all code paths return a value

Check failure on line 472 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-js

'DelegateeMetadata.UpdateUnbondingPeriod(long)': not all code paths return a value
{
UnbondingPeriod = unbondingPeriod;

Check failure on line 474 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-and-test (Release)

Cannot assign to 'UnbondingPeriod' because it is read-only

Check failure on line 474 in Lib9c/Delegation/DelegateeMetadata.cs

View workflow job for this annotation

GitHub Actions / build-js

Cannot assign to 'UnbondingPeriod' because it is read-only
}
Expand Down
Loading

0 comments on commit 323328e

Please sign in to comment.