diff --git a/testutil/sample/crosschain.go b/testutil/sample/crosschain.go index 293dbff61f..db0b943964 100644 --- a/testutil/sample/crosschain.go +++ b/testutil/sample/crosschain.go @@ -149,7 +149,10 @@ func GasPriceWithChainID(t *testing.T, chainID int64) types.GasPrice { } func GasPriceFromRand(r *rand.Rand, chainID int64) *types.GasPrice { - price := r.Uint64() + var price uint64 + for price == 0 { + price = r.Uint64() + } priorityFee := r.Uint64() % price return &types.GasPrice{ Creator: "", @@ -367,7 +370,7 @@ func InboundVoteFromRand(from, to int64, r *rand.Rand, asset string) types.MsgVo Receiver: EthAddressFromRand(r).String(), ReceiverChain: to, Amount: math.NewUint(r.Uint64()), - Message: base64.StdEncoding.EncodeToString(RandomBytes(r)), + Message: "95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5", InboundBlockHeight: r.Uint64(), CallOptions: &types.CallOptions{ GasLimit: 1000000000, diff --git a/testutil/sample/crypto.go b/testutil/sample/crypto.go index 4f62878d0f..b67698bca0 100644 --- a/testutil/sample/crypto.go +++ b/testutil/sample/crypto.go @@ -33,30 +33,13 @@ func PubKeySet() *crypto.PubKeySet { return &pubKeySet } -func Secp256PrivateKeyFromRand(r *rand.Rand) secp256k1.PrivKey { +func Ed25519PrivateKeyFromRand(r *rand.Rand) (*ed25519.PrivKey, error) { randomBytes := make([]byte, 32) _, err := r.Read(randomBytes) if err != nil { - panic(err) - } - return secp256k1.GenPrivKeySecp256k1(randomBytes) -} - -func Ed25519PrivateKeyFromRand(r *rand.Rand) *ed25519.PrivKey { - randomBytes := make([]byte, 32) - _, err := r.Read(randomBytes) - if err != nil { - panic(err) + return nil, err } - return ed25519.GenPrivKeyFromSecret(randomBytes) -} - -func PubKeySetFromRand(r *rand.Rand) *crypto.PubKeySet { - pubKeySet := crypto.PubKeySet{ - Secp256k1: crypto.PubKey(Secp256PrivateKeyFromRand(r).PubKey().Bytes()), - Ed25519: crypto.PubKey(Ed25519PrivateKeyFromRand(r).PubKey().String()), - } - return &pubKeySet + return ed25519.GenPrivKeyFromSecret(randomBytes), nil } // PubKeyString returns a sample public key string @@ -73,17 +56,17 @@ func PubKeyString() string { return pubkey.String() } -func PubkeyStringFromRand(r *rand.Rand) string { - priKey := Ed25519PrivateKeyFromRand(r) +func PubkeyStringFromRand(r *rand.Rand) (string, error) { + priKey, err := Ed25519PrivateKeyFromRand(r) s, err := cosmos.Bech32ifyPubKey(cosmos.Bech32PubKeyTypeAccPub, priKey.PubKey()) if err != nil { - panic(err) + return "", err } pubkey, err := crypto.NewPubKey(s) if err != nil { - panic(err) + return "", err } - return pubkey.String() + return pubkey.String(), nil } // PrivKeyAddressPair returns a private key, address pair diff --git a/testutil/sample/observer.go b/testutil/sample/observer.go index 794e5ea4de..20136593b0 100644 --- a/testutil/sample/observer.go +++ b/testutil/sample/observer.go @@ -116,11 +116,7 @@ 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 - } - + fiftyPercent := sdk.MustNewDecFromStr("0.5") return &types.ChainParams{ ChainId: chainID, ConfirmationCount: r.Uint64(), diff --git a/x/crosschain/simulation/operation_abort_stuck_cctx.go b/x/crosschain/simulation/operation_abort_stuck_cctx.go index 198ae1d523..f828b8f5d7 100644 --- a/x/crosschain/simulation/operation_abort_stuck_cctx.go +++ b/x/crosschain/simulation/operation_abort_stuck_cctx.go @@ -1,7 +1,6 @@ package simulation import ( - "fmt" "math/rand" "github.com/cosmos/cosmos-sdk/baseapp" @@ -47,7 +46,11 @@ func SimulateMsgAbortStuckCCTX(k keeper.Keeper) simtypes.Operation { tss, found := k.GetObserverKeeper().GetTSS(ctx) if !found { - return simtypes.OperationMsg{}, nil, fmt.Errorf("tss not found") + return simtypes.NoOpMsg( + types.ModuleName, + types.TypeMsgAbortStuckCCTX, + "no TSS found", + ), nil, nil } pendingNonces, found := k.GetObserverKeeper().GetPendingNonces(ctx, tss.TssPubkey, chainID) @@ -63,7 +66,7 @@ func SimulateMsgAbortStuckCCTX(k keeper.Keeper) simtypes.Operation { if pendingNonces.NonceLow == pendingNonces.NonceHigh { return simtypes.NoOpMsg( types.ModuleName, - types.TypeMsgAddOutboundTracker, + types.TypeMsgAbortStuckCCTX, "no pending nonces found", ), nil, nil } diff --git a/x/crosschain/simulation/operation_gas_price_voter.go b/x/crosschain/simulation/operation_gas_price_voter.go index 299ac9d03e..5f0d4c7690 100644 --- a/x/crosschain/simulation/operation_gas_price_voter.go +++ b/x/crosschain/simulation/operation_gas_price_voter.go @@ -1,7 +1,6 @@ package simulation import ( - "fmt" "math/rand" "github.com/cosmos/cosmos-sdk/baseapp" @@ -39,8 +38,12 @@ func SimulateMsgVoteGasPrice(k keeper.Keeper) simtypes.Operation { randomChainID := GetRandomChainID(r, supportedChains) // Vote for random gas price. Gas prices do not use a ballot system, so we can vote directly without having to schedule future operations. - // The random nature of the price might create weird gas prices for the chain, but it is fine for now. We can remove the randomness if needed - price := r.Uint64() + + var price uint64 + for price == 0 { + maxGasPrice := uint64(1000 * 1e9) // 1000 Gwei + price = uint64(1e9) + r.Uint64()%maxGasPrice + } // Select priority fee between 0 and price priorityFee := r.Uint64() % price msg := types.MsgVoteGasPrice{ @@ -48,8 +51,8 @@ func SimulateMsgVoteGasPrice(k keeper.Keeper) simtypes.Operation { ChainId: randomChainID, Price: price, PriorityFee: priorityFee, - BlockNumber: r.Uint64(), - Supply: fmt.Sprintf("%d", r.Int63()), + BlockNumber: uint64(ctx.BlockHeight()) + r.Uint64()%1000, + Supply: sdk.NewInt(r.Int63n(1e18)).String(), } // System contracts are deployed on the first block, so we cannot vote on gas prices before that diff --git a/x/crosschain/simulation/operation_migrate_erc20_custody_funds.go b/x/crosschain/simulation/operation_migrate_erc20_custody_funds.go index 81ae8f11d0..ff486b27e7 100644 --- a/x/crosschain/simulation/operation_migrate_erc20_custody_funds.go +++ b/x/crosschain/simulation/operation_migrate_erc20_custody_funds.go @@ -21,7 +21,7 @@ func SimulateMigrateERC20CustodyFunds(k keeper.Keeper) simtypes.Operation { ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWhitelistERC20, err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgMigrateERC20CustodyFunds, err.Error()), nil, nil } authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) @@ -99,7 +99,7 @@ func SimulateMigrateERC20CustodyFunds(k keeper.Keeper) simtypes.Operation { return simtypes.NoOpMsg( types.ModuleName, msg.Type(), - "unable to validate MsgRemoveOutboundTracker msg", + "unable to validate MsgMigrateERC20CustodyFunds", ), nil, err } diff --git a/x/crosschain/simulation/operation_remove_outbound_tracker.go b/x/crosschain/simulation/operation_remove_outbound_tracker.go index 6bfe453a37..6eaa52be81 100644 --- a/x/crosschain/simulation/operation_remove_outbound_tracker.go +++ b/x/crosschain/simulation/operation_remove_outbound_tracker.go @@ -48,7 +48,7 @@ func SimulateMsgRemoveOutboundTracker(k keeper.Keeper) simtypes.Operation { return simtypes.NoOpMsg( types.ModuleName, msg.Type(), - "unable to validate MsgRemoveOutboundTracker msg", + "unable to validate MsgRemoveOutboundTracker", ), nil, err } diff --git a/x/crosschain/simulation/operation_update_erc20_pause_status.go b/x/crosschain/simulation/operation_update_erc20_pause_status.go index 0091a49f01..a4fa5b30c0 100644 --- a/x/crosschain/simulation/operation_update_erc20_pause_status.go +++ b/x/crosschain/simulation/operation_update_erc20_pause_status.go @@ -20,7 +20,7 @@ func SimulateUpdateERC20CustodyPauseStatus(k keeper.Keeper) simtypes.Operation { ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWhitelistERC20, err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, types.TypeUpdateERC20CustodyPauseStatus, err.Error()), nil, nil } authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) @@ -38,7 +38,7 @@ func SimulateUpdateERC20CustodyPauseStatus(k keeper.Keeper) simtypes.Operation { filteredChains := chains.FilterChains(supportedChains, chains.FilterExternalChains) //pick a random chain - randomChain := supportedChains[r.Intn(len(filteredChains))] + randomChain := filteredChains[r.Intn(len(filteredChains))] _, found := k.GetObserverKeeper().GetChainNonces(ctx, randomChain.ChainId) if !found { @@ -96,7 +96,7 @@ func SimulateUpdateERC20CustodyPauseStatus(k keeper.Keeper) simtypes.Operation { return simtypes.NoOpMsg( types.ModuleName, msg.Type(), - "unable to validate MsgRemoveOutboundTracker msg", + "unable to validate MsgUpdateERC20CustodyPauseStatus msg", ), nil, err } diff --git a/x/crosschain/simulation/operation_update_tss_address.go b/x/crosschain/simulation/operation_update_tss_address.go index 6cec61ae77..aac70b512b 100644 --- a/x/crosschain/simulation/operation_update_tss_address.go +++ b/x/crosschain/simulation/operation_update_tss_address.go @@ -21,7 +21,7 @@ func SimulateMsgUpdateTssAddress(k keeper.Keeper) simtypes.Operation { ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWhitelistERC20, err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateTssAddress, err.Error()), nil, nil } authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) @@ -105,7 +105,7 @@ func SimulateMsgUpdateTssAddress(k keeper.Keeper) simtypes.Operation { return simtypes.NoOpMsg( types.ModuleName, msg.Type(), - "unable to validate MsgRemoveOutboundTracker msg", + "unable to validate MsgUpdateTssAddress msg", ), nil, err } diff --git a/x/crosschain/simulation/operation_vote_outbound.go b/x/crosschain/simulation/operation_vote_outbound.go index 54cae05a58..51f7113c2c 100644 --- a/x/crosschain/simulation/operation_vote_outbound.go +++ b/x/crosschain/simulation/operation_vote_outbound.go @@ -90,7 +90,7 @@ func SimulateVoteOutbound(k keeper.Keeper) simtypes.Operation { tss, found := k.GetObserverKeeper().GetTSS(ctx) if !found { - return simtypes.OperationMsg{}, nil, fmt.Errorf("tss not found") + return simtypes.NoOpMsg(types.ModuleName, authz.OutboundVoter.String(), "tss not found"), nil, nil } asset, err := GetAsset(ctx, k.GetFungibleKeeper(), from) diff --git a/x/crosschain/simulation/operation_whitelist_erc20.go b/x/crosschain/simulation/operation_whitelist_erc20.go index bbc654f740..b67cad6a44 100644 --- a/x/crosschain/simulation/operation_whitelist_erc20.go +++ b/x/crosschain/simulation/operation_whitelist_erc20.go @@ -37,6 +37,13 @@ func SimulateMsgWhitelistERC20(k keeper.Keeper) simtypes.Operation { } filteredChains := chains.FilterChains(supportedChains, chains.FilterByVM(chains.Vm_evm)) + if len(filteredChains) == 0 { + return simtypes.NoOpMsg( + types.ModuleName, + types.TypeMsgWhitelistERC20, + "no EVM-compatible chains found", + ), nil, nil + } //pick a random chain // Keep the switch case to add solana support in future @@ -116,7 +123,7 @@ func SimulateMsgWhitelistERC20(k keeper.Keeper) simtypes.Operation { return simtypes.NoOpMsg( types.ModuleName, msg.Type(), - "unable to validate MsgRemoveOutboundTracker msg", + "unable to validate MsgWhitelistERC20", ), nil, err } diff --git a/x/crosschain/simulation/operations.go b/x/crosschain/simulation/operations.go index 0b4765f0f5..0400b74231 100644 --- a/x/crosschain/simulation/operations.go +++ b/x/crosschain/simulation/operations.go @@ -57,7 +57,7 @@ const ( OpWeightUpdateRateLimiterFlags = "op_weight_msg_update_rate_limiter_flags" // #nosec G101 not a hardcoded credential OpWeightRefundAbortedCCTX = "op_weight_msg_refund_aborted_cctx" // #nosec G101 not a hardcoded credential OpWeightUpdateERC20CustodyPauseStatus = "op_weight_msg_update_erc20_custody_pause_status" // #nosec G101 not a hardcoded credential - OppWeightMigrateERC20CustodyFunds = "op_weight_msg_migrate_erc20_custody_funds" // #nosec G101 not a hardcoded credential + OpWeightMigrateERC20CustodyFunds = "op_weight_msg_migrate_erc20_custody_funds" // #nosec G101 not a hardcoded credential ) @@ -158,7 +158,7 @@ func WeightedOperations( }, ) - appParams.GetOrGenerate(cdc, OppWeightMigrateERC20CustodyFunds, &weightMigrateERC20CustodyFunds, nil, + appParams.GetOrGenerate(cdc, OpWeightMigrateERC20CustodyFunds, &weightMigrateERC20CustodyFunds, nil, func(_ *rand.Rand) { weightMigrateERC20CustodyFunds = DefaultWeightMigrateERC20CustodyFunds }, diff --git a/x/fungible/keeper/deposits.go b/x/fungible/keeper/deposits.go index 60e8f5e6af..3a5c24d1bb 100644 --- a/x/fungible/keeper/deposits.go +++ b/x/fungible/keeper/deposits.go @@ -37,7 +37,6 @@ func (k Keeper) ZRC20DepositAndCallContract( protocolContractVersion crosschaintypes.ProtocolContractVersion, isCrossChainCall bool, ) (*evmtypes.MsgEthereumTxResponse, bool, error) { - //fmt.Println("start ZRC20DepositAndCallContract", asset, senderChainID) // get ZRC20 contract zrc20Contract, _, err := k.getAndCheckZRC20(ctx, amount, senderChainID, coinType, asset) if err != nil { diff --git a/x/observer/simulation/decoders.go b/x/observer/simulation/decoders.go index 30da902c6e..6ebf8514c9 100644 --- a/x/observer/simulation/decoders.go +++ b/x/observer/simulation/decoders.go @@ -91,7 +91,7 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { cdc.MustUnmarshal(kvB.Value, ¶msB) return fmt.Sprintf("%v\n%v", paramsA, paramsB) default: - panic(fmt.Sprintf("invalid observer key prefix %X", kvA.Key[:1])) + panic(fmt.Sprintf("invalid observer key prefix %X (first 8 bytes: %X)", kvA.Key[:1], kvA.Key[:min(8, len(kvA.Key))])) } } } diff --git a/x/observer/simulation/operation_add_observer.go b/x/observer/simulation/operation_add_observer.go index f5d147832f..95eff5ddbb 100644 --- a/x/observer/simulation/operation_add_observer.go +++ b/x/observer/simulation/operation_add_observer.go @@ -60,11 +60,14 @@ func SimulateAddObserver(k keeper.Keeper) simtypes.Operation { "no node accounts available which can be added as observer", ), nil, nil } - + pubkey, err := sample.PubkeyStringFromRand(r) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddObserver, err.Error()), nil, nil + } msg := types.MsgAddObserver{ Creator: policyAccount.Address.String(), ObserverAddress: newObserver, - ZetaclientGranteePubkey: sample.PubkeyStringFromRand(r), + ZetaclientGranteePubkey: pubkey, AddNodeAccountOnly: false, } diff --git a/x/observer/simulation/operation_add_observer_node_account.go b/x/observer/simulation/operation_add_observer_node_account.go index cbaca90f68..e182123fe1 100644 --- a/x/observer/simulation/operation_add_observer_node_account.go +++ b/x/observer/simulation/operation_add_observer_node_account.go @@ -75,10 +75,14 @@ func SimulateAddObserverNodeAccount(k keeper.Keeper) simtypes.Operation { ), nil, nil } + pubkey, err := sample.PubkeyStringFromRand(r) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAddObserver, err.Error()), nil, nil + } msg := types.MsgAddObserver{ Creator: policyAccount.Address.String(), ObserverAddress: newObserver, - ZetaclientGranteePubkey: sample.PubkeyStringFromRand(r), + ZetaclientGranteePubkey: pubkey, AddNodeAccountOnly: true, } diff --git a/x/observer/simulation/operation_disable_cctx.go b/x/observer/simulation/operation_disable_cctx.go index 324a6ead76..9364aa2d87 100644 --- a/x/observer/simulation/operation_disable_cctx.go +++ b/x/observer/simulation/operation_disable_cctx.go @@ -19,7 +19,7 @@ func SimulateDisableCCTX(k keeper.Keeper) simtypes.Operation { ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateKeygen, err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDisableCCTX, err.Error()), nil, nil } authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress()) diff --git a/x/observer/simulation/operation_enable_cctx.go b/x/observer/simulation/operation_enable_cctx.go index 3e963caa15..7ee1d06776 100644 --- a/x/observer/simulation/operation_enable_cctx.go +++ b/x/observer/simulation/operation_enable_cctx.go @@ -19,7 +19,7 @@ func SimulateEnableCCTX(k keeper.Keeper) simtypes.Operation { ) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) { policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateKeygen, err.Error()), nil, nil + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgEnableCCTX, err.Error()), nil, nil } authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address) spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress()) @@ -27,7 +27,7 @@ func SimulateEnableCCTX(k keeper.Keeper) simtypes.Operation { msg := types.MsgEnableCCTX{ Creator: policyAccount.Address.String(), EnableInbound: true, - EnableOutbound: false, + EnableOutbound: true, } err = msg.ValidateBasic() diff --git a/x/observer/simulation/operation_reset_chain_nonces.go b/x/observer/simulation/operation_reset_chain_nonces.go index 709490aafa..38ccacac37 100644 --- a/x/observer/simulation/operation_reset_chain_nonces.go +++ b/x/observer/simulation/operation_reset_chain_nonces.go @@ -53,7 +53,7 @@ func SimulateResetChainNonces(k keeper.Keeper) simtypes.Operation { fmt.Errorf("pending nonces not found for chain %d %s", randomChain.ChainId, randomChain.ChainName) } - nonceIncrement := int64(r.Intn(100-1)) + 1 + nonceIncrement := int64(r.Intn(99)) + 1 msg := types.MsgResetChainNonces{ Creator: policyAccount.Address.String(), diff --git a/x/observer/simulation/operation_update_chain_params.go b/x/observer/simulation/operation_update_chain_params.go index ad4beb768c..21f45d9ee3 100644 --- a/x/observer/simulation/operation_update_chain_params.go +++ b/x/observer/simulation/operation_update_chain_params.go @@ -32,6 +32,10 @@ func SimulateUpdateChainParams(k keeper.Keeper) simtypes.Operation { } cp := sample.ChainParamsFromRand(r, randomChain.ChainId) + err = types.ValidateChainParams(cp) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateChainParams, err.Error()), nil, nil + } msg := types.MsgUpdateChainParams{ Creator: policyAccount.Address.String(), diff --git a/x/observer/simulation/operations.go b/x/observer/simulation/operations.go index d0acc1d020..84bc312de9 100644 --- a/x/observer/simulation/operations.go +++ b/x/observer/simulation/operations.go @@ -262,7 +262,7 @@ func GetRandomAccountAndObserver( }) if !foundObserver { - return simtypes.Account{}, "no observer found", nil, nil + return simtypes.Account{}, "", nil, fmt.Errorf("no observer found") } simAccount, err := GetSimAccount(randomObserver, accounts)