From adf2c12179f65866e15afc3c4f3135baa2a1b1c5 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Mon, 9 Dec 2024 18:58:32 -0500 Subject: [PATCH] add update chain params --- testutil/sample/observer.go | 25 ++++++ .../operation_update_chain_params.go | 78 +++++++++++++++++++ x/observer/simulation/operations.go | 5 ++ 3 files changed, 108 insertions(+) create mode 100644 x/observer/simulation/operation_update_chain_params.go diff --git a/testutil/sample/observer.go b/testutil/sample/observer.go index a89e15c9c0..d80314851f 100644 --- a/testutil/sample/observer.go +++ b/testutil/sample/observer.go @@ -113,6 +113,31 @@ func ChainParams(chainID int64) *types.ChainParams { } } +func ChainParamsFromRand(r *rand.Rand, chainID int64) *types.ChainParams { + fiftyPercent, err := sdk.NewDecFromStr("0.5") + if err != nil { + return nil + } + + return &types.ChainParams{ + ChainId: chainID, + ConfirmationCount: r.Uint64(), + + GasPriceTicker: Uint64InRange(1, 300), + InboundTicker: Uint64InRange(1, 300), + OutboundTicker: Uint64InRange(1, 300), + WatchUtxoTicker: Uint64InRange(1, 300), + ZetaTokenContractAddress: EthAddress().String(), + ConnectorContractAddress: EthAddress().String(), + Erc20CustodyContractAddress: EthAddress().String(), + OutboundScheduleInterval: Int64InRange(1, 100), + OutboundScheduleLookahead: Int64InRange(1, 500), + BallotThreshold: fiftyPercent, + MinObserverDelegation: sdk.NewDec(r.Int63()), + IsSupported: true, + } +} + func ChainParamsSupported(chainID int64) *types.ChainParams { cp := ChainParams(chainID) cp.IsSupported = true diff --git a/x/observer/simulation/operation_update_chain_params.go b/x/observer/simulation/operation_update_chain_params.go new file mode 100644 index 0000000000..c921a6b4f3 --- /dev/null +++ b/x/observer/simulation/operation_update_chain_params.go @@ -0,0 +1,78 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/simulation" + "github.com/zeta-chain/node/testutil/sample" + "github.com/zeta-chain/node/x/observer/keeper" + "github.com/zeta-chain/node/x/observer/types" +) + +// SimulateMsgUpdateChainParams generates a MsgUpdateChainParams and delivers it. +func SimulateMsgUpdateChainParams(k keeper.Keeper) simtypes.Operation { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account, _ string, + ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { + policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateChainParams, err.Error()), nil, nil + } + + authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) + spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress()) + + supportedChains := k.GetSupportedChains(ctx) + if len(supportedChains) == 0 { + return simtypes.NoOpMsg( + types.ModuleName, + types.TypeMsgUpdateChainParams, + "no supported chains found", + ), nil, nil + } + + randomChainId := int64(0) + // remove zeta chain from the supported chains + for { + c := supportedChains[r.Intn(len(supportedChains))] + if !c.IsZetaChain() { + randomChainId = c.ChainId + break + } + } + + // pick a random chain from the supported chains + + cp := sample.ChainParamsFromRand(r, randomChainId) + + msg := types.MsgUpdateChainParams{ + Creator: policyAccount.Address.String(), + ChainParams: cp, + } + + err = msg.ValidateBasic() + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, msg.Type(), err.Error()), nil, err + } + + txCtx := simulation.OperationInput{ + R: r, + App: app, + TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig, + Cdc: nil, + Msg: &msg, + MsgType: msg.Type(), + Context: ctx, + SimAccount: policyAccount, + AccountKeeper: k.GetAuthKeeper(), + Bankkeeper: k.GetBankKeeper(), + ModuleName: types.ModuleName, + CoinsSpentInMsg: spendable, + } + + return simulation.GenAndDeliverTxWithRandFees(txCtx) + } +} diff --git a/x/observer/simulation/operations.go b/x/observer/simulation/operations.go index 3ad588736a..4e1d08f3a9 100644 --- a/x/observer/simulation/operations.go +++ b/x/observer/simulation/operations.go @@ -129,6 +129,11 @@ func WeightedOperations( weightMsgTypeMsgUpdateKeygen, SimulateMsgUpdateKeygen(k), ), + + simulation.NewWeightedOperation( + weightMsgTypeMsgUpdateChainParams, + SimulateMsgUpdateChainParams(k), + ), } }