-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3039 from planetarium/fix/pos
Fix PoS related features
- Loading branch information
Showing
16 changed files
with
488 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System.Collections.Immutable; | ||
using Lib9c.Renderers; | ||
using Libplanet.Action; | ||
using Libplanet.Blockchain.Policies; | ||
using Libplanet.Blockchain; | ||
using Libplanet.Crypto; | ||
using Libplanet.Store; | ||
using Libplanet.Store.Trie; | ||
using Libplanet.Types.Assets; | ||
using Libplanet.Types.Blocks; | ||
using Libplanet.Types.Consensus; | ||
using Libplanet.Types.Tx; | ||
using Nekoyume.Action; | ||
using Nekoyume.Action.Loader; | ||
using Nekoyume.Blockchain.Policy; | ||
using Nekoyume.Model.State; | ||
using System.Numerics; | ||
|
||
namespace Lib9c.Proposer.Tests | ||
{ | ||
public class ProposerTest | ||
{ | ||
private readonly PrivateKey _admin; | ||
private readonly PrivateKey _proposer; | ||
private readonly BlockChain _blockChain; | ||
|
||
public ProposerTest() | ||
{ | ||
_admin = new PrivateKey(); | ||
_proposer = new PrivateKey(); | ||
var ncg = Currency.Uncapped("ncg", 2, null); | ||
var policy = new DebugPolicy(); | ||
var actionTypeLoader = new NCActionLoader(); | ||
IStagePolicy stagePolicy = new VolatileStagePolicy(); | ||
var mint = new PrepareRewardAssets | ||
{ | ||
RewardPoolAddress = _proposer.Address, | ||
Assets = new List<FungibleAssetValue> | ||
{ | ||
1 * Currencies.Mead, | ||
}, | ||
}; | ||
|
||
var validatorSet = new ValidatorSet( | ||
new List<Validator> { new(_proposer.PublicKey, 10_000_000_000_000_000_000) }); | ||
|
||
var initializeStates = new InitializeStates( | ||
validatorSet, | ||
new RankingState0(), | ||
new ShopState(), | ||
new Dictionary<string, string>(), | ||
new GameConfigState(), | ||
new RedeemCodeState(new Dictionary<PublicKey, RedeemCodeState.Reward>()), | ||
new ActivatedAccountsState(), | ||
new GoldCurrencyState(ncg), | ||
new GoldDistribution[] { }, | ||
new PendingActivationState[] { }); | ||
|
||
List <ActionBase> actions = new List<ActionBase> | ||
{ | ||
initializeStates, | ||
}; | ||
|
||
var genesis = BlockChain.ProposeGenesisBlock( | ||
privateKey: _proposer, | ||
transactions: ImmutableList<Transaction>.Empty | ||
.Add(Transaction.Create( | ||
0, _proposer, null, actions.ToPlainValues())), | ||
timestamp: DateTimeOffset.MinValue); | ||
|
||
var store = new MemoryStore(); | ||
var stateStore = new TrieStateStore(new MemoryKeyValueStore()); | ||
|
||
_blockChain = BlockChain.Create( | ||
policy, | ||
stagePolicy, | ||
store, | ||
stateStore, | ||
genesis, | ||
new ActionEvaluator( | ||
policy.PolicyActionsRegistry, | ||
stateStore, | ||
new NCActionLoader() | ||
), | ||
new[] { new BlockRenderer(), } | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ProposeBlock() | ||
{ | ||
Block block = _blockChain.ProposeBlock(_proposer); | ||
_blockChain.Append( | ||
block, | ||
GenerateBlockCommit( | ||
block, | ||
_proposer, | ||
10_000_000_000_000_000_000)); | ||
} | ||
|
||
[Fact] | ||
public void AssertInvalidProposer() | ||
{ | ||
Block block = _blockChain.ProposeBlock(_proposer); | ||
Assert.Throws<InvalidBlockCommitException>(() => _blockChain.Append( | ||
block, | ||
GenerateBlockCommit( | ||
block, | ||
new PrivateKey(), | ||
10_000_000_000_000_000_000))); | ||
} | ||
|
||
[Fact] | ||
public void AssertInvalidPower() | ||
{ | ||
Block block = _blockChain.ProposeBlock(_proposer); | ||
Assert.Throws<InvalidBlockCommitException>(() => _blockChain.Append( | ||
block, | ||
GenerateBlockCommit( | ||
block, | ||
_proposer, | ||
10_000_000_000_000_000))); | ||
} | ||
|
||
private BlockCommit? GenerateBlockCommit( | ||
Block block, PrivateKey privateKey, BigInteger power) | ||
{ | ||
return block.Index != 0 | ||
? new BlockCommit( | ||
block.Index, | ||
0, | ||
block.Hash, | ||
ImmutableArray.Create( | ||
new VoteMetadata( | ||
block.Index, | ||
0, | ||
block.Hash, | ||
DateTimeOffset.UtcNow, | ||
privateKey.PublicKey, | ||
power, | ||
VoteFlag.PreCommit).Sign(privateKey))) | ||
: null; | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
.Lib9c.Tests/Action/Guild/Migration/MigratePlanetariumValidatorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
namespace Lib9c.Tests.Action.Guild.Migration | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
using Lib9c.Tests.Util; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.Types.Assets; | ||
using Libplanet.Types.Consensus; | ||
using Nekoyume.Action.Guild; | ||
using Nekoyume.Action.Guild.Migration; | ||
using Nekoyume.Action.Guild.Migration.LegacyModels; | ||
using Nekoyume.Action.ValidatorDelegation; | ||
using Nekoyume.Model.Guild; | ||
using Nekoyume.Model.Stake; | ||
using Nekoyume.TypedAddress; | ||
using Nekoyume.ValidatorDelegation; | ||
using Xunit; | ||
|
||
// TODO: Remove this test class after the migration is completed. | ||
public class MigratePlanetariumValidatorTest : GuildTestBase | ||
{ | ||
[Fact] | ||
public void Execute() | ||
{ | ||
var guildAddress = AddressUtil.CreateGuildAddress(); | ||
var validatorKey = new PrivateKey().PublicKey; | ||
var validatorAddress = validatorKey.Address; | ||
var power = 10_000_000_000_000_000_000; | ||
var guildGold = Currencies.GuildGold; | ||
var delegated = FungibleAssetValue.FromRawValue(guildGold, power); | ||
|
||
var world = EnsureLegacyPlanetariumValidator( | ||
World, guildAddress, validatorKey, power); | ||
|
||
var guildRepository = new GuildRepository(world, new ActionContext { }); | ||
var validatorRepository = new ValidatorRepository(world, new ActionContext { }); | ||
var guildDelegatee = guildRepository.GetGuildDelegatee(validatorAddress); | ||
var validatorDelegatee = validatorRepository.GetValidatorDelegatee(validatorAddress); | ||
|
||
Assert.False(validatorDelegatee.IsActive); | ||
Assert.Equal(ValidatorDelegatee.InactiveDelegationPoolAddress, guildDelegatee.DelegationPoolAddress); | ||
Assert.Equal(ValidatorDelegatee.InactiveDelegationPoolAddress, validatorDelegatee.DelegationPoolAddress); | ||
Assert.Equal(delegated, world.GetBalance(ValidatorDelegatee.InactiveDelegationPoolAddress, Currencies.GuildGold)); | ||
Assert.Equal(guildGold * 0, world.GetBalance(ValidatorDelegatee.ActiveDelegationPoolAddress, Currencies.GuildGold)); | ||
|
||
var action = new MigratePlanetariumValidator(); | ||
var actionContext = new ActionContext | ||
{ | ||
PreviousState = world, | ||
Signer = new PrivateKey().Address, | ||
}; | ||
world = action.Execute(actionContext); | ||
|
||
guildRepository = new GuildRepository(world, new ActionContext { }); | ||
validatorRepository = new ValidatorRepository(world, new ActionContext { }); | ||
guildDelegatee = guildRepository.GetGuildDelegatee(validatorAddress); | ||
validatorDelegatee = validatorRepository.GetValidatorDelegatee(validatorAddress); | ||
|
||
Assert.True(validatorRepository.GetValidatorDelegatee(validatorAddress).IsActive); | ||
Assert.Equal(ValidatorDelegatee.ActiveDelegationPoolAddress, guildDelegatee.DelegationPoolAddress); | ||
Assert.Equal(ValidatorDelegatee.ActiveDelegationPoolAddress, validatorDelegatee.DelegationPoolAddress); | ||
Assert.Equal(delegated, world.GetBalance(ValidatorDelegatee.ActiveDelegationPoolAddress, Currencies.GuildGold)); | ||
Assert.Equal(guildGold * 0, world.GetBalance(ValidatorDelegatee.InactiveDelegationPoolAddress, Currencies.GuildGold)); | ||
|
||
Assert.Throws<InvalidOperationException>(() => | ||
{ | ||
var actionContext = new ActionContext | ||
{ | ||
PreviousState = world, | ||
Signer = new PrivateKey().Address, | ||
}; | ||
world = action.Execute(actionContext); | ||
}); | ||
} | ||
|
||
private static IWorld EnsureLegacyPlanetariumValidator( | ||
IWorld world, GuildAddress guildAddress, PublicKey validatorKey, BigInteger power) | ||
{ | ||
world = world.SetDelegationMigrationHeight(0L); | ||
|
||
var toDelegate = FungibleAssetValue.FromRawValue(Currencies.GuildGold, power); | ||
world = world | ||
.MintAsset( | ||
new ActionContext { }, | ||
StakeState.DeriveAddress(validatorKey.Address), | ||
toDelegate) | ||
.MintAsset( | ||
new ActionContext { }, | ||
validatorKey.Address, | ||
Currencies.Mead * 1); | ||
|
||
world = new PromoteValidator(validatorKey, toDelegate).Execute(new ActionContext | ||
{ | ||
PreviousState = world, | ||
Signer = validatorKey.Address, | ||
}); | ||
|
||
world = new MakeGuild(validatorKey.Address).Execute(new ActionContext | ||
{ | ||
PreviousState = world, | ||
Signer = GuildConfig.PlanetariumGuildOwner, | ||
}); | ||
|
||
world = world.SetValidatorSet(new ValidatorSet( | ||
new List<Validator> | ||
{ | ||
new Validator(validatorKey, power), | ||
})); | ||
|
||
return world; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.