Skip to content

Commit

Permalink
refactor based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 12, 2024
1 parent a90a6eb commit 1d431cc
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 62 deletions.
7 changes: 5 additions & 2 deletions testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 8 additions & 25 deletions testutil/sample/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Check failure on line 60 in testutil/sample/crypto.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
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
Expand Down
6 changes: 1 addition & 5 deletions testutil/sample/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 6 additions & 3 deletions x/crosschain/simulation/operation_abort_stuck_cctx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simulation

import (
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
13 changes: 8 additions & 5 deletions x/crosschain/simulation/operation_gas_price_voter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simulation

import (
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -39,17 +38,21 @@ 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{
Creator: randomObserver,
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/simulation/operation_update_tss_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/simulation/operation_vote_outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion x/crosschain/simulation/operation_whitelist_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

)

Expand Down Expand Up @@ -158,7 +158,7 @@ func WeightedOperations(
},
)

appParams.GetOrGenerate(cdc, OppWeightMigrateERC20CustodyFunds, &weightMigrateERC20CustodyFunds, nil,
appParams.GetOrGenerate(cdc, OpWeightMigrateERC20CustodyFunds, &weightMigrateERC20CustodyFunds, nil,
func(_ *rand.Rand) {
weightMigrateERC20CustodyFunds = DefaultWeightMigrateERC20CustodyFunds
},
Expand Down
1 change: 0 additions & 1 deletion x/fungible/keeper/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion x/observer/simulation/decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
cdc.MustUnmarshal(kvB.Value, &paramsB)
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))]))
}
}
}
7 changes: 5 additions & 2 deletions x/observer/simulation/operation_add_observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
6 changes: 5 additions & 1 deletion x/observer/simulation/operation_add_observer_node_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
2 changes: 1 addition & 1 deletion x/observer/simulation/operation_disable_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions x/observer/simulation/operation_enable_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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())

msg := types.MsgEnableCCTX{
Creator: policyAccount.Address.String(),
EnableInbound: true,
EnableOutbound: false,
EnableOutbound: true,
}

err = msg.ValidateBasic()
Expand Down
2 changes: 1 addition & 1 deletion x/observer/simulation/operation_reset_chain_nonces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions x/observer/simulation/operation_update_chain_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion x/observer/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1d431cc

Please sign in to comment.