Skip to content

Commit fea4dec

Browse files
authored
Problem: expedited related gov params are not adjusted (#1623)
* Problem: expedited related gov params are not adjusted * test * test expedited * lint
1 parent f10363c commit fea4dec

File tree

12 files changed

+331
-133
lines changed

12 files changed

+331
-133
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [#1611](https://github.com/crypto-org-chain/cronos/pull/1611) Fix multisig account failed on threshold encode after send tx.
2323
* [#1617](https://github.com/crypto-org-chain/cronos/pull/1617) Fix unsuppored sign mode SIGN_MODE_TEXTUAL for bank transfer.
2424
* [#1621](https://github.com/crypto-org-chain/cronos/pull/1621) Update ethermint to the fix of broken opBlockhash and tx validation.
25+
* [#1623](https://github.com/crypto-org-chain/cronos/pull/1623) Ensure expedited related gov params pass the basic validation.
2526

2627
*Sep 13, 2024*
2728

app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ func New(
954954

955955
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
956956
// Make sure it's called after `app.mm` and `app.configurator` are set.
957-
app.RegisterUpgradeHandlers(app.appCodec, app.IBCKeeper.ClientKeeper)
957+
app.RegisterUpgradeHandlers(app.appCodec)
958958

959959
// add test gRPC service for testing gRPC queries in isolation
960960
// testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})

app/upgrades.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ package app
33
import (
44
"context"
55
"fmt"
6+
"time"
67

8+
sdkmath "cosmossdk.io/math"
79
storetypes "cosmossdk.io/store/types"
810
upgradetypes "cosmossdk.io/x/upgrade/types"
911
"github.com/cosmos/cosmos-sdk/codec"
1012
sdk "github.com/cosmos/cosmos-sdk/types"
1113
"github.com/cosmos/cosmos-sdk/types/module"
14+
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
15+
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
1216
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
13-
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
1417
evmtypes "github.com/evmos/ethermint/x/evm/types"
1518
)
1619

17-
func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clientkeeper.Keeper) {
20+
func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec) {
1821
planName := "v1.4"
1922
app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
2023
m, err := app.ModuleManager.RunMigrations(ctx, app.configurator, fromVM)
@@ -32,6 +35,9 @@ func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clie
3235
if err := app.EvmKeeper.SetParams(sdkCtx, evmParams); err != nil {
3336
return m, err
3437
}
38+
if err := UpdateExpeditedParams(ctx, app.GovKeeper); err != nil {
39+
return m, err
40+
}
3541
}
3642
return m, nil
3743
})
@@ -51,3 +57,55 @@ func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clie
5157
}
5258
}
5359
}
60+
61+
func UpdateExpeditedParams(ctx context.Context, gov govkeeper.Keeper) error {
62+
govParams, err := gov.Params.Get(ctx)
63+
if err != nil {
64+
return err
65+
}
66+
if len(govParams.MinDeposit) > 0 {
67+
minDeposit := govParams.MinDeposit[0]
68+
expeditedAmount := minDeposit.Amount.MulRaw(govv1.DefaultMinExpeditedDepositTokensRatio)
69+
govParams.ExpeditedMinDeposit = sdk.NewCoins(sdk.NewCoin(minDeposit.Denom, expeditedAmount))
70+
}
71+
threshold, err := sdkmath.LegacyNewDecFromStr(govParams.Threshold)
72+
if err != nil {
73+
return fmt.Errorf("invalid threshold string: %w", err)
74+
}
75+
expeditedThreshold, err := sdkmath.LegacyNewDecFromStr(govParams.ExpeditedThreshold)
76+
if err != nil {
77+
return fmt.Errorf("invalid expedited threshold string: %w", err)
78+
}
79+
if expeditedThreshold.LTE(threshold) {
80+
expeditedThreshold = threshold.Mul(DefaultThresholdRatio())
81+
}
82+
if expeditedThreshold.GT(sdkmath.LegacyOneDec()) {
83+
expeditedThreshold = sdkmath.LegacyOneDec()
84+
}
85+
govParams.ExpeditedThreshold = expeditedThreshold.String()
86+
if govParams.ExpeditedVotingPeriod != nil && govParams.VotingPeriod != nil && *govParams.ExpeditedVotingPeriod >= *govParams.VotingPeriod {
87+
votingPeriod := DurationToDec(*govParams.VotingPeriod)
88+
period := DecToDuration(DefaultPeriodRatio().Mul(votingPeriod))
89+
govParams.ExpeditedVotingPeriod = &period
90+
}
91+
if err := govParams.ValidateBasic(); err != nil {
92+
return err
93+
}
94+
return gov.Params.Set(ctx, govParams)
95+
}
96+
97+
func DefaultThresholdRatio() sdkmath.LegacyDec {
98+
return govv1.DefaultExpeditedThreshold.Quo(govv1.DefaultThreshold)
99+
}
100+
101+
func DefaultPeriodRatio() sdkmath.LegacyDec {
102+
return DurationToDec(govv1.DefaultExpeditedPeriod).Quo(DurationToDec(govv1.DefaultPeriod))
103+
}
104+
105+
func DurationToDec(d time.Duration) sdkmath.LegacyDec {
106+
return sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", d.Seconds()))
107+
}
108+
109+
func DecToDuration(d sdkmath.LegacyDec) time.Duration {
110+
return time.Second * time.Duration(d.RoundInt64())
111+
}

app/upgrades_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package app_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"cosmossdk.io/math"
8+
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
9+
sdk "github.com/cosmos/cosmos-sdk/types"
10+
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
11+
"github.com/crypto-org-chain/cronos/v2/app"
12+
"github.com/evmos/ethermint/crypto/ethsecp256k1"
13+
"github.com/stretchr/testify/suite"
14+
)
15+
16+
type AppTestSuite struct {
17+
suite.Suite
18+
19+
ctx sdk.Context
20+
app *app.App
21+
govParams govv1.Params
22+
}
23+
24+
func TestAppTestSuite(t *testing.T) {
25+
suite.Run(t, new(AppTestSuite))
26+
}
27+
28+
func (suite *AppTestSuite) SetupTest() {
29+
checkTx := false
30+
privKey, err := ethsecp256k1.GenerateKey()
31+
suite.Require().NoError(err)
32+
suite.app = app.Setup(suite.T(), sdk.AccAddress(privKey.PubKey().Address()).String())
33+
suite.ctx = suite.app.NewContext(checkTx).WithBlockHeader(tmproto.Header{Height: 1, ChainID: app.TestAppChainID, Time: time.Now().UTC()})
34+
suite.govParams, err = suite.app.GovKeeper.Params.Get(suite.ctx)
35+
suite.Require().NoError(err)
36+
suite.Require().Equal(govv1.DefaultParams(), suite.govParams)
37+
}
38+
39+
func (suite *AppTestSuite) TestUpdateExpeditedParams() {
40+
const baseDenom = "basetcro"
41+
42+
testCases := []struct {
43+
name string
44+
malleate func()
45+
exp func(params govv1.Params)
46+
}{
47+
{
48+
name: "update ExpeditedMinDeposit with baseDenom",
49+
malleate: func() {
50+
suite.govParams.MinDeposit = sdk.NewCoins(sdk.NewCoin(baseDenom, math.NewInt(2000000000000)))
51+
},
52+
exp: func(params govv1.Params) {
53+
expected := sdk.NewCoins(sdk.NewCoin(suite.govParams.MinDeposit[0].Denom, suite.govParams.MinDeposit[0].Amount.MulRaw(govv1.DefaultMinExpeditedDepositTokensRatio)))
54+
suite.Require().Equal(expected[0], params.ExpeditedMinDeposit[0])
55+
},
56+
},
57+
{
58+
name: "update ExpeditedThreshold when DefaultExpeditedThreshold >= Threshold",
59+
malleate: func() {
60+
suite.govParams.Threshold = "0.99"
61+
},
62+
exp: func(params govv1.Params) {
63+
suite.Require().Equal(math.LegacyOneDec().String(), params.ExpeditedThreshold)
64+
},
65+
},
66+
{
67+
name: "update ExpeditedThreshold when DefaultExpeditedThreshold >= Threshold",
68+
malleate: func() {
69+
suite.govParams.Threshold = govv1.DefaultExpeditedThreshold.String()
70+
},
71+
exp: func(params govv1.Params) {
72+
expected := app.DefaultThresholdRatio().Mul(math.LegacyMustNewDecFromStr(suite.govParams.Threshold))
73+
suite.Require().Equal(expected.String(), params.ExpeditedThreshold)
74+
},
75+
},
76+
{
77+
name: "no update ExpeditedThreshold when DefaultExpeditedThreshold < Threshold",
78+
malleate: func() {
79+
suite.govParams.Threshold = govv1.DefaultExpeditedThreshold.Quo(math.LegacyMustNewDecFromStr("1.1")).String()
80+
},
81+
exp: func(params govv1.Params) {
82+
suite.Require().Equal(suite.govParams.ExpeditedThreshold, params.ExpeditedThreshold)
83+
},
84+
},
85+
{
86+
name: "update ExpeditedVotingPeriod when DefaultExpeditedPeriod >= VotingPeriod",
87+
malleate: func() {
88+
period := govv1.DefaultExpeditedPeriod
89+
suite.govParams.VotingPeriod = &period
90+
},
91+
exp: func(params govv1.Params) {
92+
votingPeriod := app.DurationToDec(*suite.govParams.VotingPeriod)
93+
expected := app.DecToDuration(app.DefaultPeriodRatio().Mul(votingPeriod))
94+
suite.Require().Equal(expected, *params.ExpeditedVotingPeriod)
95+
},
96+
},
97+
{
98+
name: "no update ExpeditedVotingPeriod when DefaultExpeditedPeriod < VotingPeriod",
99+
malleate: func() {
100+
period := govv1.DefaultExpeditedPeriod + 1
101+
suite.govParams.VotingPeriod = &period
102+
},
103+
exp: func(params govv1.Params) {
104+
suite.Require().Equal(*suite.govParams.ExpeditedVotingPeriod, *params.ExpeditedVotingPeriod)
105+
},
106+
},
107+
}
108+
109+
for _, tc := range testCases {
110+
suite.Run(tc.name, func() {
111+
suite.SetupTest()
112+
tc.malleate()
113+
suite.Require().NoError(suite.app.GovKeeper.Params.Set(suite.ctx, suite.govParams))
114+
suite.Require().NoError(app.UpdateExpeditedParams(suite.ctx, suite.app.GovKeeper))
115+
params, err := suite.app.GovKeeper.Params.Get(suite.ctx)
116+
suite.Require().NoError(err)
117+
tc.exp(params)
118+
})
119+
}
120+
}

integration_tests/cosmoscli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ def query_gravity_params(self):
11101110
return self.query_params("gravity")
11111111

11121112
def query_params(self, module="cronos", **kwargs):
1113-
return json.loads(
1113+
res = json.loads(
11141114
self.raw(
11151115
"query",
11161116
module,
@@ -1119,6 +1119,8 @@ def query_params(self, module="cronos", **kwargs):
11191119
**kwargs,
11201120
)
11211121
)
1122+
res = res.get("params") or res
1123+
return res
11221124

11231125
def query_signer_set_txs(self):
11241126
return json.loads(

integration_tests/gravity_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def prepare_gravity(custom_cronos, custom_geth):
116116
send_transaction(w3, {"to": admin.address, "value": 10**17}, KEYS["validator"])
117117

118118
# deploy gravity contract to geth
119-
gravity_id = cli.query_gravity_params()["params"]["gravity_id"]
119+
gravity_id = cli.query_gravity_params()["gravity_id"]
120120
signer_set = cli.query_latest_signer_set_tx()["signer_set"]["signers"]
121121
powers = [int(signer["power"]) for signer in signer_set]
122122
threshold = int(2**32 * 0.66) # gravity normalize the power to [0, 2**32]

integration_tests/test_basic.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import hashlib
21
import json
32
import subprocess
43
import time
@@ -12,28 +11,29 @@
1211
from hexbytes import HexBytes
1312
from pystarport import cluster, ports
1413

15-
from .cosmoscli import CosmosCLI
14+
from .cosmoscli import CosmosCLI, module_address
1615
from .network import Geth
1716
from .utils import (
1817
ADDRS,
1918
CONTRACTS,
2019
KEYS,
2120
Greeter,
2221
RevertTestContract,
23-
approve_proposal,
22+
assert_gov_params,
2423
build_batch_tx,
2524
contract_address,
2625
contract_path,
2726
deploy_contract,
2827
derive_new_account,
29-
eth_to_bech32,
28+
get_expedited_params,
3029
get_receipts_by_block,
3130
get_sync_info,
3231
modify_command_in_supervisor_config,
3332
send_transaction,
3433
send_txs,
3534
sign_transaction,
3635
submit_any_proposal,
36+
submit_gov_proposal,
3737
w3_wait_for_block,
3838
wait_for_block,
3939
wait_for_new_blocks,
@@ -43,31 +43,43 @@
4343

4444
def test_ica_enabled(cronos, tmp_path):
4545
cli = cronos.cosmos_cli()
46+
param0 = cli.query_params("gov")
47+
param1 = get_expedited_params(param0)
48+
# governance module account as signer
49+
authority = module_address("gov")
50+
submit_gov_proposal(
51+
cronos,
52+
tmp_path,
53+
messages=[
54+
{
55+
"@type": "/cosmos.gov.v1.MsgUpdateParams",
56+
"authority": authority,
57+
"params": {
58+
**param0,
59+
**param1,
60+
},
61+
}
62+
],
63+
)
64+
assert_gov_params(cli, param0)
65+
4666
p = cli.query_ica_params()
4767
assert p["controller_enabled"]
4868
p["controller_enabled"] = False
49-
proposal = tmp_path / "proposal.json"
50-
# governance module account as signer
51-
data = hashlib.sha256("gov".encode()).digest()[:20]
52-
signer = eth_to_bech32(data)
5369
type = "/ibc.applications.interchain_accounts.controller.v1.MsgUpdateParams"
54-
proposal_src = {
55-
"messages": [
70+
submit_gov_proposal(
71+
cronos,
72+
tmp_path,
73+
messages=[
5674
{
5775
"@type": type,
58-
"signer": signer,
76+
"signer": authority,
5977
"params": p,
6078
}
6179
],
62-
"deposit": "1basetcro",
63-
"title": "title",
64-
"summary": "summary",
65-
}
66-
proposal.write_text(json.dumps(proposal_src))
67-
rsp = cli.submit_gov_proposal(proposal, from_="community")
68-
assert rsp["code"] == 0, rsp["raw_log"]
69-
approve_proposal(cronos, rsp["events"])
70-
print("check params have been updated now")
80+
deposit="5basetcro",
81+
expedited=True,
82+
)
7183
p = cli.query_ica_params()
7284
assert not p["controller_enabled"]
7385

@@ -912,30 +924,22 @@ def test_submit_send_enabled(cronos, tmp_path):
912924
cli = cronos.cosmos_cli()
913925
denoms = ["basetcro", "stake"]
914926
assert len(cli.query_bank_send(*denoms)) == 0, "should be empty"
915-
proposal = tmp_path / "proposal.json"
916-
# governance module account as signer
917-
signer = "crc10d07y265gmmuvt4z0w9aw880jnsr700jdufnyd"
918927
send_enable = [
919928
{"denom": "basetcro"},
920929
{"denom": "stake", "enabled": True},
921930
]
922-
proposal_src = {
923-
"messages": [
931+
authority = module_address("gov")
932+
submit_gov_proposal(
933+
cronos,
934+
tmp_path,
935+
messages=[
924936
{
925937
"@type": "/cosmos.bank.v1beta1.MsgSetSendEnabled",
926-
"authority": signer,
938+
"authority": authority,
927939
"sendEnabled": send_enable,
928940
}
929941
],
930-
"deposit": "1basetcro",
931-
"title": "title",
932-
"summary": "summary",
933-
}
934-
proposal.write_text(json.dumps(proposal_src))
935-
rsp = cli.submit_gov_proposal(proposal, from_="community")
936-
assert rsp["code"] == 0, rsp["raw_log"]
937-
approve_proposal(cronos, rsp["events"])
938-
print("check params have been updated now")
942+
)
939943
assert cli.query_bank_send(*denoms) == send_enable
940944

941945

0 commit comments

Comments
 (0)