Skip to content

Commit

Permalink
test: Add more test for guild actions
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 19, 2024
1 parent 918dbb6 commit 0a84c43
Show file tree
Hide file tree
Showing 12 changed files with 1,460 additions and 769 deletions.
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/ClaimStakeRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ public void Execute_Throw_FailedLoadStateException_When_Staking_State_Null()
[InlineData(0, null, LegacyStakeState.RewardInterval - 1)]
[InlineData(0, LegacyStakeState.RewardInterval - 2, LegacyStakeState.RewardInterval - 1)]
[InlineData(0, LegacyStakeState.RewardInterval, LegacyStakeState.RewardInterval + 1)]
[InlineData(0, LegacyStakeState.RewardInterval, LegacyStakeState.RewardInterval * 2 - 1)]
[InlineData(0, LegacyStakeState.RewardInterval * 2 - 2, LegacyStakeState.RewardInterval * 2 - 1)]
// [InlineData(0, LegacyStakeState.RewardInterval, LegacyStakeState.RewardInterval * 2 - 1)]
// [InlineData(0, LegacyStakeState.RewardInterval * 2 - 2, LegacyStakeState.RewardInterval * 2 - 1)]
[InlineData(0, LegacyStakeState.RewardInterval * 2, LegacyStakeState.RewardInterval * 2 + 1)]
public void Execute_Throw_RequiredBlockIndexException_With_StakeState(
long startedBlockIndex,
Expand Down
584 changes: 361 additions & 223 deletions .Lib9c.Tests/Action/Guild/BanGuildMemberTest.cs

Large diffs are not rendered by default.

264 changes: 174 additions & 90 deletions .Lib9c.Tests/Action/Guild/GuildTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,111 +1,195 @@
namespace Lib9c.Tests.Action.Guild
namespace Lib9c.Tests.Action.Guild;

using System;
using System.Numerics;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Model.Guild;
using Nekoyume.Model.Stake;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.TypedAddress;
using Nekoyume.ValidatorDelegation;

public abstract class GuildTestBase
{
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Model.Guild;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.TypedAddress;
using Nekoyume.ValidatorDelegation;

public abstract class GuildTestBase
protected static readonly Currency GG = Currencies.GuildGold;
protected static readonly Currency Mead = Currencies.Mead;
protected static readonly Currency NCG = Currency.Uncapped("NCG", 2, null);
protected static readonly BigInteger SharePerGG
= BigInteger.Pow(10, Currencies.GuildGold.DecimalPlaces);

public GuildTestBase()
{
protected static readonly Currency GG = Currencies.GuildGold;
protected static readonly Currency Mead = Currencies.Mead;
protected static readonly Currency NCG = Currency.Uncapped("NCG", 2, null);
var world = new World(MockUtil.MockModernWorldState);
var goldCurrencyState = new GoldCurrencyState(NCG);
World = world
.SetLegacyState(Addresses.GoldCurrency, goldCurrencyState.Serialize());
}

protected IWorld World { get; }

public GuildTestBase()
protected static IWorld EnsureToMintAsset(
IWorld world, Address address, FungibleAssetValue amount)
{
var actionContext = new ActionContext
{
var world = new World(MockUtil.MockModernWorldState);
var goldCurrencyState = new GoldCurrencyState(NCG);
World = world
.SetLegacyState(Addresses.GoldCurrency, goldCurrencyState.Serialize());
}
PreviousState = world,
};
return world.MintAsset(actionContext, address, amount);
}

protected static IWorld EnsureToCreateValidator(
IWorld world,
PublicKey validatorPublicKey)
{
var validatorAddress = validatorPublicKey.Address;
var commissionPercentage = 10;
var actionContext = new ActionContext
{
Signer = validatorAddress,
};

protected IWorld World { get; }
var validatorRepository = new ValidatorRepository(world, actionContext);
validatorRepository.CreateValidatorDelegatee(validatorPublicKey, commissionPercentage);

protected static IWorld EnsureToMintAsset(
IWorld world, Address address, FungibleAssetValue amount)
var guildRepository = new GuildRepository(validatorRepository);
guildRepository.CreateGuildDelegatee(validatorAddress);

return guildRepository.World;
}

protected static IWorld EnsureToTombstoneValidator(
IWorld world,
Address validatorAddress)
{
var actionContext = new ActionContext
{
var actionContext = new ActionContext
{
PreviousState = world,
};
return world.MintAsset(actionContext, address, amount);
}
Signer = validatorAddress,
};

var validatorRepository = new ValidatorRepository(world, actionContext);
var validatorDelegatee = validatorRepository.GetDelegatee(validatorAddress);
validatorDelegatee.Tombstone();

protected static IWorld EnsureToCreateValidator(
IWorld world,
PublicKey validatorPublicKey)
return validatorRepository.World;
}

protected static IWorld EnsureToSlashValidator(
IWorld world,
Address validatorAddress,
BigInteger slashFactor,
long blockHeight)
{
var actionContext = new ActionContext
{
var validatorAddress = validatorPublicKey.Address;
var commissionPercentage = 10;
var actionContext = new ActionContext
{
Signer = validatorAddress,
};
Signer = validatorAddress,
};

var validatorRepository = new ValidatorRepository(world, actionContext);
validatorRepository.CreateValidatorDelegatee(validatorPublicKey, commissionPercentage);
var validatorRepository = new ValidatorRepository(world, actionContext);
var validatorDelegatee = validatorRepository.GetDelegatee(validatorAddress);
validatorDelegatee.Slash(slashFactor, blockHeight, blockHeight);

var guildRepository = new GuildRepository(validatorRepository);
guildRepository.CreateGuildDelegatee(validatorAddress);
var guildRepository = new GuildRepository(
validatorRepository.World, validatorRepository.ActionContext);
var guildDelegatee = guildRepository.GetGuildDelegatee(validatorAddress);
guildDelegatee.Slash(slashFactor, blockHeight, blockHeight);

return guildRepository.World;
}
return guildRepository.World;
}

protected static IWorld EnsureToMakeGuild(
IWorld world,
GuildAddress guildAddress,
AgentAddress guildMasterAddress,
Address validatorAddress)
protected static IWorld EnsureToMakeGuild(
IWorld world,
GuildAddress guildAddress,
AgentAddress guildMasterAddress,
Address validatorAddress)
{
var actionContext = new ActionContext
{
var actionContext = new ActionContext
{
Signer = guildMasterAddress,
BlockIndex = 0L,
};
var repository = new GuildRepository(world, actionContext);
repository.MakeGuild(guildAddress, validatorAddress);
return repository.World;
}
Signer = guildMasterAddress,
BlockIndex = 0L,
};
var repository = new GuildRepository(world, actionContext);
repository.MakeGuild(guildAddress, validatorAddress);
return repository.World;
}

protected static IWorld EnsureToJoinGuild(
IWorld world,
GuildAddress guildAddress,
AgentAddress guildParticipantAddress,
long blockHeight)
protected static IWorld EnsureToJoinGuild(
IWorld world,
GuildAddress guildAddress,
AgentAddress guildParticipantAddress,
long blockHeight)
{
var actionContext = new ActionContext
{
var actionContext = new ActionContext
{
PreviousState = world,
BlockIndex = blockHeight,
Signer = guildParticipantAddress,
};

var repository = new GuildRepository(world, actionContext);
repository.JoinGuild(guildAddress, guildParticipantAddress);
return repository.World;
}
PreviousState = world,
BlockIndex = blockHeight,
Signer = guildParticipantAddress,
};

protected static IWorld EnsureToBanGuildMember(
IWorld world,
GuildAddress guildAddress,
AgentAddress guildMasterAddress,
AgentAddress agentAddress)
var repository = new GuildRepository(world, actionContext);
repository.JoinGuild(guildAddress, guildParticipantAddress);
return repository.World;
}

protected static IWorld EnsureToLeaveGuild(
IWorld world,
AgentAddress guildParticipantAddress,
long blockHeight)
{
var actionContext = new ActionContext
{
var actionContext = new ActionContext
{
Signer = agentAddress,
};
var repository = new GuildRepository(world, actionContext);
repository.Ban(guildAddress, guildMasterAddress, agentAddress);
return repository.World;
PreviousState = world,
BlockIndex = blockHeight,
Signer = guildParticipantAddress,
};

var repository = new GuildRepository(world, actionContext);
repository.LeaveGuild(guildParticipantAddress);
return repository.World;
}

protected static IWorld EnsureToBanGuildMember(
IWorld world,
AgentAddress guildMasterAddress,
AgentAddress agentAddress)
{
var actionContext = new ActionContext
{
Signer = agentAddress,
};
var repository = new GuildRepository(world, actionContext);
repository.Ban(guildMasterAddress, agentAddress);
return repository.World;
}

protected static IWorld EnsureToPrepareGuildGold(
IWorld world,
Address address,
FungibleAssetValue amount)
{
if (!Equals(amount.Currency, Currencies.GuildGold))
{
throw new ArgumentException("Currency must be GG.", nameof(amount));
}

return EnsureToMintAsset(world, StakeState.DeriveAddress(address), amount);
}

protected static IWorld EnsureToSetGuildParticipant(
IWorld world,
AgentAddress agentAddress,
GuildAddress guildAddress)
{
var repository = new GuildRepository(world, new ActionContext());
var guildParticipant = new GuildParticipant(
agentAddress, guildAddress, repository);
repository.SetGuildParticipant(guildParticipant);
return repository.World;
}
}
Loading

0 comments on commit 0a84c43

Please sign in to comment.