Skip to content

Commit

Permalink
bugfix: fix logic error in dpos methods
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 9, 2024
1 parent 2f876d0 commit 014ef32
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 0 additions & 2 deletions Lib9c/Action/DPoS/Control/Bond.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ internal static (IWorld, FungibleAssetValue) Cancel(
Address delegationAddress,
IImmutableSet<Currency> nativeTokens)
{
long blockHeight = ctx.BlockIndex;

// Currency check
if (!share.Currency.Equals(Asset.Share))
{
Expand Down
7 changes: 7 additions & 0 deletions Lib9c/Action/DPoS/Control/DelegateCtrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Action.DPoS.Util;
using Nekoyume.Module;
using Serilog;

namespace Nekoyume.Action.DPoS.Control
{
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions Lib9c/Action/DPoS/Control/ValidatorCtrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions Lib9c/Action/DPoS/Sys/UpdateValidators.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
Expand Down Expand Up @@ -36,13 +37,13 @@ public override IWorld Execute(IActionContext context)
states = ValidatorSetCtrl.Update(states, context);
ValidatorSet bondedSet;
(states, bondedSet) = ValidatorSetCtrl.FetchBondedValidatorSet(states);
foreach (var validator in bondedSet.Set)
{
states = states.SetValidator(
new Libplanet.Types.Consensus.Validator(
validator.OperatorPublicKey,
validator.ConsensusToken.RawValue));
}
var validatorSet = new Libplanet.Types.Consensus.ValidatorSet(
bondedSet.Set.Select(
v => new Libplanet.Types.Consensus.Validator(
v.OperatorPublicKey,
v.ConsensusToken.RawValue))
.ToList());
states = states.UpdateValidatorSet(validatorSet);

return states;
}
Expand Down

0 comments on commit 014ef32

Please sign in to comment.