Skip to content

Commit

Permalink
Merge pull request #2161 from planetarium/release/1.2.0
Browse files Browse the repository at this point in the history
Release/1.2.0
  • Loading branch information
ipdae authored Oct 5, 2023
2 parents fe86a78 + ccd74ec commit 7a62fd6
Show file tree
Hide file tree
Showing 21 changed files with 1,154 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void Main(string[] args)

Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().CreateLogger();
Libplanet.Crypto.CryptoConfig.CryptoBackend = new Secp256K1CryptoBackend<SHA256>();
var policySource = new BlockPolicySource(Log.Logger, LogEventLevel.Verbose);
var policySource = new BlockPolicySource();
IBlockPolicy policy =
policySource.GetPolicy(
maxTransactionsBytesPolicy: null,
Expand Down
21 changes: 16 additions & 5 deletions .Lib9c.Tests/Action/ClaimStakeRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public ClaimStakeRewardTest(ITestOutputHelper outputHelper)
_stakePolicySheet = initialStatesWithAvatarStateV2.GetSheet<StakePolicySheet>();
}

// NOTE: object[] {
// long startedBlockIndex,
// long? receivedBlockIndex,
// long stakedBalance,
// long blockIndex,
// (Address balanceAddr, FungibleAssetValue fav)[] expectedBalances,
// (int itemSheetId, int count)[] expectedItems)
// }
public static IEnumerable<object[]>
GetMemberData_Execute_Success_With_StakePolicySheetFixtureV1()
{
Expand Down Expand Up @@ -200,6 +208,13 @@ public static IEnumerable<object[]>
};
}

// NOTE: object[] {
// long startedBlockIndex,
// long? receivedBlockIndex,
// long stakedBalance,
// long blockIndex,
// (Address balanceAddr, FungibleAssetValue fav)[] expectedBalances,
// (int itemSheetId, int count)[] expectedItems)
public static IEnumerable<object[]>
GetMemberData_Execute_Success_With_StakePolicySheetFixtureV2()
{
Expand Down Expand Up @@ -651,11 +666,7 @@ private static StakeStateV2 PrepareStakeStateV2(
long startedBlockIndex,
long? receivedBlockIndex)
{
var contract = new Contract(
stakePolicySheet.StakeRegularFixedRewardSheetValue,
stakePolicySheet.StakeRegularRewardSheetValue,
stakePolicySheet.RewardIntervalValue,
stakePolicySheet.LockupIntervalValue);
var contract = new Contract(stakePolicySheet);
return receivedBlockIndex is null
? new StakeStateV2(contract, startedBlockIndex)
: new StakeStateV2(contract, startedBlockIndex, receivedBlockIndex.Value);
Expand Down
6 changes: 4 additions & 2 deletions .Lib9c.Tests/Action/RewardGoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace Lib9c.Tests.Action
using System.Threading.Tasks;
using Bencodex;
using Bencodex.Types;
using Lib9c.Renderers;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blockchain.Renderers;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
Expand Down Expand Up @@ -479,7 +481,7 @@ void AssertMinerReward(int blockIndex, string expected)
[InlineData(false)]
public async Task Genesis_StateRootHash(bool mainnet)
{
BlockPolicySource blockPolicySource = new BlockPolicySource(Logger.None);
BlockPolicySource blockPolicySource = new BlockPolicySource();
NCStagePolicy stagePolicy = new NCStagePolicy(default, 2);
IBlockPolicy policy = blockPolicySource.GetPolicy();
Block genesis;
Expand Down Expand Up @@ -553,7 +555,7 @@ public async Task Genesis_StateRootHash(bool mainnet)
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader()
),
renderers: blockPolicySource.GetRenderers()
renderers: new IRenderer[] { new ActionRenderer(), new BlockRenderer() }
);
Assert.Equal(genesis.StateRootHash, blockChain.Genesis.StateRootHash);
}
Expand Down
260 changes: 260 additions & 0 deletions .Lib9c.Tests/Action/Scenario/StakeAndClaimScenarioTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
namespace Lib9c.Tests.Action.Scenario
{
using System.Collections.Generic;
using Bencodex.Types;
using Lib9c.Tests.Fixtures.TableCSV;
using Lib9c.Tests.Fixtures.TableCSV.Stake;
using Lib9c.Tests.Util;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.Stake;
using Nekoyume.Model.State;
using Nekoyume.TableData;
using Nekoyume.TableData.Stake;
using Serilog;
using Xunit;
using Xunit.Abstractions;

/// <summary>
/// This class is used for testing the stake and claim scenario with patching table sheets.
/// This class considers only AvatarStateV2 not AvatarState.
/// </summary>
public class StakeAndClaimScenarioTest
{
private readonly Address _agentAddr;
private readonly Address _avatarAddr;
private readonly IAccount _initialStateWithoutStakePolicySheet;
private readonly Currency _ncg;

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

var tuple = InitializeUtil.InitializeStates(
sheetsOverride: new Dictionary<string, string>
{
{
nameof(GameConfigSheet),
GameConfigSheetFixtures.Default
},
{
"StakeRegularFixedRewardSheet_V1",
StakeRegularFixedRewardSheetFixtures.V1
},
{
"StakeRegularFixedRewardSheet_V2",
StakeRegularFixedRewardSheetFixtures.V2
},
{
"StakeRegularRewardSheet_V1",
StakeRegularRewardSheetFixtures.V1
},
{
"StakeRegularRewardSheet_V2",
StakeRegularRewardSheetFixtures.V2
},
{
nameof(StakePolicySheet),
StakePolicySheetFixtures.V2
},
});
_agentAddr = tuple.agentAddr;
_avatarAddr = tuple.avatarAddr;
_initialStateWithoutStakePolicySheet = tuple.initialStatesWithAvatarStateV2;
_ncg = _initialStateWithoutStakePolicySheet.GetGoldCurrency();
}

[Fact]
public void Test()
{
// Mint NCG to agent.
var state = MintAsset(
_initialStateWithoutStakePolicySheet,
_agentAddr,
10_000_000 * _ncg,
0);

// Stake 50 NCG via stake2.
const long stakedAmount = 50;
const int stake2BlockIndex = 1;
state = Stake2(state, _agentAddr, stakedAmount, stake2BlockIndex);

// Validate staked.
var stakedNCG = stakedAmount * _ncg;
ValidateStakedState(state, _agentAddr, stakedNCG, stake2BlockIndex);

// Claim stake reward via claim_stake_reward9.
state = ClaimStakeReward9(
state,
_agentAddr,
_avatarAddr,
stake2BlockIndex + StakeState.LockupInterval);

// Validate staked.
ValidateStakedStateV2(
state,
_agentAddr,
stakedNCG,
stake2BlockIndex,
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
StakeState.RewardInterval,
StakeState.LockupInterval);

// Withdraw stake via stake3.
state = Stake3(state, _agentAddr, 0, stake2BlockIndex + StakeState.LockupInterval + 1);

// Stake 50 NCG via stake3 before patching.
const long firstStake3BlockIndex = stake2BlockIndex + StakeState.LockupInterval + 1;
state = Stake3(
state,
_agentAddr,
stakedAmount,
firstStake3BlockIndex);

// Validate staked.
ValidateStakedStateV2(
state,
_agentAddr,
stakedNCG,
firstStake3BlockIndex,
"StakeRegularFixedRewardSheet_V2",
"StakeRegularRewardSheet_V2",
50_400,
201_600);

// Patch StakePolicySheet and so on.
state = state.SetState(
Addresses.GetSheetAddress(nameof(StakePolicySheet)),
StakePolicySheetFixtures.V3.Serialize());

// Stake 50 NCG via stake3 after patching.
state = Stake3(
state,
_agentAddr,
stakedAmount,
firstStake3BlockIndex + 1);

// Validate staked.
ValidateStakedStateV2(
state,
_agentAddr,
stakedNCG,
firstStake3BlockIndex + 1,
"StakeRegularFixedRewardSheet_V1",
"StakeRegularRewardSheet_V1",
40,
150);
}

private static IAccount MintAsset(
IAccount state,
Address recipient,
FungibleAssetValue amount,
long blockIndex)
{
return state.MintAsset(
new ActionContext
{
PreviousState = state,
Signer = Addresses.Admin,
BlockIndex = blockIndex,
Rehearsal = false,
},
recipient,
amount);
}

private static IAccount Stake2(
IAccount state,
Address agentAddr,
long stakingAmount,
long blockIndex)
{
var stake2 = new Stake2(stakingAmount);
return stake2.Execute(new ActionContext
{
PreviousState = state,
Signer = agentAddr,
BlockIndex = blockIndex,
Rehearsal = false,
});
}

private static IAccount Stake3(
IAccount state,
Address agentAddr,
long stakingAmount,
long blockIndex)
{
var stake3 = new Stake(stakingAmount);
return stake3.Execute(new ActionContext
{
PreviousState = state,
Signer = agentAddr,
BlockIndex = blockIndex,
Rehearsal = false,
});
}

private static IAccount ClaimStakeReward9(
IAccount state,
Address agentAddr,
Address avatarAddr,
long blockIndex)
{
var claimStakingReward9 = new ClaimStakeReward(avatarAddr);
return claimStakingReward9.Execute(new ActionContext
{
PreviousState = state,
Signer = agentAddr,
BlockIndex = blockIndex,
Rehearsal = false,
});
}

private static void ValidateStakedState(
IAccountState state,
Address agentAddr,
FungibleAssetValue expectStakedAmount,
long expectStartedBlockIndex)
{
var stakeAddr = StakeState.DeriveAddress(agentAddr);
var actualStakedAmount = state.GetBalance(stakeAddr, expectStakedAmount.Currency);
Assert.Equal(expectStakedAmount, actualStakedAmount);
var stakeState = new StakeState((Dictionary)state.GetState(stakeAddr));
Assert.Equal(expectStartedBlockIndex, stakeState.StartedBlockIndex);
}

private static void ValidateStakedStateV2(
IAccountState state,
Address agentAddr,
FungibleAssetValue expectStakedAmount,
long expectStartedBlockIndex,
string expectStakeRegularFixedRewardSheetName,
string expectStakeRegularRewardSheetName,
long expectRewardInterval,
long expectLockupInterval)
{
var stakeAddr = StakeState.DeriveAddress(agentAddr);
var actualStakedAmount = state.GetBalance(stakeAddr, expectStakedAmount.Currency);
Assert.Equal(expectStakedAmount, actualStakedAmount);
var stakeState = new StakeStateV2(state.GetState(stakeAddr));
Assert.Equal(expectStartedBlockIndex, stakeState.StartedBlockIndex);
Assert.Equal(
expectStakeRegularFixedRewardSheetName,
stakeState.Contract.StakeRegularFixedRewardSheetTableName);
Assert.Equal(
expectStakeRegularRewardSheetName,
stakeState.Contract.StakeRegularRewardSheetTableName);
Assert.Equal(expectRewardInterval, stakeState.Contract.RewardInterval);
Assert.Equal(expectLockupInterval, stakeState.Contract.LockupInterval);
}
}
}
Loading

0 comments on commit 7a62fd6

Please sign in to comment.