From 939328c0b20cbb83219255367af4c9dbc1485d1c Mon Sep 17 00:00:00 2001 From: Chanhyuck Ko Date: Fri, 5 Apr 2024 16:04:34 +0900 Subject: [PATCH] bugfix: fix logic error in dpos methods --- Lib9c/Action/DPoS/Control/Bond.cs | 2 -- Lib9c/Action/DPoS/Control/DelegateCtrl.cs | 7 +++++++ Lib9c/Action/DPoS/Control/ValidatorCtrl.cs | 17 +++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Lib9c/Action/DPoS/Control/Bond.cs b/Lib9c/Action/DPoS/Control/Bond.cs index e1e535ad52..a834fc46c8 100644 --- a/Lib9c/Action/DPoS/Control/Bond.cs +++ b/Lib9c/Action/DPoS/Control/Bond.cs @@ -81,8 +81,6 @@ internal static (IWorld, FungibleAssetValue) Cancel( Address delegationAddress, IImmutableSet nativeTokens) { - long blockHeight = ctx.BlockIndex; - // Currency check if (!share.Currency.Equals(Asset.Share)) { diff --git a/Lib9c/Action/DPoS/Control/DelegateCtrl.cs b/Lib9c/Action/DPoS/Control/DelegateCtrl.cs index b6b3f718d7..2f3da3c31d 100644 --- a/Lib9c/Action/DPoS/Control/DelegateCtrl.cs +++ b/Lib9c/Action/DPoS/Control/DelegateCtrl.cs @@ -9,6 +9,7 @@ using Nekoyume.Action.DPoS.Model; using Nekoyume.Action.DPoS.Util; using Nekoyume.Module; +using Serilog; namespace Nekoyume.Action.DPoS.Control { @@ -122,6 +123,12 @@ internal static IWorld Distribute( delegation.LatestDistributeHeight, blockHeight); + // Skip if there is no reward to distribute. + if (delegationRewardSum.RawValue == 0) + { + continue; + } + if (!(ValidatorCtrl.TokenPortionByShare( states, delegation.ValidatorAddress, diff --git a/Lib9c/Action/DPoS/Control/ValidatorCtrl.cs b/Lib9c/Action/DPoS/Control/ValidatorCtrl.cs index 8bf071f46e..36536cea6f 100644 --- a/Lib9c/Action/DPoS/Control/ValidatorCtrl.cs +++ b/Lib9c/Action/DPoS/Control/ValidatorCtrl.cs @@ -214,12 +214,17 @@ internal static IWorld Unbond( validator.UnbondingCompletionBlockHeight = blockHeight + UnbondingSet.Period; if (validator.Status == BondingStatus.Bonded) { - states = states.TransferAsset( - ctx, - ReservedAddress.BondedPool, - ReservedAddress.UnbondedPool, - Asset.GovernanceFromConsensus( - states.GetBalance(validator.Address, Asset.ConsensusToken))); + var consensusToken = states.GetBalance(validator.Address, Asset.ConsensusToken); + if (consensusToken.RawValue > 0) + { + // Transfer consensus token to unbonded pool if remaining. + states = states.TransferAsset( + ctx, + ReservedAddress.BondedPool, + ReservedAddress.UnbondedPool, + Asset.GovernanceFromConsensus( + states.GetBalance(validator.Address, Asset.ConsensusToken))); + } } validator.Status = BondingStatus.Unbonding;