From 41aa11b848e2faf3c4c0b8adb72e79ed870c6b65 Mon Sep 17 00:00:00 2001 From: Tanmay Date: Fri, 6 Dec 2024 16:06:23 -0500 Subject: [PATCH] reafactor update TSS to use existing cctx --- Makefile | 2 +- simulation/simulation_test.go | 75 ++++++++++++++++++- simulation/state.go | 4 + .../operation_update_tss_address.go | 37 +++++++-- x/observer/simulation/decoders.go | 9 +-- 5 files changed, 113 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 6572a9a5ec..64d35523e8 100644 --- a/Makefile +++ b/Makefile @@ -404,7 +404,7 @@ test-sim-fullappsimulation: $(call run-sim-test,"TestFullAppSimulation",TestFullAppSimulation,100,200,30m) test-sim-import-export: - $(call run-sim-test,"test-import-export",TestAppImportExport,50,200,30m) + $(call run-sim-test,"test-import-export",TestAppImportExport,10,100,30m) test-sim-after-import: $(call run-sim-test,"test-sim-after-import",TestAppSimulationAfterImport,100,200,30m) diff --git a/simulation/simulation_test.go b/simulation/simulation_test.go index 709709256b..9466544ceb 100644 --- a/simulation/simulation_test.go +++ b/simulation/simulation_test.go @@ -1,6 +1,7 @@ package simulation_test import ( + "bytes" "encoding/json" "fmt" "math/rand" @@ -17,6 +18,7 @@ import ( storetypes "github.com/cosmos/cosmos-sdk/store/types" cosmossimutils "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/kv" cosmossim "github.com/cosmos/cosmos-sdk/types/simulation" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -425,7 +427,7 @@ func TestAppImportExport(t *testing.T) { storeA := ctxSimApp.KVStore(skp.A) storeB := ctxNewSimApp.KVStore(skp.B) - failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.SkipPrefixes) + failedKVAs, failedKVBs := DiffKVStores(storeA, storeB, skp.SkipPrefixes) require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") t.Logf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B) @@ -573,3 +575,74 @@ func TestAppSimulationAfterImport(t *testing.T) { ) require.NoError(t, err) } + +func DiffKVStores(a sdk.KVStore, b sdk.KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []kv.Pair) { + iterA := a.Iterator(nil, nil) + + defer iterA.Close() + + iterB := b.Iterator(nil, nil) + + defer iterB.Close() + + //for { + // if !iterA.Valid() && !iterB.Valid() { + // return kvAs, kvBs + // } + // var kvA, kvB kv.Pair + // if iterA.Valid() { + // kvA = kv.Pair{Key: iterA.Key(), Value: iterA.Value()} + // iterA.Next() + // } + // if iterB.Valid() { + // kvB = kv.Pair{Key: iterB.Key(), Value: iterB.Value()} + // iterB.Next() + // } + // kvAs = append(kvAs, kvA) + // kvBs = append(kvBs, kvB) + //} + + for { + if !iterA.Valid() && !iterB.Valid() { + return kvAs, kvBs + } + + var kvA, kvB kv.Pair + if iterA.Valid() { + kvA = kv.Pair{Key: iterA.Key(), Value: iterA.Value()} + + iterA.Next() + } + + if iterB.Valid() { + kvB = kv.Pair{Key: iterB.Key(), Value: iterB.Value()} + } + + compareValue := true + + for _, prefix := range prefixesToSkip { + // Skip value comparison if we matched a prefix + if bytes.HasPrefix(kvA.Key, prefix) { + compareValue = false + break + } + } + + if !compareValue { + // We're skipping this key due to an exclusion prefix. If it's present in B, iterate past it. If it's + // absent don't iterate. + if bytes.Equal(kvA.Key, kvB.Key) { + iterB.Next() + } + continue + } + + // always iterate B when comparing + iterB.Next() + + if !bytes.Equal(kvA.Key, kvB.Key) || !bytes.Equal(kvA.Value, kvB.Value) { + kvAs = append(kvAs, kvA) + kvBs = append(kvBs, kvB) + } + } +} diff --git a/simulation/state.go b/simulation/state.go index da5d718e97..f775a19387 100644 --- a/simulation/state.go +++ b/simulation/state.go @@ -135,6 +135,9 @@ func updateObserverState(t *testing.T, rawState map[string]json.RawMessage, cdc tss.OperatorAddressList = observers observerState.Tss = &tss + tssHistory := make([]observertypes.TSS, 0) + tssHistory = append(tssHistory, tss) + chains := zetachains.DefaultChainsList() chainsNonces := make([]observertypes.ChainNonces, 0) pendingNonces := make([]observertypes.PendingNonces, 0) @@ -155,6 +158,7 @@ func updateObserverState(t *testing.T, rawState map[string]json.RawMessage, cdc observerState.ChainNonces = chainsNonces observerState.PendingNonces = pendingNonces + observerState.TssHistory = tssHistory return observerState } diff --git a/x/crosschain/simulation/operation_update_tss_address.go b/x/crosschain/simulation/operation_update_tss_address.go index ffda54df08..cb13c5082f 100644 --- a/x/crosschain/simulation/operation_update_tss_address.go +++ b/x/crosschain/simulation/operation_update_tss_address.go @@ -1,7 +1,6 @@ package simulation import ( - "fmt" "math/rand" "github.com/cosmos/cosmos-sdk/baseapp" @@ -9,7 +8,6 @@ import ( 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" @@ -37,15 +35,40 @@ func SimulateMsgUpdateTssAddress(k keeper.Keeper) simtypes.Operation { ), nil, nil } + cctxList := k.GetAllCrossChainTx(ctx) + if len(cctxList) == 0 { + return simtypes.NoOpMsg( + types.ModuleName, + types.TypeMsgUpdateTssAddress, + "no cross chain txs found", + ), nil, nil + } + minedCCTX := types.CrossChainTx{} + foundMined := false + for _, cctx := range cctxList { + if cctx.CctxStatus.Status == types.CctxStatus_OutboundMined { + minedCCTX = cctx + foundMined = true + break + } + } + if !foundMined { + return simtypes.NoOpMsg( + types.ModuleName, + types.TypeMsgUpdateTssAddress, + "no mined cross chain txs found in mined state", + ), 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}} + //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, + MigrationCctxIndex: minedCCTX.Index, } - k.SetCrossChainTx(ctx, cctx) + //k.SetCrossChainTx(ctx, cctx) k.GetObserverKeeper().SetFundMigrator(ctx, tssmigrator) } diff --git a/x/observer/simulation/decoders.go b/x/observer/simulation/decoders.go index 871e608a36..30da902c6e 100644 --- a/x/observer/simulation/decoders.go +++ b/x/observer/simulation/decoders.go @@ -61,11 +61,10 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string { cdc.MustUnmarshal(kvB.Value, &allChainParamsB) return fmt.Sprintf("%v\n%v", allChainParamsA, allChainParamsB) case bytes.Equal(kvA.Key, types.KeyPrefix(types.TSSHistoryKey)): - var tssHistoryA, tssHistoryB []types.TSS + var tssHistoryA, tssHistoryB types.TSS + cdc.MustUnmarshal(kvA.Value, &tssHistoryA) + cdc.MustUnmarshal(kvB.Value, &tssHistoryB) return fmt.Sprintf("%v\n%v", tssHistoryA, tssHistoryB) - //cdc.MustUnmarshal(kvA.Value, &tssHistoryA) - //cdc.MustUnmarshal(kvB.Value, &tssHistoryB) - //return fmt.Sprintf("%v\n%v", tssHistoryA, tssHistoryB) case bytes.Equal(kvA.Key, types.KeyPrefix(types.TssFundMigratorKey)): var tssFundMigratorA, tssFundMigratorB types.TssFundMigratorInfo cdc.MustUnmarshal(kvA.Value, &tssFundMigratorA) @@ -92,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)) + panic(fmt.Sprintf("invalid observer key prefix %X", kvA.Key[:1])) } } }