From f4b4a22a18682565ba45a043c9e4634be578171f Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Tue, 22 Aug 2017 17:05:21 +0800 Subject: [PATCH 1/3] container, tests: make geth be able to stop and resume --- container/ethereum.go | 98 +++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/container/ethereum.go b/container/ethereum.go index 92a0fcc2..d6f072a9 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -71,10 +71,13 @@ type Ethereum interface { ConsensusMonitor(err chan<- error, quit chan struct{}) WaitForPeersConnected(int) error - WaitForBlocks(int) error + WaitForBlocks(int, ...time.Duration) error WaitForBlockHeight(int) error AddPeer(string) error + + StartMining() error + StopMining() error } func NewEthereum(c *client.Client, options ...Option) *ethereum { @@ -178,13 +181,16 @@ 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{} @@ -242,11 +248,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{}) @@ -263,10 +264,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 } } @@ -292,13 +294,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{ @@ -428,6 +429,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 { @@ -446,32 +448,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 { @@ -479,6 +494,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 { @@ -505,6 +521,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) { From 9862079bf089d36241d9731fb4fb153d19aebe1b Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Thu, 24 Aug 2017 10:51:59 +0800 Subject: [PATCH 2/3] tests: add TFS-03 recoverability test --- tests/recoverability_test.go | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/recoverability_test.go diff --git a/tests/recoverability_test.go b/tests/recoverability_test.go new file mode 100644 index 00000000..6b249f09 --- /dev/null +++ b/tests/recoverability_test.go @@ -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 . + +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) +}) From d90e5240c0f5ca6344f4a9a576c1dfeff384ffee Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Fri, 25 Aug 2017 14:23:50 +0800 Subject: [PATCH 3/3] container: remove host port binding --- container/blockchain.go | 2 -- container/ethereum.go | 11 ----------- container/ethereum_test.go | 1 - 3 files changed, 14 deletions(-) diff --git a/container/blockchain.go b/container/blockchain.go index d227da03..be930e01 100644 --- a/container/blockchain.go +++ b/container/blockchain.go @@ -185,7 +185,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( @@ -260,7 +259,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])) diff --git a/container/ethereum.go b/container/ethereum.go index d6f072a9..47dedddc 100644 --- a/container/ethereum.go +++ b/container/ethereum.go @@ -194,17 +194,6 @@ func (eth *ethereum) Start() error { 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{}{} diff --git a/container/ethereum_test.go b/container/ethereum_test.go index 4d387947..f6365a86 100644 --- a/container/ethereum_test.go +++ b/container/ethereum_test.go @@ -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"),