Skip to content

Commit

Permalink
fix: Block leaving guild while unbonding
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Jan 22, 2025
1 parent c9f098f commit d966331
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
18 changes: 16 additions & 2 deletions .Lib9c.Tests/Action/Guild/QuitGuildTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,25 @@ private void ExecuteWithFixture(IQuitGuildFixture fixture)
var expectedTotalGG = slashedGG - expectedAgengGG;
var expectedTotalShares = totalShare - agentShare;
var quitGuild = new QuitGuild();

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

Assert.Throws<InvalidOperationException>(() => world = quitGuild.Execute(actionContext));
height += ValidatorDelegatee.DefaultUnbondingPeriod;
world = EnsureToReleaseUnbonding(world, agentAddress, height++);
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 +254,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

0 comments on commit d966331

Please sign in to comment.