Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare initial states for testing #2506

Draft
wants to merge 44 commits into
base: feature/dpos
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5354eb0
bump: libplanet
limebell Mar 20, 2024
083ee2e
feat: add dpos related actions
limebell Mar 15, 2024
11ec77e
test: add unit tests
limebell Mar 20, 2024
a18acd3
Merge pull request #2473 from limebell/feature/dpos
limebell Mar 26, 2024
7adb80b
refactor: Add the `type_id` value to pos-related actions.
s2quake Mar 26, 2024
21b7ce3
Merge pull request #2488 from s2quake/feature/dpos
s2quake Mar 26, 2024
8d6d406
feat: change get methods to iworldstate
limebell Mar 26, 2024
520bba6
chore: prevent typo error
limebell Mar 26, 2024
6da9f8a
Merge pull request #2487 from limebell/dpos/get-worldstate
limebell Mar 26, 2024
90cc46c
refactor: move DPoS feaures under Lib9c project
limebell Mar 29, 2024
051c5c2
Merge pull request #2498 from limebell/refactor/move-dpos
limebell Mar 29, 2024
69f72d1
fix: Fix an issue with deletaion not being listed when bond is execut…
s2quake Apr 2, 2024
3bcb166
test: Add test code for ValidatorDelegationSetCtrl.
s2quake Apr 2, 2024
38f8d2f
Merge pull request #2505 from s2quake/fix/delegations-not-listed
s2quake Apr 2, 2024
ce02e8b
refactor: separate PoSAction and implement ActionBase
limebell Apr 3, 2024
d457a6a
Merge pull request #2507 from limebell/dpos/actionbase
limebell Apr 3, 2024
119b691
refator: refactor dpos actions
limebell Apr 4, 2024
519c3cf
Merge pull request #2510 from limebell/dpos/refactor-systemaction
limebell Apr 4, 2024
4005c46
fix: Fix test fails due to missing `UseGas`.
s2quake Apr 9, 2024
443a5d4
Merge pull request #2520 from s2quake/fix/test-fails-missing-usegas
s2quake Apr 9, 2024
d0ba581
refactor: Change decimal places for NCG, ConsensusToken, Share.
s2quake Apr 9, 2024
928513f
test: Fixes test failures caused by changing decimal places.
s2quake Apr 9, 2024
06b38a2
Merge pull request #2521 from s2quake/refactor/currency-for-dpos
s2quake Apr 9, 2024
e6a4af6
refactor: rename AllocateRewardCtrl
limebell Apr 5, 2024
98ffc27
bump: libplanet
limebell Apr 5, 2024
181d40f
feat: adjust changes from the libplanet API changes
limebell Apr 9, 2024
24bca50
test: add regression test
limebell Apr 5, 2024
939328c
bugfix: fix logic error in dpos methods
limebell Apr 5, 2024
4f2e2b9
Merge pull request #2515 from limebell/bugfix/zero-asset
limebell Apr 9, 2024
a37c1d7
feat: Add slashing-related code.
s2quake Apr 9, 2024
5b3d0c7
test: Add test code for slashing.
s2quake Apr 9, 2024
6921c25
Merge pull request #2517 from s2quake/feature/dpos-slashing
s2quake Apr 16, 2024
5bd2644
bump: libplanet
limebell Apr 18, 2024
6f3d8ad
adjust api changes from libplanet
limebell Apr 18, 2024
d76f626
Merge pull request #2538 from limebell/bump/libplanet-dpos-begintx
limebell Apr 18, 2024
0e5609e
feat: introduce RecordProposer action
limebell Apr 19, 2024
13801c7
bump: libplanet
limebell Apr 22, 2024
909f918
chore: apply API changes from libplanet
limebell Apr 24, 2024
5d425ee
feat: implement gold distribution replacing fee collector
limebell Apr 22, 2024
4dafb05
test: fix DistributeTest
limebell Apr 24, 2024
c35ec5c
bump: libplanet
limebell Apr 25, 2024
5e56b64
temp: test
limebell Apr 25, 2024
0750a86
1
limebell Apr 25, 2024
b093458
feat: prepare initial states
limebell Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ static void Main(string[] args)
IKeyValueStore stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(storePath, "states"));
var stateStore = new TrieStateStore(stateKeyValueStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
new PolicyActionsRegistry(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore,
new NCActionLoader());
var chain = new BlockChain(
Expand Down
8 changes: 7 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,11 @@ public PluginActionEvaluator(IPluginKeyValueStore keyValueStore)
{
var stateStore = new TrieStateStore(new WrappedKeyValueStore(keyValueStore));
_actionEvaluator = new ActionEvaluator(
_ => new RewardGold(),
new PolicyActionsRegistry(
_ => new IAction[] { }.ToImmutableArray(),
_ => new IAction[] { new RewardGold() }.ToImmutableArray(),
_ => new IAction[] { }.ToImmutableArray(),
_ => new IAction[] { }.ToImmutableArray()),
stateStore,
new NCActionLoader());
}
Expand Down
7 changes: 6 additions & 1 deletion .Lib9c.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Lib9c.Tests.Action
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

Expand All @@ -28,13 +29,17 @@ public class ActionContext : IActionContext

public int BlockProtocolVersion { get; set; }

public BlockCommit LastCommit { get; set; }

public IWorld PreviousState { get; set; }

public int RandomSeed { get; set; }

public HashDigest<SHA256>? PreviousStateRootHash { get; set; }

public bool BlockAction { get; }
public bool IsBlockAction { get; }

public FungibleAssetValue? MaxGasPrice { get; set; }

public void UseGas(long gas)
{
Expand Down
67 changes: 67 additions & 0 deletions .Lib9c.Tests/Action/DPoS/BlockTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Lib9c.Tests.Action.DPoS
{
using System;
using System.Collections.Immutable;
using Libplanet.Action;
using Libplanet.Action.Loader;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.DPoS.Sys;
using Serilog;
using Xunit.Abstractions;

public class BlockTest
{
private IBlockPolicy _policy;
private BlockChain _blockChain;

public BlockTest(ITestOutputHelper outputHelper)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.TestOutput(outputHelper)
.CreateLogger();

var genesisProposer = new PrivateKey();
_policy = new BlockPolicy(
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());

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var actionEvaluator = new ActionEvaluator(
new PolicyActionsRegistry(
_ => _policy.BeginBlockActions,
_ => _policy.EndBlockActions,
_ => _policy.BeginTxActions,
_ => _policy.EndTxActions),
stateStore: stateStore,
actionTypeLoader: new SingleActionLoader(typeof(ActionBase)));
var genesisBlock = BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: ImmutableList<Transaction>.Empty,
privateKey: genesisProposer,
timestamp: DateTimeOffset.UtcNow);
var blockChainStates = new BlockChainStates(store, stateStore);
_blockChain = new BlockChain(
policy: _policy,
stagePolicy: new VolatileStagePolicy(),
store: new MemoryStore(),
stateStore: stateStore,
genesisBlock: genesisBlock,
blockChainStates: blockChainStates,
actionEvaluator: actionEvaluator);
}
}
}
196 changes: 196 additions & 0 deletions .Lib9c.Tests/Action/DPoS/Control/DelegateCtrlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
namespace Lib9c.Tests.Action.DPoS.Control
{
using System.Collections.Immutable;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Action.DPoS.Control;
using Nekoyume.Action.DPoS.Exception;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Module;
using Xunit;

public class DelegateCtrlTest : PoSTest
{
private readonly PublicKey _operatorPublicKey;
private readonly Address _operatorAddress;
private readonly Address _delegatorAddress;
private readonly Address _validatorAddress;
private ImmutableHashSet<Currency> _nativeTokens;
private IWorld _states;

public DelegateCtrlTest()
{
_operatorPublicKey = new PrivateKey().PublicKey;
_operatorAddress = _operatorPublicKey.Address;
_delegatorAddress = CreateAddress();
_validatorAddress = Validator.DeriveAddress(_operatorAddress);
_nativeTokens = ImmutableHashSet.Create(
Asset.GovernanceToken, Asset.ConsensusToken, Asset.Share);
_states = InitializeStates();
}

[Fact]
public void InvalidCurrencyTest()
{
Initialize(500, 500, 100);
_states = _states.MintAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
Asset.ConsensusFromGovernance(50));
Assert.Throws<InvalidCurrencyException>(
() => _states = DelegateCtrl.Execute(
_states,
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
_validatorAddress,
Asset.ConsensusFromGovernance(30),
_nativeTokens));
}

[Fact]
public void InvalidValidatorTest()
{
Initialize(500, 500, 100);
Assert.Throws<NullValidatorException>(
() => _states = DelegateCtrl.Execute(
_states,
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
CreateAddress(),
Asset.GovernanceToken * 10,
_nativeTokens));
}

[Fact]
public void InvalidShareTest()
{
Initialize(500, 500, 100);
_states = _states.BurnAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_validatorAddress,
Asset.ConsensusFromGovernance(100));
Assert.Throws<InvalidExchangeRateException>(
() => _states = DelegateCtrl.Execute(
_states,
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
_validatorAddress,
Asset.GovernanceToken * 10,
_nativeTokens));
}

[Theory]
[InlineData(500, 500, 100, 10)]
[InlineData(500, 500, 100, 20)]
public void BalanceTest(
int operatorMintAmount,
int delegatorMintAmount,
int selfDelegateAmount,
int delegateAmount)
{
Initialize(operatorMintAmount, delegatorMintAmount, selfDelegateAmount);
_states = DelegateCtrl.Execute(
_states,
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
_validatorAddress,
Asset.GovernanceToken * delegateAmount,
_nativeTokens);
Assert.Equal(
Asset.GovernanceToken * 0,
_states.GetBalance(_validatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.ConsensusToken));
Assert.Equal(
ShareFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.Share));
Assert.Equal(
ShareFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.Share));
Assert.Equal(
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.GovernanceToken * (operatorMintAmount - selfDelegateAmount),
_states.GetBalance(_operatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.GovernanceToken * (selfDelegateAmount + delegateAmount),
_states.GetBalance(ReservedAddress.UnbondedPool, Asset.GovernanceToken));
var balanceA = _states.GetBalance(
Delegation.DeriveAddress(_operatorAddress, _validatorAddress),
Asset.Share);
var balanceB = _states.GetBalance(
Delegation.DeriveAddress(_delegatorAddress, _validatorAddress),
Asset.Share);
Assert.Equal(
ValidatorCtrl.GetValidator(_states, _validatorAddress)!.DelegatorShares,
balanceA + balanceB);
}

private void Initialize(
int operatorMintAmount, int delegatorMintAmount, int selfDelegateAmount)
{
_states = InitializeStates();
_states = _states.MintAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_operatorAddress,
Asset.GovernanceToken * operatorMintAmount);
_states = _states.MintAsset(
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_delegatorAddress,
Asset.GovernanceToken * delegatorMintAmount);
_states = ValidatorCtrl.Create(
_states,
new ActionContext
{
PreviousState = _states,
BlockIndex = 1,
},
_operatorAddress,
_operatorPublicKey,
Asset.GovernanceToken * selfDelegateAmount,
_nativeTokens);
}
}
}
Loading
Loading