From 9711b7383b87dcce25b2288f28178f5119ba7bf0 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 11 Nov 2024 16:17:46 +0800 Subject: [PATCH] cleanup --- CHANGELOG.md | 1 + app/versiondb.go | 1 + versiondb/client/cmd.go | 1 - versiondb/client/fixdata.go | 83 ------------------------------------ versiondb/tsrocksdb/store.go | 4 +- 5 files changed, 4 insertions(+), 86 deletions(-) delete mode 100644 versiondb/client/fixdata.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b2a0fa24c..3f9a4f7789 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug Fixes * [#1679](https://github.com/crypto-org-chain/cronos/pull/1679) Include no trace detail on insufficient balance fix. +* [#1685](https://github.com/crypto-org-chain/cronos/pull/1685) Fix versiondb corrupted data on startup. ### Improvements diff --git a/app/versiondb.go b/app/versiondb.go index 37b9946539..aaa4602020 100644 --- a/app/versiondb.go +++ b/app/versiondb.go @@ -35,6 +35,7 @@ func (app *App) setupVersionDB( return nil, err } + // see: https://github.com/crypto-org-chain/cronos/issues/1683 if err := versionDB.FixData(exposedKeys); err != nil { return nil, err } diff --git a/versiondb/client/cmd.go b/versiondb/client/cmd.go index c2ffd3324c..48165c4509 100644 --- a/versiondb/client/cmd.go +++ b/versiondb/client/cmd.go @@ -28,7 +28,6 @@ func ChangeSetGroupCmd(opts Options) *cobra.Command { ChangeSetToVersionDBCmd(), RestoreAppDBCmd(opts), RestoreVersionDBCmd(), - FixDataCmd(), ) return cmd } diff --git a/versiondb/client/fixdata.go b/versiondb/client/fixdata.go deleted file mode 100644 index 0802d94df3..0000000000 --- a/versiondb/client/fixdata.go +++ /dev/null @@ -1,83 +0,0 @@ -package client - -import ( - "encoding/binary" - "encoding/hex" - "fmt" - - "github.com/crypto-org-chain/cronos/versiondb/tsrocksdb" - "github.com/linxGnu/grocksdb" - "github.com/spf13/cobra" -) - -const ( - FlagDryRun = "dryrun" - FlagStore = "store" -) - -// DefaultStores is the list of store names in cronos v1.3 -var DefaultStores = []string{ - "acc", "bank", "staking", "mint", "dist", "slashing", "gov", - "params", "upgrade", "evidence", "capability", "consensus", - "feegrant", "crisis", "ibc", "transfer", "feeibc", "icacontroller", - "icahost", "icaauth", "evm", "feemarket", "e2ee", "cronos", -} - -func FixDataCmd() *cobra.Command { - cmd := &cobra.Command{ - Use: "fixdata", - Args: cobra.ExactArgs(1), - Short: "Fix versiondb data for a specific issue, see: https://github.com/crypto-org-chain/cronos/issues/1683", - RunE: func(cmd *cobra.Command, args []string) error { - dir := args[0] - dryRun, err := cmd.Flags().GetBool(FlagDryRun) - if err != nil { - return err - } - - storeNames, err := cmd.Flags().GetStringArray(FlagStore) - if err != nil { - return err - } - - var ( - db *grocksdb.DB - cfHandle *grocksdb.ColumnFamilyHandle - ) - if dryRun { - db, cfHandle, err = tsrocksdb.OpenVersionDBForReadOnly(dir, false) - } else { - db, cfHandle, err = tsrocksdb.OpenVersionDB(dir) - } - if err != nil { - return fmt.Errorf("failed to open versiondb: %w", err) - } - - version := int64(0) - for _, storeName := range storeNames { - store := tsrocksdb.NewStoreWithDB(db, cfHandle) - iter, err := store.IteratorAtVersion(storeName, nil, nil, &version) - if err != nil { - return fmt.Errorf("failed to create iterator: %w", err) - } - for ; iter.Valid(); iter.Next() { - if dryRun { - // print the key value pairs - key := iter.Key() - value := iter.Value() - ts := binary.LittleEndian.Uint64(key[len(key)-8:]) - key = key[:len(key)-8] - fmt.Printf("key: %s, ts: %d, value: %s\n", hex.EncodeToString(key), ts, hex.EncodeToString(value)) - } - } - if err := iter.Close(); err != nil { - return fmt.Errorf("failed to close iterator: %w", err) - } - } - return nil - }, - } - cmd.Flags().Bool(FlagDryRun, false, "Dry run mode, do not modify the database") - cmd.Flags().StringArray(FlagStore, DefaultStores, "List of store names to fix") - return cmd -} diff --git a/versiondb/tsrocksdb/store.go b/versiondb/tsrocksdb/store.go index c3e874ad41..3f9fc6787c 100644 --- a/versiondb/tsrocksdb/store.go +++ b/versiondb/tsrocksdb/store.go @@ -257,6 +257,7 @@ func (s Store) fixDataStore(name string) error { batch := grocksdb.NewWriteBatch() defer batch.Destroy() + prefix := storePrefix(name) for ; iter.Valid(); iter.Next() { key := iter.Key() if len(key) < TimestampSize { @@ -280,8 +281,7 @@ func (s Store) fixDataStore(name string) error { continue } - fmt.Println("[debug] fix key", string(key), "ts", binary.LittleEndian.Uint64(ts)) - batch.PutCFWithTS(s.cfHandle, key, ts, iter.Value()) + batch.PutCFWithTS(s.cfHandle, cloneAppend(prefix, key), ts, iter.Value()) } return s.db.Write(defaultSyncWriteOpts, batch)