152 lines
4.2 KiB
Go
152 lines
4.2 KiB
Go
// 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 tests
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"sync"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/getamis/istanbul-tools/container"
|
|
)
|
|
|
|
var _ = Describe("Block synchronization testing", func() {
|
|
const (
|
|
numberOfValidators = 4
|
|
)
|
|
var (
|
|
blockchain container.Blockchain
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
blockchain = container.NewDefaultBlockchain(numberOfValidators)
|
|
Expect(blockchain.Start(true)).To(BeNil())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
Expect(blockchain.Stop(true)).To(BeNil())
|
|
blockchain.Finalize()
|
|
})
|
|
|
|
Describe("TFS-06: block synchronization testing", func() {
|
|
const numberOfNodes = 2
|
|
var nodes []container.Ethereum
|
|
|
|
BeforeEach(func() {
|
|
var err error
|
|
nodes, err = blockchain.CreateNodes(numberOfNodes,
|
|
container.ImageRepository("quay.io/amis/geth"),
|
|
container.ImageTag("istanbul_develop"),
|
|
container.DataDir("/data"),
|
|
container.WebSocket(),
|
|
container.WebSocketAddress("0.0.0.0"),
|
|
container.WebSocketAPI("admin,eth,net,web3,personal,miner"),
|
|
container.WebSocketOrigin("*"),
|
|
container.NAT("any"),
|
|
)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
for _, n := range nodes {
|
|
err = n.Start()
|
|
Expect(err).To(BeNil())
|
|
}
|
|
})
|
|
|
|
AfterEach(func() {
|
|
for _, n := range nodes {
|
|
n.Stop()
|
|
}
|
|
})
|
|
|
|
It("TFS-06-01: Node connection", func(done Done) {
|
|
By("Connect all nodes to the validators")
|
|
for _, n := range nodes {
|
|
for _, v := range blockchain.Validators() {
|
|
Expect(n.AddPeer(v.NodeAddress())).To(BeNil())
|
|
}
|
|
}
|
|
|
|
By("Wait for p2p connection")
|
|
waitFor(nodes, func(node container.Ethereum, wg *sync.WaitGroup) {
|
|
Expect(node.WaitForPeersConnected(numberOfValidators)).To(BeNil())
|
|
wg.Done()
|
|
})
|
|
|
|
close(done)
|
|
}, 15)
|
|
|
|
It("TFS-06-02: Node synchronization", func(done Done) {
|
|
const targetBlockHeight = 10
|
|
|
|
By("Wait for blocks")
|
|
waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
|
|
Expect(geth.WaitForBlocks(targetBlockHeight)).To(BeNil())
|
|
wg.Done()
|
|
})
|
|
|
|
By("Stop consensus")
|
|
for _, v := range blockchain.Validators() {
|
|
client := v.NewIstanbulClient()
|
|
Expect(client).NotTo(BeNil())
|
|
err := client.StopMining(context.Background())
|
|
Expect(err).To(BeNil())
|
|
client.Close()
|
|
}
|
|
|
|
By("Get target block")
|
|
latestBlock, err := blockchain.Validators()[0].NewClient().BlockByNumber(context.Background(), big.NewInt(targetBlockHeight))
|
|
Expect(err).To(BeNil())
|
|
Expect(latestBlock).NotTo(BeNil())
|
|
|
|
By("Connect all nodes to the validators")
|
|
for _, n := range nodes {
|
|
for _, v := range blockchain.Validators() {
|
|
Expect(n.AddPeer(v.NodeAddress())).To(BeNil())
|
|
}
|
|
}
|
|
|
|
By("Wait for p2p connection")
|
|
waitFor(nodes, func(node container.Ethereum, wg *sync.WaitGroup) {
|
|
Expect(node.WaitForPeersConnected(numberOfValidators)).To(BeNil())
|
|
wg.Done()
|
|
})
|
|
|
|
By("Wait for block synchronization between nodes and validators")
|
|
waitFor(nodes, func(geth container.Ethereum, wg *sync.WaitGroup) {
|
|
Expect(geth.WaitForBlockHeight(targetBlockHeight)).To(BeNil())
|
|
wg.Done()
|
|
})
|
|
|
|
By("Check target block hash of nodes")
|
|
for i := 0; i < numberOfNodes; i++ {
|
|
nodeClient := nodes[i].NewClient()
|
|
block, err := nodeClient.BlockByNumber(context.Background(), big.NewInt(targetBlockHeight))
|
|
|
|
Expect(err).To(BeNil())
|
|
Expect(block).NotTo(BeNil())
|
|
Expect(latestBlock.Hash()).To(BeEquivalentTo(block.Hash()))
|
|
}
|
|
|
|
close(done)
|
|
}, 30)
|
|
})
|
|
})
|