Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add func to copy data #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build
snapshots/
here/
*/data/
*.lz4
102 changes: 96 additions & 6 deletions cmd/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/tendermint/tendermint/state"
tmstore "github.com/tendermint/tendermint/store"
tmtypes "github.com/tendermint/tendermint/types"
db "github.com/tendermint/tm-db"

"github.com/binaryholdings/cosmos-pruner/internal/rootmulti"
Expand All @@ -46,12 +47,13 @@ func pruneCmd() *cobra.Command {
errs, _ := errgroup.WithContext(ctx)
var err error
if tendermint {
errs.Go(func() error {
if err = pruneTMData(args[0]); err != nil {
return err
}
return nil
})
// errs.Go(func() error {
// if err = pruneTMData(args[0]); err != nil {
// return err
// }
// return nil
// })
copyTMData(args[0])
}

if cosmosSdk {
Expand Down Expand Up @@ -201,6 +203,94 @@ func pruneTMData(home string) error {
return nil
}

// pruneTMData prunes the tendermint blocks and state based on the amount of blocks to keep
func copyTMData(home string) error {

dbDir := rootify(dataDir, home)

o := opt.Options{
DisableSeeksCompaction: true,
}

// Get BlockStore
blockStoreDB, err := db.NewGoLevelDBWithOpts("blockstore", dbDir, &o)
if err != nil {
return err
}
blockStore := tmstore.NewBlockStore(blockStoreDB)

// Get StateStore
stateDB, err := db.NewGoLevelDBWithOpts("state", dbDir, &o)
if err != nil {
return err
}

stateStore := state.NewStore(stateDB)

latestState, err := stateStore.Load()
if err != nil {
return err
}
SeenCommit := blockStore.LoadSeenCommit(latestState.LastBlockHeight)
latestBlock := blockStore.LoadBlock(latestState.LastBlockHeight)

// blockStoreDB.Close()
stateDB.Close()

//create a new db to copy over the latest state

// Get BlockStore
newBlockStoreDB, err := db.NewGoLevelDBWithOpts("new_blockstore", dbDir, &o)
if err != nil {
return err
}
newBlockStore := tmstore.NewBlockStore(newBlockStoreDB)

// Get StateStore
newStateDB, err := db.NewGoLevelDBWithOpts("new_state", dbDir, &o)
if err != nil {
return err
}

newStateStore := state.NewStore(newStateDB)

newStateStore.Bootstrap(latestState)

newBlockStore.SaveSeenCommit(latestState.LastBlockHeight, SeenCommit)

parts := latestBlock.MakePartSet(tmtypes.BlockPartSizeBytes)
newBlockStore.SaveBlock(latestBlock, parts, SeenCommit)

newBlockStoreDB.Close()
newStateDB.Close()

// var (
// oldBlocks = dbDir + "/blockstore.db"
// oldState = dbDir + "/state.db"
// newBlocks = dbDir + "/new_blockstore.db"
// newState = dbDir + "/new_state.db"
// )

// err = os.RemoveAll(oldBlocks)
// if err != nil {
// panic(err)
// }
// err = os.RemoveAll(oldState)
// if err != nil {
// panic(err)
// }
// err = os.Rename(newBlocks, oldBlocks)
// if err != nil {
// panic(err)
// }
// err = os.Rename(newState, oldState)
// if err != nil {
// panic(err)
// }

return nil
}

// Utils

func rootify(path, root string) string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewRootCmd() *cobra.Command {
}

// --cosmos-sdk flag
rootCmd.PersistentFlags().BoolVar(&cosmosSdk, "cosmos-sdk", true, "set t`o false if using only with tendermint (default true)")
rootCmd.PersistentFlags().BoolVar(&cosmosSdk, "cosmos-sdk", false, "set t`o false if using only with tendermint (default true)")
if err := viper.BindPFlag("cosmos-sdk", rootCmd.PersistentFlags().Lookup("cosmos-sdk")); err != nil {
panic(err)
}
Expand Down