mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-16 17:03:46 +00:00
pos-845: subtests
This commit is contained in:
parent
043547129a
commit
3e4b872e16
1 changed files with 80 additions and 74 deletions
|
|
@ -148,6 +148,7 @@ func TestValidatorWentOffline(t *testing.T) {
|
||||||
func TestForkWithBlockTime(t *testing.T) {
|
func TestForkWithBlockTime(t *testing.T) {
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
name string
|
||||||
sprint uint64
|
sprint uint64
|
||||||
blockTime map[string]uint64
|
blockTime map[string]uint64
|
||||||
change uint64
|
change uint64
|
||||||
|
|
@ -155,6 +156,7 @@ func TestForkWithBlockTime(t *testing.T) {
|
||||||
forkExpected bool
|
forkExpected bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
name: "No fork after 2 sprints with producer delay = max block time",
|
||||||
sprint: 128,
|
sprint: 128,
|
||||||
blockTime: map[string]uint64{
|
blockTime: map[string]uint64{
|
||||||
"0": 5,
|
"0": 5,
|
||||||
|
|
@ -166,6 +168,7 @@ func TestForkWithBlockTime(t *testing.T) {
|
||||||
forkExpected: false,
|
forkExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "No Fork after 1 sprint producer delay = max block time",
|
||||||
sprint: 64,
|
sprint: 64,
|
||||||
blockTime: map[string]uint64{
|
blockTime: map[string]uint64{
|
||||||
"0": 5,
|
"0": 5,
|
||||||
|
|
@ -176,6 +179,7 @@ func TestForkWithBlockTime(t *testing.T) {
|
||||||
forkExpected: false,
|
forkExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "Fork after 4 sprints with producer delay < max block time",
|
||||||
sprint: 16,
|
sprint: 16,
|
||||||
blockTime: map[string]uint64{
|
blockTime: map[string]uint64{
|
||||||
"0": 2,
|
"0": 2,
|
||||||
|
|
@ -191,89 +195,91 @@ func TestForkWithBlockTime(t *testing.T) {
|
||||||
genesis := initGenesis(t)
|
genesis := initGenesis(t)
|
||||||
|
|
||||||
for _, test := range cases {
|
for _, test := range cases {
|
||||||
genesis.Config.Bor.Sprint = test.sprint
|
t.Run(test.name, func(t *testing.T) {
|
||||||
genesis.Config.Bor.Period = test.blockTime
|
genesis.Config.Bor.Sprint = test.sprint
|
||||||
genesis.Config.Bor.BackupMultiplier = test.blockTime
|
genesis.Config.Bor.Period = test.blockTime
|
||||||
genesis.Config.Bor.ProducerDelay = test.producerDelay
|
genesis.Config.Bor.BackupMultiplier = test.blockTime
|
||||||
|
genesis.Config.Bor.ProducerDelay = test.producerDelay
|
||||||
|
|
||||||
stacks, nodes, _ := setupMiner(t, 2, genesis)
|
stacks, nodes, _ := setupMiner(t, 2, genesis)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, stack := range stacks {
|
for _, stack := range stacks {
|
||||||
stack.Close()
|
stack.Close()
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Iterate over all the nodes and start mining
|
|
||||||
for _, node := range nodes {
|
|
||||||
if err := node.StartMining(1); err != nil {
|
|
||||||
t.Fatal("Error occured while starting miner", "node", node, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
blockHeaders := make([]*types.Header, 2)
|
|
||||||
ticker := time.NewTicker(time.Duration(test.blockTime["0"]) * time.Second)
|
|
||||||
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
|
|
||||||
go func(i int) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
blockHeaders[i] = nodes[i].BlockChain().GetHeaderByNumber(test.sprint*test.change + 10)
|
|
||||||
if blockHeaders[i] != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
}(i)
|
// Iterate over all the nodes and start mining
|
||||||
}
|
for _, node := range nodes {
|
||||||
|
if err := node.StartMining(1); err != nil {
|
||||||
|
t.Fatal("Error occured while starting miner", "node", node, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
blockHeaders := make([]*types.Header, 2)
|
||||||
|
ticker := time.NewTicker(time.Duration(test.blockTime["0"]) * time.Second)
|
||||||
|
|
||||||
wg.Wait()
|
for i := 0; i < 2; i++ {
|
||||||
ticker.Stop()
|
wg.Add(1)
|
||||||
|
|
||||||
// Before the end of sprint
|
go func(i int) {
|
||||||
blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(test.sprint - 1)
|
defer wg.Done()
|
||||||
blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(test.sprint - 1)
|
|
||||||
assert.Equal(t, blockHeaderVal0.Hash(), blockHeaderVal1.Hash())
|
|
||||||
assert.Equal(t, blockHeaderVal0.Time, blockHeaderVal1.Time)
|
|
||||||
|
|
||||||
author0, err := nodes[0].Engine().Author(blockHeaderVal0)
|
for {
|
||||||
if err != nil {
|
select {
|
||||||
t.Error("Error occured while fetching author", "err", err)
|
case <-ticker.C:
|
||||||
}
|
blockHeaders[i] = nodes[i].BlockChain().GetHeaderByNumber(test.sprint*test.change + 10)
|
||||||
author1, err := nodes[1].Engine().Author(blockHeaderVal1)
|
if blockHeaders[i] != nil {
|
||||||
if err != nil {
|
return
|
||||||
t.Error("Error occured while fetching author", "err", err)
|
}
|
||||||
}
|
default:
|
||||||
assert.Equal(t, author0, author1)
|
|
||||||
|
|
||||||
// After the end of sprint
|
}
|
||||||
author2, err := nodes[0].Engine().Author(blockHeaders[0])
|
}
|
||||||
if err != nil {
|
|
||||||
t.Error("Error occured while fetching author", "err", err)
|
}(i)
|
||||||
}
|
}
|
||||||
author3, err := nodes[1].Engine().Author(blockHeaders[1])
|
|
||||||
if err != nil {
|
wg.Wait()
|
||||||
t.Error("Error occured while fetching author", "err", err)
|
ticker.Stop()
|
||||||
}
|
|
||||||
|
// Before the end of sprint
|
||||||
|
blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(test.sprint - 1)
|
||||||
|
blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(test.sprint - 1)
|
||||||
|
assert.Equal(t, blockHeaderVal0.Hash(), blockHeaderVal1.Hash())
|
||||||
|
assert.Equal(t, blockHeaderVal0.Time, blockHeaderVal1.Time)
|
||||||
|
|
||||||
|
author0, err := nodes[0].Engine().Author(blockHeaderVal0)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error occured while fetching author", "err", err)
|
||||||
|
}
|
||||||
|
author1, err := nodes[1].Engine().Author(blockHeaderVal1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error occured while fetching author", "err", err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, author0, author1)
|
||||||
|
|
||||||
|
// After the end of sprint
|
||||||
|
author2, err := nodes[0].Engine().Author(blockHeaders[0])
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error occured while fetching author", "err", err)
|
||||||
|
}
|
||||||
|
author3, err := nodes[1].Engine().Author(blockHeaders[1])
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error occured while fetching author", "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.forkExpected {
|
||||||
|
assert.NotEqual(t, blockHeaders[0].Hash(), blockHeaders[1].Hash())
|
||||||
|
assert.NotEqual(t, blockHeaders[0].Time, blockHeaders[1].Time)
|
||||||
|
assert.NotEqual(t, author2, author3)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, blockHeaders[0].Hash(), blockHeaders[1].Hash())
|
||||||
|
assert.Equal(t, blockHeaders[0].Time, blockHeaders[1].Time)
|
||||||
|
assert.Equal(t, author2, author3)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if test.forkExpected {
|
|
||||||
assert.NotEqual(t, blockHeaders[0].Hash(), blockHeaders[1].Hash())
|
|
||||||
assert.NotEqual(t, blockHeaders[0].Time, blockHeaders[1].Time)
|
|
||||||
assert.NotEqual(t, author2, author3)
|
|
||||||
} else {
|
|
||||||
assert.Equal(t, blockHeaders[0].Hash(), blockHeaders[1].Hash())
|
|
||||||
assert.Equal(t, blockHeaders[0].Time, blockHeaders[1].Time)
|
|
||||||
assert.Equal(t, author2, author3)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue