2017-08-21 02:18:37 -07:00
|
|
|
// 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/>.
|
|
|
|
|
2017-08-31 20:26:54 -07:00
|
|
|
package functional
|
2017-08-21 02:18:37 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-08-22 20:08:42 -07:00
|
|
|
"math"
|
2017-08-23 23:52:31 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-08-21 02:18:37 -07:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
|
|
|
"github.com/getamis/istanbul-tools/container"
|
2017-08-31 20:26:54 -07:00
|
|
|
"github.com/getamis/istanbul-tools/tests"
|
2017-08-21 02:18:37 -07:00
|
|
|
)
|
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
var _ = Describe("TFS-02: Dynamic validators addition/removal testing", func() {
|
2017-08-21 02:18:37 -07:00
|
|
|
const (
|
|
|
|
numberOfValidators = 4
|
|
|
|
)
|
|
|
|
var (
|
|
|
|
blockchain container.Blockchain
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2017-09-01 01:52:08 -07:00
|
|
|
blockchain = container.NewDefaultBlockchain(dockerNetwork, numberOfValidators)
|
2017-08-22 00:44:43 -07:00
|
|
|
Expect(blockchain.Start(true)).To(BeNil())
|
2017-08-21 02:18:37 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
Expect(blockchain.Stop(false)).To(BeNil())
|
|
|
|
blockchain.Finalize()
|
|
|
|
})
|
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
It("TFS-02-01: Add validators", func() {
|
2017-08-23 02:04:33 -07:00
|
|
|
testValidators := 1
|
2017-08-21 02:18:37 -07:00
|
|
|
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Ensure the number of validators is correct", func() {
|
2017-08-21 02:18:37 -07:00
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-21 02:18:37 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Add validators", func() {
|
|
|
|
_, err := blockchain.AddValidators(testValidators)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
})
|
2017-08-21 02:18:37 -07:00
|
|
|
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Wait for several blocks", func() {
|
2017-08-31 20:26:54 -07:00
|
|
|
tests.WaitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
|
2017-08-23 02:04:33 -07:00
|
|
|
Expect(geth.WaitForBlocks(5)).To(BeNil())
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Ensure the number of validators is correct", func() {
|
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-23 02:04:33 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+testValidators))
|
|
|
|
}
|
2017-08-21 02:18:37 -07:00
|
|
|
})
|
|
|
|
})
|
2017-08-22 01:28:22 -07:00
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
It("TFS-02-02: New validators consensus participation", func() {
|
2017-08-24 02:46:26 -07:00
|
|
|
testValidator := 1
|
|
|
|
|
|
|
|
newValidators, err := blockchain.AddValidators(testValidator)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
|
2017-08-31 20:26:54 -07:00
|
|
|
tests.WaitFor(blockchain.Validators()[numberOfValidators:], func(eth container.Ethereum, wg *sync.WaitGroup) {
|
2017-08-24 02:46:26 -07:00
|
|
|
Expect(eth.WaitForProposed(newValidators[0].Address(), 100*time.Second)).Should(BeNil())
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
It("TFS-02-03: Remove validators", func() {
|
2017-08-22 01:28:22 -07:00
|
|
|
numOfCandidates := 3
|
|
|
|
|
2017-08-22 20:54:10 -07:00
|
|
|
By("Ensure that numbers of validator is equal to $numberOfValidators", func() {
|
2017-08-22 01:28:22 -07:00
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-22 01:28:22 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Add validators", func() {
|
|
|
|
_, err := blockchain.AddValidators(numOfCandidates)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
})
|
2017-08-22 01:28:22 -07:00
|
|
|
|
|
|
|
By("Ensure that consensus is working in 50 seconds", func() {
|
|
|
|
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 50*time.Second)).Should(BeNil())
|
|
|
|
})
|
2017-08-23 02:04:33 -07:00
|
|
|
|
|
|
|
By("Check if the number of validators is correct", func() {
|
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-23 02:04:33 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators+numOfCandidates))
|
|
|
|
}
|
|
|
|
})
|
2017-08-22 01:28:22 -07:00
|
|
|
|
|
|
|
// remove validators [1,2,3]
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Remove validators", func() {
|
|
|
|
removalCandidates := blockchain.Validators()[:numOfCandidates]
|
|
|
|
processingTime := time.Duration(math.Pow(2, float64(len(removalCandidates)))*7) * time.Second
|
|
|
|
Expect(blockchain.RemoveValidators(removalCandidates, processingTime)).Should(BeNil())
|
|
|
|
})
|
|
|
|
|
2017-08-23 20:02:47 -07:00
|
|
|
By("Ensure that consensus is working in 20 seconds", func() {
|
|
|
|
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 20*time.Second)).Should(BeNil())
|
2017-08-22 01:28:22 -07:00
|
|
|
})
|
|
|
|
|
2017-08-23 02:04:33 -07:00
|
|
|
By("Check if the number of validators is correct", func() {
|
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-23 02:04:33 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-08-22 20:54:10 -07:00
|
|
|
By("Ensure that consensus is working in 30 seconds", func() {
|
2017-08-22 01:28:22 -07:00
|
|
|
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 30*time.Second)).Should(BeNil())
|
|
|
|
})
|
|
|
|
})
|
2017-08-23 23:52:31 -07:00
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
It("TFS-02-04: Reduce validator network size below 2F+1", func() {
|
2017-08-23 23:52:31 -07:00
|
|
|
By("Ensure that blocks are generated by validators", func() {
|
2017-08-31 20:26:54 -07:00
|
|
|
tests.WaitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
|
2017-08-23 23:52:31 -07:00
|
|
|
Expect(geth.WaitForBlocks(5)).To(BeNil())
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Reduce validator network size but keep it more than 2F+1", func() {
|
|
|
|
// stop validators [3]
|
|
|
|
stopCandidates := blockchain.Validators()[numberOfValidators-1:]
|
|
|
|
for _, candidates := range stopCandidates {
|
2017-09-06 20:30:30 -07:00
|
|
|
c := candidates.NewClient()
|
2017-08-23 23:52:31 -07:00
|
|
|
Expect(c.StopMining(context.Background())).Should(BeNil())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Verify number of validators", func() {
|
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-23 23:52:31 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Ensure that blocks are generated by validators", func() {
|
2017-08-31 20:26:54 -07:00
|
|
|
tests.WaitFor(blockchain.Validators()[:numberOfValidators-1], func(geth container.Ethereum, wg *sync.WaitGroup) {
|
2017-08-23 23:52:31 -07:00
|
|
|
Expect(geth.WaitForBlocks(5)).To(BeNil())
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-08-24 00:04:45 -07:00
|
|
|
|
2017-09-05 20:49:04 -07:00
|
|
|
It("TFS-02-05: Reduce validator network size below 2F+1", func() {
|
2017-08-24 00:04:45 -07:00
|
|
|
By("Ensure that blocks are generated by validators", func() {
|
2017-08-31 20:26:54 -07:00
|
|
|
tests.WaitFor(blockchain.Validators(), func(geth container.Ethereum, wg *sync.WaitGroup) {
|
2017-08-24 00:04:45 -07:00
|
|
|
Expect(geth.WaitForBlocks(5)).To(BeNil())
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Reduce validator network size to less than 2F+1", func() {
|
|
|
|
stopCandidates := blockchain.Validators()[numberOfValidators-2:]
|
|
|
|
// stop validators [3,4]
|
|
|
|
for _, candidates := range stopCandidates {
|
2017-09-06 20:30:30 -07:00
|
|
|
c := candidates.NewClient()
|
2017-08-24 00:04:45 -07:00
|
|
|
Expect(c.StopMining(context.Background())).Should(BeNil())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
By("Verify number of validators", func() {
|
|
|
|
for _, v := range blockchain.Validators() {
|
2017-09-06 20:30:30 -07:00
|
|
|
client := v.NewClient()
|
2017-08-24 00:04:45 -07:00
|
|
|
validators, err := client.GetValidators(context.Background(), nil)
|
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
Expect(len(validators)).Should(BeNumerically("==", numberOfValidators))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
By("No block generated", func() {
|
|
|
|
// REMARK: ErrNoBlock will return if validators not generate block after 10 second.
|
|
|
|
Expect(blockchain.EnsureConsensusWorking(blockchain.Validators(), 11*time.Second)).Should(Equal(container.ErrNoBlock))
|
|
|
|
})
|
|
|
|
})
|
2017-08-21 02:18:37 -07:00
|
|
|
})
|