From ce02e8b1fcd6ab61b52e66ec87247a118541babf Mon Sep 17 00:00:00 2001 From: Chanhyuck Ko Date: Wed, 3 Apr 2024 15:08:46 +0900 Subject: [PATCH] refactor: separate PoSAction and implement ActionBase --- Lib9c/Action/DPoS/DPoSBeginBlockAction.cs | 51 ++++++++++++++++ Lib9c/Action/DPoS/DPoSEndBlockAction.cs | 53 +++++++++++++++++ Lib9c/Action/DPoS/PoSAction.cs | 65 --------------------- Lib9c/Action/DPoS/Sys/CancelUndelegation.cs | 8 +-- Lib9c/Action/DPoS/Sys/Delegate.cs | 8 +-- Lib9c/Action/DPoS/Sys/PromoteValidator.cs | 8 +-- Lib9c/Action/DPoS/Sys/Redelegate.cs | 8 +-- Lib9c/Action/DPoS/Sys/Undelegate.cs | 8 +-- Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs | 8 +-- Lib9c/Action/DPoS/Sys/WithdrawValidator.cs | 8 +-- 10 files changed, 132 insertions(+), 93 deletions(-) create mode 100644 Lib9c/Action/DPoS/DPoSBeginBlockAction.cs create mode 100644 Lib9c/Action/DPoS/DPoSEndBlockAction.cs delete mode 100644 Lib9c/Action/DPoS/PoSAction.cs diff --git a/Lib9c/Action/DPoS/DPoSBeginBlockAction.cs b/Lib9c/Action/DPoS/DPoSBeginBlockAction.cs new file mode 100644 index 0000000000..f353c4436e --- /dev/null +++ b/Lib9c/Action/DPoS/DPoSBeginBlockAction.cs @@ -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 +{ + /// + /// A BeginBlock action for DPoS that updates . + /// + public sealed class DPoSBeginBlockAction : ActionBase + { + /// + /// Creates a new instance of . + /// + public DPoSBeginBlockAction() + { + } + + /// + public override IValue PlainValue => new Bencodex.Types.Boolean(true); + + /// + public override void LoadPlainValue(IValue plainValue) + { + // Method intentionally left empty. + } + + /// + 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; + } + } +} diff --git a/Lib9c/Action/DPoS/DPoSEndBlockAction.cs b/Lib9c/Action/DPoS/DPoSEndBlockAction.cs new file mode 100644 index 0000000000..a0179f7e5f --- /dev/null +++ b/Lib9c/Action/DPoS/DPoSEndBlockAction.cs @@ -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 +{ + /// + /// A EndBlock action for DPoS that updates . + /// + public sealed class DPoSEndBlockAction : ActionBase + { + /// + /// Creates a new instance of . + /// + public DPoSEndBlockAction() + { + } + + /// + public override IValue PlainValue => new Bencodex.Types.Boolean(true); + + /// + public override void LoadPlainValue(IValue plainValue) + { + // Method intentionally left empty. + } + + /// + 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; + } + } +} diff --git a/Lib9c/Action/DPoS/PoSAction.cs b/Lib9c/Action/DPoS/PoSAction.cs deleted file mode 100644 index e11f525a0f..0000000000 --- a/Lib9c/Action/DPoS/PoSAction.cs +++ /dev/null @@ -1,65 +0,0 @@ -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 -{ - /// - /// A block action for DPoS that updates . - /// - public sealed class PoSAction : IAction - { - /// - /// Creates a new instance of . - /// - public PoSAction() - { - } - - /// - public IValue PlainValue => new Bencodex.Types.Boolean(true); - - /// - public void LoadPlainValue(IValue plainValue) - { - // Method intentionally left empty. - } - - /// - public IWorld Execute(IActionContext context) - { - IActionContext ctx = context; - var states = ctx.PreviousState; - - // if (ctx.Rehearsal) - // Rehearsal mode is not implemented - states = ValidatorSetCtrl.Update(states, ctx); - var nativeTokens = ImmutableHashSet.Create( - Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share); - - states = AllocateReward.Execute( - states, - ctx, - nativeTokens, - ctx.LastCommit?.Votes, - ctx.Miner); - - // Endblock, Update ValidatorSet - var bondedSet = ValidatorSetCtrl.FetchBondedValidatorSet(states).Item2.Set; - foreach (var validator in bondedSet) - { - states = states.SetValidator( - new Libplanet.Types.Consensus.Validator( - validator.OperatorPublicKey, - validator.ConsensusToken.RawValue)); - } - - return states; - } - } -} diff --git a/Lib9c/Action/DPoS/Sys/CancelUndelegation.cs b/Lib9c/Action/DPoS/Sys/CancelUndelegation.cs index 9e0fd4268d..b174a8f8d8 100644 --- a/Lib9c/Action/DPoS/Sys/CancelUndelegation.cs +++ b/Lib9c/Action/DPoS/Sys/CancelUndelegation.cs @@ -16,7 +16,7 @@ namespace Nekoyume.Action.DPoS.Sys /// of tokens to a given . /// [ActionType(ActionTypeValue)] - public sealed class CancelUndelegation : IAction + public sealed class CancelUndelegation : ActionBase { private const string ActionTypeValue = "cancel_undelegation"; @@ -49,13 +49,13 @@ public CancelUndelegation() public FungibleAssetValue Amount { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { var dict = (Bencodex.Types.Dictionary)plainValue; Validator = dict["validator"].ToAddress(); @@ -63,7 +63,7 @@ public void LoadPlainValue(IValue plainValue) } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState; diff --git a/Lib9c/Action/DPoS/Sys/Delegate.cs b/Lib9c/Action/DPoS/Sys/Delegate.cs index c05fa2e419..2cffc1a716 100644 --- a/Lib9c/Action/DPoS/Sys/Delegate.cs +++ b/Lib9c/Action/DPoS/Sys/Delegate.cs @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys /// of tokens to a given . /// [ActionType(ActionTypeValue)] - public sealed class Delegate : IAction + public sealed class Delegate : ActionBase { private const string ActionTypeValue = "delegate"; @@ -44,13 +44,13 @@ public Delegate() public FungibleAssetValue Amount { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { var dict = (Bencodex.Types.Dictionary)plainValue; Validator = dict["validator"].ToAddress(); @@ -58,7 +58,7 @@ public void LoadPlainValue(IValue plainValue) } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState; diff --git a/Lib9c/Action/DPoS/Sys/PromoteValidator.cs b/Lib9c/Action/DPoS/Sys/PromoteValidator.cs index fdd3bfbc0c..90e00121ca 100644 --- a/Lib9c/Action/DPoS/Sys/PromoteValidator.cs +++ b/Lib9c/Action/DPoS/Sys/PromoteValidator.cs @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys /// A system action for DPoS that promotes non-validator node to a validator. /// [ActionType(ActionTypeValue)] - public sealed class PromoteValidator : IAction + public sealed class PromoteValidator : ActionBase { private const string ActionTypeValue = "promote_validator"; @@ -50,13 +50,13 @@ public PromoteValidator() public FungibleAssetValue Amount { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { var dict = (Bencodex.Types.Dictionary)plainValue; Validator = dict["validator"].ToPublicKey(); @@ -64,7 +64,7 @@ public void LoadPlainValue(IValue plainValue) } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; if (!ctx.Signer.Equals(Validator.Address)) diff --git a/Lib9c/Action/DPoS/Sys/Redelegate.cs b/Lib9c/Action/DPoS/Sys/Redelegate.cs index 724c355a17..b05fa65bb3 100644 --- a/Lib9c/Action/DPoS/Sys/Redelegate.cs +++ b/Lib9c/Action/DPoS/Sys/Redelegate.cs @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys /// of shared tokens to from . /// [ActionType(ActionTypeValue)] - public sealed class Redelegate : IAction + public sealed class Redelegate : ActionBase { private const string ActionTypeValue = "redelegate"; @@ -55,14 +55,14 @@ internal Redelegate() public FungibleAssetValue ShareAmount { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { var dict = (Bencodex.Types.Dictionary)plainValue; SrcValidator = dict["src"].ToAddress(); @@ -71,7 +71,7 @@ public void LoadPlainValue(IValue plainValue) } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState; diff --git a/Lib9c/Action/DPoS/Sys/Undelegate.cs b/Lib9c/Action/DPoS/Sys/Undelegate.cs index 822043af10..1fca561795 100644 --- a/Lib9c/Action/DPoS/Sys/Undelegate.cs +++ b/Lib9c/Action/DPoS/Sys/Undelegate.cs @@ -15,7 +15,7 @@ namespace Nekoyume.Action.DPoS.Sys /// of shared tokens to a given . /// [ActionType(ActionTypeValue)] - public sealed class Undelegate : IAction + public sealed class Undelegate : ActionBase { private const string ActionTypeValue = "undelegate"; @@ -47,13 +47,13 @@ internal Undelegate() public FungibleAssetValue ShareAmount { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { var dict = (Bencodex.Types.Dictionary)plainValue; Validator = dict["validator"].ToAddress(); @@ -61,7 +61,7 @@ public void LoadPlainValue(IValue plainValue) } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState; diff --git a/Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs b/Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs index bf6abcf345..e1cd9b6feb 100644 --- a/Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs +++ b/Lib9c/Action/DPoS/Sys/WithdrawDelegator.cs @@ -16,7 +16,7 @@ namespace Nekoyume.Action.DPoS.Sys /// A system action for DPoS that withdraws reward tokens from given . /// [ActionType(ActionTypeValue)] - public sealed class WithdrawDelegator : IAction + public sealed class WithdrawDelegator : ActionBase { private const string ActionTypeValue = "withdraw_delegator"; @@ -41,18 +41,18 @@ public WithdrawDelegator() public Address Validator { get; set; } /// - 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()); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { Validator = plainValue.ToAddress(); } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState; diff --git a/Lib9c/Action/DPoS/Sys/WithdrawValidator.cs b/Lib9c/Action/DPoS/Sys/WithdrawValidator.cs index ce951a0a3e..3014dae67f 100644 --- a/Lib9c/Action/DPoS/Sys/WithdrawValidator.cs +++ b/Lib9c/Action/DPoS/Sys/WithdrawValidator.cs @@ -14,7 +14,7 @@ namespace Nekoyume.Action.DPoS.Sys /// A system action for DPoS that withdraws commission tokens from . /// [ActionType(ActionTypeValue)] - public sealed class WithdrawValidator : IAction + public sealed class WithdrawValidator : ActionBase { private const string ActionTypeValue = "withdraw_validator"; @@ -26,17 +26,17 @@ public WithdrawValidator() } /// - public IValue PlainValue => Bencodex.Types.Dictionary.Empty + public override IValue PlainValue => Bencodex.Types.Dictionary.Empty .Add("type_id", new Text(ActionTypeValue)); /// - public void LoadPlainValue(IValue plainValue) + public override void LoadPlainValue(IValue plainValue) { // Method intentionally left empty. } /// - public IWorld Execute(IActionContext context) + public override IWorld Execute(IActionContext context) { IActionContext ctx = context; var states = ctx.PreviousState;