Merge pull request #85 from getamis/feature/add-genesis-subcommand

cmd/istanbul: add genesis subcommand
This commit is contained in:
yutelin 2017-09-29 15:26:42 +08:00 committed by GitHub
commit 084214d29a
3 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,93 @@
// 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 genesis
import (
"encoding/json"
"fmt"
"math/big"
"net"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/urfave/cli"
istcommon "github.com/getamis/istanbul-tools/common"
"github.com/getamis/istanbul-tools/genesis"
)
type validatorInfo struct {
Address common.Address
Nodekey string
NodeInfo string
}
var (
GenesisCommand = cli.Command{
Name: "genesis",
Usage: "Istanbul genesis block generator",
Action: gen,
Flags: []cli.Flag{
numOfValidatorsFlag,
staticNodesFlag,
verboseFlag,
},
}
)
func gen(ctx *cli.Context) error {
num := ctx.Int(numOfValidatorsFlag.Name)
keys, nodekeys, addrs := istcommon.GenerateKeys(num)
var nodes []string
if ctx.Bool(verboseFlag.Name) {
for i := 0; i < num; i++ {
v := &validatorInfo{
Address: addrs[i],
Nodekey: nodekeys[i],
NodeInfo: discover.NewNode(
discover.PubkeyID(&keys[i].PublicKey),
net.ParseIP("0.0.0.0"),
0,
uint16(30303)).String(),
}
str, _ := json.MarshalIndent(v, "", "\t")
fmt.Println(string(str))
nodes = append(nodes, string(v.NodeInfo))
}
fmt.Print("\n===========================================================\n\n")
}
if ctx.Bool(staticNodesFlag.Name) {
staticNodes, _ := json.MarshalIndent(nodes, "", " ")
fmt.Println(string(staticNodes))
fmt.Print("\n===========================================================\n\n")
}
genesis := genesis.New(
genesis.Validators(addrs...),
genesis.Alloc(addrs, new(big.Int).Exp(big.NewInt(10), big.NewInt(50), nil)),
)
jsonBytes, _ := json.MarshalIndent(genesis, "", " ")
fmt.Println(string(jsonBytes))
return nil
}

View File

@ -0,0 +1,36 @@
// 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 genesis
import "github.com/urfave/cli"
var (
numOfValidatorsFlag = cli.IntFlag{
Name: "num",
Usage: "Number of validators",
}
verboseFlag = cli.BoolFlag{
Name: "verbose",
Usage: "Print more information",
}
staticNodesFlag = cli.BoolFlag{
Name: "nodes",
Usage: "Print static-nodes.json",
}
)

View File

@ -23,6 +23,7 @@ import (
"github.com/urfave/cli"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
"github.com/getamis/istanbul-tools/cmd/istanbul/genesis"
"github.com/getamis/istanbul-tools/cmd/utils"
)
@ -35,6 +36,7 @@ func main() {
app.Commands = []cli.Command{
extra.ExtraCommand,
genesis.GenesisCommand,
}
if err := app.Run(os.Args); err != nil {