Skip to content

Commit

Permalink
fix: Fix guild and participant modules
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 19, 2024
1 parent 6ed6fcf commit c18d338
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .Lib9c.Tests/Util/DelegationUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IWorld MakeGuild(
RandomSeed = Random.Shared.Next(),
};
var makeGuild = new MakeGuild(validatorAddress);
return makeGuild.ExecutePublic(actionContext);
return makeGuild.Execute(actionContext);
}

public static FungibleAssetValue GetGuildCoinFromNCG(FungibleAssetValue balance)
Expand Down
7 changes: 1 addition & 6 deletions Lib9c/Action/Guild/BanGuildMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override IWorld Execute(IActionContext context)
var repository = new GuildRepository(world, context);
var signer = context.GetAgentAddress();

if (repository.GetJoinedGuild(signer) is not { } guildAddress)
{
throw new InvalidOperationException("The signer does not have a guild.");
}

repository.Ban(guildAddress, signer, Target);
repository.Ban(signer, Target);

return repository.World;
}
Expand Down
7 changes: 1 addition & 6 deletions Lib9c/Action/Guild/UnbanGuildMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public override IWorld Execute(IActionContext context)
var repository = new GuildRepository(world, context);
var signer = context.GetAgentAddress();

if (repository.GetJoinedGuild(signer) is not { } guildAddress)
{
throw new InvalidOperationException("The signer does not join any guild.");
}

repository.Unban(guildAddress, signer, Target);
repository.Unban(signer, Target);
return repository.World;
}
}
Expand Down
43 changes: 23 additions & 20 deletions Lib9c/Action/ValidatorDelegation/ReleaseValidatorUnbondings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,34 @@ private IWorld Unstake(
var agentAddress = new AgentAddress(unbondLockIn.DelegatorAddress);
var guildRepository = new GuildRepository(world, context);
var goldCurrency = world.GetGoldCurrency();
if (guildRepository.TryGetGuildParticipant(agentAddress, out var guildParticipant))
if (!IsValidator(world, context, unbondLockIn.DelegateeAddress))
{
var guild = guildRepository.GetGuild(guildParticipant.GuildAddress);
var stakeStateAddress = guildParticipant.DelegationPoolAddress;
var gg = world.GetBalance(stakeStateAddress, ValidatorDelegatee.ValidatorDelegationCurrency);
if (gg.Sign > 0)
if (guildRepository.TryGetGuildParticipant(agentAddress, out var guildParticipant))
{
var (ncg, _) = ConvertToGoldCurrency(gg, goldCurrency);
world = world.BurnAsset(context, stakeStateAddress, gg);
world = world.TransferAsset(
context, stakeStateAddress, agentAddress, ncg);
var guild = guildRepository.GetGuild(guildParticipant.GuildAddress);
var stakeStateAddress = guildParticipant.DelegationPoolAddress;
var gg = world.GetBalance(stakeStateAddress, ValidatorDelegatee.ValidatorDelegationCurrency);
if (gg.Sign > 0)
{
var (ncg, _) = ConvertToGoldCurrency(gg, goldCurrency);
world = world.BurnAsset(context, stakeStateAddress, gg);
world = world.TransferAsset(
context, stakeStateAddress, agentAddress, ncg);
}
}
}
else if (!IsValidator(world, context, unbondLockIn.DelegateeAddress))
{
if (releasedFAV is not FungibleAssetValue gg || gg.Sign < 1)
else
{
return world;
}
if (releasedFAV is not FungibleAssetValue gg || gg.Sign < 1)
{
return world;
}

var stakeStateAddress = StakeState.DeriveAddress(agentAddress);
var (ncg, _) = ConvertToGoldCurrency(gg, goldCurrency);
world = world
.TransferAsset(context, stakeStateAddress, agentAddress, ncg)
.BurnAsset(context, stakeStateAddress, gg);
var stakeStateAddress = StakeState.DeriveAddress(agentAddress);
var (ncg, _) = ConvertToGoldCurrency(gg, goldCurrency);
world = world
.TransferAsset(context, stakeStateAddress, agentAddress, ncg)
.BurnAsset(context, stakeStateAddress, gg);
}
}

return world;
Expand Down
4 changes: 3 additions & 1 deletion Lib9c/Model/Guild/GuildRejoinCooldown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace Nekoyume.Model.Guild
{
public class GuildRejoinCooldown : IBencodable, IEquatable<GuildRejoinCooldown>
{
public static readonly long CooldownPeriod = ValidatorDelegatee.ValidatorUnbondingPeriod;

public GuildRejoinCooldown(AgentAddress agentAddress, long quitHeight)
{
AgentAddress = agentAddress;
ReleaseHeight = quitHeight + ValidatorDelegatee.ValidatorUnbondingPeriod;
ReleaseHeight = quitHeight + CooldownPeriod;
}

public GuildRejoinCooldown(AgentAddress agentAddress, IValue bencoded)
Expand Down
13 changes: 11 additions & 2 deletions Lib9c/Module/Guild/GuildBanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ public static bool IsBanned(this GuildRepository repository, GuildAddress guildA

public static void Ban(
this GuildRepository repository,
GuildAddress guildAddress,
AgentAddress signer,
AgentAddress target)
{
if (repository.GetJoinedGuild(signer) is not { } guildAddress)
{
throw new InvalidOperationException("The signer does not have a guild.");
}

if (!repository.TryGetGuild(guildAddress, out var guild))
{
throw new InvalidOperationException("There is no such guild.");
Expand All @@ -60,8 +64,13 @@ public static void Ban(
account => account.SetState(target, (Boolean)true)));
}

public static void Unban(this GuildRepository repository, GuildAddress guildAddress, AgentAddress signer, Address target)
public static void Unban(this GuildRepository repository, AgentAddress signer, Address target)
{
if (repository.GetJoinedGuild(signer) is not { } guildAddress)
{
throw new InvalidOperationException("The signer does not join any guild.");
}

if (!repository.TryGetGuild(guildAddress, out var guild))
{
throw new InvalidOperationException("There is no such guild.");
Expand Down
17 changes: 12 additions & 5 deletions Lib9c/Module/Guild/GuildModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static GuildRepository MakeGuild(
GuildAddress guildAddress,
Address validatorAddress)
{
var signer = new AgentAddress(repository.ActionContext.Signer);
if (repository.GetJoinedGuild(signer) is not null)
var signer = repository.ActionContext.Signer;
if (repository.GetJoinedGuild(new AgentAddress(signer)) is not null)
{
throw new InvalidOperationException("The signer already has a guild.");
}
Expand All @@ -56,9 +56,16 @@ public static GuildRepository MakeGuild(
throw new InvalidOperationException("The validator does not exist.");
}

var guild = new Model.Guild.Guild(guildAddress, signer, validatorAddress, repository);
if (validatorRepository.TryGetValidatorDelegatee(signer, out var _))
{
throw new InvalidOperationException("Validator cannot make a guild.");
}

var guildMasterAddress = new AgentAddress(signer);
var guild = new Model.Guild.Guild(
guildAddress, guildMasterAddress, validatorAddress, repository);
repository.SetGuild(guild);
repository.JoinGuild(guildAddress, signer);
repository.JoinGuild(guildAddress, guildMasterAddress);

return repository;
}
Expand Down Expand Up @@ -89,7 +96,7 @@ public static GuildRepository RemoveGuild(

var validatorRepository = new ValidatorRepository(repository.World, repository.ActionContext);
var validatorDelegatee = validatorRepository.GetValidatorDelegatee(guild.ValidatorAddress);
var bond = validatorRepository.GetBond(validatorDelegatee, signer);
var bond = validatorRepository.GetBond(validatorDelegatee, guild.Address);
if (bond.Share > 0)
{
throw new InvalidOperationException("The signer has a bond with the validator.");
Expand Down
102 changes: 25 additions & 77 deletions Lib9c/Module/Guild/GuildParticipantModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Libplanet.Types.Assets;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Model.Guild;
using Nekoyume.Module.ValidatorDelegation;
using Nekoyume.TypedAddress;
using Nekoyume.ValidatorDelegation;

Expand All @@ -28,9 +29,11 @@ public static GuildRepository JoinGuild(
GuildAddress guildAddress,
AgentAddress target)
{
var height = repository.ActionContext.BlockIndex;
var signer = repository.ActionContext.Signer;
if (repository.TryGetGuildParticipant(target, out _))
{
throw new ArgumentException("The signer already joined a guild.");
throw new InvalidOperationException("The signer already joined a guild.");
}

if (repository.GetGuildRejoinCooldown(target) is { } cooldown
Expand All @@ -40,13 +43,20 @@ public static GuildRepository JoinGuild(
$"The signer is in the rejoin cooldown period until block {cooldown.ReleaseHeight}");
}

var validatorRepository = new ValidatorRepository(repository.World, repository.ActionContext);
if (validatorRepository.TryGetValidatorDelegatee(signer, out var _))
{
throw new InvalidOperationException("Validator cannot join a guild.");
}

var guildParticipant = new GuildParticipant(target, guildAddress, repository);
var guildGold = repository.GetBalance(guildParticipant.DelegationPoolAddress, Currencies.GuildGold);
var guild = repository.GetGuild(guildAddress);
repository.SetGuildParticipant(guildParticipant);
repository.IncreaseGuildMemberCount(guildAddress);
if (guildGold.RawValue > 0)
{
repository.Delegate(target, guildGold);
guildParticipant.Delegate(guild, guildGold, height);
}

return repository;
Expand All @@ -57,6 +67,7 @@ public static GuildRepository MoveGuild(
AgentAddress guildParticipantAddress,
GuildAddress dstGuildAddress)
{
var height = repository.ActionContext.BlockIndex;
var guildParticipant1 = repository.GetGuildParticipant(guildParticipantAddress);
var srcGuild = repository.GetGuild(guildParticipant1.GuildAddress);
var dstGuild = repository.GetGuild(dstGuildAddress);
Expand All @@ -69,14 +80,15 @@ public static GuildRepository MoveGuild(
}

var guildParticipant2 = new GuildParticipant(guildParticipantAddress, dstGuildAddress, repository);
var bond = validatorRepository.GetBond(srcValidatorDelegatee, guildParticipantAddress);
var bond = validatorRepository.GetBond(srcValidatorDelegatee, srcGuild.Address);
var share = bond.Share;
repository.RemoveGuildParticipant(guildParticipantAddress);
repository.DecreaseGuildMemberCount(guildParticipant1.GuildAddress);
repository.SetGuildParticipant(guildParticipant2);
repository.IncreaseGuildMemberCount(dstGuildAddress);
if (bond.Share > 0)
if (share > 0)
{
repository.Redelegate(guildParticipantAddress, dstGuildAddress);
guildParticipant1.Redelegate(srcGuild, dstGuild, share, height);
}

return repository;
Expand All @@ -103,17 +115,21 @@ public static GuildRepository LeaveGuild(
"The signer is a guild master. Guild master cannot quit the guild.");
}

var height = repository.ActionContext.BlockIndex;
var guildParticipant = repository.GetGuildParticipant(agentAddress);
var validatorRepository = new ValidatorRepository(repository.World, repository.ActionContext);
var validatorDelegatee = validatorRepository.GetValidatorDelegatee(guild.ValidatorAddress);
var bond = validatorRepository.GetBond(validatorDelegatee, agentAddress);
var bond = validatorRepository.GetBond(validatorDelegatee, guild.Address);
var share = bond.Share;

repository.RemoveGuildParticipant(agentAddress);
repository.DecreaseGuildMemberCount(guild.Address);
if (bond.Share > 0)
{
repository.Undelegate(agentAddress);
guildParticipant.Undelegate(guild, share, height);
}

repository.RemoveGuildParticipant(agentAddress);
repository.DecreaseGuildMemberCount(guild.Address);

repository.SetGuildRejoinCooldown(agentAddress, repository.ActionContext.BlockIndex);

return repository;
Expand All @@ -136,74 +152,6 @@ public static bool TryGetGuildParticipant(
}
}

// TODO: Hide this method as private after migration.
public static GuildRepository Delegate(
this GuildRepository repository,
AgentAddress guildParticipantAddress,
FungibleAssetValue fav)
{
var height = repository.ActionContext.BlockIndex;

// TODO: Remove below unnecessary height condition after migration.
if (repository.World.GetDelegationMigrationHeight() is long migrationHeight)
{
height = Math.Max(height, migrationHeight);
}

var guildParticipant = repository.GetGuildParticipant(guildParticipantAddress);
var guild = repository.GetGuild(guildParticipant.GuildAddress);
guildParticipant.Delegate(guild, fav, height);

return repository;
}

private static GuildRepository Undelegate(
this GuildRepository repository,
AgentAddress guildParticipantAddress)
{
var height = repository.ActionContext.BlockIndex;
var guildParticipant = repository.GetGuildParticipant(guildParticipantAddress);
var guild = repository.GetGuild(guildParticipant.GuildAddress);
var share = repository.GetBond(
new ValidatorRepository(repository.World, repository.ActionContext)
.GetValidatorDelegatee(guild.ValidatorAddress),
guildParticipantAddress).Share;
guildParticipant.Undelegate(guild, share, height);

return repository;
}

private static GuildRepository Undelegate(
this GuildRepository repository,
AgentAddress guildParticipantAddress,
BigInteger share)
{
var height = repository.ActionContext.BlockIndex;
var guildParticipant = repository.GetGuildParticipant(guildParticipantAddress);
var guild = repository.GetGuild(guildParticipant.GuildAddress);
guildParticipant.Undelegate(guild, share, height);

return repository;
}

public static GuildRepository Redelegate(
this GuildRepository repository,
AgentAddress guildParticipantAddress,
GuildAddress dstGuildAddress)
{
var height = repository.ActionContext.BlockIndex;
var guildParticipant = repository.GetGuildParticipant(guildParticipantAddress);
var guild = repository.GetGuild(guildParticipant.GuildAddress);
var share = repository.GetBond(
new ValidatorRepository(repository.World, repository.ActionContext)
.GetValidatorDelegatee(guild.ValidatorAddress),
guildParticipantAddress).Share;
var dstGuild = repository.GetGuild(dstGuildAddress);
guildParticipant.Redelegate(guild, dstGuild, share, height);

return repository;
}

private static GuildRepository SetGuildRejoinCooldown(
this GuildRepository repository,
AgentAddress guildParticipantAddress,
Expand Down

0 comments on commit c18d338

Please sign in to comment.