1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus 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 dexon-consensus 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 dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.
package test
import (
"testing"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
"github.com/dexon-foundation/dexon-consensus/core"
"github.com/dexon-foundation/dexon-consensus/core/blockdb"
"github.com/dexon-foundation/dexon-consensus/core/types"
"github.com/stretchr/testify/suite"
)
type StopperTestSuite struct {
suite.Suite
}
func (s *StopperTestSuite) deliver(
blocks []*types.Block, app *App, db blockdb.BlockDatabase) {
hashes := common.Hashes{}
for _, b := range blocks {
hashes = append(hashes, b.Hash)
s.Require().NoError(db.Put(*b))
}
for _, h := range hashes {
app.StronglyAcked(h)
}
app.TotalOrderingDelivered(hashes, core.TotalOrderingModeNormal)
for _, h := range hashes {
app.BlockDelivered(h, types.FinalizationResult{
Timestamp: time.Time{},
})
}
}
func (s *StopperTestSuite) deliverToAllNodes(
blocks []*types.Block,
apps map[types.NodeID]*App,
dbs map[types.NodeID]blockdb.BlockDatabase) {
for nID := range apps {
s.deliver(blocks, apps[nID], dbs[nID])
}
}
func (s *StopperTestSuite) TestStopByConfirmedBlocks() {
// This test case makes sure this stopper would stop when
// all nodes confirmed at least 'x' count of blocks produced
// by themselves.
var (
req = s.Require()
apps = make(map[types.NodeID]*App)
dbs = make(map[types.NodeID]blockdb.BlockDatabase)
nodes = GenerateRandomNodeIDs(2)
)
for _, nID := range nodes {
apps[nID] = NewApp(nil)
db, err := blockdb.NewMemBackedBlockDB()
req.NoError(err)
dbs[nID] = db
}
stopper := NewStopByConfirmedBlocks(2, apps, dbs)
b00 := &types.Block{
ProposerID: nodes[0],
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b00}, apps, dbs)
b10 := &types.Block{
ProposerID: nodes[1],
Hash: common.NewRandomHash(),
}
b11 := &types.Block{
ProposerID: nodes[1],
ParentHash: b10.Hash,
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b10, b11}, apps, dbs)
req.False(stopper.ShouldStop(nodes[1]))
b12 := &types.Block{
ProposerID: nodes[1],
ParentHash: b11.Hash,
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b12}, apps, dbs)
req.False(stopper.ShouldStop(nodes[1]))
b01 := &types.Block{
ProposerID: nodes[0],
ParentHash: b00.Hash,
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b01}, apps, dbs)
req.True(stopper.ShouldStop(nodes[0]))
}
func (s *StopperTestSuite) TestStopByRound() {
// This test case make sure at least one block from round R
// is delivered by each node.
var (
req = s.Require()
apps = make(map[types.NodeID]*App)
dbs = make(map[types.NodeID]blockdb.BlockDatabase)
nodes = GenerateRandomNodeIDs(2)
)
for _, nID := range nodes {
apps[nID] = NewApp(nil)
db, err := blockdb.NewMemBackedBlockDB()
req.NoError(err)
dbs[nID] = db
}
stopper := NewStopByRound(10, apps, dbs)
b00 := &types.Block{
ProposerID: nodes[0],
Position: types.Position{
Round: 0,
ChainID: 0,
Height: 0,
},
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b00}, apps, dbs)
b10 := &types.Block{
ProposerID: nodes[1],
Position: types.Position{
Round: 0,
ChainID: 1,
Height: 0,
},
Hash: common.NewRandomHash(),
}
b11 := &types.Block{
ProposerID: nodes[1],
ParentHash: b10.Hash,
Position: types.Position{
Round: 0,
ChainID: 1,
Height: 1,
},
Hash: common.NewRandomHash(),
}
s.deliverToAllNodes([]*types.Block{b10, b11}, apps, dbs)
req.False(stopper.ShouldStop(nodes[0]))
req.False(stopper.ShouldStop(nodes[1]))
// Deliver one block at round 10 to node0
b12 := &types.Block{
ProposerID: nodes[1],
ParentHash: b11.Hash,
Position: types.Position{
Round: 10,
ChainID: 1,
Height: 2,
},
Hash: common.NewRandomHash(),
}
// None should stop when only one node reach that round.
s.deliver([]*types.Block{b12}, apps[nodes[0]], dbs[nodes[0]])
req.False(stopper.ShouldStop(nodes[0]))
req.False(stopper.ShouldStop(nodes[1]))
// Everyone should stop now.
s.deliver([]*types.Block{b12}, apps[nodes[1]], dbs[nodes[1]])
req.True(stopper.ShouldStop(nodes[1]))
req.True(stopper.ShouldStop(nodes[0]))
}
func TestStopper(t *testing.T) {
suite.Run(t, new(StopperTestSuite))
}
|