[Feature Added] Recreate genesis file using existing node key information (#1)

- This patch removes package info
This commit is contained in:
Chang Yang Jiao 2018-06-20 10:30:50 -04:00 committed by fixanoid
parent ed7063cf2d
commit c1f8d1ab55
13 changed files with 197 additions and 15 deletions

View File

@ -1,9 +1,10 @@
CURDIR = $(shell pwd)
GOPATH= $(dir $(abspath $(dir $(abspath $(dir ${CURDIR})))))
GOBIN = $(CURDIR)/build/bin
GO ?= latest
istanbul:
go build -v -o ./build/bin/istanbul ./cmd/istanbul
@GOPATH=$(GOPATH) go build -v -o ./build/bin/istanbul ./cmd/istanbul
@echo "Done building."
@echo "Run \"$(GOBIN)/istanbul\" to launch istanbul."

View File

@ -17,7 +17,7 @@
package client
import (
logging "github.com/getamis/istanbul-tools/log"
logging "github.com/istanbul-tools/log"
)
var log = logging.New()

View File

@ -22,9 +22,10 @@ import (
"github.com/urfave/cli"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
"github.com/getamis/istanbul-tools/cmd/istanbul/setup"
"github.com/getamis/istanbul-tools/cmd/utils"
"github.com/istanbul-tools/cmd/istanbul/extra"
"github.com/istanbul-tools/cmd/istanbul/setup"
"github.com/istanbul-tools/cmd/istanbul/reinit"
"github.com/istanbul-tools/cmd/utils"
)
func main() {
@ -36,6 +37,7 @@ func main() {
app.Commands = []cli.Command{
extra.ExtraCommand,
setup.SetupCommand,
reinit.ReinitCommand,
}
if err := app.Run(os.Args); err != nil {

136
cmd/istanbul/reinit/cmd.go Normal file
View File

@ -0,0 +1,136 @@
// 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 reinit
import (
"fmt"
"strings"
"sort"
"bytes"
"math/big"
"encoding/json"
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/istanbul-tools/genesis"
"github.com/urfave/cli"
)
var (
ReinitCommand = cli.Command{
Name: "reinit",
Action: reinit,
Usage: "Reinitialize a genesis block using previous node info",
ArgsUsage: "\"nodekey1,nodekey2,...\"",
Flags: []cli.Flag{
nodeKeyFlag,
quorumFlag,
},
Description: `This tool helps generate a genesis block`,
}
)
func reinit(ctx *cli.Context) error {
if !ctx.IsSet(nodeKeyFlag.Name) {
return cli.NewExitError("Must supply nodekeys", 10);
}
nodeKeyString := ctx.String(nodeKeyFlag.Name);
nodekeys := strings.Split(nodeKeyString, ",");
isQuorum := ctx.Bool(quorumFlag.Name);
var stringAddrs []string;
_, _, addr := generateKeysWithNodeKey(nodekeys);
// Convert to String to sort
for i := 0; i < len(addr); i++ {
addrString, _ := json.Marshal(addr[i]);
stringAddrs = append(stringAddrs, string(addrString));
}
sort.Strings(stringAddrs);
// Convert back to address
var addrs []common.Address;
for i := 0; i < len(stringAddrs); i++ {
var address common.Address;
json.Unmarshal([]byte(stringAddrs[i]), &address);
addrs = append(addrs, address);
}
// Generate Genesis block
genesisString, _ := getGenesisWithAddrs(addrs, isQuorum);
// genesisS, _ := json.Marshal(genesis);
fmt.Println(string(genesisString));
return nil;
}
func generateKeysWithNodeKey(nodekeysIn []string) (keys []*ecdsa.PrivateKey, nodekeys []string, addrs []common.Address) {
for i := 0; i < len(nodekeysIn); i++ {
nodekey := nodekeysIn[i]
nodekeys = append(nodekeys, nodekey)
key, err := crypto.HexToECDSA(nodekey)
if err != nil {
fmt.Println("Failed to generate key", "err", err)
return nil, nil, nil
}
keys = append(keys, key)
addr := crypto.PubkeyToAddress(key.PublicKey)
addrs = append(addrs, addr)
}
return keys, nodekeys, addrs
}
func getGenesisWithAddrs(addrs []common.Address, isQuorum bool) ([]byte, error) {
// generate genesis block
istanbulGenesis := genesis.New(
genesis.Validators(addrs...),
genesis.Alloc(addrs, new(big.Int).Exp(big.NewInt(10), big.NewInt(50), nil)),
)
var jsonBytes []byte
var err error
if isQuorum {
jsonBytes, err = json.MarshalIndent(genesis.ToQuorum(istanbulGenesis, true), "", " ")
} else {
jsonBytes, err = json.MarshalIndent(istanbulGenesis, "", " ")
}
return jsonBytes, err
}
func appendValidators(genesis *core.Genesis, addrs []common.Address) {
if len(genesis.ExtraData) < types.IstanbulExtraVanity {
genesis.ExtraData = append(genesis.ExtraData, bytes.Repeat([]byte{0x00}, types.IstanbulExtraVanity)...)
}
genesis.ExtraData = genesis.ExtraData[:types.IstanbulExtraVanity]
ist := &types.IstanbulExtra{
Validators: addrs,
Seal: []byte{},
CommittedSeal: [][]byte{},
}
istPayload, err := rlp.EncodeToBytes(&ist)
if err != nil {
panic("failed to encode istanbul extra")
}
genesis.ExtraData = append(genesis.ExtraData, istPayload...)
}

View File

@ -0,0 +1,43 @@
// 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 reinit
import (
"strings"
"github.com/urfave/cli"
)
var (
nodeKeyFlag = cli.StringFlag{
Name: "nodekey",
Usage: "String of comma separated nodekey values",
}
quorumFlag = cli.BoolFlag{
Name: "quorum",
Usage: "Use Quorum",
}
)
func splitAndTrim(input string) []string {
result := strings.Split(input, ",")
for i, r := range result {
result[i] = strings.TrimSpace(r)
}
return result
}

View File

@ -29,9 +29,9 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/p2p/discover"
istcommon "github.com/getamis/istanbul-tools/common"
"github.com/getamis/istanbul-tools/docker/compose"
"github.com/getamis/istanbul-tools/genesis"
istcommon "github.com/istanbul-tools/common"
"github.com/istanbul-tools/docker/compose"
"github.com/istanbul-tools/genesis"
"github.com/urfave/cli"
)

View File

@ -17,7 +17,7 @@
package common
import (
logging "github.com/getamis/istanbul-tools/log"
logging "github.com/istanbul-tools/log"
)
var log = logging.New()

View File

@ -24,7 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/getamis/istanbul-tools/client"
"github.com/istanbul-tools/client"
)
var (

View File

@ -22,7 +22,7 @@ import (
"strings"
"text/template"
"github.com/getamis/istanbul-tools/docker/service"
"github.com/istanbul-tools/docker/service"
)
type Compose interface {

View File

@ -21,7 +21,7 @@ import (
"fmt"
"text/template"
"github.com/getamis/istanbul-tools/docker/service"
"github.com/istanbul-tools/docker/service"
)
type quorum struct {

View File

@ -28,7 +28,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/getamis/istanbul-tools/common"
"github.com/istanbul-tools/common"
)
const (

View File

@ -17,7 +17,7 @@
package genesis
import (
logging "github.com/getamis/istanbul-tools/log"
logging "github.com/istanbul-tools/log"
)
var log = logging.New()

View File

@ -23,7 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
"github.com/istanbul-tools/cmd/istanbul/extra"
)
type Option func(*core.Genesis)