diff --git a/Makefile b/Makefile index 959bd7cd..207ac26e 100644 --- a/Makefile +++ b/Makefile @@ -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." diff --git a/client/log.go b/client/log.go index fd6382e7..7b2e74ca 100644 --- a/client/log.go +++ b/client/log.go @@ -17,7 +17,7 @@ package client import ( - logging "github.com/getamis/istanbul-tools/log" + logging "github.com/istanbul-tools/log" ) var log = logging.New() diff --git a/cmd/istanbul/main.go b/cmd/istanbul/main.go index f580d12a..6bf42171 100644 --- a/cmd/istanbul/main.go +++ b/cmd/istanbul/main.go @@ -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 { diff --git a/cmd/istanbul/reinit/cmd.go b/cmd/istanbul/reinit/cmd.go new file mode 100644 index 00000000..0d6b9d62 --- /dev/null +++ b/cmd/istanbul/reinit/cmd.go @@ -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 . + +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...) +} diff --git a/cmd/istanbul/reinit/flags.go b/cmd/istanbul/reinit/flags.go new file mode 100644 index 00000000..2c9578e3 --- /dev/null +++ b/cmd/istanbul/reinit/flags.go @@ -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 . + +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 +} diff --git a/cmd/istanbul/setup/cmd.go b/cmd/istanbul/setup/cmd.go index 3b935fff..39cbe97f 100644 --- a/cmd/istanbul/setup/cmd.go +++ b/cmd/istanbul/setup/cmd.go @@ -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" ) diff --git a/common/log.go b/common/log.go index 73a5ca94..f399458f 100644 --- a/common/log.go +++ b/common/log.go @@ -17,7 +17,7 @@ package common import ( - logging "github.com/getamis/istanbul-tools/log" + logging "github.com/istanbul-tools/log" ) var log = logging.New() diff --git a/common/transactions.go b/common/transactions.go index 952d565d..d0b5c536 100644 --- a/common/transactions.go +++ b/common/transactions.go @@ -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 ( diff --git a/docker/compose/istanbul.go b/docker/compose/istanbul.go index 6fdd335e..3c173fc5 100644 --- a/docker/compose/istanbul.go +++ b/docker/compose/istanbul.go @@ -22,7 +22,7 @@ import ( "strings" "text/template" - "github.com/getamis/istanbul-tools/docker/service" + "github.com/istanbul-tools/docker/service" ) type Compose interface { diff --git a/docker/compose/quorum.go b/docker/compose/quorum.go index b04a3ecf..eb3d9b90 100644 --- a/docker/compose/quorum.go +++ b/docker/compose/quorum.go @@ -21,7 +21,7 @@ import ( "fmt" "text/template" - "github.com/getamis/istanbul-tools/docker/service" + "github.com/istanbul-tools/docker/service" ) type quorum struct { diff --git a/genesis/genesis.go b/genesis/genesis.go index 05c3205b..bfd78a53 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -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 ( diff --git a/genesis/log.go b/genesis/log.go index a064bb69..25fb7d35 100644 --- a/genesis/log.go +++ b/genesis/log.go @@ -17,7 +17,7 @@ package genesis import ( - logging "github.com/getamis/istanbul-tools/log" + logging "github.com/istanbul-tools/log" ) var log = logging.New() diff --git a/genesis/options.go b/genesis/options.go index 561e42e9..32788ba7 100644 --- a/genesis/options.go +++ b/genesis/options.go @@ -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)