Skip to content

Commit

Permalink
test: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Nov 1, 2024
1 parent 40450ed commit d0b3155
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private Block MineGenesisBlock()
block.Hash,
DateTimeOffset.UtcNow,
validator.PublicKey,
null,
BigInteger.One,
VoteFlag.PreCommit).Sign(validator)))
: null;
}
Expand Down
20 changes: 4 additions & 16 deletions NineChronicles.Headless.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,30 @@
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Extensions.ActionEvaluatorCommonComponents;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace NineChronicles.Headless.Tests.Action;

public class ActionContext : IActionContext
{
private long UsedGas { get; set; }

public Address Signer { get; init; }
public TxId? TxId { get; init; }
public Address Miner { get; init; }
public long BlockIndex { get; init; }
public int BlockProtocolVersion { get; init; }
public BlockCommit LastCommit { get; init; }
public IWorld PreviousState { get; init; }
public int RandomSeed { get; init; }
public bool IsPolicyAction { get; init; }
public FungibleAssetValue? MaxGasPrice { get; set; }
public IReadOnlyList<ITransaction> Txs { get; init; }
public IReadOnlyList<EvidenceBase> Evidence { get; init; }
public void UseGas(long gas)
{
UsedGas += gas;
}

public IRandom GetRandom()
{
return new Random(RandomSeed);
}

public long GasUsed()
{
return UsedGas;
}

public long GasLimit()
{
return 0L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Linq;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Action;
using Libplanet.Types.Assets;
using Libplanet.Types.Consensus;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.ValidatorDelegation;

namespace NineChronicles.Headless.Tests.Common.Actions
{
[ActionType("initialize_validator")]
public sealed class InitializeValidator : ActionBase
{
public InitializeValidator(
ValidatorSet validatorSet,
Currency goldCurrency)
{
Validators = validatorSet.Validators.ToArray();
GoldCurrency = goldCurrency;
}

public Validator[] Validators { get; set; }

public Currency GoldCurrency { get; set; }

public override IValue PlainValue
=> Dictionary.Empty
.Add("validator_set", new List(Validators.Select(item => item.Bencoded)))
.Add("gold_currency", GoldCurrency.Serialize());

public override void LoadPlainValue(IValue value)
{
if (value is not Dictionary dict ||
dict["validator_set"] is not List list ||
dict["gold_currency"] is not Dictionary currencyDict)
{
throw new InvalidCastException("Invalid types");
}

Validators = list.Select(item => new Validator((Dictionary)item)).ToArray();
GoldCurrency = new Currency(currencyDict);
}

public override IWorld Execute(IActionContext context)
{
var world = context.PreviousState;

var goldCurrency = GoldCurrency;
var currencyState = new GoldCurrencyState(goldCurrency);
world = world
.SetLegacyState(GoldCurrencyState.Address, currencyState.Serialize())
.SetLegacyState(Addresses.GoldDistribution, new List().Serialize());

if (currencyState.InitialSupply > 0)
{
world = world.MintAsset(
context,
GoldCurrencyState.Address,
currencyState.Currency * currencyState.InitialSupply);
}

var repository = new ValidatorRepository(world, context);
var validators = Validators;
foreach (var validator in validators)
{
var validatorDelegatee = new ValidatorDelegatee(
validator.OperatorAddress,
validator.PublicKey,
ValidatorDelegatee.DefaultCommissionPercentage,
context.BlockIndex,
repository);
var delegationFAV = FungibleAssetValue.FromRawValue(
validatorDelegatee.DelegationCurrency, validator.Power);
var validatorOperatorAddress = validator.OperatorAddress;
var validatorDelegator = repository.GetValidatorDelegator(
validatorOperatorAddress, validatorOperatorAddress);

repository.SetValidatorDelegatee(validatorDelegatee);
repository.UpdateWorld(
repository.World.MintAsset(
repository.ActionContext,
validatorDelegator.DelegationPoolAddress,
delegationFAV));
validatorDelegator.Delegate(validatorDelegatee, delegationFAV, context.BlockIndex);
}

repository.SetAbstainHistory(new());
world = repository.World;

return world;
}
}
}
2 changes: 1 addition & 1 deletion NineChronicles.Headless.Tests/Common/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class Fixtures

public static readonly Address AvatarAddress = new Address("983c3Fbfe8243a0e36D55C6C1aE26A7c8Bb6CBd4");

public static readonly Address StakeStateAddress = StakeState.DeriveAddress(UserAddress);
public static readonly Address StakeStateAddress = LegacyStakeState.DeriveAddress(UserAddress);

public static readonly TableSheets TableSheetsFX = new(TableSheetsImporter.ImportSheets());

Expand Down
2 changes: 1 addition & 1 deletion NineChronicles.Headless.Tests/GraphQLTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ InitializeStates initializeStates
0,
block.Hash,
ValidatorPrivateKeys.Select(
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, null, VoteFlag.PreCommit).Sign(k))
k => new VoteMetadata(block.Index, 0, block.Hash, block.Timestamp, k.PublicKey, BigInteger.One, VoteFlag.PreCommit).Sign(k))
.ToImmutableArray());

blockchain.Append(block, blockCommit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class StakeAchievementsTypeTest
{
[Theory]
[MemberData(nameof(Members))]
public async Task AchievementsByLevel(StakeState.StakeAchievements achievements, int level, Dictionary<string, object> expected)
public async Task AchievementsByLevel(LegacyStakeState.StakeAchievements achievements, int level, Dictionary<string, object> expected)
{
string query = @$"
{{
Expand All @@ -27,7 +27,7 @@ public async Task AchievementsByLevel(StakeState.StakeAchievements achievements,
{
new object[]
{
new StakeState.StakeAchievements(new Dictionary<int, int>
new LegacyStakeState.StakeAchievements(new Dictionary<int, int>
{
[1] = 1,
[2] = 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StakeStateTypeTest
{
[Theory]
[MemberData(nameof(Members))]
public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long deposit, long blockIndex, Dictionary<string, object> expected)
public async Task Query(StakeState stakeState, Address stakeStateAddress, long deposit, long blockIndex, Dictionary<string, object> expected)
{
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
Expand Down Expand Up @@ -60,7 +60,7 @@ public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long
{
new object[]
{
new StakeStateV2(
new StakeState(
new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 0),
Fixtures.StakeStateAddress,
100,
Expand All @@ -70,14 +70,14 @@ public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long
["address"] = Fixtures.StakeStateAddress.ToString(),
["deposit"] = "100.00",
["startedBlockIndex"] = 0L,
["cancellableBlockIndex"] = StakeState.LockupInterval,
["cancellableBlockIndex"] = LegacyStakeState.LockupInterval,
["receivedBlockIndex"] = 0L,
["claimableBlockIndex"] = 0L + StakeState.RewardInterval,
["claimableBlockIndex"] = 0L + LegacyStakeState.RewardInterval,
}
},
new object[]
{
new StakeStateV2(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 100),
new StakeState(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 100),
Fixtures.StakeStateAddress,
100,
0,
Expand All @@ -86,14 +86,14 @@ public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long
["address"] = Fixtures.StakeStateAddress.ToString(),
["deposit"] = "100.00",
["startedBlockIndex"] = 100L,
["cancellableBlockIndex"] = 100 + StakeState.LockupInterval,
["cancellableBlockIndex"] = 100 + LegacyStakeState.LockupInterval,
["receivedBlockIndex"] = 0L,
["claimableBlockIndex"] = 100 + StakeState.RewardInterval,
["claimableBlockIndex"] = 100 + LegacyStakeState.RewardInterval,
}
},
new object[]
{
new StakeStateV2(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 100),
new StakeState(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 100),
Fixtures.StakeStateAddress,
100,
0,
Expand All @@ -102,14 +102,14 @@ public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long
["address"] = Fixtures.StakeStateAddress.ToString(),
["deposit"] = "100.00",
["startedBlockIndex"] = 100L,
["cancellableBlockIndex"] = StakeState.LockupInterval + 100,
["cancellableBlockIndex"] = LegacyStakeState.LockupInterval + 100,
["receivedBlockIndex"] = 0L,
["claimableBlockIndex"] = StakeState.RewardInterval + 100,
["claimableBlockIndex"] = LegacyStakeState.RewardInterval + 100,
}
},
new object[]
{
new StakeStateV2(
new StakeState(
new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 10, 50412),
Fixtures.StakeStateAddress,
100,
Expand All @@ -126,7 +126,7 @@ public async Task Query(StakeStateV2 stakeState, Address stakeStateAddress, long
},
new object[]
{
new StakeStateV2(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 10, 50412),
new StakeState(new Contract("StakeRegularFixedRewardSheet_V1", "StakeRegularRewardSheet_V1", 50400, 201600), 10, 50412),
Fixtures.StakeStateAddress,
100,
ActionObsoleteConfig.V100290ObsoleteIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Bencodex;
using Bencodex.Types;
Expand All @@ -11,12 +10,12 @@
using GraphQL.NewtonsoftJson;
using Lib9c;
using Libplanet.Action;
using Libplanet.Action.Sys;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
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;
Expand All @@ -25,6 +24,7 @@
using Nekoyume.Blockchain.Policy;
using NineChronicles.Headless.GraphTypes;
using NineChronicles.Headless.Tests.Common;
using NineChronicles.Headless.Tests.Common.Actions;
using NineChronicles.Headless.Utils;
using Xunit;
using static NineChronicles.Headless.NCActionUtils;
Expand All @@ -51,11 +51,11 @@ public TransactionHeadlessQueryTest()
Block genesisBlock = BlockChain.ProposeGenesisBlock(
transactions: new IAction[]
{
new Initialize(
new InitializeValidator(
new ValidatorSet(
new[] { new Validator(_proposer.PublicKey, BigInteger.One) }
new[] { new Validator(_proposer.PublicKey, 10_000_000_000_000_000_000) }
.ToList()),
states: ImmutableDictionary.Create<Address, IValue>())
Currency.Uncapped("ncg", 2, null))
}.Select((sa, nonce) => Transaction.Create(nonce, new PrivateKey(), null, new[] { sa.PlainValue }))
.ToImmutableList(),
privateKey: new PrivateKey()
Expand Down Expand Up @@ -428,7 +428,7 @@ private Task<ExecutionResult> ExecuteAsync(string query)
hash,
DateTimeOffset.UtcNow,
validator.PublicKey,
null,
10_000_000_000_000_000_000,
VoteFlag.PreCommit).Sign(validator)))
: (BlockCommit?)null;
}
Expand Down

0 comments on commit d0b3155

Please sign in to comment.