container, genesis: add functional options for genesis creation

This commit is contained in:
Alan Chen 2017-08-30 14:18:51 +08:00
parent 5a06bddfb0
commit 83f94962d4
3 changed files with 59 additions and 15 deletions

View File

@ -278,7 +278,11 @@ func (bc *blockchain) setupGenesis(addrs []common.Address) {
if err != nil {
log.Fatal("Failed to create setup dir", err)
}
err = genesis.Save(setupDir, genesis.New(addrs))
err = genesis.Save(setupDir,
genesis.New(
genesis.Validators(addrs...),
),
)
if err != nil {
log.Fatal("Failed to save genesis", err)
}

View File

@ -19,19 +19,14 @@ package genesis
import (
"encoding/json"
"io/ioutil"
"log"
"math/big"
"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 +35,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 +51,14 @@ 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 Save(dataDir string, genesis *core.Genesis) error {

45
genesis/options.go Normal file
View File

@ -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
}
}