Skip to content

Commit

Permalink
add update chain params
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 9, 2024
1 parent ede755b commit adf2c12
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
25 changes: 25 additions & 0 deletions testutil/sample/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions x/observer/simulation/operation_update_chain_params.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions x/observer/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func WeightedOperations(
weightMsgTypeMsgUpdateKeygen,
SimulateMsgUpdateKeygen(k),
),

simulation.NewWeightedOperation(
weightMsgTypeMsgUpdateChainParams,
SimulateMsgUpdateChainParams(k),
),
}

}
Expand Down

0 comments on commit adf2c12

Please sign in to comment.