From 7ce828b03cfb9c84c852708e31e2c7280fe8ca97 Mon Sep 17 00:00:00 2001 From: s2quake Date: Fri, 27 Dec 2024 18:05:39 +0900 Subject: [PATCH] wip --- .../ReleaseValidatorUnbondings.cs | 3 +- Lib9c/Delegation/Delegatee.cs | 36 +++--- Lib9c/Delegation/DelegateeMetadata.cs | 8 +- Lib9c/Delegation/DelegationRepository.cs | 79 ++++++------ Lib9c/Delegation/Delegator.cs | 22 +--- Lib9c/Delegation/DelegatorMetadata.cs | 6 +- Lib9c/Delegation/IDelegatee.cs | 93 --------------- Lib9c/Delegation/IDelegateeMetadata.cs | 48 -------- Lib9c/Delegation/IDelegationRepository.cs | 112 ------------------ Lib9c/Delegation/IDelegator.cs | 47 -------- Lib9c/Delegation/IDelegatorMetadata.cs | 23 ---- Lib9c/Delegation/IUnbonding.cs | 5 +- Lib9c/Delegation/RebondGrace.cs | 64 ++++------ Lib9c/Delegation/UnbondLockIn.cs | 74 ++++-------- Lib9c/Delegation/UnbondingFactory.cs | 22 ++-- Lib9c/Delegation/UnbondingSet.cs | 47 +++----- Lib9c/Model/Guild/GuildRepository.cs | 3 +- .../ValidatorDelegation/ValidatorDelegatee.cs | 3 - .../ValidatorRepository.cs | 3 +- 19 files changed, 158 insertions(+), 540 deletions(-) delete mode 100644 Lib9c/Delegation/IDelegatee.cs delete mode 100644 Lib9c/Delegation/IDelegateeMetadata.cs delete mode 100644 Lib9c/Delegation/IDelegationRepository.cs delete mode 100644 Lib9c/Delegation/IDelegator.cs delete mode 100644 Lib9c/Delegation/IDelegatorMetadata.cs diff --git a/Lib9c/Action/ValidatorDelegation/ReleaseValidatorUnbondings.cs b/Lib9c/Action/ValidatorDelegation/ReleaseValidatorUnbondings.cs index aabba19fcd..28e6788b1d 100644 --- a/Lib9c/Action/ValidatorDelegation/ReleaseValidatorUnbondings.cs +++ b/Lib9c/Action/ValidatorDelegation/ReleaseValidatorUnbondings.cs @@ -44,7 +44,7 @@ public override IWorld Execute(IActionContext context) var repository = new GuildRepository(world, context); var unbondingSet = repository.GetUnbondingSet(); - var unbondings = unbondingSet.UnbondingsToRelease(context.BlockIndex); + var unbondings = repository.UnbondingsToRelease(context.BlockIndex); unbondings = unbondings.Select(unbonding => { @@ -52,6 +52,7 @@ public override IWorld Execute(IActionContext context) { case UnbondLockIn unbondLockIn: unbondLockIn = unbondLockIn.Release(context.BlockIndex, out var releasedFAV); + repository.Release(unbondLockIn, releasedFAV); repository.SetUnbondLockIn(unbondLockIn); repository.UpdateWorld( Unstake(repository.World, context, unbondLockIn, releasedFAV)); diff --git a/Lib9c/Delegation/Delegatee.cs b/Lib9c/Delegation/Delegatee.cs index d78b40f2d2..a4e38f90ec 100644 --- a/Lib9c/Delegation/Delegatee.cs +++ b/Lib9c/Delegation/Delegatee.cs @@ -10,7 +10,7 @@ namespace Nekoyume.Delegation { - public abstract class Delegatee : IDelegatee + public abstract class Delegatee where TRepository : DelegationRepository where TDelegatee : Delegatee where TDelegator : Delegator @@ -110,15 +110,6 @@ public BigInteger ShareFromFAV(FungibleAssetValue fav) public FungibleAssetValue FAVFromShare(BigInteger share) => Metadata.FAVFromShare(share); - BigInteger IDelegatee.Bond(IDelegator delegator, FungibleAssetValue fav, long height) - => Bond((TDelegator)delegator, fav, height); - - FungibleAssetValue IDelegatee.Unbond(IDelegator delegator, BigInteger share, long height) - => Unbond((TDelegator)delegator, share, height); - - void IDelegatee.DistributeReward(IDelegator delegator, long height) - => DistributeReward((TDelegator)delegator, height); - public void Jail(long releaseHeight) { Metadata.JailedUntil = releaseHeight; @@ -331,6 +322,15 @@ record = record.AddLumpSumRewards(rewards); } } + // private IUnbonding GetUnbondingFromRef( + // UnbondingRef reference) + // => reference.UnbondingType switch + // { + // UnbondingType.UnbondLockIn => Repository.GetUnlimitedUnbondLockIn(reference.Address), + // UnbondingType.RebondGrace => Repository.GetUnlimitedRebondGrace(reference.Address), + // _ => throw new ArgumentException("Invalid unbonding type.") + // }; + public virtual void Slash(BigInteger slashFactor, long infractionHeight, long height) { FungibleAssetValue slashed = TotalDelegated.DivRem(slashFactor, out var rem); @@ -348,9 +348,18 @@ public virtual void Slash(BigInteger slashFactor, long infractionHeight, long he foreach (var item in Metadata.UnbondingRefs) { - var unbonding = UnbondingFactory.GetUnbondingFromRef(item, Repository); + var unbonding = Repository.GetUnbondingFromRef(item); - unbonding = unbonding.Slash(slashFactor, infractionHeight, height, out var slashedFAV); + unbonding = unbonding.Slash(slashFactor, infractionHeight, height, out var slashed1); + + FungibleAssetValue? slashedFAV = null; + foreach (var (address, slashedEach) in slashed1) + { + var delegatee = Repository.GetDelegatee(address); + var delegator = Repository.GetDelegator(unbonding.DelegatorAddress); + delegatee.Unbond(delegator, delegatee.ShareFromFAV(slashedEach), height); + slashedFAV = slashedFAV.HasValue ? slashedFAV + slashedEach : slashedEach; + } if (slashedFAV.HasValue) { @@ -390,9 +399,6 @@ public virtual void Slash(BigInteger slashFactor, long infractionHeight, long he DelegationChanged?.Invoke(this, height); } - void IDelegatee.Slash(BigInteger slashFactor, long infractionHeight, long height) - => Slash(slashFactor, infractionHeight, height); - public void AddUnbondingRef(UnbondingRef reference) => Metadata.AddUnbondingRef(reference); diff --git a/Lib9c/Delegation/DelegateeMetadata.cs b/Lib9c/Delegation/DelegateeMetadata.cs index 29dd536f07..0cbd0235f9 100644 --- a/Lib9c/Delegation/DelegateeMetadata.cs +++ b/Lib9c/Delegation/DelegateeMetadata.cs @@ -12,7 +12,7 @@ namespace Nekoyume.Delegation { - public class DelegateeMetadata : IDelegateeMetadata + public class DelegateeMetadata : IBencodable { private const string StateTypeName = "delegatee_metadata"; private const long StateVersion = 1; @@ -91,7 +91,7 @@ public DelegateeMetadata( -1L, false, ImmutableSortedSet.Empty) - { + { } public DelegateeMetadata( @@ -434,9 +434,9 @@ public virtual Address LumpSumRewardsRecordAddress(long height) => DelegationAddress.RewardBaseAddress(Address, height); public override bool Equals(object? obj) - => obj is IDelegateeMetadata other && Equals(other); + => obj is DelegateeMetadata other && Equals(other); - public virtual bool Equals(IDelegateeMetadata? other) + public virtual bool Equals(DelegateeMetadata? other) => ReferenceEquals(this, other) || (other is DelegateeMetadata delegatee && (GetType() != delegatee.GetType()) diff --git a/Lib9c/Delegation/DelegationRepository.cs b/Lib9c/Delegation/DelegationRepository.cs index 0a8167e737..e9bb4b2d20 100644 --- a/Lib9c/Delegation/DelegationRepository.cs +++ b/Lib9c/Delegation/DelegationRepository.cs @@ -1,4 +1,7 @@ #nullable enable +using System; +using System.Collections.Immutable; +using System.Linq; using Bencodex.Types; using Libplanet.Action; using Libplanet.Action.State; @@ -8,7 +11,7 @@ namespace Nekoyume.Delegation { - public abstract class DelegationRepository : IDelegationRepository + public abstract class DelegationRepository where TRepository : DelegationRepository where TDelegatee : Delegatee where TDelegator : Delegator @@ -172,15 +175,15 @@ public UnbondLockIn GetUnbondLockIn(TDelegatee delegatee, Address delegatorAddre Address address = delegatee.UnbondLockInAddress(delegatorAddress); IValue? value = unbondLockInAccount.GetState(address); return value is IValue bencoded - ? new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, bencoded, this) - : new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, delegatee.Address, delegatorAddress, this); + ? new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, bencoded) + : new UnbondLockIn(address, delegatee.MaxUnbondLockInEntries, delegatee.Address, delegatorAddress); } public UnbondLockIn GetUnlimitedUnbondLockIn(Address address) { IValue? value = unbondLockInAccount.GetState(address); return value is IValue bencoded - ? new UnbondLockIn(address, int.MaxValue, bencoded, this) + ? new UnbondLockIn(address, int.MaxValue, bencoded) : throw new FailedLoadStateException("UnbondLockIn not found."); } @@ -189,22 +192,22 @@ public RebondGrace GetRebondGrace(TDelegatee delegatee, Address delegatorAddress Address address = delegatee.RebondGraceAddress(delegatorAddress); IValue? value = rebondGraceAccount.GetState(address); return value is IValue bencoded - ? new RebondGrace(address, delegatee.MaxRebondGraceEntries, bencoded, this) - : new RebondGrace(address, delegatee.MaxRebondGraceEntries, this); + ? new RebondGrace(address, delegatee.MaxRebondGraceEntries, bencoded) + : new RebondGrace(address, delegatee.MaxRebondGraceEntries); } public RebondGrace GetUnlimitedRebondGrace(Address address) { IValue? value = rebondGraceAccount.GetState(address); return value is IValue bencoded - ? new RebondGrace(address, int.MaxValue, bencoded, this) + ? new RebondGrace(address, int.MaxValue, bencoded) : throw new FailedLoadStateException("RebondGrace not found."); } public UnbondingSet GetUnbondingSet() => unbondingSetAccount.GetState(UnbondingSet.Address) is IValue bencoded - ? new UnbondingSet(bencoded, this) - : new UnbondingSet(this); + ? new UnbondingSet(bencoded) + : new UnbondingSet(); /// public RewardBase? GetCurrentRewardBase(TDelegatee delegatee) @@ -325,35 +328,37 @@ public virtual void UpdateWorld(IWorld world) lumpSumRewardsRecordAccount = world.GetAccount(LumpSumRewardsRecordAccountAddress); } - IDelegatee IDelegationRepository.GetDelegatee(Address address) => GetDelegatee(address); - - IDelegator IDelegationRepository.GetDelegator(Address address) => GetDelegator(address); - - Bond IDelegationRepository.GetBond(IDelegatee delegatee, Address delegatorAddress) - => GetBond((TDelegatee)delegatee, delegatorAddress); - - UnbondLockIn IDelegationRepository.GetUnbondLockIn(IDelegatee delegatee, Address delegatorAddress) - => GetUnbondLockIn((TDelegatee)delegatee, delegatorAddress); - - RebondGrace IDelegationRepository.GetRebondGrace(IDelegatee delegatee, Address delegatorAddress) - => GetRebondGrace((TDelegatee)delegatee, delegatorAddress); - - RewardBase? IDelegationRepository.GetCurrentRewardBase(IDelegatee delegatee) - => GetCurrentRewardBase((TDelegatee)delegatee); - - RewardBase? IDelegationRepository.GetRewardBase(IDelegatee delegatee, long height) - => GetRewardBase((TDelegatee)delegatee, height); - - LumpSumRewardsRecord? IDelegationRepository.GetCurrentLumpSumRewardsRecord(IDelegatee delegatee) - => GetCurrentLumpSumRewardsRecord((TDelegatee)delegatee); - - LumpSumRewardsRecord? IDelegationRepository.GetLumpSumRewardsRecord(IDelegatee delegatee, long height) - => GetLumpSumRewardsRecord((TDelegatee)delegatee, height); + public ImmutableArray UnbondingsToRelease(long height) + { + var unbondingSet = GetUnbondingSet(); + var UnbondingRefs = unbondingSet.UnbondingRefs; + return UnbondingRefs + .TakeWhile(kv => kv.Key <= height) + .SelectMany(kv => kv.Value) + .Select(unbondingRef => GetUnbondingFromRef(unbondingRef)) + .ToImmutableArray(); + } - void IDelegationRepository.SetDelegatee(IDelegatee delegatee) - => SetDelegatee((TDelegatee)delegatee); + public void Release(UnbondLockIn unbondLockIn, FungibleAssetValue? releasedFAV) + { + if (releasedFAV.HasValue) + { + var delegateeMetadata = GetDelegateeMetadata(unbondLockIn.DelegateeAddress); + var delegatorMetadata = GetDelegatorMetadata(unbondLockIn.DelegatorAddress); + TransferAsset( + delegateeMetadata.DelegationPoolAddress, + delegatorMetadata.DelegationPoolAddress, + releasedFAV.Value); + } + } - void IDelegationRepository.SetDelegator(IDelegator delegator) - => SetDelegator((TDelegator)delegator); + internal IUnbonding GetUnbondingFromRef( + UnbondingRef reference) + => reference.UnbondingType switch + { + UnbondingType.UnbondLockIn => GetUnlimitedUnbondLockIn(reference.Address), + UnbondingType.RebondGrace => GetUnlimitedRebondGrace(reference.Address), + _ => throw new ArgumentException("Invalid unbonding type.") + }; } } diff --git a/Lib9c/Delegation/Delegator.cs b/Lib9c/Delegation/Delegator.cs index a526f860db..7c803dc6a8 100644 --- a/Lib9c/Delegation/Delegator.cs +++ b/Lib9c/Delegation/Delegator.cs @@ -8,7 +8,7 @@ namespace Nekoyume.Delegation { - public abstract class Delegator : IDelegator + public abstract class Delegator where TRepository : DelegationRepository where TDelegatee : Delegatee where TDelegator : Delegator @@ -80,10 +80,6 @@ public virtual void Delegate( Repository.SetDelegator((TDelegator)this); } - void IDelegator.Delegate( - IDelegatee delegatee, FungibleAssetValue fav, long height) - => Delegate((TDelegatee)delegatee, fav, height); - public virtual void Undelegate( TDelegatee delegatee, BigInteger share, long height) { @@ -123,11 +119,6 @@ public virtual void Undelegate( Repository.SetDelegator((TDelegator)this); } - void IDelegator.Undelegate( - IDelegatee delegatee, BigInteger share, long height) - => Undelegate((TDelegatee)delegatee, share, height); - - public virtual void Redelegate( TDelegatee srcDelegatee, TDelegatee dstDelegatee, BigInteger share, long height) { @@ -173,10 +164,6 @@ public virtual void Redelegate( Repository.SetDelegator((TDelegator)this); } - void IDelegator.Redelegate( - IDelegatee srcDelegatee, IDelegatee dstDelegatee, BigInteger share, long height) - => Redelegate((TDelegatee)srcDelegatee, (TDelegatee)dstDelegatee, share, height); - public void CancelUndelegate( TDelegatee delegatee, FungibleAssetValue fav, long height) { @@ -214,10 +201,6 @@ public void CancelUndelegate( Repository.SetDelegator((TDelegator)this); } - void IDelegator.CancelUndelegate( - IDelegatee delegatee, FungibleAssetValue fav, long height) - => CancelUndelegate((TDelegatee)delegatee, fav, height); - public void ClaimReward( TDelegatee delegatee, long height) { @@ -225,8 +208,5 @@ public void ClaimReward( delegatee.StartNewRewardPeriod(height); Repository.SetDelegator((TDelegator)this); } - - void IDelegator.ClaimReward(IDelegatee delegatee, long height) - => ClaimReward((TDelegatee)delegatee, height); } } diff --git a/Lib9c/Delegation/DelegatorMetadata.cs b/Lib9c/Delegation/DelegatorMetadata.cs index 9fe2be2088..ec7788c85f 100644 --- a/Lib9c/Delegation/DelegatorMetadata.cs +++ b/Lib9c/Delegation/DelegatorMetadata.cs @@ -8,7 +8,7 @@ namespace Nekoyume.Delegation { - public class DelegatorMetadata : IDelegatorMetadata + public class DelegatorMetadata : IBencodable { private Address? _address; @@ -95,9 +95,9 @@ public void RemoveDelegatee(Address delegatee) } public override bool Equals(object? obj) - => obj is IDelegator other && Equals(other); + => obj is DelegatorMetadata other && Equals(other); - public virtual bool Equals(IDelegator? other) + public virtual bool Equals(DelegatorMetadata? other) => ReferenceEquals(this, other) || (other is DelegatorMetadata delegator && GetType() != delegator.GetType() diff --git a/Lib9c/Delegation/IDelegatee.cs b/Lib9c/Delegation/IDelegatee.cs deleted file mode 100644 index 463f9aaeb0..0000000000 --- a/Lib9c/Delegation/IDelegatee.cs +++ /dev/null @@ -1,93 +0,0 @@ -#nullable enable -using System; -using System.Collections.Immutable; -using System.Numerics; -using Libplanet.Crypto; -using Libplanet.Types.Assets; - -namespace Nekoyume.Delegation -{ - public interface IDelegatee - { - Address Address { get; } - - Address AccountAddress { get; } - - Currency DelegationCurrency { get; } - - ImmutableSortedSet RewardCurrencies { get; } - - Address DelegationPoolAddress { get; } - - Address RewardRemainderPoolAddress { get; } - - long UnbondingPeriod { get; } - - int MaxUnbondLockInEntries { get; } - - int MaxRebondGraceEntries { get; } - - Address RewardPoolAddress { get; } - - FungibleAssetValue TotalDelegated { get; } - - BigInteger TotalShares { get; } - - bool Jailed { get; } - - long JailedUntil { get; } - - bool Tombstoned { get; } - - BigInteger ShareFromFAV(FungibleAssetValue fav); - - FungibleAssetValue FAVFromShare(BigInteger share); - - BigInteger Bond(IDelegator delegator, FungibleAssetValue fav, long height); - - FungibleAssetValue Unbond(IDelegator delegator, BigInteger share, long height); - - void DistributeReward(IDelegator delegator, long height); - - void CollectRewards(long height); - - void Slash(BigInteger slashFactor, long infractionHeight, long height); - - void Jail(long releaseHeight); - - void Unjail(long height); - - void Tombstone(); - - Address BondAddress(Address delegatorAddress); - - Address UnbondLockInAddress(Address delegatorAddress); - - Address RebondGraceAddress(Address delegatorAddress); - - /// - /// Get the of the current . - /// - /// - /// The of the current . - /// - Address CurrentRewardBaseAddress(); - - /// - /// Get the of the at the given height. - /// - /// - /// The height of the . - /// - /// - /// The of the at the given height. - /// - Address RewardBaseAddress(long height); - - Address CurrentLumpSumRewardsRecordAddress(); - - Address LumpSumRewardsRecordAddress(long height); - - event EventHandler? DelegationChanged; - } -} diff --git a/Lib9c/Delegation/IDelegateeMetadata.cs b/Lib9c/Delegation/IDelegateeMetadata.cs deleted file mode 100644 index dcaeff072b..0000000000 --- a/Lib9c/Delegation/IDelegateeMetadata.cs +++ /dev/null @@ -1,48 +0,0 @@ -#nullable enable -using System.Collections.Immutable; -using System.Numerics; -using Bencodex; -using Libplanet.Crypto; -using Libplanet.Types.Assets; - -namespace Nekoyume.Delegation -{ - public interface IDelegateeMetadata : IBencodable - { - Address DelegateeAddress { get; } - - Address DelegateeAccountAddress { get; } - - Address Address { get; } - - Currency DelegationCurrency { get; } - - ImmutableSortedSet RewardCurrencies { get; } - - Address DelegationPoolAddress { get; } - - Address RewardRemainderPoolAddress { get; } - - long UnbondingPeriod { get; } - - int MaxUnbondLockInEntries { get; } - - int MaxRebondGraceEntries { get; } - - Address RewardPoolAddress { get; } - - FungibleAssetValue TotalDelegatedFAV { get; } - - BigInteger TotalShares { get; } - - bool Jailed { get; } - - long JailedUntil { get; } - - bool Tombstoned { get; } - - BigInteger ShareFromFAV(FungibleAssetValue fav); - - FungibleAssetValue FAVFromShare(BigInteger share); - } -} diff --git a/Lib9c/Delegation/IDelegationRepository.cs b/Lib9c/Delegation/IDelegationRepository.cs deleted file mode 100644 index cec2148a04..0000000000 --- a/Lib9c/Delegation/IDelegationRepository.cs +++ /dev/null @@ -1,112 +0,0 @@ -#nullable enable -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Crypto; -using Libplanet.Types.Assets; - -namespace Nekoyume.Delegation -{ - public interface IDelegationRepository - { - Address DelegateeAccountAddress { get; } - - Address DelegatorAccountAddress { get; } - - IWorld World { get; } - - IActionContext ActionContext { get; } - - IDelegatee GetDelegatee(Address address); - - IDelegator GetDelegator(Address address); - - DelegateeMetadata GetDelegateeMetadata(Address delegateeAddress); - - DelegatorMetadata GetDelegatorMetadata(Address delegatorAddress); - - Bond GetBond(IDelegatee delegatee, Address delegatorAddress); - - UnbondLockIn GetUnbondLockIn(IDelegatee delegatee, Address delegatorAddress); - - UnbondLockIn GetUnlimitedUnbondLockIn(Address address); - - RebondGrace GetRebondGrace(IDelegatee delegatee, Address delegatorAddress); - - RebondGrace GetUnlimitedRebondGrace(Address address); - - UnbondingSet GetUnbondingSet(); - - /// - /// Get the current of the . - /// - /// - /// The to get the current . - /// - /// - /// The current of the . - /// - RewardBase? GetCurrentRewardBase(IDelegatee delegatee); - - /// - /// Get the of the - /// at the given . - /// - /// - /// The to get the of. - /// - /// - /// The height to get the at. - /// - /// - /// The of the - /// at the given . - /// - RewardBase? GetRewardBase(IDelegatee delegatee, long height); - - LumpSumRewardsRecord? GetCurrentLumpSumRewardsRecord(IDelegatee delegatee); - - LumpSumRewardsRecord? GetLumpSumRewardsRecord(IDelegatee delegatee, long height); - - FungibleAssetValue GetBalance(Address address, Currency currency); - - void SetDelegatee(IDelegatee delegatee); - - void SetDelegator(IDelegator delegator); - - void SetDelegateeMetadata(DelegateeMetadata delegateeMetadata); - - void SetDelegatorMetadata(DelegatorMetadata delegatorMetadata); - - void SetBond(Bond bond); - - void SetUnbondLockIn(UnbondLockIn unbondLockIn); - - void SetRebondGrace(RebondGrace rebondGrace); - - void SetUnbondingSet(UnbondingSet unbondingSet); - - /// - /// Set the of the . - /// - /// - /// The to set. - /// - void SetRewardBase(RewardBase rewardBase); - - void SetLumpSumRewardsRecord(LumpSumRewardsRecord lumpSumRewardsRecord); - - /// - /// Remove the from the . - /// This is used when the is no longer needed. - /// This can be removed when the migration for is done. - /// - /// - /// The to remove. - /// - void RemoveLumpSumRewardsRecord(LumpSumRewardsRecord lumpSumRewardsRecord); - - void TransferAsset(Address sender, Address recipient, FungibleAssetValue value); - - void UpdateWorld(IWorld world); - } -} diff --git a/Lib9c/Delegation/IDelegator.cs b/Lib9c/Delegation/IDelegator.cs deleted file mode 100644 index ee8e375982..0000000000 --- a/Lib9c/Delegation/IDelegator.cs +++ /dev/null @@ -1,47 +0,0 @@ -#nullable enable -using System; -using System.Collections.Immutable; -using System.Numerics; -using Libplanet.Crypto; -using Libplanet.Types.Assets; - -namespace Nekoyume.Delegation -{ - public interface IDelegator - { - Address Address { get; } - - Address AccountAddress { get; } - - Address DelegationPoolAddress { get; } - - Address RewardAddress { get; } - - ImmutableSortedSet
Delegatees { get; } - - void Delegate( - IDelegatee delegatee, - FungibleAssetValue fav, - long height); - - void Undelegate( - IDelegatee delegatee, - BigInteger share, - long height); - - void Redelegate( - IDelegatee srcDelegatee, - IDelegatee dstDelegatee, - BigInteger share, - long height); - - void CancelUndelegate( - IDelegatee delegatee, - FungibleAssetValue fav, - long height); - - void ClaimReward( - IDelegatee delegatee, - long height); - } -} diff --git a/Lib9c/Delegation/IDelegatorMetadata.cs b/Lib9c/Delegation/IDelegatorMetadata.cs deleted file mode 100644 index c2a15deba7..0000000000 --- a/Lib9c/Delegation/IDelegatorMetadata.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Immutable; -using Bencodex; -using Libplanet.Crypto; - -namespace Nekoyume.Delegation -{ - public interface IDelegatorMetadata : IBencodable - { - Address DelegatorAddress { get; } - - Address DelegatorAccountAddress { get; } - - Address Address { get; } - - Address DelegationPoolAddress { get; } - - ImmutableSortedSet
Delegatees { get; } - - public void AddDelegatee(Address delegatee); - - public void RemoveDelegatee(Address delegatee); - } -} diff --git a/Lib9c/Delegation/IUnbonding.cs b/Lib9c/Delegation/IUnbonding.cs index fae5adbea5..419632a34d 100644 --- a/Lib9c/Delegation/IUnbonding.cs +++ b/Lib9c/Delegation/IUnbonding.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Numerics; using Libplanet.Crypto; using Libplanet.Types.Assets; @@ -8,6 +9,8 @@ public interface IUnbonding { Address Address { get; } + Address DelegatorAddress { get; } + long LowestExpireHeight { get; } bool IsFull { get; } @@ -20,6 +23,6 @@ IUnbonding Slash( BigInteger slashFactor, long infractionHeight, long height, - out FungibleAssetValue? slashedFAV); + out SortedDictionary slashed); } } diff --git a/Lib9c/Delegation/RebondGrace.cs b/Lib9c/Delegation/RebondGrace.cs index ac158207d1..4df4d0beec 100644 --- a/Lib9c/Delegation/RebondGrace.cs +++ b/Lib9c/Delegation/RebondGrace.cs @@ -16,23 +16,20 @@ public sealed class RebondGrace : IUnbonding, IBencodable, IEquatable _entryComparer = new UnbondingEntry.Comparer(); - private readonly IDelegationRepository? _repository; - - public RebondGrace(Address address, int maxEntries, IDelegationRepository? repository = null) + public RebondGrace(Address address, int maxEntries) : this( address, maxEntries, - ImmutableSortedDictionary>.Empty, - repository) + ImmutableSortedDictionary>.Empty) { } - public RebondGrace(Address address, int maxEntries, IValue bencoded, IDelegationRepository? repository = null) - : this(address, maxEntries, (List)bencoded, repository) + public RebondGrace(Address address, int maxEntries, IValue bencoded) + : this(address, maxEntries, (List)bencoded) { } - public RebondGrace(Address address, int maxEntries, List bencoded, IDelegationRepository? repository = null) + public RebondGrace(Address address, int maxEntries, List bencoded) : this( address, maxEntries, @@ -42,17 +39,15 @@ public RebondGrace(Address address, int maxEntries, List bencoded, IDelegationRe ((List)list[1]).Select(e => new UnbondingEntry(e)).ToImmutableList()) : throw new InvalidCastException( $"Unable to cast object of type '{kv.GetType()}' to type '{typeof(List)}'.")) - .ToImmutableSortedDictionary(), - repository) + .ToImmutableSortedDictionary()) { } public RebondGrace( Address address, int maxEntries, - IEnumerable entries, - IDelegationRepository? repository = null) - : this(address, maxEntries, repository) + IEnumerable entries) + : this(address, maxEntries) { foreach (var entry in entries) { @@ -63,8 +58,7 @@ public RebondGrace( private RebondGrace( Address address, int maxEntries, - ImmutableSortedDictionary> entries, - IDelegationRepository? repository) + ImmutableSortedDictionary> entries) { if (maxEntries < 0) { @@ -77,7 +71,6 @@ private RebondGrace( Address = address; MaxEntries = maxEntries; Entries = entries; - _repository = repository; } public Address Address { get; } @@ -88,8 +81,6 @@ private RebondGrace( public Address DelegatorAddress { get; } - public IDelegationRepository? Repository => _repository; - public long LowestExpireHeight => Entries.First().Key; public bool IsFull => Entries.Values.Sum(e => e.Count) >= MaxEntries; @@ -145,11 +136,9 @@ public RebondGrace Slash( BigInteger slashFactor, long infractionHeight, long height, - out FungibleAssetValue? slashedFAV) + out SortedDictionary slashed) { - CannotMutateRelationsWithoutRepository(); - - var slashed = new SortedDictionary(); + slashed = new SortedDictionary(); var updatedEntries = Entries; var entriesToSlash = Entries.TakeWhile(e => e.Key >= infractionHeight); foreach (var (expireHeight, entries) in entriesToSlash) @@ -173,14 +162,14 @@ public RebondGrace Slash( updatedEntries = Entries.SetItem(expireHeight, slashedEntries); } - slashedFAV = null; - foreach (var (address, slashedEach) in slashed) - { - var delegatee = _repository!.GetDelegatee(address); - var delegator = _repository!.GetDelegator(DelegatorAddress); - delegatee.Unbond(delegator, delegatee.ShareFromFAV(slashedEach), height); - slashedFAV = slashedFAV.HasValue ? slashedFAV + slashedEach : slashedEach; - } + // slashedFAV = null; + // foreach (var (address, slashedEach) in slashed) + // { + // var delegatee = _repository!.GetDelegatee(address); + // var delegator = _repository!.GetDelegator(DelegatorAddress); + // delegatee.Unbond(delegator, delegatee.ShareFromFAV(slashedEach), height); + // slashedFAV = slashedFAV.HasValue ? slashedFAV + slashedEach : slashedEach; + // } return UpdateEntries(updatedEntries); @@ -190,8 +179,8 @@ IUnbonding IUnbonding.Slash( BigInteger slashFactor, long infractionHeight, long height, - out FungibleAssetValue? slashedFAV) - => Slash(slashFactor, infractionHeight, height, out slashedFAV); + out SortedDictionary slashed) + => Slash(slashFactor, infractionHeight, height, out slashed); public override bool Equals(object? obj) => obj is RebondGrace other && Equals(other); @@ -246,15 +235,6 @@ private RebondGrace AddEntry(UnbondingEntry entry) private RebondGrace UpdateEntries( ImmutableSortedDictionary> entries) - => new RebondGrace(Address, MaxEntries, entries, _repository); - - private void CannotMutateRelationsWithoutRepository() - { - if (_repository is null) - { - throw new InvalidOperationException( - "Cannot mutate without repository."); - } - } + => new RebondGrace(Address, MaxEntries, entries); } } diff --git a/Lib9c/Delegation/UnbondLockIn.cs b/Lib9c/Delegation/UnbondLockIn.cs index 22987f5cac..1f65db4f24 100644 --- a/Lib9c/Delegation/UnbondLockIn.cs +++ b/Lib9c/Delegation/UnbondLockIn.cs @@ -17,33 +17,28 @@ public sealed class UnbondLockIn : IUnbonding, IBencodable, IEquatable _entryComparer = new UnbondingEntry.Comparer(); - private readonly IDelegationRepository? _repository; - public UnbondLockIn( Address address, int maxEntries, Address delegateeAddress, - Address delegatorAddress, - IDelegationRepository? repository) + Address delegatorAddress) : this( address, maxEntries, delegateeAddress, delegatorAddress, - ImmutableSortedDictionary>.Empty, - repository) + ImmutableSortedDictionary>.Empty) { - _repository = repository; } public UnbondLockIn( - Address address, int maxEntries, IValue bencoded, IDelegationRepository? repository = null) - : this(address, maxEntries, (List)bencoded, repository) + Address address, int maxEntries, IValue bencoded) + : this(address, maxEntries, (List)bencoded) { } public UnbondLockIn( - Address address, int maxEntries, List bencoded, IDelegationRepository? repository = null) + Address address, int maxEntries, List bencoded) : this( address, maxEntries, @@ -56,8 +51,7 @@ public UnbondLockIn( : throw new InvalidCastException( $"Unable to cast object of type '{kv.GetType()}' " + $"to type '{typeof(List)}'.")) - .ToImmutableSortedDictionary(), - repository) + .ToImmutableSortedDictionary()) { } @@ -66,14 +60,12 @@ public UnbondLockIn( int maxEntries, Address delegateeAddress, Address delegatorAddress, - IEnumerable entries, - IDelegationRepository? repository = null) + IEnumerable entries) : this( address, maxEntries, delegateeAddress, - delegatorAddress, - repository) + delegatorAddress) { foreach (var entry in entries) { @@ -86,8 +78,7 @@ private UnbondLockIn( int maxEntries, Address delegateeAddress, Address delegatorAddress, - ImmutableSortedDictionary> entries, - IDelegationRepository? repository) + ImmutableSortedDictionary> entries) { if (maxEntries < 0) { @@ -102,7 +93,6 @@ private UnbondLockIn( Entries = entries; DelegateeAddress = delegateeAddress; DelegatorAddress = delegatorAddress; - _repository = repository; } public Address Address { get; } @@ -139,7 +129,6 @@ public List Bencoded public UnbondLockIn Release(long height, out FungibleAssetValue? releasedFAV) { - CannotMutateRelationsWithoutRepository(); if (height <= 0) { throw new ArgumentOutOfRangeException( @@ -168,18 +157,18 @@ public UnbondLockIn Release(long height, out FungibleAssetValue? releasedFAV) } } - if (releasedFAV.HasValue) - { - if (DelegateeAddress != Addresses.NonValidatorDelegatee) - { - var delegateeMetadata = _repository!.GetDelegateeMetadata(DelegateeAddress); - var delegatorMetadata = _repository.GetDelegatorMetadata(DelegatorAddress); - _repository!.TransferAsset( - delegateeMetadata.DelegationPoolAddress, - delegatorMetadata.DelegationPoolAddress, - releasedFAV.Value); - } - } + // if (releasedFAV.HasValue) + // { + // if (DelegateeAddress != Addresses.NonValidatorDelegatee) + // { + // var delegateeMetadata = _repository!.GetDelegateeMetadata(DelegateeAddress); + // var delegatorMetadata = _repository.GetDelegatorMetadata(DelegatorAddress); + // _repository!.TransferAsset( + // delegateeMetadata.DelegationPoolAddress, + // delegatorMetadata.DelegationPoolAddress, + // releasedFAV.Value); + // } + // } return UpdateEntries(updatedEntries); } @@ -190,9 +179,9 @@ public UnbondLockIn Slash( BigInteger slashFactor, long infractionHeight, long height, - out FungibleAssetValue? slashedFAV) + out SortedDictionary slashedFAV) { - slashedFAV = null; + slashedFAV = new SortedDictionary(); var updatedEntries = Entries; var entriesToSlash = Entries.TakeWhile(e => e.Key >= infractionHeight); foreach (var (expireHeight, entries) in entriesToSlash) @@ -203,9 +192,7 @@ public UnbondLockIn Slash( var slashedEntry = entry.Slash(slashFactor, infractionHeight, out var slashedSingle); int index = slashedEntries.BinarySearch(slashedEntry, _entryComparer); slashedEntries = slashedEntries.Insert(index < 0 ? ~index : index, slashedEntry); - slashedFAV = slashedFAV.HasValue - ? slashedFAV.Value + slashedSingle - : slashedSingle; + slashedFAV.Add(entry.UnbondeeAddress, slashedSingle); } updatedEntries = Entries.SetItem(expireHeight, slashedEntries); @@ -218,7 +205,7 @@ IUnbonding IUnbonding.Slash( BigInteger slashFactor, long infractionHeight, long height, - out FungibleAssetValue? slashedFAV) + out SortedDictionary slashedFAV) => Slash(slashFactor, infractionHeight, height, out slashedFAV); public override bool Equals(object? obj) @@ -320,7 +307,7 @@ internal FungibleAssetValue Cancellable(long height) private UnbondLockIn UpdateEntries( ImmutableSortedDictionary> entries) - => new UnbondLockIn(Address, MaxEntries, DelegateeAddress, DelegatorAddress, entries, _repository); + => new UnbondLockIn(Address, MaxEntries, DelegateeAddress, DelegatorAddress, entries); private UnbondLockIn AddEntry(UnbondingEntry entry) { @@ -342,14 +329,5 @@ private UnbondLockIn AddEntry(UnbondingEntry entry) Entries.Add( entry.ExpireHeight, ImmutableList.Empty.Add(entry))); } - - private void CannotMutateRelationsWithoutRepository() - { - if (_repository is null) - { - throw new InvalidOperationException( - "Cannot mutate without repository."); - } - } } } diff --git a/Lib9c/Delegation/UnbondingFactory.cs b/Lib9c/Delegation/UnbondingFactory.cs index 67f549865b..c8e5134651 100644 --- a/Lib9c/Delegation/UnbondingFactory.cs +++ b/Lib9c/Delegation/UnbondingFactory.cs @@ -5,18 +5,18 @@ namespace Nekoyume.Delegation { public static class UnbondingFactory { - public static IUnbonding GetUnbondingFromRef( - UnbondingRef reference, IDelegationRepository repository) - => reference.UnbondingType switch - { - UnbondingType.UnbondLockIn => repository.GetUnlimitedUnbondLockIn(reference.Address), - UnbondingType.RebondGrace => repository.GetUnlimitedRebondGrace(reference.Address), - _ => throw new ArgumentException("Invalid unbonding type.") - }; + // public static IUnbonding GetUnbondingFromRef( + // UnbondingRef reference, IDelegationRepository repository) + // => reference.UnbondingType switch + // { + // UnbondingType.UnbondLockIn => repository.GetUnlimitedUnbondLockIn(reference.Address), + // UnbondingType.RebondGrace => repository.GetUnlimitedRebondGrace(reference.Address), + // _ => throw new ArgumentException("Invalid unbonding type.") + // }; - public static IUnbonding GetUnbondingFromRef( - IValue bencoded, IDelegationRepository repository) - => GetUnbondingFromRef(new UnbondingRef(bencoded), repository); + // public static IUnbonding GetUnbondingFromRef( + // IValue bencoded, IDelegationRepository repository) + // => GetUnbondingFromRef(new UnbondingRef(bencoded), repository); public static UnbondingRef ToReference(IUnbonding unbonding) => unbonding switch diff --git a/Lib9c/Delegation/UnbondingSet.cs b/Lib9c/Delegation/UnbondingSet.cs index 98a4b53fd5..aeb8095e7e 100644 --- a/Lib9c/Delegation/UnbondingSet.cs +++ b/Lib9c/Delegation/UnbondingSet.cs @@ -11,23 +11,21 @@ namespace Nekoyume.Delegation { public sealed class UnbondingSet : IBencodable { - private readonly IDelegationRepository _repository; private ImmutableSortedDictionary _lowestExpireHeights; - public UnbondingSet(IDelegationRepository repository) + public UnbondingSet() : this( ImmutableSortedDictionary>.Empty, - ImmutableSortedDictionary.Empty, - repository) + ImmutableSortedDictionary.Empty) { } - public UnbondingSet(IValue bencoded, IDelegationRepository repository) - : this((List)bencoded, repository) + public UnbondingSet(IValue bencoded) + : this((List)bencoded) { } - public UnbondingSet(List bencoded, IDelegationRepository repository) + public UnbondingSet(List bencoded) : this( ((List)bencoded[0]).Select( kv => new KeyValuePair>( @@ -38,19 +36,16 @@ public UnbondingSet(List bencoded, IDelegationRepository repository) kv => new KeyValuePair( new UnbondingRef(((List)kv)[0]), (Integer)((List)kv)[1])) - .ToImmutableSortedDictionary(), - repository) + .ToImmutableSortedDictionary()) { } private UnbondingSet( ImmutableSortedDictionary> unbondings, - ImmutableSortedDictionary lowestExpireHeights, - IDelegationRepository repository) + ImmutableSortedDictionary lowestExpireHeights) { UnbondingRefs = unbondings; _lowestExpireHeights = lowestExpireHeights; - _repository = repository; } public static Address Address => new Address( @@ -63,8 +58,6 @@ private UnbondingSet( public ImmutableArray FlattenedUnbondingRefs => UnbondingRefs.Values.SelectMany(e => e).ToImmutableArray(); - public IDelegationRepository Repository => _repository; - public List Bencoded => List.Empty .Add(new List( @@ -82,12 +75,12 @@ public List Bencoded public bool IsEmpty => UnbondingRefs.IsEmpty; - public ImmutableArray UnbondingsToRelease(long height) - => UnbondingRefs - .TakeWhile(kv => kv.Key <= height) - .SelectMany(kv => kv.Value) - .Select(unbondingRef => UnbondingFactory.GetUnbondingFromRef(unbondingRef, _repository)) - .ToImmutableArray(); + // public ImmutableArray UnbondingsToRelease(long height) + // => UnbondingRefs + // .TakeWhile(kv => kv.Key <= height) + // .SelectMany(kv => kv.Value) + // .Select(unbondingRef => UnbondingFactory.GetUnbondingFromRef(unbondingRef)) + // .ToImmutableArray(); public UnbondingSet SetUnbondings(IEnumerable unbondings) { @@ -111,7 +104,7 @@ public UnbondingSet SetUnbonding(IUnbonding unbonding) catch (ArgumentException) { return this; - } + } } UnbondingRef unbondigRef = UnbondingFactory.ToReference(unbonding); @@ -129,8 +122,7 @@ public UnbondingSet SetUnbonding(IUnbonding unbonding) unbonding.LowestExpireHeight, refs.Add(unbondigRef)), _lowestExpireHeights.SetItem( - unbondigRef, unbonding.LowestExpireHeight), - _repository); + unbondigRef, unbonding.LowestExpireHeight)); } return new UnbondingSet( @@ -138,8 +130,7 @@ public UnbondingSet SetUnbonding(IUnbonding unbonding) unbonding.LowestExpireHeight, ImmutableSortedSet.Empty.Add(unbondigRef)), _lowestExpireHeights.SetItem( - unbondigRef, unbonding.LowestExpireHeight), - _repository); + unbondigRef, unbonding.LowestExpireHeight)); } private UnbondingSet RemoveUnbonding(IUnbonding unbonding) @@ -155,14 +146,12 @@ private UnbondingSet RemoveUnbonding(IUnbonding unbonding) { return new UnbondingSet( UnbondingRefs.Remove(expireHeight), - _lowestExpireHeights.Remove(unbondigRef), - _repository); + _lowestExpireHeights.Remove(unbondigRef)); } return new UnbondingSet( UnbondingRefs.SetItem(expireHeight, refs), - _lowestExpireHeights.Remove(unbondigRef), - _repository); + _lowestExpireHeights.Remove(unbondigRef)); } else { diff --git a/Lib9c/Model/Guild/GuildRepository.cs b/Lib9c/Model/Guild/GuildRepository.cs index 6cd46fa2f2..d06c7b29f0 100644 --- a/Lib9c/Model/Guild/GuildRepository.cs +++ b/Lib9c/Model/Guild/GuildRepository.cs @@ -6,6 +6,7 @@ using Nekoyume.Delegation; using Nekoyume.TypedAddress; using Nekoyume.Model.Stake; +using Nekoyume.ValidatorDelegation; namespace Nekoyume.Model.Guild { @@ -18,7 +19,7 @@ public class GuildRepository private IAccount _guildAccount; private IAccount _guildParticipantAccount; - public GuildRepository(IDelegationRepository repository) + public GuildRepository(ValidatorRepository repository) : this(repository.World, repository.ActionContext) { } diff --git a/Lib9c/ValidatorDelegation/ValidatorDelegatee.cs b/Lib9c/ValidatorDelegation/ValidatorDelegatee.cs index 92db73c64e..4e3025af2a 100644 --- a/Lib9c/ValidatorDelegation/ValidatorDelegatee.cs +++ b/Lib9c/ValidatorDelegation/ValidatorDelegatee.cs @@ -289,9 +289,6 @@ public bool Equals(ValidatorDelegatee? other) && CommissionPercentage == validatorDelegatee.CommissionPercentage && CommissionPercentageLastUpdateHeight == validatorDelegatee.CommissionPercentageLastUpdateHeight; - public bool Equals(IDelegatee? other) - => Equals(other as ValidatorDelegatee); - public override bool Equals(object? obj) => Equals(obj as ValidatorDelegatee); diff --git a/Lib9c/ValidatorDelegation/ValidatorRepository.cs b/Lib9c/ValidatorDelegation/ValidatorRepository.cs index 814141b92d..f3d9e2a8e5 100644 --- a/Lib9c/ValidatorDelegation/ValidatorRepository.cs +++ b/Lib9c/ValidatorDelegation/ValidatorRepository.cs @@ -6,6 +6,7 @@ using Libplanet.Crypto; using Nekoyume.Action; using Nekoyume.Delegation; +using Nekoyume.Model.Guild; namespace Nekoyume.ValidatorDelegation { @@ -16,7 +17,7 @@ public sealed class ValidatorRepository private IAccount _validatorListAccount; - public ValidatorRepository(IDelegationRepository repository) + public ValidatorRepository(GuildRepository repository) : this(repository.World, repository.ActionContext) { }