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

Merge gaiareplay into gaiad to reduce number of binaries #4272

Merged
merged 3 commits into from
May 6, 2019
Merged
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 .pending/breaking/gaia/4272-Merge-gaiarepla
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#4272 Merge gaiareplay functionality into gaiad replay.
Drop `gaiareplay` in favor of new `gaiad replay` command.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cmd/gaia/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
86 changes: 35 additions & 51 deletions cmd/gaia/cmd/gaiareplay/main.go → cmd/gaia/cmd/gaiad/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"time"

"github.com/cosmos/cosmos-sdk/store"

cpm "github.com/otiai10/copy"
"github.com/spf13/cobra"

Expand All @@ -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 <root-dir>",
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
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -115,19 +100,19 @@ 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)

cc := proxy.NewLocalClientCreator(myapp)
proxyApp := proxy.NewAppConns(cc)
err = proxyApp.Start()
if err != nil {
panic(err)
return err
}
defer func() {
_ = proxyApp.Stop()
Expand All @@ -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{
Expand All @@ -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)

Expand All @@ -163,44 +148,43 @@ 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
fmt.Printf("loading and applying block %d\n", i)
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 {
panic(fmt.Sprintf("couldn't find block %d", i))
return fmt.Errorf("couldn't find block %d", i)
}

t2 := time.Now()

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

}