Merge pull request #45 from getamis/feature/TFS-03

TFS-03 Recoverability testing
This commit is contained in:
Alan Chen 2017-08-25 15:11:17 +08:00 committed by GitHub
commit 80e29f5e37
4 changed files with 155 additions and 45 deletions

View File

@ -223,7 +223,6 @@ func (bc *blockchain) CreateNodes(num int, options ...Option) (nodes []Ethereum,
return nil, err
}
opts = append(opts, HostDataDir(dataDir))
opts = append(opts, HostPort(freeport.GetPort()))
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
geth := NewEthereum(
@ -298,7 +297,6 @@ func (bc *blockchain) setupValidators(keys []*ecdsa.PrivateKey, options ...Optio
log.Fatal("Failed to create data dir", err)
}
opts = append(opts, HostDataDir(dataDir))
opts = append(opts, HostPort(freeport.GetPort()))
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
opts = append(opts, Key(keys[i]))

View File

@ -72,12 +72,15 @@ type Ethereum interface {
WaitForProposed(expectedAddress common.Address, t time.Duration) error
WaitForPeersConnected(int) error
WaitForBlocks(int) error
WaitForBlocks(int, ...time.Duration) error
WaitForBlockHeight(int) error
// Want for block for no more than the given number during the given time duration
WaitForNoBlocks(int, time.Duration) error
AddPeer(string) error
StartMining() error
StopMining() error
}
func NewEthereum(c *client.Client, options ...Option) *ethereum {
@ -181,27 +184,19 @@ func (eth *ethereum) Init(genesisFile string) error {
eth.showLog(context.Background())
}
return eth.client.ContainerRemove(context.Background(), id,
types.ContainerRemoveOptions{
Force: true,
})
return eth.client.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true})
}
func (eth *ethereum) Start() error {
defer func() {
if eth.logging {
go eth.showLog(context.Background())
}
}()
exposedPorts := make(map[nat.Port]struct{})
portBindings := nat.PortMap{}
if eth.port != "" {
port := fmt.Sprintf("%d", utils.ListenPortFlag.Value)
exposedPorts[nat.Port(port)] = struct{}{}
portBindings[nat.Port(port)] = []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: eth.port,
},
}
}
if eth.rpcPort != "" {
port := fmt.Sprintf("%d", utils.RPCPortFlag.Value)
exposedPorts[nat.Port(port)] = struct{}{}
@ -245,11 +240,6 @@ func (eth *ethereum) Start() error {
return err
}
defer func() {
if eth.logging {
go eth.showLog(context.Background())
}
}()
eth.containerID = resp.ID
err = eth.client.ContainerStart(context.Background(), eth.containerID, types.ContainerStartOptions{})
@ -266,10 +256,11 @@ func (eth *ethereum) Start() error {
}
_, err = cli.BlockByNumber(context.Background(), big.NewInt(0))
if err != nil {
time.Sleep(healthCheckRetryDelay)
<-time.After(healthCheckRetryDelay)
continue
} else {
eth.ok = true
break
}
}
@ -295,13 +286,12 @@ func (eth *ethereum) Start() error {
}
func (eth *ethereum) Stop() error {
timeout := 10 * time.Second
err := eth.client.ContainerStop(context.Background(), eth.containerID, &timeout)
err := eth.client.ContainerStop(context.Background(), eth.containerID, nil)
if err != nil {
return err
}
os.RemoveAll(eth.dataDir)
defer os.RemoveAll(eth.dataDir)
return eth.client.ContainerRemove(context.Background(), eth.containerID,
types.ContainerRemoveOptions{
@ -458,6 +448,7 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
if client == nil {
return errors.New("failed to retrieve client")
}
defer client.Close()
ticker := time.NewTicker(time.Second * 1)
for _ = range ticker.C {
@ -476,32 +467,45 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
return nil
}
func (eth *ethereum) WaitForBlocks(num int) error {
func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error {
var first *big.Int
client := eth.NewIstanbulClient()
if client == nil {
return errors.New("failed to retrieve client")
}
defer client.Close()
ticker := time.NewTicker(time.Millisecond * 500)
for _ = range ticker.C {
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
}
if first == nil {
first = new(big.Int).Set(n)
continue
}
// Check if new blocks are getting generated
if new(big.Int).Sub(n, first).Int64() >= int64(num) {
ticker.Stop()
break
}
var t time.Duration
if len(waitingTime) > 0 {
t = waitingTime[0]
} else {
t = 1 * time.Hour
}
return nil
timeout := time.After(t)
ticker := time.NewTicker(time.Millisecond * 500)
for {
select {
case <-timeout:
ticker.Stop()
return ErrNoBlock
case <-ticker.C:
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
}
if first == nil {
first = new(big.Int).Set(n)
continue
}
// Check if new blocks are getting generated
if new(big.Int).Sub(n, first).Int64() >= int64(num) {
ticker.Stop()
return nil
}
}
}
}
func (eth *ethereum) WaitForBlockHeight(num int) error {
@ -509,6 +513,7 @@ func (eth *ethereum) WaitForBlockHeight(num int) error {
if client == nil {
return errors.New("failed to retrieve client")
}
defer client.Close()
ticker := time.NewTicker(time.Millisecond * 500)
for _ = range ticker.C {
@ -566,6 +571,26 @@ func (eth *ethereum) AddPeer(address string) error {
return client.AddPeer(context.Background(), address)
}
func (eth *ethereum) StartMining() error {
client := eth.NewIstanbulClient()
if client == nil {
return errors.New("failed to retrieve client")
}
defer client.Close()
return client.StartMining(context.Background())
}
func (eth *ethereum) StopMining() error {
client := eth.NewIstanbulClient()
if client == nil {
return errors.New("failed to retrieve client")
}
defer client.Close()
return client.StopMining(context.Background())
}
// ----------------------------------------------------------------------------
func (eth *ethereum) showLog(context context.Context) {

View File

@ -34,7 +34,6 @@ func TestEthereumContainer(t *testing.T) {
ImageRepository("quay.io/amis/geth"),
ImageTag("istanbul_develop"),
DataDir("/data"),
HostPort(freeport.GetPort()),
WebSocket(),
WebSocketAddress("0.0.0.0"),
WebSocketAPI("admin,eth,net,web3,personal"),

View File

@ -0,0 +1,88 @@
// 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 (
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/getamis/istanbul-tools/container"
)
var _ = Describe("TFS-03: Recoverability testing", func() {
const (
numberOfValidators = 4
)
var (
blockchain container.Blockchain
)
BeforeEach(func() {
blockchain = container.NewDefaultBlockchain(numberOfValidators)
Expect(blockchain.Start(true)).To(BeNil())
})
AfterEach(func() {
blockchain.Stop(true) // This will return container not found error since we stop one
blockchain.Finalize()
})
It("TFS-04-01: Add validators in a network with < 2F+1 validators to > 2F+1", func(done Done) {
By("The consensus should work at the beginning", func() {
waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
Expect(geth.WaitForBlocks(5)).To(BeNil())
wg.Done()
})
})
numOfValidatorsToBeStopped := 2
By("Stop several validators until there are less than 2F+1 validators", func() {
waitFor(blockchain.Validators()[:numOfValidatorsToBeStopped], func(geth container.Ethereum, wg *sync.WaitGroup) {
Expect(geth.StopMining()).To(BeNil())
wg.Done()
})
})
By("The consensus should not work after resuming", func() {
waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
// container.ErrNoBlock should be returned if we didn't see any block in 10 seconds
Expect(geth.WaitForBlocks(1, 10*time.Second)).To(BeEquivalentTo(container.ErrNoBlock))
wg.Done()
})
})
By("Resume the stopped validators", func() {
waitFor(blockchain.Validators()[:numOfValidatorsToBeStopped], func(geth container.Ethereum, wg *sync.WaitGroup) {
Expect(geth.StartMining()).To(BeNil())
wg.Done()
})
})
By("The consensus should work after resuming", func() {
waitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
Expect(geth.WaitForBlocks(5)).To(BeNil())
wg.Done()
})
})
close(done)
}, 120)
})