Merge pull request #45 from getamis/feature/TFS-03
TFS-03 Recoverability testing
This commit is contained in:
commit
80e29f5e37
|
@ -223,7 +223,6 @@ func (bc *blockchain) CreateNodes(num int, options ...Option) (nodes []Ethereum,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
opts = append(opts, HostDataDir(dataDir))
|
opts = append(opts, HostDataDir(dataDir))
|
||||||
opts = append(opts, HostPort(freeport.GetPort()))
|
|
||||||
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
|
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
|
||||||
|
|
||||||
geth := NewEthereum(
|
geth := NewEthereum(
|
||||||
|
@ -298,7 +297,6 @@ func (bc *blockchain) setupValidators(keys []*ecdsa.PrivateKey, options ...Optio
|
||||||
log.Fatal("Failed to create data dir", err)
|
log.Fatal("Failed to create data dir", err)
|
||||||
}
|
}
|
||||||
opts = append(opts, HostDataDir(dataDir))
|
opts = append(opts, HostDataDir(dataDir))
|
||||||
opts = append(opts, HostPort(freeport.GetPort()))
|
|
||||||
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
|
opts = append(opts, HostWebSocketPort(freeport.GetPort()))
|
||||||
opts = append(opts, Key(keys[i]))
|
opts = append(opts, Key(keys[i]))
|
||||||
|
|
||||||
|
|
|
@ -72,12 +72,15 @@ type Ethereum interface {
|
||||||
|
|
||||||
WaitForProposed(expectedAddress common.Address, t time.Duration) error
|
WaitForProposed(expectedAddress common.Address, t time.Duration) error
|
||||||
WaitForPeersConnected(int) error
|
WaitForPeersConnected(int) error
|
||||||
WaitForBlocks(int) error
|
WaitForBlocks(int, ...time.Duration) error
|
||||||
WaitForBlockHeight(int) error
|
WaitForBlockHeight(int) error
|
||||||
// Want for block for no more than the given number during the given time duration
|
// Want for block for no more than the given number during the given time duration
|
||||||
WaitForNoBlocks(int, time.Duration) error
|
WaitForNoBlocks(int, time.Duration) error
|
||||||
|
|
||||||
AddPeer(string) error
|
AddPeer(string) error
|
||||||
|
|
||||||
|
StartMining() error
|
||||||
|
StopMining() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEthereum(c *client.Client, options ...Option) *ethereum {
|
func NewEthereum(c *client.Client, options ...Option) *ethereum {
|
||||||
|
@ -181,27 +184,19 @@ func (eth *ethereum) Init(genesisFile string) error {
|
||||||
eth.showLog(context.Background())
|
eth.showLog(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
return eth.client.ContainerRemove(context.Background(), id,
|
return eth.client.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true})
|
||||||
types.ContainerRemoveOptions{
|
|
||||||
Force: true,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eth *ethereum) Start() error {
|
func (eth *ethereum) Start() error {
|
||||||
|
defer func() {
|
||||||
|
if eth.logging {
|
||||||
|
go eth.showLog(context.Background())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
exposedPorts := make(map[nat.Port]struct{})
|
exposedPorts := make(map[nat.Port]struct{})
|
||||||
portBindings := nat.PortMap{}
|
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 != "" {
|
if eth.rpcPort != "" {
|
||||||
port := fmt.Sprintf("%d", utils.RPCPortFlag.Value)
|
port := fmt.Sprintf("%d", utils.RPCPortFlag.Value)
|
||||||
exposedPorts[nat.Port(port)] = struct{}{}
|
exposedPorts[nat.Port(port)] = struct{}{}
|
||||||
|
@ -245,11 +240,6 @@ func (eth *ethereum) Start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
|
||||||
if eth.logging {
|
|
||||||
go eth.showLog(context.Background())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
eth.containerID = resp.ID
|
eth.containerID = resp.ID
|
||||||
|
|
||||||
err = eth.client.ContainerStart(context.Background(), eth.containerID, types.ContainerStartOptions{})
|
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))
|
_, err = cli.BlockByNumber(context.Background(), big.NewInt(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(healthCheckRetryDelay)
|
<-time.After(healthCheckRetryDelay)
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
eth.ok = true
|
eth.ok = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,13 +286,12 @@ func (eth *ethereum) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eth *ethereum) Stop() error {
|
func (eth *ethereum) Stop() error {
|
||||||
timeout := 10 * time.Second
|
err := eth.client.ContainerStop(context.Background(), eth.containerID, nil)
|
||||||
err := eth.client.ContainerStop(context.Background(), eth.containerID, &timeout)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
os.RemoveAll(eth.dataDir)
|
defer os.RemoveAll(eth.dataDir)
|
||||||
|
|
||||||
return eth.client.ContainerRemove(context.Background(), eth.containerID,
|
return eth.client.ContainerRemove(context.Background(), eth.containerID,
|
||||||
types.ContainerRemoveOptions{
|
types.ContainerRemoveOptions{
|
||||||
|
@ -458,6 +448,7 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return errors.New("failed to retrieve client")
|
return errors.New("failed to retrieve client")
|
||||||
}
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Second * 1)
|
ticker := time.NewTicker(time.Second * 1)
|
||||||
for _ = range ticker.C {
|
for _ = range ticker.C {
|
||||||
|
@ -476,32 +467,45 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eth *ethereum) WaitForBlocks(num int) error {
|
func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error {
|
||||||
var first *big.Int
|
var first *big.Int
|
||||||
|
|
||||||
client := eth.NewIstanbulClient()
|
client := eth.NewIstanbulClient()
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return errors.New("failed to retrieve client")
|
return errors.New("failed to retrieve client")
|
||||||
}
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Millisecond * 500)
|
var t time.Duration
|
||||||
for _ = range ticker.C {
|
if len(waitingTime) > 0 {
|
||||||
n, err := client.BlockNumber(context.Background())
|
t = waitingTime[0]
|
||||||
if err != nil {
|
} else {
|
||||||
return err
|
t = 1 * time.Hour
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
func (eth *ethereum) WaitForBlockHeight(num int) error {
|
||||||
|
@ -509,6 +513,7 @@ func (eth *ethereum) WaitForBlockHeight(num int) error {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return errors.New("failed to retrieve client")
|
return errors.New("failed to retrieve client")
|
||||||
}
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Millisecond * 500)
|
ticker := time.NewTicker(time.Millisecond * 500)
|
||||||
for _ = range ticker.C {
|
for _ = range ticker.C {
|
||||||
|
@ -566,6 +571,26 @@ func (eth *ethereum) AddPeer(address string) error {
|
||||||
return client.AddPeer(context.Background(), address)
|
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) {
|
func (eth *ethereum) showLog(context context.Context) {
|
||||||
|
|
|
@ -34,7 +34,6 @@ func TestEthereumContainer(t *testing.T) {
|
||||||
ImageRepository("quay.io/amis/geth"),
|
ImageRepository("quay.io/amis/geth"),
|
||||||
ImageTag("istanbul_develop"),
|
ImageTag("istanbul_develop"),
|
||||||
DataDir("/data"),
|
DataDir("/data"),
|
||||||
HostPort(freeport.GetPort()),
|
|
||||||
WebSocket(),
|
WebSocket(),
|
||||||
WebSocketAddress("0.0.0.0"),
|
WebSocketAddress("0.0.0.0"),
|
||||||
WebSocketAPI("admin,eth,net,web3,personal"),
|
WebSocketAPI("admin,eth,net,web3,personal"),
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
Loading…
Reference in New Issue