Skip to content

Commit 014ef32

Browse files
committed
bugfix: fix logic error in dpos methods
1 parent 2f876d0 commit 014ef32

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

Lib9c/Action/DPoS/Control/Bond.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ internal static (IWorld, FungibleAssetValue) Cancel(
8181
Address delegationAddress,
8282
IImmutableSet<Currency> nativeTokens)
8383
{
84-
long blockHeight = ctx.BlockIndex;
85-
8684
// Currency check
8785
if (!share.Currency.Equals(Asset.Share))
8886
{

Lib9c/Action/DPoS/Control/DelegateCtrl.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Nekoyume.Action.DPoS.Model;
1010
using Nekoyume.Action.DPoS.Util;
1111
using Nekoyume.Module;
12+
using Serilog;
1213

1314
namespace Nekoyume.Action.DPoS.Control
1415
{
@@ -122,6 +123,12 @@ internal static IWorld Distribute(
122123
delegation.LatestDistributeHeight,
123124
blockHeight);
124125

126+
// Skip if there is no reward to distribute.
127+
if (delegationRewardSum.RawValue == 0)
128+
{
129+
continue;
130+
}
131+
125132
if (!(ValidatorCtrl.TokenPortionByShare(
126133
states,
127134
delegation.ValidatorAddress,

Lib9c/Action/DPoS/Control/ValidatorCtrl.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,17 @@ internal static IWorld Unbond(
214214
validator.UnbondingCompletionBlockHeight = blockHeight + UnbondingSet.Period;
215215
if (validator.Status == BondingStatus.Bonded)
216216
{
217-
states = states.TransferAsset(
218-
ctx,
219-
ReservedAddress.BondedPool,
220-
ReservedAddress.UnbondedPool,
221-
Asset.GovernanceFromConsensus(
222-
states.GetBalance(validator.Address, Asset.ConsensusToken)));
217+
var consensusToken = states.GetBalance(validator.Address, Asset.ConsensusToken);
218+
if (consensusToken.RawValue > 0)
219+
{
220+
// Transfer consensus token to unbonded pool if remaining.
221+
states = states.TransferAsset(
222+
ctx,
223+
ReservedAddress.BondedPool,
224+
ReservedAddress.UnbondedPool,
225+
Asset.GovernanceFromConsensus(
226+
states.GetBalance(validator.Address, Asset.ConsensusToken)));
227+
}
223228
}
224229

225230
validator.Status = BondingStatus.Unbonding;

Lib9c/Action/DPoS/Sys/UpdateValidators.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using Bencodex.Types;
23
using Libplanet.Action;
34
using Libplanet.Action.State;
@@ -36,13 +37,13 @@ public override IWorld Execute(IActionContext context)
3637
states = ValidatorSetCtrl.Update(states, context);
3738
ValidatorSet bondedSet;
3839
(states, bondedSet) = ValidatorSetCtrl.FetchBondedValidatorSet(states);
39-
foreach (var validator in bondedSet.Set)
40-
{
41-
states = states.SetValidator(
42-
new Libplanet.Types.Consensus.Validator(
43-
validator.OperatorPublicKey,
44-
validator.ConsensusToken.RawValue));
45-
}
40+
var validatorSet = new Libplanet.Types.Consensus.ValidatorSet(
41+
bondedSet.Set.Select(
42+
v => new Libplanet.Types.Consensus.Validator(
43+
v.OperatorPublicKey,
44+
v.ConsensusToken.RawValue))
45+
.ToList());
46+
states = states.UpdateValidatorSet(validatorSet);
4647

4748
return states;
4849
}

0 commit comments

Comments
 (0)