core: pass the genesis to SetupNodes

This commit is contained in:
Edwin 2017-08-15 15:35:27 +08:00
parent be14faf7c5
commit 1da4c038f5
6 changed files with 57 additions and 90 deletions

View File

@ -34,7 +34,7 @@ import (
"github.com/docker/go-connections/nat"
"github.com/getamis/go-ethereum/cmd/utils"
"github.com/getamis/go-ethereum/ethclient"
"github.com/getamis/istanbul-tools/core/genesis"
"github.com/getamis/istanbul-tools/core"
)
const (
@ -108,12 +108,12 @@ func (eth *ethereum) Init(genesisFile string) error {
"init",
"--" + utils.DataDirFlag.Name,
eth.dataDir,
filepath.Join("/", genesis.FileName),
filepath.Join("/", core.GenesisFile),
},
},
&container.HostConfig{
Binds: []string{
genesisFile + ":" + filepath.Join("/", genesis.FileName),
genesisFile + ":" + filepath.Join("/", core.GenesisFile),
eth.hostDataDir + ":" + eth.dataDir,
},
}, nil, "")

View File

@ -22,13 +22,12 @@ import (
"testing"
"github.com/getamis/istanbul-tools/core"
"github.com/getamis/istanbul-tools/core/genesis"
)
func TestEthereumContainer(t *testing.T) {
envs := core.SetupEnv(1)
defer core.Teardown(envs)
err := core.SetupNodes(envs)
err := core.SetupNodes(envs, core.NewGenesis(envs))
if err != nil {
t.Error(err)
}
@ -47,7 +46,7 @@ func TestEthereumContainer(t *testing.T) {
RPCPort(fmt.Sprintf("%d", env.RpcPort)),
)
err := geth.Init(filepath.Join(env.DataDir, genesis.FileName))
err := geth.Init(filepath.Join(env.DataDir, core.GenesisFile))
if err != nil {
t.Error(err)
}

View File

@ -22,16 +22,23 @@ import (
"fmt"
"io/ioutil"
"log"
"math/big"
"net"
"net/url"
"os"
"path/filepath"
"time"
"github.com/docker/docker/client"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/istanbul"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/getamis/istanbul-tools/core/genesis"
"github.com/ethereum/go-ethereum/params"
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
"github.com/phayes/freeport"
"github.com/satori/go.uuid"
)
@ -42,6 +49,8 @@ const (
clientIdentifier = "geth"
staticNodeJson = "static-nodes.json"
GenesisFile = "genesis.json"
)
type Env struct {
@ -90,10 +99,8 @@ func SetupEnv(numbers int) []*Env {
return envs
}
func SetupNodes(envs []*Env) error {
func SetupNodes(envs []*Env, g *core.Genesis) error {
nodes := toStaticNodes(envs)
addrs := toAddress(envs)
g := genesis.New(addrs)
for _, env := range envs {
if err := saveNodeKey(env.DataDir, env.Key); err != nil {
@ -104,13 +111,39 @@ func SetupNodes(envs []*Env) error {
return err
}
if err := genesis.Save(env.DataDir, g); err != nil {
if err := saveGenesis(env.DataDir, g); err != nil {
return err
}
}
return nil
}
func NewGenesis(envs []*Env) *core.Genesis {
extraData, err := extradata.Encode("0x00", toAddress(envs))
if err != nil {
log.Fatalf("Failed to generate genesis, err:%s", err)
}
return &core.Genesis{
Timestamp: uint64(time.Now().Unix()),
GasLimit: 4700000,
Difficulty: big.NewInt(1),
Alloc: make(core.GenesisAlloc),
Config: &params.ChainConfig{
HomesteadBlock: big.NewInt(1),
EIP150Block: big.NewInt(2),
EIP155Block: big.NewInt(3),
EIP158Block: big.NewInt(3),
Istanbul: &params.IstanbulConfig{
ProposerPolicy: uint64(istanbul.DefaultConfig.ProposerPolicy),
Epoch: istanbul.DefaultConfig.Epoch,
},
},
Mixhash: types.IstanbulDigest,
ExtraData: hexutil.MustDecode(extraData),
}
}
func saveNodeKey(dataDir string, key *ecdsa.PrivateKey) error {
err := os.MkdirAll(dataDir, 0700)
if err != nil {
@ -143,6 +176,17 @@ func saveStaticNode(dataDir string, nodes []string) error {
return ioutil.WriteFile(keyPath, raw, 0600)
}
func saveGenesis(dataDir string, genesis *core.Genesis) error {
filePath := filepath.Join(dataDir, GenesisFile)
raw, err := json.Marshal(genesis)
if err != nil {
return err
}
return ioutil.WriteFile(filePath, raw, 0600)
}
func toStaticNodes(envs []*Env) []string {
nodes := make([]string, len(envs))

View File

@ -23,7 +23,7 @@ import (
func TestWriteFile(t *testing.T) {
envs := SetupEnv(4)
err := SetupNodes(envs)
err := SetupNodes(envs, NewGenesis(envs))
if err != nil {
t.Fatal("failed to setup nodes", err)
}

View File

@ -1,75 +0,0 @@
// 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"
"io/ioutil"
"math/big"
"path/filepath"
"time"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/istanbul"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
)
const (
FileName = "genesis.json"
)
func New(addrs []common.Address) *core.Genesis {
extraData, err := extradata.Encode("0x00", addrs)
if err != nil {
log.Fatalf("Failed to generate genesis, err:%s", err)
}
return &core.Genesis{
Timestamp: uint64(time.Now().Unix()),
GasLimit: 4700000,
Difficulty: big.NewInt(1),
Alloc: make(core.GenesisAlloc),
Config: &params.ChainConfig{
HomesteadBlock: big.NewInt(1),
EIP150Block: big.NewInt(2),
EIP155Block: big.NewInt(3),
EIP158Block: big.NewInt(3),
Istanbul: &params.IstanbulConfig{
ProposerPolicy: uint64(istanbul.DefaultConfig.ProposerPolicy),
Epoch: istanbul.DefaultConfig.Epoch,
},
},
Mixhash: types.IstanbulDigest,
ExtraData: hexutil.MustDecode(extraData),
}
}
func Save(dataDir string, genesis *core.Genesis) error {
filePath := filepath.Join(dataDir, FileName)
raw, err := json.Marshal(genesis)
if err != nil {
return err
}
return ioutil.WriteFile(filePath, raw, 0600)
}

View File

@ -28,7 +28,6 @@ import (
"github.com/getamis/istanbul-tools/container"
"github.com/getamis/istanbul-tools/core"
"github.com/getamis/istanbul-tools/core/genesis"
)
// var geths []container.Ethereum
@ -44,7 +43,7 @@ var _ = Describe("4 validators Istanbul", func() {
BeforeSuite(func() {
envs = core.SetupEnv(numberOfValidators)
err := core.SetupNodes(envs)
err := core.SetupNodes(envs, core.NewGenesis(envs))
Expect(err).To(BeNil())
for _, env := range envs {
@ -64,7 +63,7 @@ var _ = Describe("4 validators Istanbul", func() {
container.Logging(true),
)
err := geth.Init(filepath.Join(env.DataDir, genesis.FileName))
err := geth.Init(filepath.Join(env.DataDir, core.GenesisFile))
Expect(err).To(BeNil())
geths = append(geths, geth)