Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block leaving guild while unbonding #3144

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .Lib9c.Tests/Action/Guild/QuitGuildTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,29 @@ private void ExecuteWithFixture(IQuitGuildFixture fixture)
var expectedTotalGG = slashedGG - expectedAgengGG;
var expectedTotalShares = totalShare - agentShare;
var quitGuild = new QuitGuild();

if (agentNCG.MajorUnit > 50)
{
world = EnsureToStake(world, agentAddress, agentNCG.Currency * 50, height++);
var actionContextToThrow = new ActionContext
{
PreviousState = world,
Signer = agentAddress,
BlockIndex = height++,
};

Assert.Throws<InvalidOperationException>(() => world = quitGuild.Execute(actionContextToThrow));
height += ValidatorDelegatee.DefaultUnbondingPeriod;
world = EnsureToReleaseUnbonding(world, agentAddress, height++);
}

var actionContext = new ActionContext
{
PreviousState = world,
Signer = agentAddress,
BlockIndex = height++,
};

world = quitGuild.Execute(actionContext);
var delegatee = new GuildRepository(world, new ActionContext { }).GetDelegatee(validatorKey.Address);
world = EnsureToReleaseUnbonding(
Expand All @@ -241,9 +258,10 @@ private void ExecuteWithFixture(IQuitGuildFixture fixture)

Assert.Throws<FailedLoadStateException>(
() => guildRepository.GetGuildParticipant(agentAddress));
Assert.Equal(expectedTotalGG, guildDelegatee.TotalDelegated);
var comparerGG = new FungibleAssetValueEqualityComparer(GGEpsilon);
Assert.Equal(expectedTotalGG, guildDelegatee.TotalDelegated, comparerGG);
Assert.Equal(expectedTotalShares, guildDelegatee.TotalShares);
Assert.Equal(expectedTotalGG, validatorDelegatee.TotalDelegated);
Assert.Equal(expectedTotalGG, validatorDelegatee.TotalDelegated, comparerGG);
Assert.Equal(expectedTotalShares, validatorDelegatee.TotalShares);
}

Expand Down
13 changes: 12 additions & 1 deletion .Lib9c.Tests/Action/Guild/RemoveGuildTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,18 @@ private void ExecuteWithFixture(IRemoveGuildFixture fixture)
{
PreviousState = world,
Signer = masterAddress,
BlockIndex = height,
BlockIndex = height++,
};
Assert.Throws<InvalidOperationException>(() => removeGuild.Execute(actionContext));

height += ValidatorDelegatee.DefaultUnbondingPeriod;
world = EnsureToReleaseUnbonding(world, masterAddress, height++);

actionContext = new ActionContext
{
PreviousState = world,
Signer = masterAddress,
BlockIndex = height++,
};
world = removeGuild.Execute(actionContext);

Expand Down
6 changes: 6 additions & 0 deletions Lib9c/Module/Guild/GuildModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public static GuildRepository RemoveGuild(
throw new InvalidOperationException("There are remained participants in the guild.");
}

if (repository.GetDelegator(signer).UnbondingRefs.Count > 0)
{
throw new InvalidOperationException(
$"The signer cannot remove guild while unbonding");
}

var delegatee = repository.GetDelegatee(guild.ValidatorAddress);
var bond = repository.GetBond(delegatee, signer);
if (bond.Share > 0)
Expand Down
6 changes: 6 additions & 0 deletions Lib9c/Module/Guild/GuildParticipantModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public static GuildRepository LeaveGuild(
"The signer is a guild master. Guild master cannot quit the guild.");
}

if (repository.GetDelegator(agentAddress).UnbondingRefs.Count > 0)
{
throw new InvalidOperationException(
$"The signer cannot leave guild while unbonding");
}

var height = repository.ActionContext.BlockIndex;
var guildParticipant = repository.GetGuildParticipant(agentAddress);
var delegatee = repository.GetDelegatee(guild.ValidatorAddress);
Expand Down
Loading