github.com/klaytn/klaytn@v1.10.2/cmd/homi/extra/cmd.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // Copyright 2017 AMIS Technologies
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser 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-ethereum 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 Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package extra
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  
    24  	"gopkg.in/urfave/cli.v1"
    25  
    26  	"github.com/klaytn/klaytn/common"
    27  	"github.com/naoina/toml"
    28  )
    29  
    30  var ExtraCommand = cli.Command{
    31  	Name:  "extra",
    32  	Usage: "Istanbul extraData manipulation",
    33  	Subcommands: []cli.Command{
    34  		{
    35  			Action:    decode,
    36  			Name:      "decode",
    37  			Usage:     "To decode an Istanbul extraData",
    38  			ArgsUsage: "<extra data>",
    39  			Flags: []cli.Flag{
    40  				extraDataFlag,
    41  			},
    42  			Description: `
    43  		This command decodes extraData to vanity and validators.
    44  		`,
    45  		},
    46  		{
    47  			Action:    encode,
    48  			Name:      "encode",
    49  			Usage:     "To encode an Istanbul extraData",
    50  			ArgsUsage: "<config file> or \"0xValidator1,0xValidator2...\"",
    51  			Flags: []cli.Flag{
    52  				configFlag,
    53  				validatorsFlag,
    54  				vanityFlag,
    55  			},
    56  			Description: `
    57  		This command encodes vanity and validators to extraData. Please refer to example/config.toml.
    58  		`,
    59  		},
    60  	},
    61  }
    62  
    63  func encode(ctx *cli.Context) error {
    64  	path := ctx.String(configFlag.Name)
    65  	validators := ctx.String(validatorsFlag.Name)
    66  	if len(path) == 0 && len(validators) == 0 {
    67  		return cli.NewExitError("Must supply config file or enter validators", 0)
    68  	}
    69  
    70  	if len(path) != 0 {
    71  		extraData, err := fromConfig(path)
    72  		if err != nil {
    73  			return cli.NewExitError("Failed to encode from config data", 0)
    74  		}
    75  		fmt.Println("Encoded Istanbul extra-data:", extraData)
    76  	}
    77  
    78  	if len(validators) != 0 {
    79  		extraData, err := fromRawData(ctx.String(vanityFlag.Name), validators)
    80  		if err != nil {
    81  			return cli.NewExitError("Failed to encode from flags", 0)
    82  		}
    83  		fmt.Println("Encoded Istanbul extra-data:", extraData)
    84  	}
    85  	return nil
    86  }
    87  
    88  func fromRawData(vanity string, validators string) (string, error) {
    89  	vs := splitAndTrim(validators)
    90  
    91  	addrs := make([]common.Address, len(vs))
    92  	for i, v := range vs {
    93  		addrs[i] = common.HexToAddress(v)
    94  	}
    95  	return Encode(vanity, addrs)
    96  }
    97  
    98  func fromConfig(path string) (string, error) {
    99  	file, err := os.Open(path)
   100  	if err != nil {
   101  		return "", cli.NewExitError(fmt.Sprintf("Failed to read config file: %v", err), 1)
   102  	}
   103  	defer file.Close()
   104  
   105  	var config struct {
   106  		Vanity     string
   107  		Validators []common.Address
   108  	}
   109  
   110  	if err := toml.NewDecoder(file).Decode(&config); err != nil {
   111  		return "", cli.NewExitError(fmt.Sprintf("Failed to parse config file: %v", err), 2)
   112  	}
   113  
   114  	return Encode(config.Vanity, config.Validators)
   115  }
   116  
   117  func decode(ctx *cli.Context) error {
   118  	if !ctx.IsSet(extraDataFlag.Name) {
   119  		return cli.NewExitError("Must supply extra data", 10)
   120  	}
   121  
   122  	extraString := ctx.String(extraDataFlag.Name)
   123  	vanity, istanbulExtra, err := Decode(extraString)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	fmt.Println("vanity: ", "0x"+common.Bytes2Hex(vanity))
   129  
   130  	for _, v := range istanbulExtra.Validators {
   131  		fmt.Println("validator: ", v.Hex())
   132  	}
   133  
   134  	if len(istanbulExtra.Seal) != 0 {
   135  		fmt.Println("seal:", "0x"+common.Bytes2Hex(istanbulExtra.Seal))
   136  	}
   137  
   138  	for _, seal := range istanbulExtra.CommittedSeal {
   139  		fmt.Println("committed seal: ", "0x"+common.Bytes2Hex(seal))
   140  	}
   141  
   142  	return nil
   143  }