cmd/geth, cmd/utils: init/removedb on light/full dbs simultaneously
This commit is contained in:
parent
02fa3e3179
commit
181a3309df
@ -103,17 +103,14 @@ Use "ethereum dump 0" to dump the genesis block.
|
|||||||
// initGenesis will initialise the given JSON format genesis file and writes it as
|
// initGenesis will initialise the given JSON format genesis file and writes it as
|
||||||
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
|
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
|
||||||
func initGenesis(ctx *cli.Context) error {
|
func initGenesis(ctx *cli.Context) error {
|
||||||
|
// Make sure we have a valid genesis JSON
|
||||||
genesisPath := ctx.Args().First()
|
genesisPath := ctx.Args().First()
|
||||||
if len(genesisPath) == 0 {
|
if len(genesisPath) == 0 {
|
||||||
utils.Fatalf("must supply path to genesis JSON file")
|
utils.Fatalf("Must supply path to genesis JSON file")
|
||||||
}
|
}
|
||||||
|
|
||||||
stack := makeFullNode(ctx)
|
|
||||||
chaindb := utils.MakeChainDatabase(ctx, stack)
|
|
||||||
|
|
||||||
file, err := os.Open(genesisPath)
|
file, err := os.Open(genesisPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("failed to read genesis file: %v", err)
|
utils.Fatalf("Failed to read genesis file: %v", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
@ -121,12 +118,19 @@ func initGenesis(ctx *cli.Context) error {
|
|||||||
if err := json.NewDecoder(file).Decode(genesis); err != nil {
|
if err := json.NewDecoder(file).Decode(genesis); err != nil {
|
||||||
utils.Fatalf("invalid genesis file: %v", err)
|
utils.Fatalf("invalid genesis file: %v", err)
|
||||||
}
|
}
|
||||||
|
// Open an initialise both full and light databases
|
||||||
|
stack := makeFullNode(ctx)
|
||||||
|
for _, name := range []string{"chaindata", "lightchaindata"} {
|
||||||
|
chaindb, err := stack.OpenDatabase(name, 0, 0)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to open database: %v", err)
|
||||||
|
}
|
||||||
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
|
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("failed to write genesis block: %v", err)
|
utils.Fatalf("Failed to write genesis block: %v", err)
|
||||||
|
}
|
||||||
|
log.Info("Successfully wrote genesis state", "database", name, "hash", hash)
|
||||||
}
|
}
|
||||||
log.Info("Successfully wrote genesis state", "hash", hash)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,24 +249,29 @@ func exportChain(ctx *cli.Context) error {
|
|||||||
|
|
||||||
func removeDB(ctx *cli.Context) error {
|
func removeDB(ctx *cli.Context) error {
|
||||||
stack, _ := makeConfigNode(ctx)
|
stack, _ := makeConfigNode(ctx)
|
||||||
dbdir := stack.ResolvePath(utils.ChainDbName(ctx))
|
|
||||||
if !common.FileExist(dbdir) {
|
|
||||||
fmt.Println(dbdir, "does not exist")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for _, name := range []string{"chaindata", "lightchaindata"} {
|
||||||
|
// Ensure the database exists in the first place
|
||||||
|
logger := log.New("database", name)
|
||||||
|
|
||||||
|
dbdir := stack.ResolvePath(name)
|
||||||
|
if !common.FileExist(dbdir) {
|
||||||
|
logger.Info("Database doesn't exist, skipping", "path", dbdir)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Confirm removal and execute
|
||||||
fmt.Println(dbdir)
|
fmt.Println(dbdir)
|
||||||
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
|
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
|
||||||
switch {
|
switch {
|
||||||
case err != nil:
|
case err != nil:
|
||||||
utils.Fatalf("%v", err)
|
utils.Fatalf("%v", err)
|
||||||
case !confirm:
|
case !confirm:
|
||||||
fmt.Println("Operation aborted")
|
logger.Warn("Database deletion aborted")
|
||||||
default:
|
default:
|
||||||
fmt.Println("Removing...")
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
os.RemoveAll(dbdir)
|
os.RemoveAll(dbdir)
|
||||||
fmt.Printf("Removed in %v\n", time.Since(start))
|
logger.Info("Database successfully deleted", "elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -908,22 +908,16 @@ func SetupNetwork(ctx *cli.Context) {
|
|||||||
params.TargetGasLimit = new(big.Int).SetUint64(ctx.GlobalUint64(TargetGasLimitFlag.Name))
|
params.TargetGasLimit = new(big.Int).SetUint64(ctx.GlobalUint64(TargetGasLimitFlag.Name))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChainDbName(ctx *cli.Context) string {
|
|
||||||
if ctx.GlobalBool(LightModeFlag.Name) {
|
|
||||||
return "lightchaindata"
|
|
||||||
} else {
|
|
||||||
return "chaindata"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
|
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
|
||||||
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
|
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
|
||||||
var (
|
var (
|
||||||
cache = ctx.GlobalInt(CacheFlag.Name)
|
cache = ctx.GlobalInt(CacheFlag.Name)
|
||||||
handles = makeDatabaseHandles()
|
handles = makeDatabaseHandles()
|
||||||
name = ChainDbName(ctx)
|
|
||||||
)
|
)
|
||||||
|
name := "chaindata"
|
||||||
|
if ctx.GlobalBool(LightModeFlag.Name) {
|
||||||
|
name = "lightchaindata"
|
||||||
|
}
|
||||||
chainDb, err := stack.OpenDatabase(name, cache, handles)
|
chainDb, err := stack.OpenDatabase(name, cache, handles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Could not open database: %v", err)
|
Fatalf("Could not open database: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user