Skip to content

Commit

Permalink
feat: prepare initial states
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 25, 2024
1 parent 0750a86 commit b093458
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 24 deletions.
7 changes: 4 additions & 3 deletions Lib9c.MessagePack/Action/NCActionEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Libplanet.Common;
using Libplanet.Types.Tx;
using MessagePack;
using Nekoyume.Action.DPoS;

namespace Nekoyume.Action
{
Expand All @@ -19,7 +20,7 @@ public struct NCActionEvaluation
[Key(0)]

[MessagePackFormatter(typeof(NCActionFormatter))]
public ActionBase? Action { get; set; }
public ActionBase Action { get; set; }

[Key(1)]
[MessagePackFormatter(typeof(AddressFormatter))]
Expand Down Expand Up @@ -55,7 +56,7 @@ public struct NCActionEvaluation

[SerializationConstructor]
public NCActionEvaluation(
ActionBase? action,
ActionBase action,
Address signer,
long blockIndex,
HashDigest<SHA256> outputStates,
Expand All @@ -81,7 +82,7 @@ public ActionEvaluation<ActionBase> ToActionEvaluation()
{
return new ActionEvaluation<ActionBase>
{
Action = Action is null ? new RewardGold() : Action,
Action = Action,
Signer = Signer,
BlockIndex = BlockIndex,
OutputState = OutputState,
Expand Down
11 changes: 9 additions & 2 deletions Lib9c.Policy/Policy/BlockPolicySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using Nekoyume.Action.DPoS;
using Nekoyume.Action.DPoS.Sys;

#if UNITY_EDITOR || UNITY_STANDALONE
Expand Down Expand Up @@ -131,8 +132,14 @@ internal IBlockPolicy GetPolicy(

// FIXME: Slight inconsistency due to pre-existing delegate.
return new BlockPolicy(
new IAction[] { }.ToImmutableArray(),
new IAction[] { new RewardGold() }.ToImmutableArray(),
beginBlockActions: new IAction[] { new AllocateReward() }.ToImmutableArray(),
endBlockActions: new IAction[]
{
new UpdateValidators(),
new RecordProposer(),
}.ToImmutableArray(),
beginTxActions: new IAction[] { new Mortgage() }.ToImmutableArray(),
endTxActions: new IAction[] { new Refund(), new Reward() }.ToImmutableArray(),
blockInterval: BlockInterval,
validateNextBlockTx: validateNextBlockTx,
validateNextBlock: validateNextBlock,
Expand Down
10 changes: 6 additions & 4 deletions Lib9c.Policy/Policy/DebugPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.DPoS;
using Nekoyume.Action.DPoS.Sys;

namespace Nekoyume.Blockchain.Policy
{
Expand All @@ -15,16 +17,16 @@ public DebugPolicy()
}

public ImmutableArray<IAction> BeginBlockActions { get; } =
new IAction[] { }.ToImmutableArray();
new IAction[] { new AllocateReward() }.ToImmutableArray();

public ImmutableArray<IAction> EndBlockActions { get; } =
new IAction[] { new RewardGold() }.ToImmutableArray();
new IAction[] { new UpdateValidators(), new RecordProposer() }.ToImmutableArray();

public ImmutableArray<IAction> BeginTxActions { get; } =
new IAction[] { }.ToImmutableArray();
new IAction[] { new Mortgage() }.ToImmutableArray();

public ImmutableArray<IAction> EndTxActions { get; } =
new IAction[] { }.ToImmutableArray();
new IAction[] { new Refund(), new Reward() }.ToImmutableArray();

public TxPolicyViolationException ValidateNextBlockTx(
BlockChain blockChain, Transaction transaction)
Expand Down
8 changes: 2 additions & 6 deletions Lib9c.Renderers/Renderers/ActionRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public ActionRenderer()
public void RenderAction(IValue action, ICommittedActionContext context, HashDigest<SHA256> nextState) =>
ActionRenderSubject.OnNext(new ActionEvaluation<ActionBase>
{
Action = context.IsBlockAction
? new RewardGold()
: (ActionBase)_actionLoader.LoadAction(context.BlockIndex, action),
Action = (ActionBase)_actionLoader.LoadAction(context.BlockIndex, action),
Signer = context.Signer,
BlockIndex = context.BlockIndex,
TxId = context.TxId,
Expand All @@ -52,9 +50,7 @@ public void RenderActionError(IValue action, ICommittedActionContext context, Ex
Log.Error(exception, "{action} execution failed.", action);
ActionRenderSubject.OnNext(new ActionEvaluation<ActionBase>
{
Action = context.IsBlockAction
? new RewardGold()
: (ActionBase)_actionLoader.LoadAction(context.BlockIndex, action),
Action = (ActionBase)_actionLoader.LoadAction(context.BlockIndex, action),
Signer = context.Signer,
BlockIndex = context.BlockIndex,
TxId = context.TxId,
Expand Down
15 changes: 7 additions & 8 deletions Lib9c.Utils/BlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Libplanet.Types.Consensus;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.DPoS;
using Nekoyume.Action.Loader;
using Nekoyume.Blockchain.Policy;
using Nekoyume.Model.State;
Expand All @@ -38,8 +39,8 @@ public static Block ProposeGenesisBlock(
DateTimeOffset? timestamp = null,
IEnumerable<ActionBase>? actionBases = null,
Currency? goldCurrency = null,
ISet<Address>? assetMinters = null
)
ISet<Address>? assetMinters = null,
Dictionary<Address, FungibleAssetValue>? initialFavs = null)
{
if (!tableSheets.TryGetValue(nameof(GameConfigSheet), out var csv))
{
Expand Down Expand Up @@ -74,7 +75,8 @@ public static Block ProposeGenesisBlock(
pendingActivationStates: pendingActivationStates,
authorizedMinersState: authorizedMinersState,
creditsState: credits is null ? null : new CreditsState(credits),
assetMinters: assetMinters
assetMinters: assetMinters,
initialFavs: initialFavs
);
List<ActionBase> actions = new List<ActionBase>
{
Expand All @@ -84,11 +86,8 @@ public static Block ProposeGenesisBlock(
{
new Initialize(
states: ImmutableDictionary.Create<Address, IValue>(),
validatorSet: new ValidatorSet(
initialValidators.Select(validator =>
new Validator(validator.Key, validator.Value)).ToList()
)
),
validatorSet: new ValidatorSet()),
new InitializeValidators(initialValidators)
};
if (!(actionBases is null))
{
Expand Down
94 changes: 94 additions & 0 deletions Lib9c/Action/DPoS/InitializeValidators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Util;
using Nekoyume.Module;
using Serilog;

namespace Nekoyume.Action.DPoS
{
[ActionType(ActionTypeValue)]
public sealed class InitializeValidators : ActionBase
{
private const string ActionTypeValue = "initialize_validators";

public InitializeValidators(Dictionary<PublicKey, BigInteger> validators)
{
Validators = validators.ToImmutableDictionary();
}

public InitializeValidators()
{
}

public ImmutableDictionary<PublicKey, BigInteger> Validators { get; set; }

public override IValue PlainValue {
get
{
var validators = Dictionary.Empty;
#pragma warning disable LAA1002
foreach (var (validator, power) in Validators)
{
validators = validators.Add((Binary)validator.Serialize(), power);
}
#pragma warning restore LAA1002

return Dictionary.Empty
.Add("type_id", new Text(ActionTypeValue))
.Add("validators", validators);
}
}

public override void LoadPlainValue(IValue plainValue)
{
var dict = (Bencodex.Types.Dictionary)plainValue;
var validatorDict = (Dictionary)dict["validators"];
Validators = validatorDict.Select(
pair =>
{
var key = pair.Key.ToPublicKey();
var power = (Integer)pair.Value;
return new KeyValuePair<PublicKey, BigInteger>(key, power);
}).ToImmutableDictionary();
}

public override IWorld Execute(IActionContext context)
{
Log.Debug("InitializeValidators");
context.UseGas(1);
var states = context.PreviousState;

var nativeTokens = ImmutableHashSet.Create(
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share);
#pragma warning disable LAA1002
foreach(var (validator, power) in Validators)
{
var amount = new FungibleAssetValue(Asset.GovernanceToken, power, 0);
states = states.MintAsset(
context,
validator.Address,
amount);
states = ValidatorCtrl.Create(
states,
context,
validator.Address,
validator,
amount,
nativeTokens);
}
#pragma warning restore LAA1002

Log.Debug("InitializeValidators complete");
return states;
}
}
}
38 changes: 37 additions & 1 deletion Lib9c/Action/InitializeStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Nekoyume.Model.State;
using Nekoyume.Module;
using Libplanet.Crypto;
using Libplanet.Types.Assets;

namespace Nekoyume.Action
{
Expand Down Expand Up @@ -57,6 +58,8 @@ public class InitializeStates : GameAction, IInitializeStatesV1

public ISet<Address> AssetMinters { get; set; }

public Dictionary InitialFavs { get; set; }

Dictionary IInitializeStatesV1.Ranking => Ranking;
Dictionary IInitializeStatesV1.Shop => Shop;
Dictionary<string, string> IInitializeStatesV1.TableSheets => TableSheets;
Expand Down Expand Up @@ -87,7 +90,8 @@ public InitializeStates(
AdminState adminAddressState = null,
AuthorizedMinersState authorizedMinersState = null,
CreditsState creditsState = null,
ISet<Address> assetMinters = null)
ISet<Address> assetMinters = null,
Dictionary<Address, FungibleAssetValue> initialFavs = null)
{
Ranking = (Dictionary)rankingState.Serialize();
Shop = (Dictionary)shopState.Serialize();
Expand All @@ -114,6 +118,17 @@ public InitializeStates(
{
AssetMinters = assetMinters;
}

if (!(initialFavs is null))
{
#pragma warning disable LAA1002
InitialFavs = new Dictionary(
initialFavs.Select(
kv => new KeyValuePair<Binary, IValue>(
(Binary)kv.Key.Serialize(),
kv.Value.Serialize())));
#pragma warning restore LAA1002
}
}

public override IWorld Execute(IActionContext context)
Expand Down Expand Up @@ -192,6 +207,17 @@ public override IWorld Execute(IActionContext context)
);
}

if (InitialFavs is { })
{
foreach (var (address, fav) in InitialFavs)
{
states = states.MintAsset(
ctx,
new Address(address),
new FungibleAssetValue(fav));
}
}

return states;
}

Expand Down Expand Up @@ -235,6 +261,11 @@ protected override IImmutableDictionary<string, IValue> PlainValueInternal
rv = rv.Add("asset_minters", new List(AssetMinters.Select(addr => addr.Serialize())));
}

if (!(InitialFavs is null))
{
rv = rv.Add("initial_favs", InitialFavs);
}

return rv;
}
}
Expand Down Expand Up @@ -274,6 +305,11 @@ protected override void LoadPlainValueInternal(IImmutableDictionary<string, IVal
{
AssetMinters = ((List)assetMinters).Select(addr => addr.ToAddress()).ToHashSet();
}

if (plainValue.TryGetValue("initial_favs", out IValue intialFavs))

Check warning on line 309 in Lib9c/Action/InitializeStates.cs

View workflow job for this annotation

GitHub Actions / typos

"intial" should be "initial".
{
InitialFavs = (Dictionary)intialFavs;

Check warning on line 311 in Lib9c/Action/InitializeStates.cs

View workflow job for this annotation

GitHub Actions / typos

"intial" should be "initial".
}
}
}
}

0 comments on commit b093458

Please sign in to comment.