cmd/istanbul, container, genesis: add subcommand 'extra'

This commit is contained in:
Alan Chen 2017-08-31 10:56:20 +08:00
parent 5e00f9837c
commit 0ffd9f3882
8 changed files with 105 additions and 67 deletions

View File

@ -14,51 +14,55 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package main
package extra
import (
"fmt"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
"github.com/naoina/toml"
"github.com/urfave/cli"
)
var (
decodeCommand = cli.Command{
ExtraCommand = cli.Command{
Name: "extra",
Usage: "Istanbul extraData manipulation",
Subcommands: []cli.Command{
cli.Command{
Action: decode,
Name: "decode",
Usage: "To decode an Istanbul extraData",
ArgsUsage: "<extra data>",
Flags: []cli.Flag{
ExtraDataFlag,
extraDataFlag,
},
Description: `
This command decodes extraData to vanity and validators.
`,
}
encodeCommand = cli.Command{
This command decodes extraData to vanity and validators.
`,
},
cli.Command{
Action: encode,
Name: "encode",
Usage: "To encode an Istanbul extraData",
ArgsUsage: "<config file> or \"0xValidator1,0xValidator2...\"",
Flags: []cli.Flag{
ConfigFlag,
ValidatorsFlag,
VanityFlag,
configFlag,
validatorsFlag,
vanityFlag,
},
Description: `
This command encodes vanity and validators to extraData. Please refer to example/config.toml.
`,
This command encodes vanity and validators to extraData. Please refer to example/config.toml.
`,
},
},
}
)
func encode(ctx *cli.Context) error {
path := ctx.String(ConfigFlag.Name)
validators := ctx.String(ValidatorsFlag.Name)
path := ctx.String(configFlag.Name)
validators := ctx.String(validatorsFlag.Name)
if len(path) == 0 && len(validators) == 0 {
return cli.NewExitError("Must supply config file or enter validators", 0)
}
@ -72,7 +76,7 @@ func encode(ctx *cli.Context) error {
}
if len(validators) != 0 {
extraData, err := fromRawData(ctx.String(VanityFlag.Name), validators)
extraData, err := fromRawData(ctx.String(vanityFlag.Name), validators)
if err != nil {
return cli.NewExitError("Failed to encode from flags", 0)
}
@ -88,7 +92,7 @@ func fromRawData(vanity string, validators string) (string, error) {
for i, v := range vs {
addrs[i] = common.HexToAddress(v)
}
return extradata.Encode(vanity, addrs)
return Encode(vanity, addrs)
}
func fromConfig(path string) (string, error) {
@ -107,16 +111,16 @@ func fromConfig(path string) (string, error) {
return "", cli.NewExitError(fmt.Sprintf("Failed to parse config file: %v", err), 2)
}
return extradata.Encode(config.Vanity, config.Validators)
return Encode(config.Vanity, config.Validators)
}
func decode(ctx *cli.Context) error {
if !ctx.IsSet(ExtraDataFlag.Name) {
if !ctx.IsSet(extraDataFlag.Name) {
return cli.NewExitError("Must supply extra data", 10)
}
extraString := ctx.String(ExtraDataFlag.Name)
vanity, istanbulExtra, err := extradata.Decode(extraString)
extraString := ctx.String(extraDataFlag.Name)
vanity, istanbulExtra, err := Decode(extraString)
if err != nil {
return err
}

View File

@ -0,0 +1,35 @@
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package extra
import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
func Decode(extraData string) ([]byte, *types.IstanbulExtra, error) {
extra, err := hexutil.Decode(extraData)
if err != nil {
return nil, nil, err
}
istanbulExtra, err := types.ExtractIstanbulExtra(&types.Header{Extra: extra})
if err != nil {
return nil, nil, err
}
return extra[:types.IstanbulExtraVanity], istanbulExtra, nil
}

View File

@ -1,4 +1,20 @@
package extradata
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package extra
import (
"bytes"

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package main
package extra
import (
"strings"
@ -23,22 +23,22 @@ import (
)
var (
ConfigFlag = cli.StringFlag{
configFlag = cli.StringFlag{
Name: "config",
Usage: "TOML configuration file",
}
ExtraDataFlag = cli.StringFlag{
extraDataFlag = cli.StringFlag{
Name: "extradata",
Usage: "Hex string for RLP encoded Istanbul extraData",
}
ValidatorsFlag = cli.StringFlag{
validatorsFlag = cli.StringFlag{
Name: "validators",
Usage: "Validators for RLP encoded Istanbul extraData",
}
VanityFlag = cli.StringFlag{
vanityFlag = cli.StringFlag{
Name: "vanity",
Usage: "Vanity for RLP encoded Istanbul extraData",
Value: "0x00",

View File

@ -1,19 +0,0 @@
package extradata
import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
func Decode(extraData string) ([]byte, *types.IstanbulExtra, error) {
extra, err := hexutil.Decode(extraData)
if err != nil {
return nil, nil, err
}
istanbulExtra, err := types.ExtractIstanbulExtra(&types.Header{Extra: extra})
if err != nil {
return nil, nil, err
}
return extra[:types.IstanbulExtraVanity], istanbulExtra, nil
}

View File

@ -20,8 +20,10 @@ import (
"fmt"
"os"
"github.com/getamis/istanbul-tools/cmd/utils"
"github.com/urfave/cli"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
"github.com/getamis/istanbul-tools/cmd/utils"
)
func main() {
@ -32,8 +34,7 @@ func main() {
app.Copyright = "Copyright 2017 The Amis Authors"
app.Commands = []cli.Command{
decodeCommand,
encodeCommand,
extra.ExtraCommand,
}
if err := app.Run(os.Args); err != nil {

View File

@ -29,8 +29,9 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
uuid "github.com/satori/go.uuid"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
)
const (
@ -98,7 +99,7 @@ func GetProposer(header *types.Header) common.Address {
return common.Address{}
}
_, istanbulExtra, err := extradata.Decode(common.ToHex(header.Extra))
_, istanbulExtra, err := extra.Decode(common.ToHex(header.Extra))
if err != nil {
return common.Address{}
}

View File

@ -23,14 +23,14 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
)
type Option func(*core.Genesis)
func Validators(addrs ...common.Address) Option {
return func(genesis *core.Genesis) {
extraData, err := extradata.Encode("0x00", addrs[:])
extraData, err := extra.Encode("0x00", addrs)
if err != nil {
log.Fatalf("Failed to generate genesis, err:%s", err)
}