Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions cmd/chain-maind/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
198 changes: 198 additions & 0 deletions cmd/chain-maind/app/dump_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package app

import (
"bytes"
"fmt"
"sort"

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"

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())

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
},
}
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.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
}
Loading