From 863b4cd7f5640480fbf4702f1314f4af0399aa0f Mon Sep 17 00:00:00 2001 From: Tanmay Date: Mon, 9 Dec 2024 11:12:40 -0500 Subject: [PATCH] set observe count for even is observer set is empty --- Makefile | 2 +- simulation/simulation_test.go | 67 +++++++++++++++++++++++++++++++++++ x/crosschain/genesis.go | 26 +++++++------- x/observer/genesis.go | 23 +++++++----- 4 files changed, 96 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 6572a9a5ec..f39e4f27ff 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,100,200,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..58f7f4aa8a 100644 --- a/simulation/simulation_test.go +++ b/simulation/simulation_test.go @@ -425,6 +425,14 @@ func TestAppImportExport(t *testing.T) { storeA := ctxSimApp.KVStore(skp.A) storeB := ctxNewSimApp.KVStore(skp.B) + a, b := CountKVStores(storeA, storeB, skp.SkipPrefixes) + if a != b { + t.Logf("storeA: %d, storeB: %d", a, b) + t.Log("key prefix: ", skp.A.Name()) + //FindDiffKeys(storeA, storeB) + //t.Fail() + } + failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.SkipPrefixes) require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") @@ -573,3 +581,62 @@ func TestAppSimulationAfterImport(t *testing.T) { ) require.NoError(t, err) } + +// DiffKVStores compares two KVstores and returns all the key/value pairs +// that differ from one another. It also skips value comparison for a set of provided prefixes. +func CountKVStores(a sdk.KVStore, b sdk.KVStore, _ [][]byte) (int, int) { + iterA := a.Iterator(nil, nil) + + defer iterA.Close() + + iterB := b.Iterator(nil, nil) + + defer iterB.Close() + + countA := 0 + countB := 0 + + for iterA.Valid() { + countA++ + iterA.Next() + } + + for iterB.Valid() { + countB++ + iterB.Next() + } + return countA, countB +} + +func FindDiffKeys(a sdk.KVStore, b sdk.KVStore) { + + keysA := map[string]bool{} + keysB := map[string]bool{} + iterA := a.Iterator(nil, nil) + + defer iterA.Close() + + iterB := b.Iterator(nil, nil) + + defer iterB.Close() + + for iterA.Valid() { + k := string(iterA.Key()) + iterA.Next() + keysA[k] = true + + } + + for iterB.Valid() { + kb := string(iterB.Key()) + iterB.Next() + keysB[kb] = true + } + + for k := range keysA { + if _, ok := keysB[k]; !ok { + fmt.Println("Key in A not in B", k) + } + } + +} diff --git a/x/crosschain/genesis.go b/x/crosschain/genesis.go index cf0ed3ec9c..bb450ef0a8 100644 --- a/x/crosschain/genesis.go +++ b/x/crosschain/genesis.go @@ -52,19 +52,19 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) cctx := *elem k.SetCrossChainTx(ctx, cctx) // set mapping inboundHash -> cctxIndex - in, _ := k.GetInboundHashToCctx(ctx, cctx.InboundParams.ObservedHash) - in.InboundHash = cctx.InboundParams.ObservedHash - found := false - for _, cctxIndex := range in.CctxIndex { - if cctxIndex == cctx.Index { - found = true - break - } - } - if !found { - in.CctxIndex = append(in.CctxIndex, cctx.Index) - } - k.SetInboundHashToCctx(ctx, in) + //in, _ := k.GetInboundHashToCctx(ctx, cctx.InboundParams.ObservedHash) + //in.InboundHash = cctx.InboundParams.ObservedHash + //found := false + //for _, cctxIndex := range in.CctxIndex { + // if cctxIndex == cctx.Index { + // found = true + // break + // } + //} + //if !found { + // in.CctxIndex = append(in.CctxIndex, cctx.Index) + //} + //k.SetInboundHashToCctx(ctx, in) if cctx.CctxStatus.Status == types.CctxStatus_Aborted && cctx.InboundParams.CoinType == coin.CoinType_Zeta && cctx.CctxStatus.IsAbortRefunded == false { k.AddZetaAbortedAmount(ctx, keeper.GetAbortedAmount(cctx)) diff --git a/x/observer/genesis.go b/x/observer/genesis.go index 12a92887c2..5ebbc58f47 100644 --- a/x/observer/genesis.go +++ b/x/observer/genesis.go @@ -1,6 +1,8 @@ package observer import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/zeta-chain/node/pkg/chains" @@ -11,10 +13,21 @@ import ( // InitGenesis initializes the observer module's state from a provided genesis // state. func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { - observerCount := uint64(0) + + fmt.Println("Observer Set Len : ", genState.Observers.Len()) + + observerCount := uint64(genState.Observers.Len()) + if genState.Observers.Len() > 0 { k.SetObserverSet(ctx, genState.Observers) - observerCount = uint64(len(genState.Observers.ObserverList)) + } else { + k.SetObserverSet(ctx, types.ObserverSet{}) + } + + if genState.LastObserverCount != nil { + k.SetLastObserverCount(ctx, genState.LastObserverCount) + } else { + k.SetLastObserverCount(ctx, &types.LastObserverCount{LastChangeHeight: 0, Count: observerCount}) } // if chain params are defined set them @@ -82,12 +95,6 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) }) } - if genState.LastObserverCount != nil { - k.SetLastObserverCount(ctx, genState.LastObserverCount) - } else { - k.SetLastObserverCount(ctx, &types.LastObserverCount{LastChangeHeight: 0, Count: observerCount}) - } - tss := types.TSS{} if genState.Tss != nil { tss = *genState.Tss