aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/utils
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-05-27 19:29:34 +0800
committerFelix Lange <fjl@twurst.com>2015-05-28 07:09:26 +0800
commit3b9808f23ca4eb1621a92aad80de5c89269f17fe (patch)
treeb41f86df14e99e1e1cf9cb40deef20c263e7605d /cmd/utils
parent27e0d2a97325edc9a870a747412d0b9a2abd1ed1 (diff)
downloadgo-tangerine-3b9808f23ca4eb1621a92aad80de5c89269f17fe.tar.gz
go-tangerine-3b9808f23ca4eb1621a92aad80de5c89269f17fe.tar.zst
go-tangerine-3b9808f23ca4eb1621a92aad80de5c89269f17fe.zip
cmd/geth, cmd/utils: don't use Ethereum for import, export and upgradedb
The blockchain commands don't need the full stack. With this change, p2p, miner, downloader, etc are no longer started for blockchain operations.
Diffstat (limited to 'cmd/utils')
-rw-r--r--cmd/utils/cmd.go49
-rw-r--r--cmd/utils/flags.go44
2 files changed, 39 insertions, 54 deletions
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
index 39b4e46da..550ac1c51 100644
--- a/cmd/utils/cmd.go
+++ b/cmd/utils/cmd.go
@@ -167,7 +167,7 @@ func FormatTransactionData(data string) []byte {
}
func ImportChain(chainmgr *core.ChainManager, fn string) error {
- fmt.Printf("importing blockchain '%s'\n", fn)
+ glog.Infoln("Importing blockchain", fn)
fh, err := os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
if err != nil {
return err
@@ -176,43 +176,36 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error {
chainmgr.Reset()
stream := rlp.NewStream(fh, 0)
- var i, n int
batchSize := 2500
blocks := make(types.Blocks, batchSize)
-
- for ; ; i++ {
- var b types.Block
- if err := stream.Decode(&b); err == io.EOF {
- break
- } else if err != nil {
- return fmt.Errorf("at block %d: %v", i, err)
- }
-
- blocks[n] = &b
- n++
-
- if n == batchSize {
- if _, err := chainmgr.InsertChain(blocks); err != nil {
- return fmt.Errorf("invalid block %v", err)
+ n := 0
+ for {
+ // Load a batch of RLP blocks.
+ i := 0
+ for ; i < batchSize; i++ {
+ var b types.Block
+ if err := stream.Decode(&b); err == io.EOF {
+ break
+ } else if err != nil {
+ return fmt.Errorf("at block %d: %v", n, err)
}
- n = 0
- blocks = make(types.Blocks, batchSize)
+ blocks[i] = &b
+ n++
}
- }
-
- if n > 0 {
- if _, err := chainmgr.InsertChain(blocks[:n]); err != nil {
- return fmt.Errorf("invalid block %v", err)
+ if i == 0 {
+ break
+ }
+ // Import the batch.
+ if _, err := chainmgr.InsertChain(blocks[:i]); err != nil {
+ return fmt.Errorf("invalid block %d: %v", n, err)
}
}
-
- fmt.Printf("imported %d blocks\n", i)
return nil
}
func ExportChain(chainmgr *core.ChainManager, fn string) error {
- fmt.Printf("exporting blockchain '%s'\n", fn)
+ glog.Infoln("Exporting blockchain to", fn)
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
@@ -221,6 +214,6 @@ func ExportChain(chainmgr *core.ChainManager, fn string) error {
if err := chainmgr.Export(fh); err != nil {
return err
}
- fmt.Printf("exported blockchain\n")
+ glog.Infoln("Exported blockchain to", fn)
return nil
}
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 155110ddc..176a546f1 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -283,20 +283,10 @@ func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
}
func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
- // Set verbosity on glog
- glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
- glog.CopyStandardLogTo("INFO")
- // Set the log type
- //glog.SetToStderr(ctx.GlobalBool(LogToStdErrFlag.Name))
- glog.SetToStderr(true)
- // Set the log dir
- glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
-
customName := ctx.GlobalString(IdentityFlag.Name)
if len(customName) > 0 {
clientID += "/" + customName
}
-
return &eth.Config{
Name: common.MakeName(clientID, version),
DataDir: ctx.GlobalString(DataDirFlag.Name),
@@ -327,32 +317,34 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
}
}
-func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Database) {
- dataDir := ctx.GlobalString(DataDirFlag.Name)
+// SetupLogger configures glog from the logging-related command line flags.
+func SetupLogger(ctx *cli.Context) {
+ glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
+ glog.CopyStandardLogTo("INFO")
+ glog.SetToStderr(true)
+ glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
+}
- blockDb, err := ethdb.NewLDBDatabase(filepath.Join(dataDir, "blockchain"))
- if err != nil {
+func GetChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) {
+ dd := ctx.GlobalString(DataDirFlag.Name)
+ var err error
+ if blockDB, err = ethdb.NewLDBDatabase(filepath.Join(dd, "blockchain")); err != nil {
Fatalf("Could not open database: %v", err)
}
-
- stateDb, err := ethdb.NewLDBDatabase(filepath.Join(dataDir, "state"))
- if err != nil {
+ if stateDB, err = ethdb.NewLDBDatabase(filepath.Join(dd, "state")); err != nil {
Fatalf("Could not open database: %v", err)
}
-
- extraDb, err := ethdb.NewLDBDatabase(filepath.Join(dataDir, "extra"))
- if err != nil {
+ if extraDB, err = ethdb.NewLDBDatabase(filepath.Join(dd, "extra")); err != nil {
Fatalf("Could not open database: %v", err)
}
eventMux := new(event.TypeMux)
pow := ethash.New()
- chainManager := core.NewChainManager(blockDb, stateDb, pow, eventMux)
- txPool := core.NewTxPool(eventMux, chainManager.State, chainManager.GasLimit)
- blockProcessor := core.NewBlockProcessor(stateDb, extraDb, pow, txPool, chainManager, eventMux)
- chainManager.SetProcessor(blockProcessor)
-
- return chainManager, blockDb, stateDb
+ chain = core.NewChainManager(blockDB, stateDB, pow, eventMux)
+ txpool := core.NewTxPool(eventMux, chain.State, chain.GasLimit)
+ proc := core.NewBlockProcessor(stateDB, extraDB, pow, txpool, chain, eventMux)
+ chain.SetProcessor(proc)
+ return chain, blockDB, stateDB, extraDB
}
func GetAccountManager(ctx *cli.Context) *accounts.Manager {