Skip to content

Commit

Permalink
test: Add SkipDuplicatedClaim test
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Oct 25, 2024
1 parent 34e179c commit 8887174
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .Lib9c.Tests/Action/Guild/ClaimRewardGuildTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 8887174

Please sign in to comment.