genesis, tests: add TFS-01 blockchain initialization and run test

This commit is contained in:
Miya Chen 2017-08-28 12:38:37 +08:00
parent c7b93d1ddf
commit 52725bc1fe
2 changed files with 70 additions and 3 deletions

View File

@ -35,7 +35,9 @@ import (
)
const (
FileName = "genesis.json"
FileName = "genesis.json"
InitGasLimit = 4700000
InitDifficulty = 1
)
func New(addrs []common.Address) *core.Genesis {
@ -46,8 +48,8 @@ func New(addrs []common.Address) *core.Genesis {
return &core.Genesis{
Timestamp: uint64(time.Now().Unix()),
GasLimit: 4700000,
Difficulty: big.NewInt(1),
GasLimit: InitGasLimit,
Difficulty: big.NewInt(InitDifficulty),
Alloc: make(core.GenesisAlloc),
Config: &params.ChainConfig{
HomesteadBlock: big.NewInt(1),

View File

@ -19,13 +19,17 @@ package tests
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/getamis/istanbul-tools/container"
"github.com/getamis/istanbul-tools/genesis"
)
var _ = Describe("TFS-01: General consensus", func() {
@ -46,6 +50,67 @@ var _ = Describe("TFS-01: General consensus", func() {
blockchain.Finalize()
})
It("TFS-01-01, TFS-01-02: Blockchain initialization and run", func() {
errc := make(chan error, len(blockchain.Validators()))
valSet := make(map[common.Address]bool, numberOfValidators)
for _, geth := range blockchain.Validators() {
valSet[geth.Address()] = true
}
for _, geth := range blockchain.Validators() {
go func(geth container.Ethereum) {
// 1. Verify genesis block
c := geth.NewClient()
header, err := c.HeaderByNumber(context.Background(), big.NewInt(0))
if err != nil {
errc <- err
return
}
if header.GasLimit.Int64() != genesis.InitGasLimit {
errStr := fmt.Sprintf("Invalid genesis gas limit. want:%v, got:%v", genesis.InitGasLimit, header.GasLimit.Int64())
errc <- errors.New(errStr)
return
}
if header.Difficulty.Int64() != genesis.InitDifficulty {
errStr := fmt.Sprintf("Invalid genesis difficulty. want:%v, got:%v", genesis.InitDifficulty, header.Difficulty.Int64())
errc <- errors.New(errStr)
return
}
if header.MixDigest != types.IstanbulDigest {
errStr := fmt.Sprintf("Invalid block mixhash. want:%v, got:%v", types.IstanbulDigest, header.MixDigest)
errc <- errors.New(errStr)
return
}
// 2. Check validator set
istClient := geth.NewIstanbulClient()
vals, err := istClient.GetValidators(context.Background(), big.NewInt(0))
if err != nil {
errc <- err
return
}
for _, val := range vals {
if _, ok := valSet[val]; !ok {
errc <- errors.New("Invalid validator address.")
return
}
}
errc <- nil
}(geth)
}
for i := 0; i < len(blockchain.Validators()); i++ {
err := <-errc
Expect(err).To(BeNil())
}
})
It("TFS-01-03: Peer connection", func(done Done) {
expectedPeerCount := len(blockchain.Validators()) - 1
waitFor(blockchain.Validators(), func(v container.Ethereum, wg *sync.WaitGroup) {