github.com/igggame/nebulas-go@v2.1.0+incompatible/cmd/neb/chaincmd.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"fmt"
    23  	"strconv"
    24  
    25  	"bytes"
    26  	"encoding/json"
    27  
    28  	"github.com/nebulasio/go-nebulas/core"
    29  	"github.com/urfave/cli"
    30  )
    31  
    32  var (
    33  	initCommand = cli.Command{
    34  		Action:    MergeFlags(initGenesis),
    35  		Name:      "init",
    36  		Usage:     "Bootstrap and initialize a new genesis block",
    37  		ArgsUsage: "<genesisPath>",
    38  		Category:  "BLOCKCHAIN COMMANDS",
    39  		Description: `
    40  The init command initializes a new genesis block and definition for the network.`,
    41  	}
    42  	genesisCommand = cli.Command{
    43  		Name:     "genesis",
    44  		Usage:    "the genesis block command",
    45  		Category: "BLOCKCHAIN COMMANDS",
    46  		Description: `
    47  The genesis command for genesis dump or other commands.`,
    48  		Subcommands: []cli.Command{
    49  			{
    50  				Name:   "dump",
    51  				Usage:  "dump the genesis",
    52  				Action: MergeFlags(dumpGenesis),
    53  				Description: `
    54     genesis dump
    55  
    56  Dump the genesis config info.`,
    57  			},
    58  		},
    59  	}
    60  
    61  	blockDumpCommand = cli.Command{
    62  		Action:    MergeFlags(dumpblock),
    63  		Name:      "dump",
    64  		Usage:     "Dump the number of newest block before tail block from storage",
    65  		ArgsUsage: "<blocknumber>",
    66  		Category:  "BLOCKCHAIN COMMANDS",
    67  		Description: `
    68  Use "./neb dump 10" to dump 10 blocks before tail block.`,
    69  	}
    70  )
    71  
    72  func initGenesis(ctx *cli.Context) error {
    73  	filePath := ctx.Args().First()
    74  	genesis, err := core.LoadGenesisConf(filePath)
    75  	if err != nil {
    76  		FatalF("load genesis conf faild: %v", err)
    77  	}
    78  
    79  	neb, err := makeNeb(ctx)
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	neb.SetGenesis(genesis)
    85  	neb.Setup()
    86  	return nil
    87  }
    88  
    89  func dumpGenesis(ctx *cli.Context) error {
    90  	neb, err := makeNeb(ctx)
    91  	if err != nil {
    92  		FatalF("dump genesis conf faild: %v", err)
    93  	}
    94  
    95  	neb.Setup()
    96  
    97  	genesis, err := core.DumpGenesis(neb.BlockChain())
    98  	if err != nil {
    99  		FatalF("dump genesis conf faild: %v", err)
   100  	}
   101  	genesisJSON, err := json.Marshal(genesis)
   102  
   103  	var buf bytes.Buffer
   104  	err = json.Indent(&buf, genesisJSON, "", "    ")
   105  	if err != nil {
   106  		FatalF("dump genesis conf faild: %v", err)
   107  	}
   108  	fmt.Println(buf.String())
   109  	return nil
   110  }
   111  
   112  func dumpblock(ctx *cli.Context) error {
   113  	neb, err := makeNeb(ctx)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	neb.Setup()
   119  
   120  	count, err := strconv.Atoi(ctx.Args().First())
   121  	if err != nil {
   122  		return err
   123  	}
   124  	fmt.Printf("blockchain dump: %s\n", neb.BlockChain().Dump(count))
   125  	return nil
   126  }