[Feature Added] Recreate genesis file using existing node key information (#1)
- This patch removes package info
This commit is contained in:
parent
ed7063cf2d
commit
c1f8d1ab55
3
Makefile
3
Makefile
|
@ -1,9 +1,10 @@
|
||||||
CURDIR = $(shell pwd)
|
CURDIR = $(shell pwd)
|
||||||
|
GOPATH= $(dir $(abspath $(dir $(abspath $(dir ${CURDIR})))))
|
||||||
GOBIN = $(CURDIR)/build/bin
|
GOBIN = $(CURDIR)/build/bin
|
||||||
GO ?= latest
|
GO ?= latest
|
||||||
|
|
||||||
istanbul:
|
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 "Done building."
|
||||||
@echo "Run \"$(GOBIN)/istanbul\" to launch istanbul."
|
@echo "Run \"$(GOBIN)/istanbul\" to launch istanbul."
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
logging "github.com/getamis/istanbul-tools/log"
|
logging "github.com/istanbul-tools/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.New()
|
var log = logging.New()
|
||||||
|
|
|
@ -22,9 +22,10 @@ import (
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
"github.com/getamis/istanbul-tools/cmd/istanbul/extra"
|
"github.com/istanbul-tools/cmd/istanbul/extra"
|
||||||
"github.com/getamis/istanbul-tools/cmd/istanbul/setup"
|
"github.com/istanbul-tools/cmd/istanbul/setup"
|
||||||
"github.com/getamis/istanbul-tools/cmd/utils"
|
"github.com/istanbul-tools/cmd/istanbul/reinit"
|
||||||
|
"github.com/istanbul-tools/cmd/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -36,6 +37,7 @@ func main() {
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
extra.ExtraCommand,
|
extra.ExtraCommand,
|
||||||
setup.SetupCommand,
|
setup.SetupCommand,
|
||||||
|
reinit.ReinitCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
|
|
@ -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...)
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -29,9 +29,9 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||||
istcommon "github.com/getamis/istanbul-tools/common"
|
istcommon "github.com/istanbul-tools/common"
|
||||||
"github.com/getamis/istanbul-tools/docker/compose"
|
"github.com/istanbul-tools/docker/compose"
|
||||||
"github.com/getamis/istanbul-tools/genesis"
|
"github.com/istanbul-tools/genesis"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
logging "github.com/getamis/istanbul-tools/log"
|
logging "github.com/istanbul-tools/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.New()
|
var log = logging.New()
|
||||||
|
|
|
@ -24,7 +24,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
|
||||||
"github.com/getamis/istanbul-tools/client"
|
"github.com/istanbul-tools/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -22,7 +22,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/getamis/istanbul-tools/docker/service"
|
"github.com/istanbul-tools/docker/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Compose interface {
|
type Compose interface {
|
||||||
|
|
|
@ -21,7 +21,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/getamis/istanbul-tools/docker/service"
|
"github.com/istanbul-tools/docker/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type quorum struct {
|
type quorum struct {
|
||||||
|
|
|
@ -28,7 +28,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
|
||||||
"github.com/getamis/istanbul-tools/common"
|
"github.com/istanbul-tools/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
package genesis
|
package genesis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
logging "github.com/getamis/istanbul-tools/log"
|
logging "github.com/istanbul-tools/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var log = logging.New()
|
var log = logging.New()
|
||||||
|
|
|
@ -23,7 +23,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"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)
|
type Option func(*core.Genesis)
|
||||||
|
|
Loading…
Reference in New Issue