Skip to content

Commit

Permalink
reduce errors for RefundAbortedCCTX
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 6, 2024
1 parent 7ee9138 commit 658c59e
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 21 deletions.
3 changes: 2 additions & 1 deletion simulation/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func updateObserverState(t *testing.T, rawState map[string]json.RawMessage, cdc
observerState.Observers.ObserverList = observers
observerState.CrosschainFlags.IsInboundEnabled = true
observerState.CrosschainFlags.IsOutboundEnabled = true
tss := sample.TSSRandom(t, r)
tss, err := sample.TSSFromRand(r)
require.NoError(t, err)
tss.OperatorAddressList = observers
observerState.Tss = &tss

Expand Down
5 changes: 4 additions & 1 deletion testutil/sample/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ func CCTXfromRand(r *rand.Rand,
Index: index,
ZetaFees: sdk.NewUint(1),
RelayedMessage: base64.StdEncoding.EncodeToString(RandomBytes(r)),
CctxStatus: &types.Status{Status: types.CctxStatus_PendingOutbound},
CctxStatus: &types.Status{
IsAbortRefunded: false,
Status: types.CctxStatus_PendingOutbound,
},
InboundParams: inbound,
OutboundParams: []*types.OutboundParams{outbound},
}
Expand Down
17 changes: 9 additions & 8 deletions testutil/sample/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/cosmos"
zetacrypto "github.com/zeta-chain/node/pkg/crypto"
Expand Down Expand Up @@ -121,22 +119,25 @@ func ChainParamsList() (cpl types.ChainParamsList) {
return
}

// TSSRandom returns a random TSS,it uses the randomness provided as a parameter
func TSSRandom(t *testing.T, r *rand.Rand) types.TSS {
// TSSFromRand returns a random TSS,it uses the randomness provided as a parameter
func TSSFromRand(r *rand.Rand) (types.TSS, error) {
pubKey := PubKey(r)
spk, err := cosmos.Bech32ifyPubKey(cosmos.Bech32PubKeyTypeAccPub, pubKey)
require.NoError(t, err)
if err != nil {
return types.TSS{}, err
}
pk, err := zetacrypto.NewPubKey(spk)
require.NoError(t, err)
if err != nil {
return types.TSS{}, err
}
pubkeyString := pk.String()
return types.TSS{
TssPubkey: pubkeyString,
TssParticipantList: []string{},
OperatorAddressList: []string{},
FinalizedZetaHeight: r.Int63(),
KeyGenZetaHeight: r.Int63(),
}

}, nil
}

// TODO: rename to TSS
Expand Down
20 changes: 10 additions & 10 deletions x/crosschain/simulation/operation_refund_aborted_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func SimulateMsgRefundAbortedCCTX(k keeper.Keeper,
// Fetch the account from the auth keeper which can then be used to fetch spendable coins}
policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAbortStuckCCTX, err.Error()), nil, nil
return simtypes.NoOpMsg(types.ModuleName, types.RefundAborted, err.Error()), nil, nil
}

authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address)
Expand All @@ -32,22 +32,22 @@ func SimulateMsgRefundAbortedCCTX(k keeper.Keeper,
abortedCctxFound := false

for _, cctx := range cctxList {

if cctx.CctxStatus.Status == types.CctxStatus_Aborted {
if !cctx.InboundParams.CoinType.SupportsRefund() {
continue
}
if cctx.CctxStatus.IsAbortRefunded {
continue
}

abortedCctx = cctx
abortedCctxFound = true
break
}
}
if !abortedCctxFound {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAbortStuckCCTX, "no aborted cctx found"), nil, nil
}

if abortedCctx.CctxStatus.IsAbortRefunded {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAbortStuckCCTX, "aborted cctx already refunded"), nil, nil
}

if !abortedCctx.InboundParams.CoinType.SupportsRefund() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAbortStuckCCTX, "coin type does not support refund"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, types.RefundAborted, "no aborted cctx found"), nil, nil
}

msg := types.MsgRefundAbortedCCTX{
Expand Down
99 changes: 99 additions & 0 deletions x/crosschain/simulation/operation_update_tss_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package simulation

import (
"fmt"
"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"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/crosschain/keeper"
"github.com/zeta-chain/node/x/crosschain/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
)

// SimulateMsgUpdateTssAddress generates a MsgUpdateTssAddress with random values and delivers it
func SimulateMsgUpdateTssAddress(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.TypeMsgWhitelistERC20, err.Error()), nil, nil
}

authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address)
spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress())

supportedChains := k.GetChainsSupportingTSSMigration(ctx)
if len(supportedChains) == 0 {
return simtypes.NoOpMsg(
types.ModuleName,
types.TypeMsgUpdateTssAddress,
"no chains found which support tss migration",
), nil, nil
}

for _, chain := range supportedChains {
index := ethcrypto.Keccak256Hash([]byte(fmt.Sprintf("%d", r.Int63()))).Hex()
cctx := types.CrossChainTx{Index: index,
CctxStatus: &types.Status{Status: types.CctxStatus_OutboundMined}}
tssmigrator := observertypes.TssFundMigratorInfo{
ChainId: chain.ChainId,
MigrationCctxIndex: index,
}
k.SetCrossChainTx(ctx, cctx)
k.GetObserverKeeper().SetFundMigrator(ctx, tssmigrator)
}

oldTss, found := k.GetObserverKeeper().GetTSS(ctx)
if !found {
return simtypes.NoOpMsg(
types.ModuleName,
types.TypeMsgUpdateTssAddress,
"no TSS found",
), nil, nil
}
newTss, err := sample.TSSFromRand(r)
newTss.FinalizedZetaHeight = oldTss.FinalizedZetaHeight + 10
newTss.KeyGenZetaHeight = oldTss.KeyGenZetaHeight + 10
if err != nil {
return simtypes.NoOpMsg(
types.ModuleName,
types.TypeMsgUpdateTssAddress,
err.Error()),
nil, nil
}
k.GetObserverKeeper().SetTSSHistory(ctx, newTss)

msg := types.MsgUpdateTssAddress{
Creator: policyAccount.Address.String(),
TssPubkey: newTss.TssPubkey,
}

err = msg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to validate MsgRemoveOutboundTracker msg"), 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)
}
}
6 changes: 5 additions & 1 deletion x/crosschain/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const (
DefaultWeightVoteInbound = 100
DefaultWeightWhitelistERC20 = 10
DefaultWeightMigrateTssFunds = 1
DefaultWeightUpdateTssAddress = 1
DefaultWeightUpdateTssAddress = 10
DefaultWeightAbortStuckCCTX = 5
DefaultWeightUpdateRateLimiterFlags = 10
DefaultWeightRefundAbortedCCTX = 10
Expand Down Expand Up @@ -206,6 +206,10 @@ func WeightedOperations(
weightMigrateERC20CustodyFunds,
SimulateMigrateERC20CustodyFunds(k),
),
simulation.NewWeightedOperation(
weightUpdateTssAddress,
SimulateMsgUpdateTssAddress(k),
),
}
}

Expand Down

0 comments on commit 658c59e

Please sign in to comment.