Skip to content

Commit ca336f6

Browse files
authored
update the remaining consensus v2 related types to core (ethereum#94)
1 parent 455cacc commit ca336f6

File tree

4 files changed

+59
-59
lines changed

4 files changed

+59
-59
lines changed

eth/bft/bft_handler.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
66
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
77
"github.com/XinFinOrg/XDPoSChain/core"
8+
"github.com/XinFinOrg/XDPoSChain/core/types"
89
"github.com/XinFinOrg/XDPoSChain/log"
910
)
1011

1112
//Define Boradcast Group functions
12-
type broadcastVoteFn func(*utils.Vote)
13-
type broadcastTimeoutFn func(*utils.Timeout)
14-
type broadcastSyncInfoFn func(*utils.SyncInfo)
13+
type broadcastVoteFn func(*types.Vote)
14+
type broadcastTimeoutFn func(*types.Timeout)
15+
type broadcastSyncInfoFn func(*types.SyncInfo)
1516

1617
type Bfter struct {
1718
blockChainReader consensus.ChainReader
@@ -22,14 +23,14 @@ type Bfter struct {
2223
}
2324

2425
type ConsensusFns struct {
25-
verifyVote func(consensus.ChainReader, *utils.Vote) (bool, error)
26-
voteHandler func(consensus.ChainReader, *utils.Vote) error
26+
verifyVote func(consensus.ChainReader, *types.Vote) (bool, error)
27+
voteHandler func(consensus.ChainReader, *types.Vote) error
2728

28-
verifyTimeout func(consensus.ChainReader, *utils.Timeout) (bool, error)
29-
timeoutHandler func(consensus.ChainReader, *utils.Timeout) error
29+
verifyTimeout func(consensus.ChainReader, *types.Timeout) (bool, error)
30+
timeoutHandler func(consensus.ChainReader, *types.Timeout) error
3031

31-
verifySyncInfo func(consensus.ChainReader, *utils.SyncInfo) (bool, error)
32-
syncInfoHandler func(consensus.ChainReader, *utils.SyncInfo) error
32+
verifySyncInfo func(consensus.ChainReader, *types.SyncInfo) (bool, error)
33+
syncInfoHandler func(consensus.ChainReader, *types.SyncInfo) error
3334
}
3435

3536
type BroadcastFns struct {
@@ -62,7 +63,7 @@ func (b *Bfter) SetConsensusFuns(engine consensus.Engine) {
6263
}
6364
}
6465

65-
func (b *Bfter) Vote(vote *utils.Vote) error {
66+
func (b *Bfter) Vote(vote *types.Vote) error {
6667
log.Trace("Receive Vote", "hash", vote.Hash().Hex(), "voted block hash", vote.ProposedBlockInfo.Hash.Hex(), "number", vote.ProposedBlockInfo.Number, "round", vote.ProposedBlockInfo.Round)
6768

6869
verified, err := b.consensus.verifyVote(b.blockChainReader, vote)
@@ -88,7 +89,7 @@ func (b *Bfter) Vote(vote *utils.Vote) error {
8889

8990
return nil
9091
}
91-
func (b *Bfter) Timeout(timeout *utils.Timeout) error {
92+
func (b *Bfter) Timeout(timeout *types.Timeout) error {
9293
log.Debug("Receive Timeout", "timeout", timeout)
9394

9495
verified, err := b.consensus.verifyTimeout(b.blockChainReader, timeout)
@@ -112,7 +113,7 @@ func (b *Bfter) Timeout(timeout *utils.Timeout) error {
112113

113114
return nil
114115
}
115-
func (b *Bfter) SyncInfo(syncInfo *utils.SyncInfo) error {
116+
func (b *Bfter) SyncInfo(syncInfo *types.SyncInfo) error {
116117
log.Debug("Receive SyncInfo", "syncInfo", syncInfo)
117118

118119
verified, err := b.consensus.verifySyncInfo(b.blockChainReader, syncInfo)
@@ -147,11 +148,11 @@ func (b *Bfter) loop() {
147148
return
148149
case obj := <-b.broadcastCh:
149150
switch v := obj.(type) {
150-
case *utils.Vote:
151+
case *types.Vote:
151152
go b.broadcast.Vote(v)
152-
case *utils.Timeout:
153+
case *types.Timeout:
153154
go b.broadcast.Timeout(v)
154-
case *utils.SyncInfo:
155+
case *types.SyncInfo:
155156
go b.broadcast.SyncInfo(v)
156157
default:
157158
log.Error("Unknown message type received", "value", v)

eth/bft/bft_handler_test.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ import (
1111
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/engines/engine_v2"
1212
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
1313
"github.com/XinFinOrg/XDPoSChain/core"
14+
"github.com/XinFinOrg/XDPoSChain/core/types"
1415
"github.com/stretchr/testify/assert"
1516
)
1617

1718
// make different votes based on Signatures
18-
func makeVotes(n int) []utils.Vote {
19-
var votes []utils.Vote
19+
func makeVotes(n int) []types.Vote {
20+
var votes []types.Vote
2021
for i := 0; i < n; i++ {
21-
votes = append(votes, utils.Vote{
22-
ProposedBlockInfo: &utils.BlockInfo{},
22+
votes = append(votes, types.Vote{
23+
ProposedBlockInfo: &types.BlockInfo{},
2324
Signature: []byte{byte(i)},
2425
GapNumber: 0,
2526
})
@@ -55,17 +56,17 @@ func TestSequentialVotes(t *testing.T) {
5556
broadcastCounter := uint32(0)
5657
targetVotes := 10
5758

58-
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) {
59+
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) {
5960
atomic.AddUint32(&verifyCounter, 1)
6061
return true, nil
6162
}
6263

63-
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
64+
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
6465
atomic.AddUint32(&handlerCounter, 1)
6566
return nil
6667
}
6768

68-
tester.bfter.broadcast.Vote = func(*utils.Vote) {
69+
tester.bfter.broadcast.Vote = func(*types.Vote) {
6970
atomic.AddUint32(&broadcastCounter, 1)
7071
}
7172

@@ -91,19 +92,19 @@ func TestNotBoardcastInvalidVote(t *testing.T) {
9192
broadcastCounter := uint32(0)
9293
targetVotes := 0
9394

94-
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) {
95+
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) {
9596
return false, fmt.Errorf("This is invalid vote")
9697
}
9798

98-
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
99+
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
99100
atomic.AddUint32(&handlerCounter, 1)
100101
return nil
101102
}
102-
tester.bfter.broadcast.Vote = func(*utils.Vote) {
103+
tester.bfter.broadcast.Vote = func(*types.Vote) {
103104
atomic.AddUint32(&broadcastCounter, 1)
104105
}
105106

106-
vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}}
107+
vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}}
107108
tester.bfter.Vote(&vote)
108109

109110
time.Sleep(50 * time.Millisecond)
@@ -118,19 +119,19 @@ func TestBoardcastButNotProcessDisqualifiedVotes(t *testing.T) {
118119
broadcastCounter := uint32(0)
119120
targetVotes := 0
120121

121-
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) {
122+
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) {
122123
return false, nil // return false but with nil in error means the message is valid but disqualified
123124
}
124125

125-
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
126+
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
126127
atomic.AddUint32(&handlerCounter, 1)
127128
return nil
128129
}
129-
tester.bfter.broadcast.Vote = func(*utils.Vote) {
130+
tester.bfter.broadcast.Vote = func(*types.Vote) {
130131
atomic.AddUint32(&broadcastCounter, 1)
131132
}
132133

133-
vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}}
134+
vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}}
134135
tester.bfter.Vote(&vote)
135136

136137
time.Sleep(50 * time.Millisecond)
@@ -145,19 +146,19 @@ func TestBoardcastButNotProcessDisqualifiedTimeout(t *testing.T) {
145146
broadcastCounter := uint32(0)
146147
targetTimeout := 0
147148

148-
tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainReader, timeout *utils.Timeout) (bool, error) {
149+
tester.bfter.consensus.verifyTimeout = func(chain consensus.ChainReader, timeout *types.Timeout) (bool, error) {
149150
return false, nil // return false but with nil in error means the message is valid but disqualified
150151
}
151152

152-
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
153+
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
153154
atomic.AddUint32(&handlerCounter, 1)
154155
return nil
155156
}
156-
tester.bfter.broadcast.Timeout = func(*utils.Timeout) {
157+
tester.bfter.broadcast.Timeout = func(*types.Timeout) {
157158
atomic.AddUint32(&broadcastCounter, 1)
158159
}
159160

160-
timeout := utils.Timeout{}
161+
timeout := types.Timeout{}
161162
tester.bfter.Timeout(&timeout)
162163

163164
time.Sleep(50 * time.Millisecond)
@@ -172,19 +173,19 @@ func TestBoardcastButNotProcessDisqualifiedSyncInfo(t *testing.T) {
172173
broadcastCounter := uint32(0)
173174
targetSyncInfo := 0
174175

175-
tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *utils.SyncInfo) (bool, error) {
176+
tester.bfter.consensus.verifySyncInfo = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) (bool, error) {
176177
return false, nil // return false but with nil in error means the message is valid but disqualified
177178
}
178179

179-
tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *utils.SyncInfo) error {
180+
tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error {
180181
atomic.AddUint32(&handlerCounter, 1)
181182
return nil
182183
}
183-
tester.bfter.broadcast.SyncInfo = func(*utils.SyncInfo) {
184+
tester.bfter.broadcast.SyncInfo = func(*types.SyncInfo) {
184185
atomic.AddUint32(&broadcastCounter, 1)
185186
}
186187

187-
syncInfo := utils.SyncInfo{}
188+
syncInfo := types.SyncInfo{}
188189
tester.bfter.SyncInfo(&syncInfo)
189190

190191
time.Sleep(50 * time.Millisecond)
@@ -203,21 +204,21 @@ func TestTimeoutHandler(t *testing.T) {
203204
broadcastCounter := uint32(0)
204205
targetVotes := 1
205206

206-
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) {
207+
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) {
207208
atomic.AddUint32(&verifyCounter, 1)
208209
return true, nil
209210
}
210211

211-
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
212+
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
212213
atomic.AddUint32(&handlerCounter, 1)
213214
return nil
214215
}
215216

216-
tester.bfter.broadcast.Timeout = func(*utils.Timeout) {
217+
tester.bfter.broadcast.Timeout = func(*types.Timeout) {
217218
atomic.AddUint32(&broadcastCounter, 1)
218219
}
219220

220-
timeoutMsg := &utils.Timeout{}
221+
timeoutMsg := &types.Timeout{}
221222

222223
err := tester.bfter.Timeout(timeoutMsg)
223224
if err != nil {
@@ -234,21 +235,21 @@ func TestTimeoutHandler(t *testing.T) {
234235
func TestTimeoutHandlerRoundNotEqual(t *testing.T) {
235236
tester := newTester()
236237

237-
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) {
238+
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) {
238239
return true, nil
239240
}
240241

241-
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
242+
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
242243
return &utils.ErrIncomingMessageRoundNotEqualCurrentRound{
243244
Type: "timeout",
244-
IncomingRound: utils.Round(1),
245-
CurrentRound: utils.Round(2),
245+
IncomingRound: types.Round(1),
246+
CurrentRound: types.Round(2),
246247
}
247248
}
248249

249-
tester.bfter.broadcast.Timeout = func(*utils.Timeout) {}
250+
tester.bfter.broadcast.Timeout = func(*types.Timeout) {}
250251

251-
timeoutMsg := &utils.Timeout{}
252+
timeoutMsg := &types.Timeout{}
252253

253254
err := tester.bfter.Timeout(timeoutMsg)
254255
assert.Equal(t, "timeout message round number: 1 does not match currentRound: 2", err.Error())

eth/handler.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/XinFinOrg/XDPoSChain/common"
3131
"github.com/XinFinOrg/XDPoSChain/consensus"
3232
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
33-
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
3433
"github.com/XinFinOrg/XDPoSChain/consensus/misc"
3534
"github.com/XinFinOrg/XDPoSChain/core"
3635
"github.com/XinFinOrg/XDPoSChain/core/types"
@@ -848,7 +847,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
848847
pm.lendingpool.AddRemotes(txs)
849848
}
850849
case msg.Code == VoteMsg:
851-
var vote utils.Vote
850+
var vote types.Vote
852851
if err := msg.Decode(&vote); err != nil {
853852
return errResp(ErrDecode, "msg %v: %v", msg, err)
854853
}
@@ -865,7 +864,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
865864
}
866865

867866
case msg.Code == TimeoutMsg:
868-
var timeout utils.Timeout
867+
var timeout types.Timeout
869868
if err := msg.Decode(&timeout); err != nil {
870869
return errResp(ErrDecode, "msg %v: %v", msg, err)
871870
}
@@ -884,7 +883,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
884883
}
885884

886885
case msg.Code == SyncInfoMsg:
887-
var syncInfo utils.SyncInfo
886+
var syncInfo types.SyncInfo
888887
if err := msg.Decode(&syncInfo); err != nil {
889888
return errResp(ErrDecode, "msg %v: %v", msg, err)
890889
}
@@ -952,7 +951,7 @@ func (pm *ProtocolManager) BroadcastTx(hash common.Hash, tx *types.Transaction)
952951

953952
// BroadcastVote will propagate a Vote to all peers which are not known to
954953
// already have the given vote.
955-
func (pm *ProtocolManager) BroadcastVote(vote *utils.Vote) {
954+
func (pm *ProtocolManager) BroadcastVote(vote *types.Vote) {
956955
hash := vote.Hash()
957956
peers := pm.peers.PeersWithoutVote(hash)
958957
if len(peers) > 0 {
@@ -970,7 +969,7 @@ func (pm *ProtocolManager) BroadcastVote(vote *utils.Vote) {
970969

971970
// BroadcastTimeout will propagate a Timeout to all peers which are not known to
972971
// already have the given timeout.
973-
func (pm *ProtocolManager) BroadcastTimeout(timeout *utils.Timeout) {
972+
func (pm *ProtocolManager) BroadcastTimeout(timeout *types.Timeout) {
974973
hash := timeout.Hash()
975974
peers := pm.peers.PeersWithoutTimeout(hash)
976975
if len(peers) > 0 {
@@ -988,7 +987,7 @@ func (pm *ProtocolManager) BroadcastTimeout(timeout *utils.Timeout) {
988987

989988
// BroadcastSyncInfo will propagate a SyncInfo to all peers which are not known to
990989
// already have the given SyncInfo.
991-
func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *utils.SyncInfo) {
990+
func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *types.SyncInfo) {
992991
hash := syncInfo.Hash()
993992
peers := pm.peers.PeersWithoutSyncInfo(hash)
994993
if len(peers) > 0 {

eth/peer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"time"
2525

2626
"github.com/XinFinOrg/XDPoSChain/common"
27-
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
2827
"github.com/XinFinOrg/XDPoSChain/core/types"
2928
"github.com/XinFinOrg/XDPoSChain/p2p"
3029
"github.com/XinFinOrg/XDPoSChain/rlp"
@@ -299,7 +298,7 @@ func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error {
299298
}
300299
}
301300

302-
func (p *peer) SendVote(vote *utils.Vote) error {
301+
func (p *peer) SendVote(vote *types.Vote) error {
303302
p.knownVote.Add(vote.Hash())
304303
if p.pairRw != nil {
305304
return p2p.Send(p.pairRw, VoteMsg, vote)
@@ -313,7 +312,7 @@ func (p *peer) AsyncSendVote() {
313312
314313
}
315314
*/
316-
func (p *peer) SendTimeout(timeout *utils.Timeout) error {
315+
func (p *peer) SendTimeout(timeout *types.Timeout) error {
317316
p.knownTimeout.Add(timeout.Hash())
318317
if p.pairRw != nil {
319318
return p2p.Send(p.pairRw, TimeoutMsg, timeout)
@@ -327,7 +326,7 @@ func (p *peer) AsyncSendTimeout() {
327326
328327
}
329328
*/
330-
func (p *peer) SendSyncInfo(syncInfo *utils.SyncInfo) error {
329+
func (p *peer) SendSyncInfo(syncInfo *types.SyncInfo) error {
331330
p.knownSyncInfo.Add(syncInfo.Hash())
332331
if p.pairRw != nil {
333332
return p2p.Send(p.pairRw, SyncInfoMsg, syncInfo)

0 commit comments

Comments
 (0)