Merge pull request #50 from getamis/feature/add-genesis-options
container, genesis: add genesis options and useful utilities
This commit is contained in:
commit
7c3244c3ba
|
@ -274,15 +274,9 @@ func (bc *blockchain) connectAll(strong bool) error {
|
|||
|
||||
func (bc *blockchain) setupGenesis(addrs []common.Address) {
|
||||
if bc.genesisFile == "" {
|
||||
setupDir, err := generateRandomDir()
|
||||
if err != nil {
|
||||
log.Fatal("Failed to create setup dir", err)
|
||||
}
|
||||
err = genesis.Save(setupDir, genesis.New(addrs))
|
||||
if err != nil {
|
||||
log.Fatal("Failed to save genesis", err)
|
||||
}
|
||||
bc.genesisFile = filepath.Join(setupDir, genesis.FileName)
|
||||
bc.genesisFile = genesis.NewFile(
|
||||
genesis.Validators(addrs...),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -24,14 +24,10 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"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 (
|
||||
|
@ -40,13 +36,8 @@ const (
|
|||
InitDifficulty = 1
|
||||
)
|
||||
|
||||
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{
|
||||
func New(options ...Option) *core.Genesis {
|
||||
genesis := &core.Genesis{
|
||||
Timestamp: uint64(time.Now().Unix()),
|
||||
GasLimit: InitGasLimit,
|
||||
Difficulty: big.NewInt(InitDifficulty),
|
||||
|
@ -61,9 +52,27 @@ func New(addrs []common.Address) *core.Genesis {
|
|||
Epoch: istanbul.DefaultConfig.Epoch,
|
||||
},
|
||||
},
|
||||
Mixhash: types.IstanbulDigest,
|
||||
ExtraData: hexutil.MustDecode(extraData),
|
||||
Mixhash: types.IstanbulDigest,
|
||||
}
|
||||
|
||||
for _, opt := range options {
|
||||
opt(genesis)
|
||||
}
|
||||
|
||||
return genesis
|
||||
}
|
||||
|
||||
func NewFile(options ...Option) string {
|
||||
dir, err := generateRandomDir()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create random directory, err: %v", err)
|
||||
}
|
||||
genesis := New(options...)
|
||||
if err := Save(dir, genesis); err != nil {
|
||||
log.Fatalf("Failed to save genesis to '%s', err: %v", dir, err)
|
||||
}
|
||||
|
||||
return filepath.Join(dir, FileName)
|
||||
}
|
||||
|
||||
func Save(dataDir string, genesis *core.Genesis) error {
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
// 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 (
|
||||
"log"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
|
||||
"github.com/getamis/istanbul-tools/cmd/istanbul/extradata"
|
||||
)
|
||||
|
||||
type Option func(*core.Genesis)
|
||||
|
||||
func Validators(addrs ...common.Address) Option {
|
||||
return func(genesis *core.Genesis) {
|
||||
extraData, err := extradata.Encode("0x00", addrs[:])
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate genesis, err:%s", err)
|
||||
}
|
||||
genesis.ExtraData = hexutil.MustDecode(extraData)
|
||||
}
|
||||
}
|
||||
|
||||
func GasLimit(limit uint64) Option {
|
||||
return func(genesis *core.Genesis) {
|
||||
genesis.GasLimit = limit
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
// 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 (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultLocalDir = "/tmp/gdata"
|
||||
clientIdentifier = "genesis"
|
||||
)
|
||||
|
||||
func generateRandomDir() (string, error) {
|
||||
err := os.MkdirAll(filepath.Join(defaultLocalDir), 0700)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
instanceDir := filepath.Join(defaultLocalDir, fmt.Sprintf("%s-%s", clientIdentifier, uuid.NewV4().String()))
|
||||
if err := os.MkdirAll(instanceDir, 0700); err != nil {
|
||||
log.Println(fmt.Sprintf("Failed to create instance dir: %v", err))
|
||||
return "", err
|
||||
}
|
||||
|
||||
return instanceDir, nil
|
||||
}
|
Loading…
Reference in New Issue