From 52725bc1fe6a257e300a8c351ad752960cf99ab6 Mon Sep 17 00:00:00 2001 From: Miya Chen Date: Mon, 28 Aug 2017 12:38:37 +0800 Subject: [PATCH] genesis, tests: add TFS-01 blockchain initialization and run test --- genesis/genesis.go | 8 ++-- tests/general_consensus_test.go | 65 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/genesis/genesis.go b/genesis/genesis.go index 5c205741..2a61ab60 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -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: ¶ms.ChainConfig{ HomesteadBlock: big.NewInt(1), diff --git a/tests/general_consensus_test.go b/tests/general_consensus_test.go index fdaaee8c..316428de 100644 --- a/tests/general_consensus_test.go +++ b/tests/general_consensus_test.go @@ -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) {