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

!chore: write upgrade handler for new minimum commission rate #152

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
29255ac
change dependency
GNaD13 Apr 28, 2024
21be234
write upgrade for new minimum commission rate
neitdung May 6, 2024
71cebe2
fix lint error
neitdung May 6, 2024
238f11a
add upgrade test and fix error when change commission rate
neitdung May 6, 2024
1ce2a27
fix lint
neitdung May 6, 2024
d395870
Fix permission file
neitdung May 6, 2024
23d6528
fix spacing issue
neitdung May 6, 2024
8342e72
specific validator commission in test
neitdung May 6, 2024
01c3675
proto third party
GNaD13 May 7, 2024
ba9af85
go mod
GNaD13 May 7, 2024
16ada9b
remove ethermint dependency
GNaD13 May 7, 2024
e36315b
fork sdk
GNaD13 May 8, 2024
d051a97
app.go
GNaD13 May 8, 2024
4d7af03
fix sdk 47 tests and testutil folder
neitdung May 8, 2024
52d87f7
fix sdk 47 cmd folder
neitdung May 8, 2024
684330b
ante.go
GNaD13 May 8, 2024
691580a
ante
GNaD13 May 8, 2024
d9a0a01
Merge pull request #5 from decentrio/dzung/sdk-47
GNaD13 May 8, 2024
dcff4e8
update sdk 47 x folder
neitdung May 8, 2024
b63bb8b
upgrade
GNaD13 May 8, 2024
bbea83d
test_helper
GNaD13 May 8, 2024
5c683c5
Merge pull request #6 from decentrio/dzung/sdk-47
GNaD13 May 8, 2024
b4e21b6
proto
GNaD13 May 8, 2024
f74fb2a
remove third party
GNaD13 May 8, 2024
51be06a
buf
GNaD13 May 8, 2024
07d3d21
nit
GNaD13 May 8, 2024
c8ca223
nit
GNaD13 May 8, 2024
8f4bccc
nit
GNaD13 May 8, 2024
84c645b
CI
GNaD13 May 8, 2024
4f0e4ae
nit
GNaD13 May 8, 2024
03659ae
chain ID
GNaD13 May 8, 2024
aa698ec
fork evmos
GNaD13 May 8, 2024
4369914
correct check
GNaD13 May 9, 2024
14924e6
ante test
GNaD13 May 9, 2024
6dddc07
TestMintedCoinsEachBlock
GNaD13 May 9, 2024
e9146bb
lint
GNaD13 May 9, 2024
c1b41dd
bump go lint versino
GNaD13 May 9, 2024
0f8690b
lint
GNaD13 May 9, 2024
881c30d
lint
GNaD13 May 9, 2024
a8610e1
migrate param space for mint module
neitdung May 9, 2024
d304de4
Merge pull request #7 from decentrio/param-space
GNaD13 May 9, 2024
5ee6bed
lint
GNaD13 May 9, 2024
ea0edcb
Merge branch 'feat/sdk-47' of https://github.com/decentrio/realio-net…
neitdung May 13, 2024
db86787
fix for sdk 47
neitdung May 13, 2024
c903fbc
add RegisterGRPCGatewayRoutes
GNaD13 May 13, 2024
1294ccd
fix lint error
neitdung May 13, 2024
e2e23ae
Merge branch 'feat/sdk-47' of https://github.com/decentrio/realio-net…
neitdung May 14, 2024
d91ca00
fix lint error
neitdung May 14, 2024
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 app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (suite *AnteTestSuite) SetupTest() {
consAddress := sdk.ConsAddress(privCons.PubKey().Address())

isCheckTx := false
suite.app = app.Setup(isCheckTx, feemarkettypes.DefaultGenesisState())
suite.app = app.Setup(isCheckTx, feemarkettypes.DefaultGenesisState(), 1)
suite.Require().NotNil(suite.app.AppCodec())

suite.ctx = suite.app.BaseApp.NewContext(isCheckTx, tmproto.Header{
Expand Down
2 changes: 1 addition & 1 deletion app/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestFork(t *testing.T) {
realio := Setup(false, nil)
realio := Setup(false, nil, 1)

ctx := realio.BaseApp.NewContext(false, tmproto.Header{Height: ForkHeight})
stakingKeeper := realio.StakingKeeper
Expand Down
20 changes: 15 additions & 5 deletions app/test_helpers.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,11 @@ func init() {
func Setup(
isCheckTx bool,
feemarketGenesis *feemarkettypes.GenesisState,
numberVals int,
) *RealioNetwork {
privVal := mock.NewPV()
pubKey, _ := privVal.GetPubKey()
encCdc := MakeEncodingConfig()

// create validator set with single validator
validator := tmtypes.NewValidator(pubKey, 1)
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
valSet := GenValSet(numberVals)

// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
Expand Down Expand Up @@ -242,3 +239,16 @@ func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, cfg, simapp.EmptyAppOptions{})
return app, simapp.NewDefaultGenesisState(cfg.Codec)
}

func GenValSet(nums int) *tmtypes.ValidatorSet {
vals := []*tmtypes.Validator{}

for i := 0; i < nums; i++ {
privVal := mock.NewPV()
pubKey, _ := privVal.GetPubKey()
vals = append(vals, tmtypes.NewValidator(pubKey, 1))
}
valSet := tmtypes.NewValidatorSet(vals)

return valSet
}
10 changes: 10 additions & 0 deletions app/upgrades.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
multistaking "github.com/realiotech/realio-network/app/upgrades/multi-staking"
v4 "github.com/realiotech/realio-network/app/upgrades/v4"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
Expand All @@ -24,6 +25,15 @@ func (app *RealioNetwork) setupUpgradeHandlers(appOpts servertypes.AppOptions) {
),
)

app.UpgradeKeeper.SetUpgradeHandler(
v4.UpgradeName,
v4.CreateUpgradeHandler(
app.mm,
app.configurator,
app.StakingKeeper,
),
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Errorf("failed to read upgrade info from disk: %w", err))
Expand Down
7 changes: 7 additions & 0 deletions app/upgrades/v4/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v4

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v4-Commission"
catShaark marked this conversation as resolved.
Show resolved Hide resolved
NewMinCommisionRate = "0.05"
)
68 changes: 68 additions & 0 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package v4

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
sk stakingkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Starting upgrade for multi staking...")
fixMinCommisionRate(ctx, sk)
return mm.RunMigrations(ctx, configurator, vm)
}
}

func fixMinCommisionRate(ctx sdk.Context, staking stakingkeeper.Keeper) {
// Upgrade every validators min-commission rate
validators := staking.GetAllValidators(ctx)
newComm := sdk.MustNewDecFromStr(NewMinCommisionRate)
params := staking.GetParams(ctx)
params.MinCommissionRate = newComm
catShaark marked this conversation as resolved.
Show resolved Hide resolved
staking.SetParams(ctx, params)
for _, v := range validators {
//nolint
if v.Commission.Rate.LT(newComm) {
// We need to remove
comm, err := updateValidatorCommission(ctx, staking, v, newComm)
if err != nil {
panic(err)
}

v.Commission = comm

// call the before-modification hook since we're about to update the commission
staking.BeforeValidatorModified(ctx, v.GetOperator())
staking.SetValidator(ctx, v)
catShaark marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func updateValidatorCommission(ctx sdk.Context, staking stakingkeeper.Keeper,
validator stakingtypes.Validator, newRate sdk.Dec,
) (stakingtypes.Commission, error) {
commission := validator.Commission
blockTime := ctx.BlockHeader().Time

if newRate.LT(staking.MinCommissionRate(ctx)) {
return commission, fmt.Errorf("cannot set validator commission to less than minimum rate of %s", staking.MinCommissionRate(ctx))
}

commission.Rate = newRate
if commission.MaxRate.LT(newRate) {
commission.MaxRate = newRate
}

commission.UpdateTime = blockTime

return commission, nil
}
36 changes: 36 additions & 0 deletions app/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app

import (
"testing"
"time"

"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

v4 "github.com/realiotech/realio-network/app/upgrades/v4"
)

func TestV4Upgrade(t *testing.T) {
app := Setup(false, nil, 4)
ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: app.LastBlockHeight() + 1})

upgradePlan := upgradetypes.Plan{
Name: v4.UpgradeName,
Height: ctx.BlockHeight(),
}

err := app.UpgradeKeeper.ScheduleUpgrade(ctx, upgradePlan)
require.NoError(t, err)
ctx = ctx.WithBlockHeader(tmproto.Header{Time: time.Now()})
app.UpgradeKeeper.ApplyUpgrade(ctx, upgradePlan)

validators := app.StakingKeeper.GetAllValidators(ctx)

upgradeMinCommRate := sdk.MustNewDecFromStr(v4.NewMinCommisionRate)
for _, val := range validators {
require.Equal(t, val.Commission.CommissionRates.Rate, upgradeMinCommRate)
}
}
2 changes: 1 addition & 1 deletion x/asset/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *GenesisTestSuite) SetupTest() {
// consensus key
consAddress := sdk.ConsAddress(testutil.GenAddress().Bytes())

suite.app = app.Setup(false, feemarkettypes.DefaultGenesisState())
suite.app = app.Setup(false, feemarkettypes.DefaultGenesisState(), 1)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{
Height: 1,
ChainID: realiotypes.MainnetChainID,
Expand Down
2 changes: 1 addition & 1 deletion x/asset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t *testing.T) {
consAddress := sdk.ConsAddress(priv.PubKey().Address())

// init app
suite.app = app.Setup(checkTx, nil)
suite.app = app.Setup(checkTx, nil, 1)

// Set Context
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Expand Down
2 changes: 1 addition & 1 deletion x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *KeeperTestSuite) DoSetupTest(t *testing.T) {
consAddress := sdk.ConsAddress(priv.PubKey().Address())

// init app
suite.app = app.Setup(checkTx, nil)
suite.app = app.Setup(checkTx, nil, 1)

// Set Context
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Expand Down
2 changes: 1 addition & 1 deletion x/mint/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
realio := app.Setup(false, nil)
realio := app.Setup(false, nil, 1)
ctx := realio.BaseApp.NewContext(false, tmproto.Header{})
acc := realio.AccountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
require.NotNil(t, acc)
Expand Down
Loading