Skip to content

Commit

Permalink
refactor: apply API changes from the libplanet bump
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Apr 5, 2024
1 parent 497aa2f commit 9953037
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ static void Main(string[] args)
IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(storePath, "states"));
var stateStore = new TrieStateStore(stateKeyValueStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
stateStore,
new NCActionLoader());
var chain = new BlockChain(
Expand Down
5 changes: 4 additions & 1 deletion .Lib9c.Plugin/PluginActionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Immutable;
using System.Security.Cryptography;
using Lib9c.Plugin.Shared;
using Libplanet.Action;
using Libplanet.Common;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Store;
using Nekoyume.Action;
using Nekoyume.Action.DPoS.Sys;
using Nekoyume.Action.Loader;


Expand All @@ -18,7 +20,8 @@ public PluginActionEvaluator(IPluginKeyValueStore keyValueStore)
{
var stateStore = new TrieStateStore(new WrappedKeyValueStore(keyValueStore));
_actionEvaluator = new ActionEvaluator(
_ => new RewardGold(),
_ => new IAction[] { new AllocateReward() }.ToImmutableArray(),
_ => new IAction[] { new UpdateValidators() }.ToImmutableArray(),
stateStore,
new NCActionLoader());
}
Expand Down
18 changes: 9 additions & 9 deletions .Lib9c.Tests/Action/DPoS/DistributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void ValidatorSetTest()
},
ReservedAddress.RewardPool,
blockReward);
_states = AllocateReward.Execute(
_states = AllocateRewardCtrl.Execute(
_states,
new ActionContext
{
Expand All @@ -172,13 +172,13 @@ public void ValidatorSetTest()
OperatorAddresses[3]);

var (baseProposerReward, _)
= (blockReward * AllocateReward.BaseProposerRewardNumerator)
.DivRem(AllocateReward.BaseProposerRewardDenominator);
= (blockReward * AllocateRewardCtrl.BaseProposerRewardNumerator)
.DivRem(AllocateRewardCtrl.BaseProposerRewardDenominator);
var (bonusProposerReward, _)
= (blockReward * (205 + 307)
* AllocateReward.BonusProposerRewardNumerator)
* AllocateRewardCtrl.BonusProposerRewardNumerator)
.DivRem((100 + (101 + 200) * 50 - 101 - 102 + 204 + 306)
* AllocateReward.BonusProposerRewardDenominator);
* AllocateRewardCtrl.BonusProposerRewardDenominator);
FungibleAssetValue proposerReward = baseProposerReward + bonusProposerReward;
FungibleAssetValue validatorRewardSum = blockReward - proposerReward;

Expand Down Expand Up @@ -214,20 +214,20 @@ public void ValidatorSetTest()
Assert.Equal(
proposerReward + commissionA,
_states.GetBalance(
AllocateReward.RewardAddress(OperatorAddresses[3]), Asset.ConsensusToken));
AllocateRewardCtrl.RewardAddress(OperatorAddresses[3]), Asset.ConsensusToken));

Assert.Equal(
commissionB,
_states.GetBalance(
AllocateReward.RewardAddress(OperatorAddresses[5]), Asset.ConsensusToken));
AllocateRewardCtrl.RewardAddress(OperatorAddresses[5]), Asset.ConsensusToken));

Address delegationAddressA
= Delegation.DeriveAddress(DelegatorAddress, validatorAddressA);

Assert.Equal(
Asset.ConsensusToken * 0,
_states.GetBalance(
AllocateReward.RewardAddress(DelegatorAddress), Asset.ConsensusToken));
AllocateRewardCtrl.RewardAddress(DelegatorAddress), Asset.ConsensusToken));

var (delegatorToken, _)
= (_states.GetBalance(
Expand All @@ -253,7 +253,7 @@ Address delegationAddressA
Assert.Equal(
delegatorToken,
_states.GetBalance(
AllocateReward.RewardAddress(DelegatorAddress), Asset.ConsensusToken));
AllocateRewardCtrl.RewardAddress(DelegatorAddress), Asset.ConsensusToken));
}
}
}
6 changes: 4 additions & 2 deletions .Lib9c.Tests/Action/RewardGoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ public async Task Genesis_StateRootHash(bool mainnet)
pendingActivationStates: pendingActivationStates.ToArray()
);
var tempActionEvaluator = new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: new TrieStateStore(new MemoryKeyValueStore()),
actionTypeLoader: new NCActionLoader());
genesis = BlockChain.ProposeGenesisBlock(
Expand All @@ -565,7 +566,8 @@ public async Task Genesis_StateRootHash(bool mainnet)
stateStore: stateStore,
genesisBlock: genesis,
actionEvaluator: new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
Expand Down
18 changes: 12 additions & 6 deletions .Lib9c.Tests/Policy/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void ValidateNextBlockTx()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
Expand Down Expand Up @@ -273,7 +274,8 @@ public void BlockCommitFromNonValidator()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
Expand Down Expand Up @@ -327,7 +329,8 @@ public void MustNotIncludeBlockActionAtTransaction()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: actionLoader
),
Expand Down Expand Up @@ -380,7 +383,8 @@ public void EarnMiningGoldWhenSuccessMining()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
Expand Down Expand Up @@ -432,7 +436,8 @@ public void ValidateNextBlockWithManyTransactions()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
)
Expand Down Expand Up @@ -533,7 +538,8 @@ public void ValidateNextBlockWithManyTransactionsPerSigner()
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
)
Expand Down
3 changes: 2 additions & 1 deletion .Lib9c.Tests/TestHelper/BlockChainHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public static BlockChain MakeBlockChain(
stateStore,
genesis,
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
policyBeginBlockActionsGetter: _ => policy.BeginBlockActions,
policyEndBlockActionsGetter: _ => policy.EndBlockActions,
stateStore: stateStore,
actionTypeLoader: new NCActionLoader()
),
Expand Down
3 changes: 2 additions & 1 deletion .Lib9c.Tools/SubCommand/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ IStateStore stateStore
var actionLoader = TypedActionLoader.Create(
typeof(ActionBase).Assembly, typeof(ActionBase));
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
stateStore,
actionLoader);
HashDigest<SHA256> stateRootHash = block.Index < 1
Expand Down
5 changes: 5 additions & 0 deletions .Libplanet.Extensions.RemoteBlockChainStates/RemoteAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public IAccount SetValidator(Validator validator)
throw new NotSupportedException();
}

public IAccount SetValidatorSet(ValidatorSet validatorSet)
{
throw new NotSupportedException();
}

public IAccount TransferAsset(IActionContext context, Address sender, Address recipient, FungibleAssetValue value, bool allowNegativeBalance = false)
{
throw new NotSupportedException();
Expand Down
3 changes: 2 additions & 1 deletion Lib9c.DevExtensions/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ Guid chainIdValue
var blockChainStates = new BlockChainStates(store, stateStore);
var actionLoader = new NCDevActionLoader();
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
stateStore,
actionLoader);

Expand Down
4 changes: 3 additions & 1 deletion 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.Sys;

#if UNITY_EDITOR || UNITY_STANDALONE
using UniRx;
Expand Down Expand Up @@ -130,7 +131,8 @@ internal IBlockPolicy GetPolicy(

// FIXME: Slight inconsistency due to pre-existing delegate.
return new BlockPolicy(
new RewardGold(),
new IAction[] { new AllocateReward() }.ToImmutableArray(),
new IAction[] { new UpdateValidators() }.ToImmutableArray(),
blockInterval: BlockInterval,
validateNextBlockTx: validateNextBlockTx,
validateNextBlock: validateNextBlock,
Expand Down
8 changes: 8 additions & 0 deletions Lib9c.Policy/Policy/DebugPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Immutable;
using Libplanet.Action;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.DPoS.Sys;

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

public IAction BlockAction { get; } = new RewardGold();

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

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

public TxPolicyViolationException ValidateNextBlockTx(
BlockChain blockChain, Transaction transaction)
{
Expand Down
7 changes: 5 additions & 2 deletions Lib9c.Policy/Policy/NCBlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using System;
using System.Collections.Immutable;

namespace Nekoyume.Blockchain.Policy
{
public class NCBlockPolicy : BlockPolicy
{
public NCBlockPolicy(
IAction blockAction,
ImmutableArray<IAction> beginBlockActions,
ImmutableArray<IAction> endBlockActions,
TimeSpan blockInterval,
Func<BlockChain, Transaction, TxPolicyViolationException>?
validateNextBlockTx = null,
Expand All @@ -21,7 +23,8 @@ public NCBlockPolicy(
Func<long, int>? getMaxTransactionsPerBlock = null,
Func<long, int>? getMaxTransactionsPerSignerPerBlock = null)
: base(
blockAction: blockAction,
beginBlockActions: beginBlockActions,
endBlockActions: endBlockActions,
blockInterval: blockInterval,
validateNextBlockTx: validateNextBlockTx,
validateNextBlock: validateNextBlock,
Expand Down
6 changes: 4 additions & 2 deletions Lib9c.Utils/BlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ public static Block ProposeGenesisBlock(
{
actions.AddRange(actionBases);
}
var blockAction = new BlockPolicySource().GetPolicy().BlockAction;
var beginBlockActions = new BlockPolicySource().GetPolicy().BeginBlockActions;
var endBlockActions = new BlockPolicySource().GetPolicy().EndBlockActions;
var actionLoader = new NCActionLoader();
var actionEvaluator = new ActionEvaluator(
_ => blockAction,
_ => beginBlockActions,
_ => endBlockActions,
new TrieStateStore(new MemoryKeyValueStore()),
actionLoader);
return
Expand Down
8 changes: 8 additions & 0 deletions Lib9c/Module/LegacyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public static IWorld SetValidator(
world.GetAccount(ReservedAddresses.LegacyAccount)
.SetValidator(validator));

public static IWorld UpdateValidatorSet(
this IWorld world,
Libplanet.Types.Consensus.ValidatorSet validatorSet) =>
world.SetAccount(
ReservedAddresses.LegacyAccount,
world.GetAccount(ReservedAddresses.LegacyAccount)
.SetValidatorSet(validatorSet));

// Methods from AccountExtensions
public static IWorld MarkBalanceChanged(
this IWorld world,
Expand Down

0 comments on commit 9953037

Please sign in to comment.