From fb17ccdef8fe4151dfb10c64416b6c252fa21ff2 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 13:54:55 +0800 Subject: [PATCH 01/19] feat: add dump iavl root and memiavl root --- cmd/chain-maind/app/dump_root.go | 194 +++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 cmd/chain-maind/app/dump_root.go diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go new file mode 100644 index 000000000..42113c040 --- /dev/null +++ b/cmd/chain-maind/app/dump_root.go @@ -0,0 +1,194 @@ +package app + +import ( + "bytes" + "fmt" + "sort" + + "cosmossdk.io/log" + "cosmossdk.io/store/types" + + "cosmossdk.io/store/rootmulti" + dbm "github.com/cosmos/cosmos-db" + "github.com/crypto-org-chain/chain-main/v4/app" + "github.com/crypto-org-chain/cronos/memiavl" + "github.com/spf13/cobra" +) + +const capaMemStoreKey = "mem_capability" + +func DumpRootCmd() *cobra.Command { + keys, _, _ := app.StoreKeys() + storeNames := make([]string, 0, len(keys)) + for name := range keys { + storeNames = append(storeNames, name) + } + sort.Strings(storeNames) + return DumpRootGroupCmd(storeNames) +} + +func DumpRootGroupCmd(storeNames []string) *cobra.Command { + cmd := &cobra.Command{ + Use: "dump-root", + Short: "dump module root", + } + cmd.AddCommand( + DumpMemIavlRoot(storeNames), + DumpIavlRoot(storeNames), + ) + return cmd +} + +func DumpMemIavlRoot(storeNames []string) *cobra.Command { + cmd := &cobra.Command{ + Use: "dump-memiavl-root", + Short: "dump mem-iavl root at version [dir]", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + dir := args[0] + version, err := cmd.Flags().GetUint32("version") + if err != nil { + return err + } + opts := memiavl.Options{ + InitialStores: storeNames, + CreateIfMissing: false, + TargetVersion: version, + } + db, err := memiavl.Load(dir, opts) + if err != nil { + return err + } + for _, storeName := range storeNames { + tree := db.TreeByName(storeName) + if tree != nil { + fmt.Printf("module %s version %d RootHash %X\n", storeName, tree.Version(), tree.RootHash()) + } else { + fmt.Printf("module %s not loaded\n", storeName) + } + } + + db.MultiTree.UpdateCommitInfo() + lastCommitInfo := convertCommitInfo(db.MultiTree.LastCommitInfo()) + + fmt.Printf("Version %d RootHash %X\n", lastCommitInfo.Version, lastCommitInfo.Hash()) + + tree := db.TreeByName(capaMemStoreKey) + if tree != nil { + fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.Version()) + } else { + fmt.Printf("module %s not loaded\n", capaMemStoreKey) + } + return nil + }, + } + cmd.Flags().Uint32("version", 0, "the version to dump") + return cmd +} + +func convertCommitInfo(commitInfo *memiavl.CommitInfo) *types.CommitInfo { + storeInfos := make([]types.StoreInfo, len(commitInfo.StoreInfos)) + for i, storeInfo := range commitInfo.StoreInfos { + storeInfos[i] = types.StoreInfo{ + Name: storeInfo.Name, + CommitId: types.CommitID{ + Version: storeInfo.CommitId.Version, + Hash: storeInfo.CommitId.Hash, + }, + } + } + return &types.CommitInfo{ + Version: commitInfo.Version, + StoreInfos: storeInfos, + } +} + +func DumpIavlRoot(storeNames []string) *cobra.Command { + // this is need to change in different height + storeNames = []string{ + "acc", "authz", "bank", "capability", "chainmain", "distribution", "evidence", "feegrant", + "feeibc", "gov", "group", "ibc", "icaauth", "icacontroller", "icahost", "mint", "nft", "nonfungibletokentransfer", + "params", "slashing", "staking", "supply", "transfer", "upgrade", + } + cmd := &cobra.Command{ + Use: "dump-iavl-root", + Short: "dump iavl root at version [dir]", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + dir := args[0] + version, err := cmd.Flags().GetInt64("version") + if err != nil { + return err + } + db, err := dbm.NewGoLevelDB("application", dir, nil) + if err != nil { + return err + } + defer db.Close() + rs := rootmulti.NewStore(db, log.NewNopLogger(), nil) + for _, storeKey := range storeNames { + rs.MountStoreWithDB(types.NewKVStoreKey(storeKey), types.StoreTypeIAVL, nil) + } + + err = rs.LoadLatestVersion() + if err != nil { + fmt.Printf("failed to load latest version: %s\n", err.Error()) + return err + } + err = rs.LoadVersion(version) + if err != nil { + fmt.Printf("failed to load version %d %s\n", version, err.Error()) + return err + } + + var cInfo *types.CommitInfo + cInfo, err = rs.GetCommitInfo(version) + if err != nil { + fmt.Printf("failed to load version %d commit info: %s\n", version, err.Error()) + return err + } + infoMaps := make(map[string]types.StoreInfo) + for _, storeInfo := range cInfo.StoreInfos { + infoMaps[storeInfo.Name] = storeInfo + } + + var infos []types.StoreInfo + for _, storeName := range storeNames { + info, ok := infoMaps[storeName] + if !ok { + fmt.Printf("module %s not loaded\n", storeName) + continue + } + commitID := info.CommitId + fmt.Printf("module %s version %d RootHash %X\n", storeName, commitID.Version, commitID.Hash) + infos = append(infos, info) + } + + if len(infos) != len(cInfo.StoreInfos) { + fmt.Printf("Warning: Partial commit info (loaded %d stores, found %d)\n", len(cInfo.StoreInfos), len(infos)) + storeMaps := make(map[string]struct{}) + for _, storeName := range storeNames { + storeMaps[storeName] = struct{}{} + } + for _, info := range cInfo.StoreInfos { + if _, ok := storeMaps[info.Name]; !ok { + fmt.Printf("module %s missed\n", info.Name) + } + } + } + + commitInfo := &types.CommitInfo{ + Version: version, + StoreInfos: infos, + } + + if rs.LastCommitID().Version != commitInfo.Version || !bytes.Equal(rs.LastCommitID().Hash, commitInfo.Hash()) { + return fmt.Errorf("failed to calculate %d commit info, rs Hash %X, commit Hash %X", rs.LastCommitID().Version, rs.LastCommitID().Hash, commitInfo.Hash()) + } + fmt.Printf("Version %d RootHash %X\n", commitInfo.Version, commitInfo.Hash()) + return nil + }, + } + cmd.Flags().Int64("version", 0, "the version to dump") + return cmd +} From 5d9fb7ec4ebc81c03087f816b63bfed7ada6d48a Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 13:58:09 +0800 Subject: [PATCH 02/19] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c20ac54c..596298df0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Unreleased ### Improvements +- [#1191](https://github.com/crypto-org-chain/chain-main/pull/1191) feat: add dump iavl root and memiavl root - [#1169](https://github.com/crypto-org-chain/chain-main/pull/1169) Update linter and tidy up code - [#1175](https://github.com/crypto-org-chain/chain-main/pull/1175) Add maxsupply module - [#1181](https://github.com/crypto-org-chain/chain-main/pull/1181) Fix Makefile chaindImage remote build env wrong check + add script to run chain locally From 229651c24d1b989c846292dfb4a47b1cd95cf343 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 14:00:54 +0800 Subject: [PATCH 03/19] update app.go --- cmd/chain-maind/app/app.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmd/chain-maind/app/app.go b/cmd/chain-maind/app/app.go index 3cf047de8..3713f0028 100644 --- a/cmd/chain-maind/app/app.go +++ b/cmd/chain-maind/app/app.go @@ -256,6 +256,12 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b if changeSetCmd != nil { rootCmd.AddCommand(changeSetCmd) } + + // dump root hash commands + dumpRootCmd := DumpRootCmd() + if dumpRootCmd != nil { + rootCmd.AddCommand(dumpRootCmd) + } } // genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter From 640c18d2779ff2c37c1d06f96d6d93cf5b62f9b8 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 14:25:14 +0800 Subject: [PATCH 04/19] update lint --- .github/workflows/lint.yml | 10 +++++++++- cmd/chain-maind/app/dump_root.go | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 10f690603..c69362d54 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -42,6 +42,14 @@ jobs: BUILD_TAGS=rocksdb,grocksdb_clean_link go build -tags $BUILD_TAGS ./cmd/chain-maind - golangci-lint run --output.text.path stdout --path-prefix=./ --timeout 30m --build-tags $BUILD_TAGS + golangci-lint run --fix --output.text.path stdout --path-prefix=./ --timeout 30m --build-tags $BUILD_TAGS # Check only if there are differences in the source code if: steps.changed-files.outputs.any_changed == 'true' + - name: check working directory is clean + id: changes + run: | + set +e + (git diff --no-ext-diff --exit-code) + echo "changed=$?" >> $GITHUB_OUTPUT + - if: steps.changes.outputs.changed == 1 + run: echo "Working directory is dirty" && exit 1 \ No newline at end of file diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 42113c040..722f266ca 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -5,14 +5,14 @@ import ( "fmt" "sort" - "cosmossdk.io/log" - "cosmossdk.io/store/types" - - "cosmossdk.io/store/rootmulti" dbm "github.com/cosmos/cosmos-db" "github.com/crypto-org-chain/chain-main/v4/app" "github.com/crypto-org-chain/cronos/memiavl" "github.com/spf13/cobra" + + "cosmossdk.io/log" + "cosmossdk.io/store/rootmulti" + "cosmossdk.io/store/types" ) const capaMemStoreKey = "mem_capability" From ccfd78b2beef89793203a30cae752a6d799a02d3 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 17:30:14 +0800 Subject: [PATCH 05/19] add special storeInfo for the same hash with iavl node --- cmd/chain-maind/app/dump_root.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 722f266ca..23c09e042 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -73,6 +73,13 @@ func DumpMemIavlRoot(storeNames []string) *cobra.Command { fmt.Printf("Version %d RootHash %X\n", lastCommitInfo.Version, lastCommitInfo.Hash()) + // if you want to hash the same with iavl node + var specialInfo types.StoreInfo + specialInfo.Name = "mem_capability" + lastCommitInfo.StoreInfos = append(lastCommitInfo.StoreInfos, specialInfo) + + fmt.Printf("calculate again Last Commit Infos: %v\n", lastCommitInfo) + tree := db.TreeByName(capaMemStoreKey) if tree != nil { fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.Version()) From c0a18f41f6e2065c278ab1ddc3f22223fca6873c Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 17:31:08 +0800 Subject: [PATCH 06/19] rm unused code --- cmd/chain-maind/app/dump_root.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 23c09e042..a0d661e8f 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -137,11 +137,6 @@ func DumpIavlRoot(storeNames []string) *cobra.Command { rs.MountStoreWithDB(types.NewKVStoreKey(storeKey), types.StoreTypeIAVL, nil) } - err = rs.LoadLatestVersion() - if err != nil { - fmt.Printf("failed to load latest version: %s\n", err.Error()) - return err - } err = rs.LoadVersion(version) if err != nil { fmt.Printf("failed to load version %d %s\n", version, err.Error()) From 14425862de394eb679c0924661d2656cf830f001 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Thu, 28 Aug 2025 19:06:25 +0800 Subject: [PATCH 07/19] update calculate again logic --- cmd/chain-maind/app/dump_root.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index a0d661e8f..9e74f5c0d 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -73,18 +73,20 @@ func DumpMemIavlRoot(storeNames []string) *cobra.Command { fmt.Printf("Version %d RootHash %X\n", lastCommitInfo.Version, lastCommitInfo.Hash()) - // if you want to hash the same with iavl node - var specialInfo types.StoreInfo - specialInfo.Name = "mem_capability" - lastCommitInfo.StoreInfos = append(lastCommitInfo.StoreInfos, specialInfo) - - fmt.Printf("calculate again Last Commit Infos: %v\n", lastCommitInfo) - - tree := db.TreeByName(capaMemStoreKey) - if tree != nil { - fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.Version()) - } else { - fmt.Printf("module %s not loaded\n", capaMemStoreKey) + if version < 24836000 { + // if you want to hash the same with iavl node + var specialInfo types.StoreInfo + specialInfo.Name = "mem_capability" + lastCommitInfo.StoreInfos = append(lastCommitInfo.StoreInfos, specialInfo) + + fmt.Printf("calculate again Last Commit Infos: %v\n", lastCommitInfo) + + tree := db.TreeByName(capaMemStoreKey) + if tree != nil { + fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.Version()) + } else { + fmt.Printf("module %s not loaded\n", capaMemStoreKey) + } } return nil }, From 673c99c4e57514eeb2d14cea594e9d1e40f36f63 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Mon, 1 Sep 2025 10:21:17 +0800 Subject: [PATCH 08/19] 1.fix changelog 2.update comment 3. close db --- CHANGELOG.md | 2 +- cmd/chain-maind/app/dump_root.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 596298df0..1f33040ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ## Unreleased ### Improvements -- [#1191](https://github.com/crypto-org-chain/chain-main/pull/1191) feat: add dump iavl root and memiavl root +- [#1191](https://github.com/crypto-org-chain/chain-main/pull/1191) feat: add dump iavl root and memiavl root commands - [#1169](https://github.com/crypto-org-chain/chain-main/pull/1169) Update linter and tidy up code - [#1175](https://github.com/crypto-org-chain/chain-main/pull/1175) Add maxsupply module - [#1181](https://github.com/crypto-org-chain/chain-main/pull/1181) Fix Makefile chaindImage remote build env wrong check + add script to run chain locally diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 9e74f5c0d..596851326 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -15,7 +15,11 @@ import ( "cosmossdk.io/store/types" ) -const capaMemStoreKey = "mem_capability" +const ( + capaMemStoreKey = "mem_capability" + + ChainMainV6UpgradeHeight = 24836000 +) func DumpRootCmd() *cobra.Command { keys, _, _ := app.StoreKeys() @@ -59,6 +63,7 @@ func DumpMemIavlRoot(storeNames []string) *cobra.Command { if err != nil { return err } + defer db.Close() for _, storeName := range storeNames { tree := db.TreeByName(storeName) if tree != nil { @@ -73,10 +78,11 @@ func DumpMemIavlRoot(storeNames []string) *cobra.Command { fmt.Printf("Version %d RootHash %X\n", lastCommitInfo.Version, lastCommitInfo.Hash()) - if version < 24836000 { - // if you want to hash the same with iavl node + if version < ChainMainV6UpgradeHeight { + // if you want to calculate hash same with iavl node + // It has an issue as described in cosmos/cosmos-sdk#14916, so we need to apply a hacky solution to it. var specialInfo types.StoreInfo - specialInfo.Name = "mem_capability" + specialInfo.Name = capaMemStoreKey lastCommitInfo.StoreInfos = append(lastCommitInfo.StoreInfos, specialInfo) fmt.Printf("calculate again Last Commit Infos: %v\n", lastCommitInfo) From 75aae6c43913c2125d8a1375d25f824825d28da2 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Mon, 1 Sep 2025 11:16:38 +0800 Subject: [PATCH 09/19] update StoreKeys --- cmd/chain-maind/app/dump_root.go | 57 ++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 596851326..bfbef43aa 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -5,6 +5,30 @@ import ( "fmt" "sort" + evidencetypes "cosmossdk.io/x/evidence/types" + "cosmossdk.io/x/feegrant" + upgradetypes "cosmossdk.io/x/upgrade/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/cosmos/cosmos-sdk/x/group" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" + icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types" + icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types" + ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types" + ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" + ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" + chainmaintypes "github.com/crypto-org-chain/chain-main/v4/x/chainmain/types" + nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types" + nfttypes "github.com/crypto-org-chain/chain-main/v4/x/nft/types" + supplytypes "github.com/crypto-org-chain/chain-main/v4/x/supply/types" + dbm "github.com/cosmos/cosmos-db" "github.com/crypto-org-chain/chain-main/v4/app" "github.com/crypto-org-chain/cronos/memiavl" @@ -120,11 +144,38 @@ func convertCommitInfo(commitInfo *memiavl.CommitInfo) *types.CommitInfo { func DumpIavlRoot(storeNames []string) *cobra.Command { // this is need to change in different height + // because some StoreKey version are zero + // such as consensusparamtypes, circuittypes storeNames = []string{ - "acc", "authz", "bank", "capability", "chainmain", "distribution", "evidence", "feegrant", - "feeibc", "gov", "group", "ibc", "icaauth", "icacontroller", "icahost", "mint", "nft", "nonfungibletokentransfer", - "params", "slashing", "staking", "supply", "transfer", "upgrade", + authtypes.StoreKey, + banktypes.StoreKey, + stakingtypes.StoreKey, + minttypes.StoreKey, + distrtypes.StoreKey, + slashingtypes.StoreKey, + govtypes.StoreKey, + paramstypes.StoreKey, + ibcexported.StoreKey, + upgradetypes.StoreKey, + feegrant.StoreKey, + evidencetypes.StoreKey, + ibctransfertypes.StoreKey, + icacontrollertypes.StoreKey, + icahosttypes.StoreKey, + capabilitytypes.StoreKey, + authzkeeper.StoreKey, + nfttransfertypes.StoreKey, + group.StoreKey, + ibcfeetypes.StoreKey, + chainmaintypes.StoreKey, + supplytypes.StoreKey, + // maxsupplytypes.StoreKey, + nfttypes.StoreKey, + // consensusparamtypes.StoreKey, + // circuittypes.StoreKey, + "icaauth", } + cmd := &cobra.Command{ Use: "dump-iavl-root", Short: "dump iavl root at version [dir]", From 473dcbf4ca490f19a706db202f62f695171c69dc Mon Sep 17 00:00:00 2001 From: songgaoye Date: Mon, 1 Sep 2025 13:11:01 +0800 Subject: [PATCH 10/19] fmt --- cmd/chain-maind/app/dump_root.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index bfbef43aa..1c20ccc4d 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -5,38 +5,40 @@ import ( "fmt" "sort" - evidencetypes "cosmossdk.io/x/evidence/types" - "cosmossdk.io/x/feegrant" - upgradetypes "cosmossdk.io/x/upgrade/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" - govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - "github.com/cosmos/cosmos-sdk/x/group" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" - slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + dbm "github.com/cosmos/cosmos-db" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types" icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types" ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types" ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" + "github.com/crypto-org-chain/chain-main/v4/app" chainmaintypes "github.com/crypto-org-chain/chain-main/v4/x/chainmain/types" nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types" nfttypes "github.com/crypto-org-chain/chain-main/v4/x/nft/types" supplytypes "github.com/crypto-org-chain/chain-main/v4/x/supply/types" - dbm "github.com/cosmos/cosmos-db" - "github.com/crypto-org-chain/chain-main/v4/app" "github.com/crypto-org-chain/cronos/memiavl" "github.com/spf13/cobra" "cosmossdk.io/log" "cosmossdk.io/store/rootmulti" "cosmossdk.io/store/types" + + evidencetypes "cosmossdk.io/x/evidence/types" + "cosmossdk.io/x/feegrant" + upgradetypes "cosmossdk.io/x/upgrade/types" + + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/cosmos/cosmos-sdk/x/group" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) const ( From 35787fbc7d8145c1d629c526a92d2d67d3d6c82f Mon Sep 17 00:00:00 2001 From: songgaoye Date: Mon, 1 Sep 2025 13:28:13 +0800 Subject: [PATCH 11/19] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f33040ed..462570f94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,13 +5,13 @@ ## Unreleased ### Improvements -- [#1191](https://github.com/crypto-org-chain/chain-main/pull/1191) feat: add dump iavl root and memiavl root commands - [#1169](https://github.com/crypto-org-chain/chain-main/pull/1169) Update linter and tidy up code - [#1175](https://github.com/crypto-org-chain/chain-main/pull/1175) Add maxsupply module - [#1181](https://github.com/crypto-org-chain/chain-main/pull/1181) Fix Makefile chaindImage remote build env wrong check + add script to run chain locally - [#1182](https://github.com/crypto-org-chain/chain-main/pull/1182) Refactor script to run chain locally to use daemon in default go PATH - [#1185](https://github.com/crypto-org-chain/chain-main/pull/1185) Error deleting legacy versions bug. - [#1189](https://github.com/crypto-org-chain/chain-main/pull/1189) versiondb interface missmatch. +- [#1191](https://github.com/crypto-org-chain/chain-main/pull/1191) feat: add dump iavl root and memiavl root commands. *July 9, 2025* From 33af983a80999aa48008e2a5a897b0041ca6ee17 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Mon, 1 Sep 2025 15:22:01 +0800 Subject: [PATCH 12/19] update fmt --- .github/workflows/lint.yml | 12 ++---------- cmd/chain-maind/app/dump_root.go | 2 -- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c69362d54..ba65ec8ed 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -42,14 +42,6 @@ jobs: BUILD_TAGS=rocksdb,grocksdb_clean_link go build -tags $BUILD_TAGS ./cmd/chain-maind - golangci-lint run --fix --output.text.path stdout --path-prefix=./ --timeout 30m --build-tags $BUILD_TAGS + golangci-lint run --output.text.path stdout --path-prefix=./ --timeout 30m --build-tags $BUILD_TAGS # Check only if there are differences in the source code - if: steps.changed-files.outputs.any_changed == 'true' - - name: check working directory is clean - id: changes - run: | - set +e - (git diff --no-ext-diff --exit-code) - echo "changed=$?" >> $GITHUB_OUTPUT - - if: steps.changes.outputs.changed == 1 - run: echo "Working directory is dirty" && exit 1 \ No newline at end of file + if: steps.changed-files.outputs.any_changed == 'true' \ No newline at end of file diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 1c20ccc4d..0b6d95374 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -17,14 +17,12 @@ import ( nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types" nfttypes "github.com/crypto-org-chain/chain-main/v4/x/nft/types" supplytypes "github.com/crypto-org-chain/chain-main/v4/x/supply/types" - "github.com/crypto-org-chain/cronos/memiavl" "github.com/spf13/cobra" "cosmossdk.io/log" "cosmossdk.io/store/rootmulti" "cosmossdk.io/store/types" - evidencetypes "cosmossdk.io/x/evidence/types" "cosmossdk.io/x/feegrant" upgradetypes "cosmossdk.io/x/upgrade/types" From e3df0513603e528fc400aa9f3c55728bc0e4ffaf Mon Sep 17 00:00:00 2001 From: songgaoye Date: Wed, 3 Sep 2025 16:36:12 +0800 Subject: [PATCH 13/19] fix comment --- cmd/chain-maind/app/dump_root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 0b6d95374..0d0fb4fa9 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -113,7 +113,7 @@ func DumpMemIavlRoot(storeNames []string) *cobra.Command { tree := db.TreeByName(capaMemStoreKey) if tree != nil { - fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.Version()) + fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.RootHash()) } else { fmt.Printf("module %s not loaded\n", capaMemStoreKey) } @@ -143,7 +143,7 @@ func convertCommitInfo(commitInfo *memiavl.CommitInfo) *types.CommitInfo { } func DumpIavlRoot(storeNames []string) *cobra.Command { - // this is need to change in different height + // this needs to change in different height // because some StoreKey version are zero // such as consensusparamtypes, circuittypes storeNames = []string{ From 9cafd02c3358c0e34fa7148b48bbfca21d948b81 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Fri, 19 Sep 2025 10:41:51 +0800 Subject: [PATCH 14/19] rm dump memiavl root --- cmd/chain-maind/app/dump_root.go | 77 -------------------------------- 1 file changed, 77 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 0d0fb4fa9..c84f487a6 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -17,7 +17,6 @@ import ( nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types" nfttypes "github.com/crypto-org-chain/chain-main/v4/x/nft/types" supplytypes "github.com/crypto-org-chain/chain-main/v4/x/supply/types" - "github.com/crypto-org-chain/cronos/memiavl" "github.com/spf13/cobra" "cosmossdk.io/log" @@ -61,87 +60,11 @@ func DumpRootGroupCmd(storeNames []string) *cobra.Command { Short: "dump module root", } cmd.AddCommand( - DumpMemIavlRoot(storeNames), DumpIavlRoot(storeNames), ) return cmd } -func DumpMemIavlRoot(storeNames []string) *cobra.Command { - cmd := &cobra.Command{ - Use: "dump-memiavl-root", - Short: "dump mem-iavl root at version [dir]", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - dir := args[0] - version, err := cmd.Flags().GetUint32("version") - if err != nil { - return err - } - opts := memiavl.Options{ - InitialStores: storeNames, - CreateIfMissing: false, - TargetVersion: version, - } - db, err := memiavl.Load(dir, opts) - if err != nil { - return err - } - defer db.Close() - for _, storeName := range storeNames { - tree := db.TreeByName(storeName) - if tree != nil { - fmt.Printf("module %s version %d RootHash %X\n", storeName, tree.Version(), tree.RootHash()) - } else { - fmt.Printf("module %s not loaded\n", storeName) - } - } - - db.MultiTree.UpdateCommitInfo() - lastCommitInfo := convertCommitInfo(db.MultiTree.LastCommitInfo()) - - fmt.Printf("Version %d RootHash %X\n", lastCommitInfo.Version, lastCommitInfo.Hash()) - - if version < ChainMainV6UpgradeHeight { - // if you want to calculate hash same with iavl node - // It has an issue as described in cosmos/cosmos-sdk#14916, so we need to apply a hacky solution to it. - var specialInfo types.StoreInfo - specialInfo.Name = capaMemStoreKey - lastCommitInfo.StoreInfos = append(lastCommitInfo.StoreInfos, specialInfo) - - fmt.Printf("calculate again Last Commit Infos: %v\n", lastCommitInfo) - - tree := db.TreeByName(capaMemStoreKey) - if tree != nil { - fmt.Printf("module %s Version %d RootHash %X\n", capaMemStoreKey, tree.Version(), tree.RootHash()) - } else { - fmt.Printf("module %s not loaded\n", capaMemStoreKey) - } - } - return nil - }, - } - cmd.Flags().Uint32("version", 0, "the version to dump") - return cmd -} - -func convertCommitInfo(commitInfo *memiavl.CommitInfo) *types.CommitInfo { - storeInfos := make([]types.StoreInfo, len(commitInfo.StoreInfos)) - for i, storeInfo := range commitInfo.StoreInfos { - storeInfos[i] = types.StoreInfo{ - Name: storeInfo.Name, - CommitId: types.CommitID{ - Version: storeInfo.CommitId.Version, - Hash: storeInfo.CommitId.Hash, - }, - } - } - return &types.CommitInfo{ - Version: commitInfo.Version, - StoreInfos: storeInfos, - } -} - func DumpIavlRoot(storeNames []string) *cobra.Command { // this needs to change in different height // because some StoreKey version are zero From 42e266422e7b0f88799373c598494114d5330e4c Mon Sep 17 00:00:00 2001 From: songgaoye Date: Fri, 19 Sep 2025 11:06:22 +0800 Subject: [PATCH 15/19] update StoreKey --- cmd/chain-maind/app/dump_root.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index c84f487a6..39c062f01 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -7,11 +7,10 @@ import ( dbm "github.com/cosmos/cosmos-db" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" - icacontrollertypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/types" - icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types" - ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types" - ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" - ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported" + icacontrollertypes "github.com/cosmos/ibc-go/v10/modules/apps/27-interchain-accounts/controller/types" + icahosttypes "github.com/cosmos/ibc-go/v10/modules/apps/27-interchain-accounts/host/types" + ibctransfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types" + ibcexported "github.com/cosmos/ibc-go/v10/modules/core/exported" "github.com/crypto-org-chain/chain-main/v4/app" chainmaintypes "github.com/crypto-org-chain/chain-main/v4/x/chainmain/types" nfttransfertypes "github.com/crypto-org-chain/chain-main/v4/x/nft-transfer/types" @@ -89,7 +88,6 @@ func DumpIavlRoot(storeNames []string) *cobra.Command { authzkeeper.StoreKey, nfttransfertypes.StoreKey, group.StoreKey, - ibcfeetypes.StoreKey, chainmaintypes.StoreKey, supplytypes.StoreKey, // maxsupplytypes.StoreKey, From 64b8ffc3ebec07204110c622311d93ea4b900bf8 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Fri, 19 Sep 2025 12:31:05 +0800 Subject: [PATCH 16/19] fix golangci-lint --- cmd/chain-maind/app/dump_root.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 39c062f01..be63a61e6 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -37,12 +37,6 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) -const ( - capaMemStoreKey = "mem_capability" - - ChainMainV6UpgradeHeight = 24836000 -) - func DumpRootCmd() *cobra.Command { keys, _, _ := app.StoreKeys() storeNames := make([]string, 0, len(keys)) From 19c36df9c68dc3f65b07974a8f6362538a5290b1 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Tue, 23 Sep 2025 15:37:52 +0800 Subject: [PATCH 17/19] use dbm read_only --- cmd/chain-maind/app/dump_root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index be63a61e6..13448c215 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -101,7 +101,7 @@ func DumpIavlRoot(storeNames []string) *cobra.Command { if err != nil { return err } - db, err := dbm.NewGoLevelDB("application", dir, nil) + db, err := dbm.NewGoLevelDB("application", dir, dbm.OptionsMap{"read_only": true}) if err != nil { return err } From 6aedb0b523336743b498c96294de51460e324cf2 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Wed, 24 Sep 2025 09:10:24 +0800 Subject: [PATCH 18/19] update DumpIavlRoot support rocksdb backend comment --- cmd/chain-maind/app/dump_root.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index 13448c215..d74f1cc6d 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -92,8 +92,9 @@ func DumpIavlRoot(storeNames []string) *cobra.Command { } cmd := &cobra.Command{ - Use: "dump-iavl-root", + Use: "dump-iavl-root [dir]", Short: "dump iavl root at version [dir]", + Long: "dump iavl root at version [dir]. To support dumping rocksdb, it should use this https://github.com/cosmos/cosmos-db/blob/9221ee7e2bccf314eff49f89092dd0767588d76e/rocksdb.go#L51.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { dir := args[0] From f7fe206f5b57843da530458f954b48204e7d7414 Mon Sep 17 00:00:00 2001 From: songgaoye Date: Wed, 8 Oct 2025 19:22:30 +0800 Subject: [PATCH 19/19] sort storeNames --- cmd/chain-maind/app/dump_root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/chain-maind/app/dump_root.go b/cmd/chain-maind/app/dump_root.go index d74f1cc6d..5847a7004 100644 --- a/cmd/chain-maind/app/dump_root.go +++ b/cmd/chain-maind/app/dump_root.go @@ -108,6 +108,7 @@ func DumpIavlRoot(storeNames []string) *cobra.Command { } defer db.Close() rs := rootmulti.NewStore(db, log.NewNopLogger(), nil) + sort.Strings(storeNames) for _, storeKey := range storeNames { rs.MountStoreWithDB(types.NewKVStoreKey(storeKey), types.StoreTypeIAVL, nil) }