-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sequencer): Allow a sequencer to increase their bond (#1015)
- Loading branch information
Showing
13 changed files
with
1,188 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
syntax = "proto3"; | ||
|
||
package dymensionxyz.dymension.sequencer; | ||
|
||
import "dymensionxyz/dymension/sequencer/description.proto"; | ||
import "google/protobuf/any.proto"; | ||
import "cosmos_proto/cosmos.proto"; | ||
import "gogoproto/gogo.proto"; | ||
import "cosmos/base/v1beta1/coin.proto"; | ||
import "google/protobuf/timestamp.proto"; | ||
import "cosmos/msg/v1/msg.proto"; | ||
|
||
option go_package = "github.com/dymensionxyz/dymension/v3/x/sequencer/types"; | ||
|
||
// EventIncreasedBond is an event emitted when a sequencer's bond is increased. | ||
message EventIncreasedBond { | ||
// sequencer is the bech32-encoded address of the sequencer which increased its bond | ||
string sequencer = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; | ||
// added_amount is the amount of coins added to the sequencer's bond | ||
cosmos.base.v1beta1.Coin added_amount = 2 [(gogoproto.nullable) = false]; | ||
// bond is the new active bond amount of the sequencer | ||
repeated cosmos.base.v1beta1.Coin bond = 3 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/dymensionxyz/dymension/v3/x/sequencer/types" | ||
) | ||
|
||
// IncreaseBond implements types.MsgServer. | ||
func (k msgServer) IncreaseBond(goCtx context.Context, msg *types.MsgIncreaseBond) (*types.MsgIncreaseBondResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
sequencer, err := k.bondUpdateAllowed(ctx, msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// transfer the bond from the sequencer to the module account | ||
seqAcc := sdk.MustAccAddressFromBech32(msg.Creator) | ||
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, seqAcc, types.ModuleName, sdk.NewCoins(msg.AddAmount)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// update the sequencers bond amount in state | ||
sequencer.Tokens = sequencer.Tokens.Add(msg.AddAmount) | ||
k.UpdateSequencer(ctx, sequencer, sequencer.Status) | ||
|
||
// emit a typed event which includes the added amount and the active bond amount | ||
err = ctx.EventManager().EmitTypedEvent( | ||
&types.EventIncreasedBond{ | ||
Sequencer: msg.Creator, | ||
Bond: sequencer.Tokens, | ||
AddedAmount: msg.AddAmount, | ||
}, | ||
) | ||
|
||
return &types.MsgIncreaseBondResponse{}, err | ||
} | ||
|
||
func (k msgServer) bondUpdateAllowed(ctx sdk.Context, msg *types.MsgIncreaseBond) (types.Sequencer, error) { | ||
// check if the sequencer already exists | ||
sequencer, found := k.GetSequencer(ctx, msg.Creator) | ||
if !found { | ||
return types.Sequencer{}, types.ErrUnknownSequencer | ||
} | ||
|
||
// check if the sequencer is bonded | ||
if !sequencer.IsBonded() { | ||
return types.Sequencer{}, types.ErrInvalidSequencerStatus | ||
} | ||
|
||
// check if sequencer is currently jailed | ||
if sequencer.Jailed { | ||
return types.Sequencer{}, types.ErrSequencerJailed | ||
} | ||
return sequencer, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
bankutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" | ||
"github.com/dymensionxyz/dymension/v3/testutil/sample" | ||
"github.com/dymensionxyz/dymension/v3/x/sequencer/types" | ||
) | ||
|
||
func (suite *SequencerTestSuite) TestIncreaseBond() { | ||
suite.SetupTest() | ||
rollappId := suite.CreateDefaultRollapp() | ||
// setup a default sequencer | ||
defaultSequencerAddress := suite.CreateDefaultSequencer(suite.Ctx, rollappId) | ||
// setup an unbonded sequencer | ||
unbondedSequencerAddress := suite.CreateDefaultSequencer(suite.Ctx, rollappId) | ||
unbondedSequencer, _ := suite.App.SequencerKeeper.GetSequencer(suite.Ctx, unbondedSequencerAddress) | ||
unbondedSequencer.Status = types.Unbonded | ||
suite.App.SequencerKeeper.UpdateSequencer(suite.Ctx, unbondedSequencer, unbondedSequencer.Status) | ||
// setup a jailed sequencer | ||
jailedSequencerAddress := suite.CreateDefaultSequencer(suite.Ctx, rollappId) | ||
jailedSequencer, _ := suite.App.SequencerKeeper.GetSequencer(suite.Ctx, jailedSequencerAddress) | ||
jailedSequencer.Jailed = true | ||
suite.App.SequencerKeeper.UpdateSequencer(suite.Ctx, jailedSequencer, jailedSequencer.Status) | ||
// fund all the sequencers which have been setup | ||
bondAmount := sdk.NewInt64Coin(types.DefaultParams().MinBond.Denom, 100) | ||
err := bankutil.FundAccount(suite.App.BankKeeper, suite.Ctx, sdk.MustAccAddressFromBech32(defaultSequencerAddress), sdk.NewCoins(bondAmount)) | ||
suite.Require().NoError(err) | ||
err = bankutil.FundAccount(suite.App.BankKeeper, suite.Ctx, sdk.MustAccAddressFromBech32(unbondedSequencerAddress), sdk.NewCoins(bondAmount)) | ||
suite.Require().NoError(err) | ||
err = bankutil.FundAccount(suite.App.BankKeeper, suite.Ctx, sdk.MustAccAddressFromBech32(jailedSequencerAddress), sdk.NewCoins(bondAmount)) | ||
suite.Require().NoError(err) | ||
|
||
testCase := []struct { | ||
name string | ||
msg types.MsgIncreaseBond | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "valid", | ||
msg: types.MsgIncreaseBond{ | ||
Creator: defaultSequencerAddress, | ||
AddAmount: bondAmount, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "invalid sequencer", | ||
msg: types.MsgIncreaseBond{ | ||
Creator: sample.AccAddress(), // a random address which is not a registered sequencer | ||
AddAmount: bondAmount, | ||
}, | ||
expectedErr: types.ErrUnknownSequencer, | ||
}, | ||
{ | ||
name: "invalid sequencer status", | ||
msg: types.MsgIncreaseBond{ | ||
Creator: unbondedSequencerAddress, | ||
AddAmount: bondAmount, | ||
}, | ||
expectedErr: types.ErrInvalidSequencerStatus, | ||
}, | ||
{ | ||
name: "jailed sequencer", | ||
msg: types.MsgIncreaseBond{ | ||
Creator: jailedSequencerAddress, | ||
AddAmount: bondAmount, | ||
}, | ||
expectedErr: types.ErrSequencerJailed, | ||
}, | ||
{ | ||
name: "sequencer doesn't have enough balance", | ||
msg: types.MsgIncreaseBond{ | ||
Creator: defaultSequencerAddress, | ||
AddAmount: sdk.NewInt64Coin(types.DefaultParams().MinBond.Denom, 99999999), // very high amount which sequencer doesn't have | ||
}, | ||
expectedErr: sdkerrors.ErrInsufficientFunds, | ||
}, | ||
} | ||
|
||
for _, tc := range testCase { | ||
suite.Run(tc.name, func() { | ||
_, err := suite.msgServer.IncreaseBond(suite.Ctx, &tc.msg) | ||
if tc.expectedErr != nil { | ||
suite.Require().ErrorIs(err, tc.expectedErr) | ||
} else { | ||
suite.Require().NoError(err) | ||
expectedBond := types.DefaultParams().MinBond.Add(bondAmount) | ||
seq, _ := suite.App.SequencerKeeper.GetSequencer(suite.Ctx, defaultSequencerAddress) | ||
suite.Require().Equal(expectedBond, seq.Tokens[0]) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.