diff --git a/cmd/istanbul/genesis/cmd.go b/cmd/istanbul/genesis/cmd.go new file mode 100644 index 00000000..87281fae --- /dev/null +++ b/cmd/istanbul/genesis/cmd.go @@ -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 . + +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 +} diff --git a/cmd/istanbul/genesis/flags.go b/cmd/istanbul/genesis/flags.go new file mode 100644 index 00000000..0842d316 --- /dev/null +++ b/cmd/istanbul/genesis/flags.go @@ -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 . + +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", + } +) diff --git a/cmd/istanbul/main.go b/cmd/istanbul/main.go index 153fd034..e6aef05d 100644 --- a/cmd/istanbul/main.go +++ b/cmd/istanbul/main.go @@ -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 {