From 5145fabef748e249d7325511c7ba61ef728be9de Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 5 May 2019 17:54:38 +0100 Subject: [PATCH 1/3] Merge gaiareplay into gaiad to reduce number of binaries --- Makefile | 2 - cmd/gaia/Makefile | 2 - cmd/gaia/cmd/gaiad/main.go | 1 + .../{gaiareplay/main.go => gaiad/replay.go} | 84 ++++++++----------- 4 files changed, 36 insertions(+), 53 deletions(-) rename cmd/gaia/cmd/{gaiareplay/main.go => gaiad/replay.go} (77%) diff --git a/Makefile b/Makefile index 5418e0f4d174..f1d979b3ea4d 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,6 @@ ifeq ($(OS),Windows_NT) else go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad ./cmd/gaia/cmd/gaiad go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaia/cmd/gaiacli - go build -mod=readonly $(BUILD_FLAGS) -o build/gaiareplay ./cmd/gaia/cmd/gaiareplay endif build-linux: go.sum @@ -93,7 +92,6 @@ update_gaia_lite_docs: install: go.sum check-ledger update_gaia_lite_docs go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiad go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiacli - go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiareplay install_debug: go.sum go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug diff --git a/cmd/gaia/Makefile b/cmd/gaia/Makefile index 8715aa75bd2f..bc57dcc78411 100644 --- a/cmd/gaia/Makefile +++ b/cmd/gaia/Makefile @@ -72,7 +72,6 @@ ifeq ($(OS),Windows_NT) else go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad ../../cmd/gaia/cmd/gaiad go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli ../../cmd/gaia/cmd/gaiacli - go build -mod=readonly $(BUILD_FLAGS) -o build/gaiareplay ../../cmd/gaia/cmd/gaiareplay endif build-linux: ../../go.sum @@ -81,7 +80,6 @@ build-linux: ../../go.sum install: ../../go.sum check-ledger go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiad go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiacli - go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiareplay install-debug: ../../go.sum go install -mod=readonly $(BUILD_FLAGS) ../../cmd/gaia/cmd/gaiadebug diff --git a/cmd/gaia/cmd/gaiad/main.go b/cmd/gaia/cmd/gaiad/main.go index 4f636d305e0f..d9f319f302bb 100644 --- a/cmd/gaia/cmd/gaiad/main.go +++ b/cmd/gaia/cmd/gaiad/main.go @@ -51,6 +51,7 @@ func main() { rootCmd.AddCommand(gaiaInit.AddGenesisAccountCmd(ctx, cdc)) rootCmd.AddCommand(gaiaInit.ValidateGenesisCmd(ctx, cdc)) rootCmd.AddCommand(client.NewCompletionCmd(rootCmd, true)) + rootCmd.AddCommand(replayCmd()) server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators) diff --git a/cmd/gaia/cmd/gaiareplay/main.go b/cmd/gaia/cmd/gaiad/replay.go similarity index 77% rename from cmd/gaia/cmd/gaiareplay/main.go rename to cmd/gaia/cmd/gaiad/replay.go index d41a65c7ebbe..43a095fbd6c2 100644 --- a/cmd/gaia/cmd/gaiareplay/main.go +++ b/cmd/gaia/cmd/gaiad/replay.go @@ -7,8 +7,6 @@ import ( "path/filepath" "time" - "github.com/cosmos/cosmos-sdk/store" - cpm "github.com/otiai10/copy" "github.com/spf13/cobra" @@ -22,46 +20,33 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" ) -var ( - rootDir string -) - -var rootCmd = &cobra.Command{ - Use: "gaiareplay", - Short: "Replay gaia transactions", - Run: func(cmd *cobra.Command, args []string) { - run(rootDir) - }, -} - -func init() { - // cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&rootDir, "root", "r", "root dir") -} - -func main() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) +func replayCmd() *cobra.Command { + return &cobra.Command{ + Use: "replay ", + Short: "Replay gaia transactions", + RunE: func(_ *cobra.Command, args []string) error { + return replayTxs(args[0]) + }, + Args: cobra.ExactArgs(1), } } -func run(rootDir string) { +func replayTxs(rootDir string) error { if false { // Copy the rootDir to a new directory, to preserve the old one. - fmt.Println("Copying rootdir over") + fmt.Fprintln(os.Stderr, "Copying rootdir over") oldRootDir := rootDir rootDir = oldRootDir + "_replay" if cmn.FileExists(rootDir) { cmn.Exit(fmt.Sprintf("temporary copy dir %v already exists", rootDir)) } - err := cpm.Copy(oldRootDir, rootDir) - if err != nil { - panic(err) + if err := cpm.Copy(oldRootDir, rootDir); err != nil { + return err } } @@ -71,25 +56,25 @@ func run(rootDir string) { // App DB // appDB := dbm.NewMemDB() - fmt.Println("Opening app database") + fmt.Fprintln(os.Stderr, "Opening app database") appDB, err := sdk.NewLevelDB("application", dataDir) if err != nil { - panic(err) + return err } // TM DB // tmDB := dbm.NewMemDB() - fmt.Println("Opening tendermint state database") + fmt.Fprintln(os.Stderr, "Opening tendermint state database") tmDB, err := sdk.NewLevelDB("state", dataDir) if err != nil { - panic(err) + return err } // Blockchain DB - fmt.Println("Opening blockstore database") + fmt.Fprintln(os.Stderr, "Opening blockstore database") bcDB, err := sdk.NewLevelDB("blockstore", dataDir) if err != nil { - panic(err) + return err } // TraceStore @@ -101,11 +86,11 @@ func run(rootDir string) { 0666, ) if err != nil { - panic(err) + return err } // Application - fmt.Println("Creating application") + fmt.Fprintln(os.Stderr, "Creating application") myapp := app.NewGaiaApp( ctx.Logger, appDB, traceStoreWriter, true, uint(1), baseapp.SetPruning(store.PruneEverything), // nothing @@ -115,11 +100,11 @@ func run(rootDir string) { var genDocPath = filepath.Join(configDir, "genesis.json") genDoc, err := tm.GenesisDocFromFile(genDocPath) if err != nil { - panic(err) + return err } genState, err := tmsm.MakeGenesisState(genDoc) if err != nil { - panic(err) + return err } // tmsm.SaveState(tmDB, genState) @@ -127,7 +112,7 @@ func run(rootDir string) { proxyApp := proxy.NewAppConns(cc) err = proxyApp.Start() if err != nil { - panic(err) + return err } defer func() { _ = proxyApp.Stop() @@ -136,7 +121,7 @@ func run(rootDir string) { state := tmsm.LoadState(tmDB) if state.LastBlockHeight == 0 { // Send InitChain msg - fmt.Println("Sending InitChain msg") + fmt.Fprintln(os.Stderr, "Sending InitChain msg") validators := tm.TM2PB.ValidatorUpdates(genState.Validators) csParams := tm.TM2PB.ConsensusParams(genDoc.ConsensusParams) req := abci.RequestInitChain{ @@ -148,11 +133,11 @@ func run(rootDir string) { } res, err := proxyApp.Consensus().InitChainSync(req) if err != nil { - panic(err) + return err } newValidatorz, err := tm.PB2TM.ValidatorUpdates(res.Validators) if err != nil { - panic(err) + return err } newValidators := tm.NewValidatorSet(newValidatorz) @@ -163,17 +148,17 @@ func run(rootDir string) { } // Create executor - fmt.Println("Creating block executor") + fmt.Fprintln(os.Stderr, "Creating block executor") blockExec := tmsm.NewBlockExecutor(tmDB, ctx.Logger, proxyApp.Consensus(), tmsm.MockMempool{}, tmsm.MockEvidencePool{}) // Create block store - fmt.Println("Creating block store") + fmt.Fprintln(os.Stderr, "Creating block store") blockStore := bcm.NewBlockStore(bcDB) tz := []time.Duration{0, 0, 0} for i := int(state.LastBlockHeight) + 1; ; i++ { - fmt.Println("Running block ", i) + fmt.Fprintln(os.Stderr, "Running block ", i) t1 := time.Now() // Apply block @@ -181,7 +166,7 @@ func run(rootDir string) { blockmeta := blockStore.LoadBlockMeta(int64(i)) if blockmeta == nil { fmt.Printf("Couldn't find block meta %d... done?\n", i) - return + return nil } block := blockStore.LoadBlock(int64(i)) if block == nil { @@ -192,15 +177,16 @@ func run(rootDir string) { state, err = blockExec.ApplyBlock(state, blockmeta.BlockID, block) if err != nil { - panic(err) + return err } t3 := time.Now() tz[0] += t2.Sub(t1) tz[1] += t3.Sub(t2) - fmt.Printf("new app hash: %X\n", state.AppHash) - fmt.Println(tz) + fmt.Fprintf(os.Stderr, "new app hash: %X\n", state.AppHash) + fmt.Fprintln(os.Stderr, tz) } + return nil } From 8f971eaa149c3c6b954f15b28a6b394f2490ffd3 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 5 May 2019 17:58:15 +0100 Subject: [PATCH 2/3] Add pending entry --- .pending/breaking/gaia/4272-Merge-gaiarepla | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .pending/breaking/gaia/4272-Merge-gaiarepla diff --git a/.pending/breaking/gaia/4272-Merge-gaiarepla b/.pending/breaking/gaia/4272-Merge-gaiarepla new file mode 100644 index 000000000000..5aecf7bc16a4 --- /dev/null +++ b/.pending/breaking/gaia/4272-Merge-gaiarepla @@ -0,0 +1,2 @@ +#4272 Merge gaiareplay functionality into gaiad replay. +Drop `gaiareplay` in favor of new `gaiad replay` command. From 97e4e2588d9e884d8dd8930849c126dfa1ba3134 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sun, 5 May 2019 18:00:18 +0100 Subject: [PATCH 3/3] Make govet happy --- cmd/gaia/cmd/gaiad/replay.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cmd/gaia/cmd/gaiad/replay.go b/cmd/gaia/cmd/gaiad/replay.go index 43a095fbd6c2..4511ff21bc6c 100644 --- a/cmd/gaia/cmd/gaiad/replay.go +++ b/cmd/gaia/cmd/gaiad/replay.go @@ -170,7 +170,7 @@ func replayTxs(rootDir string) error { } block := blockStore.LoadBlock(int64(i)) if block == nil { - panic(fmt.Sprintf("couldn't find block %d", i)) + return fmt.Errorf("couldn't find block %d", i) } t2 := time.Now() @@ -187,6 +187,4 @@ func replayTxs(rootDir string) error { fmt.Fprintf(os.Stderr, "new app hash: %X\n", state.AppHash) fmt.Fprintln(os.Stderr, tz) } - - return nil }