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

feat: liquidstaking module #2

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6864fa6
feat: liquidtsaking module
zsystm May 2, 2023
fbd7174
update core design and logic
zsystm May 2, 2023
abb40f5
implement and update core logics
zsystm May 2, 2023
88a3913
refactor code and logics
zsystm May 2, 2023
eb6b4ce
add specs of liquidstaking module
zsystm May 2, 2023
bbc6a5b
implement invariants and fix errors
zsystm Jun 21, 2023
7cba3c1
apply dynamic fee and change chunk size
zsystm Jun 21, 2023
59fa1db
add slashing param change ante handler
zsystm Jun 21, 2023
44a18d2
add one more step to abci
zsystm Jun 21, 2023
14edab6
fix: apply missing endblocker and wrong name convention on proto
zsystm Jun 22, 2023
51a8b7b
add abci events
zsystm Jun 22, 2023
dcbbce5
add demo guidance for liquidstaking module
zsystm Jun 23, 2023
3914584
add test coverage
zsystm Jun 28, 2023
156ed9e
fix query path
zsystm Jul 7, 2023
d755724
update swagger doc
zsystm Jul 7, 2023
4f2e3f7
test: comment broken test of unused module
dongsam Jul 19, 2023
8c3ba3c
test: add workflow to build, test, codecov for ci/cd
dongsam Jul 19, 2023
07cd6b1
apply re-delegation slashing penalty coverage
zsystm Jul 20, 2023
b6c3483
rise completeness: update core logics, apply financial review result …
zsystm Jul 20, 2023
30ee1b7
chore: update statik
zsystm Jul 20, 2023
ef3e686
Merge pull request #5 from b-harvest/ls-add-workflow
dongsam Jul 20, 2023
fe733b3
fix: add maximum cap for insurance fee rate (#6)
zsystm Jul 20, 2023
31450c7
fix: simulations for cosmos modules (#8)
poorphd Jul 21, 2023
75bc01b
feat!: simplify penalty handling logic (#9)
zsystm Jul 21, 2023
cd670b1
docs: cli + api (#15)
zsystm Jul 24, 2023
60a80bf
fix: fix some logics and sync spec to latest source code (#18)
zsystm Jul 31, 2023
d39e530
fix: bugs discovered during simulation (#31)
zsystm Aug 11, 2023
19cf607
package version bump from v6 to v7
zsystm Aug 11, 2023
3bc3f8e
Merge remote-tracking branch 'upstream/main' into liquidstaking-module
zsystm Aug 11, 2023
19c600f
chore: apply proto-all after merge
zsystm Aug 11, 2023
38acbf4
fix: remove un-used imports and update coinswap sim-op
zsystm Aug 11, 2023
9b2b2ff
fix: disable negative fee rate of insurance (#36)
zsystm Aug 16, 2023
ba1c02f
feat: update antehandler to cover nested authz msg cases
dongsam Aug 21, 2023
24183c9
chore: go fmt
dongsam Aug 21, 2023
8bcc92c
feat: optimize gas usage (#39)
zsystm Aug 22, 2023
8524954
Merge pull request #40 from b-harvest/fix-antehandler-for-nested-authz
dongsam Aug 22, 2023
b788533
add ValCommissionChangeLimitDecorator (#41)
zsystm Aug 23, 2023
250ce58
feat: add v8 upgrade handler and link issue related with go.mod (#42)
zsystm Aug 23, 2023
6e96a5e
fix: un-used variables and add sanity checks (#43)
zsystm Aug 23, 2023
c32d3e8
doc: liquidstaking module spec update (#48)
poorphd Aug 23, 2023
42cd98f
feat: use values from keeper (#49)
zsystm Aug 23, 2023
c117fe0
docs: update abci spec (#52)
zsystm Sep 6, 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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PROJECT := canto
DOCKER_IMAGE := $(NAMESPACE)/$(PROJECT)
COMMIT_HASH := $(shell git rev-parse --short=7 HEAD)
DOCKER_TAG := $(COMMIT_HASH)
REPO=github.com/Canto-Network/Canto/v7

export GO111MODULE = on

Expand Down Expand Up @@ -93,6 +94,9 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=canto \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)" \
-X github.com/tendermint/tendermint/version.TMCoreSemVer=$(TMVERSION)

testing_ldflags = -X github.com/Canto-Network/Canto/v7/app.enableAdvanceEpoch=true \
-X github.com/Canto-Network/Canto/v7/app.epochPerBlock=5

# DB backend selection
ifeq (cleveldb,$(findstring cleveldb,$(COSMOS_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb
Expand Down Expand Up @@ -124,6 +128,8 @@ ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS)))
BUILD_FLAGS += -trimpath
endif

TESTING_BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(testing_ldflags) $(ldflags)' -trimpath

# # The below include contains the tools and runsim targets.
# include contrib/devtools/Makefile

Expand All @@ -140,6 +146,9 @@ build-linux:
$(BUILD_TARGETS): go.sum $(BUILDDIR)/
go $@ $(BUILD_FLAGS) $(BUILD_ARGS) ./...

install-testing: go.sum
go install $(TESTING_BUILD_FLAGS) ./...

$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

Expand Down
10 changes: 5 additions & 5 deletions app/ante/cosmos/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/authz"
)

// maxNestedMsgs defines a cap for the number of nested messages on a MsgExec message
const maxNestedMsgs = 7
// MaxNestedMsgs defines a cap for the number of nested messages on a MsgExec message
const MaxNestedMsgs = 7

// AuthzLimiterDecorator blocks certain msg types from being granted or executed
// within the authorization module.
Expand Down Expand Up @@ -54,10 +54,10 @@ func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
// Otherwise any msg matching the disabled types are blocked, regardless of being in an authz msg or not.
//
// This method is recursive as MsgExec's can wrap other MsgExecs. The check for nested messages is performed up to the
// maxNestedMsgs threshold. If there are more than that limit, it returns an error
// MaxNestedMsgs threshold. If there are more than that limit, it returns an error
func (ald AuthzLimiterDecorator) checkDisabledMsgs(msgs []sdk.Msg, isAuthzInnerMsg bool, nestedLvl int) error {
if nestedLvl >= maxNestedMsgs {
return fmt.Errorf("found more nested msgs than permited. Limit is : %d", maxNestedMsgs)
if nestedLvl >= MaxNestedMsgs {
return fmt.Errorf("found more nested msgs than permited. Limit is : %d", MaxNestedMsgs)
}
for _, msg := range msgs {
switch msg := msg.(type) {
Expand Down
36 changes: 25 additions & 11 deletions app/ante/handler_options.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package ante

import (
liquidstakingkeeper "github.com/Canto-Network/Canto/v7/x/liquidstaking/keeper"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"

Expand All @@ -21,17 +24,20 @@ import (
// HandlerOptions defines the list of module keepers required to run the canto
// AnteHandler decorators.
type HandlerOptions struct {
AccountKeeper evmtypes.AccountKeeper
BankKeeper evmtypes.BankKeeper
IBCKeeper *ibckeeper.Keeper
FeeMarketKeeper evmtypes.FeeMarketKeeper
EvmKeeper ethante.EVMKeeper
FeegrantKeeper ante.FeegrantKeeper
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
Cdc codec.BinaryCodec
MaxTxGasWanted uint64
Simulation bool
AccountKeeper evmtypes.AccountKeeper
BankKeeper evmtypes.BankKeeper
IBCKeeper *ibckeeper.Keeper
FeeMarketKeeper evmtypes.FeeMarketKeeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper *slashingkeeper.Keeper
EvmKeeper ethante.EVMKeeper
FeegrantKeeper ante.FeegrantKeeper
LiquidStakingKeeper *liquidstakingkeeper.Keeper
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
Cdc codec.BinaryCodec
MaxTxGasWanted uint64
Simulation bool
}

// Validate checks if the keepers are defined
Expand All @@ -42,6 +48,9 @@ func (options HandlerOptions) Validate() error {
if options.BankKeeper == nil {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if options.SlashingKeeper == nil {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "slashing keeper is required for AnteHandler")
}
if options.SignModeHandler == nil {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
Expand Down Expand Up @@ -87,6 +96,8 @@ func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
NewValCommissionChangeLimitDecorator(options.LiquidStakingKeeper, options.StakingKeeper, options.Cdc),
NewParamChangeLimitDecorator(options.SlashingKeeper, options.Cdc),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
NewValidatorCommissionDecorator(options.Cdc),
// SetPubKeyDecorator must be called before all signature verification decorators
Expand Down Expand Up @@ -116,6 +127,7 @@ func newCosmosSimulationAnteHandler(options HandlerOptions) sdk.AnteHandler {
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
//NewValCommissionChangeLimitDecorator(options.LiquidStakingKeeper, options.StakingKeeper, options.Cdc),
// NewParamChangeLimitDecorator(options.SlashingKeeper, options.Cdc),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
// NewValidatorCommissionDecorator(options.Cdc),
Expand Down Expand Up @@ -144,6 +156,8 @@ func newCosmosAnteHandlerEip712(options HandlerOptions) sdk.AnteHandler {
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
NewValCommissionChangeLimitDecorator(options.LiquidStakingKeeper, options.StakingKeeper, options.Cdc),
NewParamChangeLimitDecorator(options.SlashingKeeper, options.Cdc),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
NewValidatorCommissionDecorator(options.Cdc),
// SetPubKeyDecorator must be called before all signature verification decorators
Expand Down
152 changes: 152 additions & 0 deletions app/ante/param_change_ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package ante

import (
"fmt"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

authzante "github.com/Canto-Network/Canto/v7/app/ante/cosmos"
"github.com/Canto-Network/Canto/v7/types"
)

// ParamChangeLimitDecorator checks that the params change proposals for slashing and staking modules.
// The liquidstaking module works closely with the slashing and staking module's params(e.g. MinimumCollateral constant is calculated based on the slashing params).
// To reduce unexpected risks, it is important to limit the params change proposals for slashing and staking modules.
type ParamChangeLimitDecorator struct {
slashingKeeper *slashingkeeper.Keeper
cdc codec.BinaryCodec
}

// NewParamChangeLimitDecorator creates a new slashing param change limit decorator.
func NewParamChangeLimitDecorator(
slashingKeeper *slashingkeeper.Keeper,
cdc codec.BinaryCodec,
) ParamChangeLimitDecorator {
return ParamChangeLimitDecorator{
slashingKeeper: slashingKeeper,
cdc: cdc,
}
}

func (pcld ParamChangeLimitDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
msgs := tx.GetMsgs()
if err = pcld.ValidateMsgs(ctx, msgs); err != nil {
return ctx, err
}

return next(ctx, tx, simulate)
}

func (pcld ParamChangeLimitDecorator) ValidateMsgs(ctx sdk.Context, msgs []sdk.Msg) error {
var slashingParams slashingtypes.Params
var validMsg func(m sdk.Msg, nestedCnt int) error
validMsg = func(m sdk.Msg, nestedCnt int) error {
if nestedCnt >= authzante.MaxNestedMsgs {
return fmt.Errorf("found more nested msgs than permited. Limit is : %d", authzante.MaxNestedMsgs)
}
switch msg := m.(type) {
case *authz.MsgExec:
for _, v := range msg.Msgs {
var innerMsg sdk.Msg
if err := pcld.cdc.UnpackAny(v, &innerMsg); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
}
nestedCnt++
if err := validMsg(innerMsg, nestedCnt); err != nil {
return err
}
}

case *govtypes.MsgSubmitProposal:
switch c := msg.GetContent().(type) {
case *proposal.ParameterChangeProposal:
for _, c := range c.Changes {
switch c.GetSubspace() {
case slashingtypes.ModuleName:
slashingParams = pcld.slashingKeeper.GetParams(ctx)
switch c.GetKey() {
// SignedBlocksWindow, MinSignedPerWindow, DowntimeJailDuration are not allowed to be decreased.
// If we decrease these slashingParams, the slashing penalty can be increased.
case string(slashingtypes.KeySignedBlocksWindow):
window, err := strconv.ParseInt(c.GetValue(), 10, 64)
if err != nil {
return err
}
if window < slashingParams.SignedBlocksWindow {
return types.ErrInvalidSignedBlocksWindow
}
case string(slashingtypes.KeyMinSignedPerWindow):
minSignedPerWindow, err := sdk.NewDecFromStr(c.GetValue())
if err != nil {
return err
}
if minSignedPerWindow.LT(slashingParams.MinSignedPerWindow) {
return types.ErrInvalidMinSignedPerWindow
}
case string(slashingtypes.KeyDowntimeJailDuration):
downtimeJailDuration, err := strconv.ParseInt(c.GetValue(), 10, 64)
if err != nil {
return err
}
if time.Duration(downtimeJailDuration) < slashingParams.DowntimeJailDuration {
return types.ErrInvalidDowntimeJailDuration
}
// SlashFractionDoubleSign, SlashFractionDowntime are not allowed to be increased.
// If we increase these slashingParams, the slashing penalty can be increased.
case string(slashingtypes.KeySlashFractionDoubleSign):
slashFractionDoubleSign, err := sdk.NewDecFromStr(c.GetValue())
if err != nil {
return err
}
if slashFractionDoubleSign.GT(slashingParams.SlashFractionDoubleSign) {
return types.ErrInvalidSlashFractionDoubleSign
}
case string(slashingtypes.KeySlashFractionDowntime):
slashFractionDowntime, err := sdk.NewDecFromStr(c.GetValue())
if err != nil {
return err
}
if slashFractionDowntime.GT(slashingParams.SlashFractionDowntime) {
return types.ErrInvalidSlashFractionDowntime
}
}
case stakingtypes.ModuleName:
switch c.GetKey() {
case string(stakingtypes.KeyUnbondingTime):
return types.ErrChangingUnbondingPeriodForbidden
case string(stakingtypes.KeyBondDenom):
return types.ErrChangingBondDenomForbidden
}
}
}
default:
return nil
}
default:
return nil
}
return nil
}

for _, m := range msgs {
if err := validMsg(m, 1); err != nil {
return err
}
}
return nil
}
Loading
Loading