Jae/gaiareplay3 (#3032)

gaiareplay tool
This commit is contained in:
Jae Kwon 2018-12-07 03:19:49 -08:00 committed by GitHub
parent 49da96bc09
commit 2473b74d86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 221 additions and 3 deletions

16
Gopkg.lock generated
View File

@ -247,6 +247,13 @@
revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe"
version = "v1.1.2"
[[projects]]
digest = "1:6d4d9067e639d96a804ee9a30fa89c7233c5c5edbfe736072ff00d48c97bccc4"
name = "github.com/otiai10/copy"
packages = ["."]
pruneopts = "UT"
revision = "7e9a647135a142c2669943d4a4d29be015ce9392"
[[projects]]
digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e"
name = "github.com/pelletier/go-toml"
@ -272,7 +279,7 @@
version = "v1.0.0"
[[projects]]
digest = "1:26663fafdea73a38075b07e8e9d82fc0056379d2be8bb4e13899e8fda7c7dd23"
digest = "1:93a746f1060a8acbcf69344862b2ceced80f854170e1caae089b2834c5fbf7f4"
name = "github.com/prometheus/client_golang"
packages = [
"prometheus",
@ -280,8 +287,8 @@
"prometheus/promhttp",
]
pruneopts = "UT"
revision = "abad2d1bd44235a26707c172eab6bca5bf2dbad3"
version = "v0.9.1"
revision = "505eaef017263e299324067d40ca2c48f6a2cf50"
version = "v0.9.2"
[[projects]]
branch = "master"
@ -662,6 +669,7 @@
"github.com/golang/protobuf/proto",
"github.com/gorilla/mux",
"github.com/mattn/go-isatty",
"github.com/otiai10/copy",
"github.com/pelletier/go-toml",
"github.com/pkg/errors",
"github.com/rakyll/statik/fs",
@ -675,6 +683,7 @@
"github.com/tendermint/iavl",
"github.com/tendermint/tendermint/abci/server",
"github.com/tendermint/tendermint/abci/types",
"github.com/tendermint/tendermint/blockchain",
"github.com/tendermint/tendermint/cmd/tendermint/commands",
"github.com/tendermint/tendermint/config",
"github.com/tendermint/tendermint/crypto",
@ -703,6 +712,7 @@
"github.com/tendermint/tendermint/rpc/core/types",
"github.com/tendermint/tendermint/rpc/lib/client",
"github.com/tendermint/tendermint/rpc/lib/server",
"github.com/tendermint/tendermint/state",
"github.com/tendermint/tendermint/types",
"github.com/tendermint/tendermint/types/time",
"github.com/tendermint/tendermint/version",

View File

@ -26,6 +26,10 @@
name = "github.com/stretchr/testify"
version = "=1.2.1"
[[constraint]]
name = "github.com/otiai10/copy"
revision = "7e9a647135a142c2669943d4a4d29be015ce9392"
[[override]]
name = "github.com/tendermint/go-amino"
version = "v0.14.1"

View File

@ -52,6 +52,7 @@ ifeq ($(OS),Windows_NT)
else
go build $(BUILD_FLAGS) -o build/gaiad ./cmd/gaia/cmd/gaiad
go build $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaia/cmd/gaiacli
go build $(BUILD_FLAGS) -o build/gaiareplay ./cmd/gaia/cmd/gaiareplay
endif
build-linux:
@ -83,6 +84,7 @@ endif
install: check-ledger update_gaia_lite_docs
go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiad
go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiacli
go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiareplay
install_examples:
go install $(BUILD_FLAGS) ./docs/examples/basecoin/cmd/basecoind

View File

@ -0,0 +1,202 @@
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
cpm "github.com/otiai10/copy"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"
bcm "github.com/tendermint/tendermint/blockchain"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/proxy"
tmsm "github.com/tendermint/tendermint/state"
tm "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/server"
)
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 run(rootDir string) {
if false {
// Copy the rootDir to a new directory, to preserve the old one.
fmt.Println("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)
}
}
configDir := filepath.Join(rootDir, "config")
dataDir := filepath.Join(rootDir, "data")
ctx := server.NewDefaultContext()
// App DB
// appDB := dbm.NewMemDB()
fmt.Println("Opening app database")
appDB, err := dbm.NewGoLevelDB("application", dataDir)
if err != nil {
panic(err)
}
// TM DB
// tmDB := dbm.NewMemDB()
fmt.Println("Opening tendermint state database")
tmDB, err := dbm.NewGoLevelDB("state", dataDir)
if err != nil {
panic(err)
}
// Blockchain DB
fmt.Println("Opening blockstore database")
bcDB, err := dbm.NewGoLevelDB("blockstore", dataDir)
if err != nil {
panic(err)
}
// TraceStore
var traceStoreWriter io.Writer
var traceStoreDir = filepath.Join(dataDir, "trace.log")
traceStoreWriter, err = os.OpenFile(
traceStoreDir,
os.O_WRONLY|os.O_APPEND|os.O_CREATE,
0666,
)
if err != nil {
panic(err)
}
// Application
fmt.Println("Creating application")
myapp := app.NewGaiaApp(
ctx.Logger, appDB, traceStoreWriter,
baseapp.SetPruning("everything"), // nothing
)
// Genesis
var genDocPath = filepath.Join(configDir, "genesis.json")
genDoc, err := tm.GenesisDocFromFile(genDocPath)
if err != nil {
panic(err)
}
genState, err := tmsm.MakeGenesisState(genDoc)
if err != nil {
panic(err)
}
// tmsm.SaveState(tmDB, genState)
cc := proxy.NewLocalClientCreator(myapp)
proxyApp := proxy.NewAppConns(cc)
err = proxyApp.Start()
if err != nil {
panic(err)
}
defer proxyApp.Stop()
var state tmsm.State = tmsm.LoadState(tmDB)
if state.LastBlockHeight == 0 {
// Send InitChain msg
fmt.Println("Sending InitChain msg")
validators := tm.TM2PB.ValidatorUpdates(genState.Validators)
csParams := tm.TM2PB.ConsensusParams(genDoc.ConsensusParams)
req := abci.RequestInitChain{
Time: genDoc.GenesisTime,
ChainId: genDoc.ChainID,
ConsensusParams: csParams,
Validators: validators,
AppStateBytes: genDoc.AppState,
}
res, err := proxyApp.Consensus().InitChainSync(req)
if err != nil {
panic(err)
}
newValidatorz, err := tm.PB2TM.ValidatorUpdates(res.Validators)
if err != nil {
panic(err)
}
newValidators := tm.NewValidatorSet(newValidatorz)
// Take the genesis state.
state = genState
state.Validators = newValidators
state.NextValidators = newValidators
}
// Create executor
fmt.Println("Creating block executor")
blockExec := tmsm.NewBlockExecutor(tmDB, ctx.Logger, proxyApp.Consensus(),
tmsm.MockMempool{}, tmsm.MockEvidencePool{})
// Create block store
fmt.Println("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)
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
}
block := blockStore.LoadBlock(int64(i))
if block == nil {
panic(fmt.Sprintf("couldn't find block %d", i))
}
t2 := time.Now()
state, err = blockExec.ApplyBlock(state, blockmeta.BlockID, block)
if err != nil {
panic(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)
}
}