From 8887174e1472a7b802dca03e62e3c185d87d7c1f Mon Sep 17 00:00:00 2001 From: ilgyu Date: Fri, 25 Oct 2024 11:37:04 +0900 Subject: [PATCH] test: Add SkipDuplicatedClaim test --- .../Action/Guild/ClaimRewardGuildTest.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/.Lib9c.Tests/Action/Guild/ClaimRewardGuildTest.cs b/.Lib9c.Tests/Action/Guild/ClaimRewardGuildTest.cs index 84197af259..a8ee2f06db 100644 --- a/.Lib9c.Tests/Action/Guild/ClaimRewardGuildTest.cs +++ b/.Lib9c.Tests/Action/Guild/ClaimRewardGuildTest.cs @@ -77,5 +77,69 @@ public void Execute() Assert.Equal(expectedReward, actualReward); } } + + [Fact] + public void SkipDuplicatedClaim() + { + // Given + var validatorKey = new PrivateKey(); + var agentAddress = AddressUtil.CreateAgentAddress(); + var guildMasterAddress = AddressUtil.CreateAgentAddress(); + var guildAddress = AddressUtil.CreateGuildAddress(); + var nParticipants = 10; + var guildParticipantAddresses = Enumerable.Range(0, nParticipants).Select( + _ => AddressUtil.CreateAgentAddress()).ToList(); + + IWorld world = World; + world = EnsureToMintAsset(world, validatorKey.Address, GG * 100); + world = EnsureToCreateValidator(world, validatorKey.PublicKey); + world = EnsureToMintAsset(world, StakeState.DeriveAddress(guildMasterAddress), GG * 100); + world = EnsureToMakeGuild(world, guildAddress, guildMasterAddress, validatorKey.Address); + world = guildParticipantAddresses.Select((addr, idx) => (addr, idx)).Aggregate(world, (w, item) => + { + w = EnsureToMintAsset(w, item.addr, Mead * 100); + w = EnsureToMintAsset(w, StakeState.DeriveAddress(item.addr), GG * ((item.idx + 1) * 100)); + return EnsureToJoinGuild(w, guildAddress, item.addr, 1L); + }); + + // When + var repository = new GuildRepository(world, new ActionContext()); + var guild = repository.GetGuild(guildAddress); + var reward = NCG * 1000; + repository.UpdateWorld(EnsureToMintAsset(repository.World, guild.RewardPoolAddress, reward)); + guild.CollectRewards(1); + world = repository.World; + + var claimRewardGuild = new ClaimRewardGuild(); + world = claimRewardGuild.Execute(new ActionContext + { + PreviousState = world, + Signer = guildParticipantAddresses[0], + BlockIndex = 2L, + }); + + world = claimRewardGuild.Execute(new ActionContext + { + PreviousState = world, + Signer = guildParticipantAddresses[0], + BlockIndex = 2L, + }); + + world = claimRewardGuild.Execute(new ActionContext + { + PreviousState = world, + Signer = guildParticipantAddresses[0], + BlockIndex = 3L, + }); + + //Then + var expectedRepository = new GuildRepository(world, new ActionContext()); + var expectedGuild = expectedRepository.GetGuild(guildAddress); + var bond = expectedRepository.GetBond(expectedGuild, guildParticipantAddresses[0]); + var expectedReward = (reward * bond.Share).DivRem(expectedGuild.TotalShares).Quotient; + var actualReward = world.GetBalance(guildParticipantAddresses[0], NCG); + + Assert.Equal(expectedReward, actualReward); + } } }