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

LSM Staking and Slashing #9

Merged
merged 26 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
00e671a
proto-gen
sampocs Jun 20, 2023
319337f
copied over staking protos
sampocs Jun 20, 2023
e81ad50
updated proto package name and fixed lint errors
sampocs Jun 20, 2023
25460ac
generated staking protos
sampocs Jun 20, 2023
01505b7
copied over module level files
sampocs Jun 20, 2023
988802b
removed min self delegation
sampocs Jun 20, 2023
b175c50
copied over staking types
sampocs Jun 20, 2023
c86a47b
copied over staking client
sampocs Jun 20, 2023
29052f5
copied over teststaking
sampocs Jun 20, 2023
b59ecbf
fixed build errors related to teststaking or min-self-delegation
sampocs Jun 20, 2023
cc8b59c
copied over staking simulation
sampocs Jun 20, 2023
0cfb4cb
copied over staking keeper
sampocs Jun 21, 2023
af19f65
removed GetLiquidDelegation
sampocs Jun 21, 2023
aa8b986
removed GetLiquidValidator
sampocs Jun 21, 2023
fd8434f
removed GetLiquidHistoricalInfo
sampocs Jun 21, 2023
b941dc8
removed GetLiquidBondedValidatorsByPower
sampocs Jun 21, 2023
38b5c5d
removed GetAllLiquidDelegations
sampocs Jun 21, 2023
c087b45
resolved batch of build errors in staking
sampocs Jun 21, 2023
7881f29
resolved batch of build errors from staking in other modules
sampocs Jun 21, 2023
c351d6d
fixed simulation tests for non-lsm txs
sampocs Jun 21, 2023
a1b58ed
misc small changes while fixing staking unit tests
sampocs Jun 21, 2023
fba2912
added back delegation_test that was removed
sampocs Jun 21, 2023
da29a2d
copied over slashing files
sampocs Jun 21, 2023
62289c7
removed/fixed tests that reference min_self_delegation
sampocs Jun 21, 2023
5743c84
fixed unit tests related to jailing a validator
sampocs Jun 22, 2023
7a79d16
fixed staking cli tests
sampocs Jun 22, 2023
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
2 changes: 1 addition & 1 deletion x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestStakingMsgs(t *testing.T) {
// create validator
description := types.NewDescription("foo_moniker", "", "", "", "")
createValidatorMsg, err := types.NewMsgCreateValidator(
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates, sdk.OneInt(),
sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates,
sampocs marked this conversation as resolved.
Show resolved Hide resolved
)
require.NoError(t, err)

Expand Down
6 changes: 3 additions & 3 deletions x/staking/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
res, err := msgServer.TokenizeShares(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgRedeemTokensforShares:
res, err := msgServer.RedeemTokens(sdk.WrapSDKContext(ctx), msg)
case *types.MsgRedeemTokensForShares:
res, err := msgServer.RedeemTokensForShares(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)

case *types.MsgTransferTokenizeShareRecord:
Expand All @@ -59,7 +59,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return sdk.WrapServiceResult(ctx, res, err)

default:
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized %s message type: %T", types.ModuleName, msg)
}
}
}
24 changes: 11 additions & 13 deletions x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) {
"initBond: %v\ngotBond: %v\nbond: %v\n",
initBond, gotBond, bond)

newMinSelfDelegation := sdk.OneInt()
msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil, &newMinSelfDelegation)
msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil)
tstaking.Handle(msgEditValidator, false)
}

Expand Down Expand Up @@ -408,8 +407,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) {
"initBond: %v\ngotBond: %v\nbond: %v\n",
initBond, gotBond, bond)

newMinSelfDelegation := initBond.Add(sdk.OneInt())
msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil, &newMinSelfDelegation)
msgEditValidator := types.NewMsgEditValidator(validatorAddr, types.Description{}, nil)
tstaking.Handle(msgEditValidator, false)
}

Expand Down Expand Up @@ -1185,15 +1183,15 @@ func TestInvalidCoinDenom(t *testing.T) {
oneCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())

commission := types.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.ZeroDec())
msgCreate, err := types.NewMsgCreateValidator(valA, PKs[0], invalidCoin, types.Description{}, commission, sdk.OneInt())
msgCreate, err := types.NewMsgCreateValidator(valA, PKs[0], invalidCoin, types.Description{}, commission)
require.NoError(t, err)
tstaking.Handle(msgCreate, false)

msgCreate, err = types.NewMsgCreateValidator(valA, PKs[0], validCoin, types.Description{}, commission, sdk.OneInt())
msgCreate, err = types.NewMsgCreateValidator(valA, PKs[0], validCoin, types.Description{}, commission)
require.NoError(t, err)
tstaking.Handle(msgCreate, true)

msgCreate, err = types.NewMsgCreateValidator(valB, PKs[1], validCoin, types.Description{}, commission, sdk.OneInt())
msgCreate, err = types.NewMsgCreateValidator(valB, PKs[1], validCoin, types.Description{}, commission)
require.NoError(t, err)
tstaking.Handle(msgCreate, true)

Expand Down Expand Up @@ -1225,47 +1223,47 @@ func TestTokenizeShares(t *testing.T) {
valIndex int64
amount sdk.Int
isSuccess bool
expStatus sdkstaking.BondStatus
expStatus types.BondStatus
expJailed bool
}{
{
"tokenize shares for less than self delegation",
0, 0,
sdk.NewInt(10000),
true,
sdkstaking.Bonded,
types.Bonded,
false,
},
{
"tokenize shares for more than self delegation",
0, 0,
sdk.TokensFromConsensusPower(initPower+1, sdk.DefaultPowerReduction),
false,
sdkstaking.Bonded,
types.Bonded,
false,
},
{
"tokenize share for full self delegation",
0, 0,
sdk.TokensFromConsensusPower(50, sdk.DefaultPowerReduction),
true,
sdkstaking.Bonded,
types.Bonded,
false,
},
{
"tokenize shares for less than delegation",
1, 0,
sdk.NewInt(1000),
true,
sdkstaking.Bonded,
types.Bonded,
false,
},
{
"tokenize shares for more than delegation",
1, 0,
sdk.NewInt(20000),
false,
sdkstaking.Bonded,
types.Bonded,
false,
},
}
Expand Down
55 changes: 11 additions & 44 deletions x/staking/teststaking/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,58 +75,25 @@ func (sh *Helper) DelegateWithPower(delegator sdk.AccAddress, val sdk.ValAddress
}

// Undelegate calls handler to unbound some stake from a validator.
func (sh *Helper) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Int, ok bool) {
func (sh *Helper) Undelegate(delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Int, ok bool) *sdk.Result {
unbondAmt := sdk.NewCoin(sh.Denom, amount)
msg := stakingtypes.NewMsgUndelegate(delegator, val, unbondAmt)
sh.Handle(msg, ok)
return sh.Handle(msg, ok)
}

func (sh *Helper) TokenizeShares(delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Coin, shareOwner sdk.AccAddress, ok bool) {
msg := &stakingtypes.MsgTokenizeShares{
DelegatorAddress: delegator.String(),
ValidatorAddress: val.String(),
Amount: amount,
TokenizedShareOwner: shareOwner.String(),
}
res, err := sh.msgSrvr.TokenizeShares(sdk.WrapSDKContext(sh.Ctx), msg)
if ok {
require.NoError(sh.t, err)
require.NotNil(sh.t, res)
} else {
require.Error(sh.t, err)
require.Nil(sh.t, res)
}
msg := stakingtypes.NewMsgTokenizeShares(delegator, val, amount, shareOwner)
sh.Handle(msg, ok)
}

func (sh *Helper) RedeemTokensForShares(delegator sdk.AccAddress, amount sdk.Coin, ok bool) {
msg := &stakingtypes.MsgRedeemTokensforShares{
DelegatorAddress: delegator.String(),
Amount: amount,
}
res, err := sh.msgSrvr.RedeemTokens(sdk.WrapSDKContext(sh.Ctx), msg)
if ok {
require.NoError(sh.t, err)
require.NotNil(sh.t, res)
} else {
require.Error(sh.t, err)
require.Nil(sh.t, res)
}
msg := stakingtypes.NewMsgRedeemTokensForShares(delegator, amount)
sh.Handle(msg, ok)
}

func (sh *Helper) TranserTokenizeShareRecord(recordID uint64, owner, newOwner sdk.AccAddress, ok bool) {
msg := &stakingtypes.MsgTransferTokenizeShareRecord{
TokenizeShareRecordId: recordID,
Sender: owner.String(),
NewOwner: newOwner.String(),
}
res, err := sh.msgSrvr.TransferTokenizeShareRecord(sdk.WrapSDKContext(sh.Ctx), msg)
if ok {
require.NoError(sh.t, err)
require.NotNil(sh.t, res)
} else {
require.Error(sh.t, err)
require.Nil(sh.t, res)
}
msg := stakingtypes.NewMsgTransferTokenizeShareRecord(recordID, owner, newOwner)
sh.Handle(msg, ok)
}

// Handle calls staking handler on a given message
Expand All @@ -144,8 +111,8 @@ func (sh *Helper) Handle(msg sdk.Msg, ok bool) *sdk.Result {

// CheckValidator asserts that a validor exists and has a given status (if status!="")
// and if has a right jailed flag.
func (sh *Helper) CheckValidator(addr sdk.ValAddress, status sdkstaking.BondStatus, jailed bool) stakingtypes.Validator {
v, ok := sh.k.GetLiquidValidator(sh.Ctx, addr)
func (sh *Helper) CheckValidator(addr sdk.ValAddress, status stakingtypes.BondStatus, jailed bool) stakingtypes.Validator {
v, ok := sh.k.GetValidator(sh.Ctx, addr)
require.True(sh.t, ok)
require.Equal(sh.t, jailed, v.Jailed, "wrong Jalied status")
if status >= 0 {
Expand All @@ -156,7 +123,7 @@ func (sh *Helper) CheckValidator(addr sdk.ValAddress, status sdkstaking.BondStat

// CheckDelegator asserts that a delegator exists
func (sh *Helper) CheckDelegator(delegator sdk.AccAddress, val sdk.ValAddress, found bool) {
_, ok := sh.k.GetLiquidDelegation(sh.Ctx, delegator, val)
_, ok := sh.k.GetDelegation(sh.Ctx, delegator, val)
require.Equal(sh.t, ok, found)
}

Expand Down
3 changes: 3 additions & 0 deletions x/staking/types/hooks_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ func (h StakingHooksTemplate) BeforeValidatorSlashed(ctx sdk.Context, valAddr sd
func (h StakingHooksTemplate) AfterUnbondingInitiated(ctx sdk.Context, id uint64) error {
return nil
}
func (h StakingHooksTemplate) BeforeTokenizeShareRecordRemoved(ctx sdk.Context, recordId uint64) error {
return nil
}
82 changes: 75 additions & 7 deletions x/staking/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ func (msg MsgEditValidator) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
}

if msg.MinSelfDelegation != nil && !msg.MinSelfDelegation.IsPositive() {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"minimum self delegation must be a positive integer",
)
}

if msg.CommissionRate != nil {
if msg.CommissionRate.GT(sdk.OneDec()) || msg.CommissionRate.IsNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "commission rate must be between 0 and 1 (inclusive)")
Expand Down Expand Up @@ -426,9 +419,25 @@ func (msg MsgUnbondValidator) ValidateBasic() error {
return nil
}

// NewMsgTokenizeShares creates a new MsgTokenizeShares instance.
//
//nolint:interfacer
func NewMsgTokenizeShares(delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount sdk.Coin, owner sdk.AccAddress) *MsgTokenizeShares {
return &MsgTokenizeShares{
DelegatorAddress: delAddr.String(),
ValidatorAddress: valAddr.String(),
Amount: amount,
TokenizedShareOwner: owner.String(),
}
}

// Route implements the sdk.Msg interface.
func (msg MsgTokenizeShares) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgTokenizeShares) Type() string { return TypeMsgTokenizeShares }

// GetSigners implements the sdk.Msg interface.
func (msg MsgTokenizeShares) GetSigners() []sdk.AccAddress {
delegator, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
Expand All @@ -437,11 +446,13 @@ func (msg MsgTokenizeShares) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{delegator}
}

// MsgTokenizeShares implements the sdk.Msg interface.
func (msg MsgTokenizeShares) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic implements the sdk.Msg interface.
func (msg MsgTokenizeShares) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err)
Expand All @@ -463,9 +474,23 @@ func (msg MsgTokenizeShares) ValidateBasic() error {
return nil
}

// NewMsgRedeemTokensForShares creates a new MsgRedeemTokensForShares instance.
//
//nolint:interfacer
func NewMsgRedeemTokensForShares(delAddr sdk.AccAddress, amount sdk.Coin) *MsgRedeemTokensForShares {
return &MsgRedeemTokensForShares{
DelegatorAddress: delAddr.String(),
Amount: amount,
}
}

// Route implements the sdk.Msg interface.
func (msg MsgRedeemTokensForShares) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgRedeemTokensForShares) Type() string { return TypeMsgRedeemTokensForShares }

// GetSigners implements the sdk.Msg interface.
func (msg MsgRedeemTokensForShares) GetSigners() []sdk.AccAddress {
delegator, err := sdk.AccAddressFromBech32(msg.DelegatorAddress)
if err != nil {
Expand All @@ -474,11 +499,13 @@ func (msg MsgRedeemTokensForShares) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{delegator}
}

// GetSignBytes implements the sdk.Msg interface.
func (msg MsgRedeemTokensForShares) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic implements the sdk.Msg interface.
func (msg MsgRedeemTokensForShares) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.DelegatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid delegator address: %s", err)
Expand All @@ -494,9 +521,24 @@ func (msg MsgRedeemTokensForShares) ValidateBasic() error {
return nil
}

// NewMsgTransferTokenizeShareRecord creates a new MsgTransferTokenizeShareRecord instance.
//
//nolint:interfacer
func NewMsgTransferTokenizeShareRecord(recordId uint64, sender, newOwner sdk.AccAddress) *MsgTransferTokenizeShareRecord {
return &MsgTransferTokenizeShareRecord{
TokenizeShareRecordId: recordId,
Sender: sender.String(),
NewOwner: newOwner.String(),
}
}

// Route implements the sdk.Msg interface.
func (msg MsgTransferTokenizeShareRecord) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgTransferTokenizeShareRecord) Type() string { return TypeMsgTransferTokenizeShareRecord }

// GetSigners implements the sdk.Msg interface.
func (msg MsgTransferTokenizeShareRecord) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
Expand All @@ -505,11 +547,13 @@ func (msg MsgTransferTokenizeShareRecord) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sender}
}

// GetSignBytes implements the sdk.Msg interface.
func (msg MsgTransferTokenizeShareRecord) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

// ValidateBasic implements the sdk.Msg interface.
func (msg MsgTransferTokenizeShareRecord) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid sender address: %s", err)
Expand All @@ -521,6 +565,18 @@ func (msg MsgTransferTokenizeShareRecord) ValidateBasic() error {
return nil
}

// NewMsgDisableTokenizeShares creates a new MsgDisableTokenizeShares instance.
//
//nolint:interfacer
func NewMsgDisableTokenizeShares(delAddr sdk.AccAddress) *MsgDisableTokenizeShares {
return &MsgDisableTokenizeShares{
DelegatorAddress: delAddr.String(),
}
}

// Route implements the sdk.Msg interface.
func (msg MsgDisableTokenizeShares) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgDisableTokenizeShares) Type() string { return TypeMsgDisableTokenizeShares }

Expand Down Expand Up @@ -548,6 +604,18 @@ func (msg MsgDisableTokenizeShares) ValidateBasic() error {
return nil
}

// NewMsgEnableTokenizeShares creates a new MsgEnableTokenizeShares instance.
//
//nolint:interfacer
func NewMsgEnableTokenizeShares(delAddr sdk.AccAddress) *MsgEnableTokenizeShares {
return &MsgEnableTokenizeShares{
DelegatorAddress: delAddr.String(),
}
}

// Route implements the sdk.Msg interface.
func (msg MsgEnableTokenizeShares) Route() string { return RouterKey }

// Type implements the sdk.Msg interface.
func (msg MsgEnableTokenizeShares) Type() string { return TypeMsgEnableTokenizeShares }

Expand Down
Loading