Skip to content

Commit

Permalink
Merge pull request #2515 from limebell/bugfix/zero-asset
Browse files Browse the repository at this point in the history
Fix errors on DPoS
  • Loading branch information
limebell authored Apr 9, 2024
2 parents 06b38a2 + 939328c commit 4f2e2b9
Show file tree
Hide file tree
Showing 24 changed files with 195 additions and 51 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[] { }.ToImmutableArray(),
_ => new IAction[] { new RewardGold() }.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.ConsensusFromGovernance(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));
}
}
}
92 changes: 92 additions & 0 deletions .Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace Lib9c.Tests.Action.DPoS.Sys
{
using System.Linq;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action.DPoS;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Action.DPoS.Sys;
using Nekoyume.Module;
using Xunit;

public class UpdateValidatorsTest : PoSTest
{
[Fact]
public void Execute()
{
// Prepare initial state.
IWorld initialState = new World(new MockWorldState());
const int count = 4;
var validatorKeys = Enumerable.Range(0, count).Select(_ => new PrivateKey().PublicKey).ToArray();
initialState = validatorKeys.Aggregate(
initialState,
(current, key) => current.MintAsset(
new ActionContext(),
key.Address,
new FungibleAssetValue(Asset.GovernanceToken, 1, 0)));
foreach (var key in validatorKeys)
{
Assert.Equal(1, initialState.GetBalance(key.Address, Asset.GovernanceToken).MajorUnit);
Assert.Equal(0, initialState.GetBalance(key.Address, Asset.GovernanceToken).MinorUnit);
}

// Stake 1 for each validator.
foreach (var key in validatorKeys)
{
initialState = new PromoteValidator(
key,
new FungibleAssetValue(Asset.GovernanceToken, 1, 0)).Execute(
new ActionContext
{
PreviousState = initialState,
Signer = key.Address,
});
}

Assert.Equal(0, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count);
Assert.Equal(0, initialState.GetValidatorSet().TotalCount);

// Execute the action.
initialState = new UpdateValidators().Execute(
new ActionContext
{
PreviousState = initialState,
LastCommit = null,
});

Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count);
Assert.Equal(count, initialState.GetValidatorSet().TotalCount);
Assert.Equal(
validatorKeys.ToHashSet(),
initialState.GetValidatorSet()
.Validators.Select(validator => validator.PublicKey)
.ToHashSet());

initialState = new Undelegate(
Validator.DeriveAddress(validatorKeys[0].Address),
new FungibleAssetValue(Asset.Share, 100, 0)).Execute(
new ActionContext
{
PreviousState = initialState,
Signer = validatorKeys[0].Address,
});

Assert.Equal(count, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count);
Assert.Equal(count, initialState.GetValidatorSet().TotalCount);

// Execute the action.
initialState = new UpdateValidators().Execute(
new ActionContext
{
PreviousState = initialState,
LastCommit = null,
});

Assert.Equal(count - 1, ValidatorSetCtrl.FetchBondedValidatorSet(initialState).Item2.Count);
Assert.Equal(count - 1, initialState.GetValidatorSet().TotalCount);
}
}
}
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
2 changes: 1 addition & 1 deletion .Libplanet
Submodule .Libplanet updated 47 files
+40 −0 CHANGES.md
+47 −0 Libplanet.Action.Tests/Common/UpdateValueAction.cs
+65 −22 Libplanet.Action/ActionEvaluator.cs
+0 −6 Libplanet.Action/PolicyBlockActionGetter.cs
+9 −0 Libplanet.Action/PolicyBlockActionsGetter.cs
+5 −0 Libplanet.Action/State/Account.cs
+11 −0 Libplanet.Action/State/IAccount.cs
+3 −1 Libplanet.Benchmarks/AppendBlock.cs
+3 −1 Libplanet.Benchmarks/BlockChain.cs
+3 −1 Libplanet.Benchmarks/ProposeBlock.cs
+7 −3 Libplanet.Explorer.Executable/Program.cs
+2 −1 Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
+26 −6 Libplanet.Extensions.Cocona.Tests/BlockPolicyParamsTest.cs
+58 −7 Libplanet.Extensions.Cocona/BlockPolicyParams.cs
+4 −2 Libplanet.Extensions.Cocona/Commands/BlockCommand.cs
+6 −2 Libplanet.Net.Tests/Consensus/ConsensusReactorTest.cs
+13 −4 Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
+6 −2 Libplanet.Net.Tests/Consensus/ContextTest.cs
+3 −1 Libplanet.Net.Tests/Consensus/HeightVoteSetTest.cs
+14 −4 Libplanet.Net.Tests/SwarmTest.Broadcast.cs
+14 −3 Libplanet.Net.Tests/SwarmTest.Fixtures.cs
+37 −11 Libplanet.Net.Tests/SwarmTest.Preload.cs
+15 −3 Libplanet.Net.Tests/SwarmTest.cs
+6 −2 Libplanet.Net.Tests/TestUtils.cs
+7 −2 Libplanet.RocksDBStore.Tests/RocksDBStoreBlockChainTest.cs
+5 −2 Libplanet.RocksDBStore.Tests/RocksDBStoreFixture.cs
+3 −1 Libplanet.RocksDBStore.Tests/RocksDBStoreTest.cs
+36 −0 Libplanet.Tests/Action/AccountTest.cs
+89 −18 Libplanet.Tests/Action/ActionEvaluatorTest.cs
+11 −5 Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
+18 −6 Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
+12 −3 Libplanet.Tests/Blockchain/BlockChainTest.ValidateNextBlock.cs
+65 −27 Libplanet.Tests/Blockchain/BlockChainTest.cs
+7 −2 Libplanet.Tests/Blockchain/DefaultStoreBlockChainTest.cs
+9 −4 Libplanet.Tests/Blockchain/Policies/BlockPolicyTest.cs
+2 −1 Libplanet.Tests/Blockchain/Policies/StagePolicyTest.cs
+21 −8 Libplanet.Tests/Blocks/PreEvaluationBlockTest.cs
+2 −1 Libplanet.Tests/Fixtures/IntegerSet.cs
+7 −2 Libplanet.Tests/Store/DefaultStoreFixture.cs
+5 −2 Libplanet.Tests/Store/MemoryStoreFixture.cs
+7 −2 Libplanet.Tests/Store/StoreFixture.cs
+2 −1 Libplanet.Tests/Store/StoreTest.cs
+2 −1 Libplanet.Tests/TestUtils.cs
+2 −1 Libplanet/Blockchain/BlockChain.Evaluate.cs
+14 −4 Libplanet/Blockchain/Policies/BlockPolicy.cs
+9 −3 Libplanet/Blockchain/Policies/IBlockPolicy.cs
+4 −1 Libplanet/Blockchain/Policies/NullBlockPolicy.cs
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[] { }.ToImmutableArray(),
new IAction[] { new RewardGold() }.ToImmutableArray(),
blockInterval: BlockInterval,
validateNextBlockTx: validateNextBlockTx,
validateNextBlock: validateNextBlock,
Expand Down
8 changes: 7 additions & 1 deletion 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 @@ -13,7 +15,11 @@ public DebugPolicy()
{
}

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

public ImmutableArray<IAction> EndBlockActions { get; } =
new IAction[] { new RewardGold() }.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
Loading

0 comments on commit 4f2e2b9

Please sign in to comment.