Skip to content

Commit

Permalink
Merge pull request #2507 from limebell/dpos/actionbase
Browse files Browse the repository at this point in the history
Separate PoSAction and implement ActionBase
  • Loading branch information
limebell authored Apr 3, 2024
2 parents 38f8d2f + ce02e8b commit d457a6a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 93 deletions.
51 changes: 51 additions & 0 deletions Lib9c/Action/DPoS/DPoSBeginBlockAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Module;

namespace Nekoyume.Action.DPoS
{
/// <summary>
/// A BeginBlock action for DPoS that updates <see cref="ValidatorSet"/>.
/// </summary>
public sealed class DPoSBeginBlockAction : ActionBase
{
/// <summary>
/// Creates a new instance of <see cref="DPoSBeginBlockAction"/>.
/// </summary>
public DPoSBeginBlockAction()
{
}

/// <inheritdoc cref="IAction.PlainValue"/>
public override IValue PlainValue => new Bencodex.Types.Boolean(true);

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public override void LoadPlainValue(IValue plainValue)
{
// Method intentionally left empty.
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public override IWorld Execute(IActionContext context)
{
var states = context.PreviousState;

// Allocate reward
var nativeTokens = ImmutableHashSet.Create(
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share);
states = AllocateReward.Execute(
states,
context,
nativeTokens,
context.LastCommit?.Votes,
context.Miner);

return states;
}
}
}
53 changes: 53 additions & 0 deletions Lib9c/Action/DPoS/DPoSEndBlockAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Module;

namespace Nekoyume.Action.DPoS
{
/// <summary>
/// A EndBlock action for DPoS that updates <see cref="ValidatorSet"/>.
/// </summary>
public sealed class DPoSEndBlockAction : ActionBase
{
/// <summary>
/// Creates a new instance of <see cref="DPoSBeginBlockAction"/>.
/// </summary>
public DPoSEndBlockAction()
{
}

/// <inheritdoc cref="IAction.PlainValue"/>
public override IValue PlainValue => new Bencodex.Types.Boolean(true);

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public override void LoadPlainValue(IValue plainValue)
{
// Method intentionally left empty.
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public override IWorld Execute(IActionContext context)
{
var states = context.PreviousState;

// Update ValidatorSet
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));
}

return states;
}
}
}
65 changes: 0 additions & 65 deletions Lib9c/Action/DPoS/PoSAction.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/CancelUndelegation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// <see cref="Amount"/> of tokens to a given <see cref="Validator"/>.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class CancelUndelegation : IAction
public sealed class CancelUndelegation : ActionBase
{
private const string ActionTypeValue = "cancel_undelegation";

Expand Down Expand Up @@ -49,21 +49,21 @@ public CancelUndelegation()
public FungibleAssetValue Amount { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validator", Validator.Serialize())
.Add("amount", Amount.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
Validator = dict["validator"].ToAddress();
Amount = dict["amount"].ToFungibleAssetValue();
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
var states = ctx.PreviousState;
Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/Delegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// of tokens to a given <see cref="Validator"/>.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class Delegate : IAction
public sealed class Delegate : ActionBase
{
private const string ActionTypeValue = "delegate";

Expand Down Expand Up @@ -44,21 +44,21 @@ public Delegate()
public FungibleAssetValue Amount { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validator", Validator.Serialize())
.Add("amount", Amount.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
Validator = dict["validator"].ToAddress();
Amount = dict["amount"].ToFungibleAssetValue();
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
var states = ctx.PreviousState;
Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/PromoteValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// A system action for DPoS that promotes non-validator node to a validator.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class PromoteValidator : IAction
public sealed class PromoteValidator : ActionBase
{
private const string ActionTypeValue = "promote_validator";

Expand Down Expand Up @@ -50,21 +50,21 @@ public PromoteValidator()
public FungibleAssetValue Amount { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validator", Validator.Serialize())
.Add("amount", Amount.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
Validator = dict["validator"].ToPublicKey();
Amount = dict["amount"].ToFungibleAssetValue();
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
if (!ctx.Signer.Equals(Validator.Address))
Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/Redelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// of shared tokens to <see cref="DstValidator"/> from <see cref="SrcValidator"/>.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class Redelegate : IAction
public sealed class Redelegate : ActionBase
{
private const string ActionTypeValue = "redelegate";

Expand Down Expand Up @@ -55,14 +55,14 @@ internal Redelegate()
public FungibleAssetValue ShareAmount { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("src", SrcValidator.Serialize())
.Add("dst", DstValidator.Serialize())
.Add("amount", ShareAmount.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
SrcValidator = dict["src"].ToAddress();
Expand All @@ -71,7 +71,7 @@ public void LoadPlainValue(IValue plainValue)
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
var states = ctx.PreviousState;
Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/Undelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// <see cref="ShareAmount"/> of shared tokens to a given <see cref="Validator"/>.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class Undelegate : IAction
public sealed class Undelegate : ActionBase
{
private const string ActionTypeValue = "undelegate";

Expand Down Expand Up @@ -47,21 +47,21 @@ internal Undelegate()
public FungibleAssetValue ShareAmount { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validator", Validator.Serialize())
.Add("amount", ShareAmount.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
Validator = dict["validator"].ToAddress();
ShareAmount = dict["amount"].ToFungibleAssetValue();
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
var states = ctx.PreviousState;
Expand Down
8 changes: 4 additions & 4 deletions Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Nekoyume.Action.DPoS.Sys
/// A system action for DPoS that withdraws reward tokens from given <see cref="Validator"/>.
/// </summary>
[ActionType(ActionTypeValue)]
public sealed class WithdrawDelegator : IAction
public sealed class WithdrawDelegator : ActionBase
{
private const string ActionTypeValue = "withdraw_delegator";

Expand All @@ -41,18 +41,18 @@ public WithdrawDelegator()
public Address Validator { get; set; }

/// <inheritdoc cref="IAction.PlainValue"/>
public IValue PlainValue => Bencodex.Types.Dictionary.Empty
public override IValue PlainValue => Bencodex.Types.Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validator", Validator.Serialize());

/// <inheritdoc cref="IAction.LoadPlainValue(IValue)"/>
public void LoadPlainValue(IValue plainValue)
public override void LoadPlainValue(IValue plainValue)
{
Validator = plainValue.ToAddress();
}

/// <inheritdoc cref="IAction.Execute(IActionContext)"/>
public IWorld Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IActionContext ctx = context;
var states = ctx.PreviousState;
Expand Down
Loading

0 comments on commit d457a6a

Please sign in to comment.