Skip to content

Commit

Permalink
update the remaining consensus v2 related types to core (ethereum#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrjerome authored May 22, 2022
1 parent 455cacc commit ca336f6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
31 changes: 16 additions & 15 deletions eth/bft/bft_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/log"
)

//Define Boradcast Group functions
type broadcastVoteFn func(*utils.Vote)
type broadcastTimeoutFn func(*utils.Timeout)
type broadcastSyncInfoFn func(*utils.SyncInfo)
type broadcastVoteFn func(*types.Vote)
type broadcastTimeoutFn func(*types.Timeout)
type broadcastSyncInfoFn func(*types.SyncInfo)

type Bfter struct {
blockChainReader consensus.ChainReader
Expand All @@ -22,14 +23,14 @@ type Bfter struct {
}

type ConsensusFns struct {
verifyVote func(consensus.ChainReader, *utils.Vote) (bool, error)
voteHandler func(consensus.ChainReader, *utils.Vote) error
verifyVote func(consensus.ChainReader, *types.Vote) (bool, error)
voteHandler func(consensus.ChainReader, *types.Vote) error

verifyTimeout func(consensus.ChainReader, *utils.Timeout) (bool, error)
timeoutHandler func(consensus.ChainReader, *utils.Timeout) error
verifyTimeout func(consensus.ChainReader, *types.Timeout) (bool, error)
timeoutHandler func(consensus.ChainReader, *types.Timeout) error

verifySyncInfo func(consensus.ChainReader, *utils.SyncInfo) (bool, error)
syncInfoHandler func(consensus.ChainReader, *utils.SyncInfo) error
verifySyncInfo func(consensus.ChainReader, *types.SyncInfo) (bool, error)
syncInfoHandler func(consensus.ChainReader, *types.SyncInfo) error
}

type BroadcastFns struct {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (b *Bfter) SetConsensusFuns(engine consensus.Engine) {
}
}

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

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

return nil
}
func (b *Bfter) Timeout(timeout *utils.Timeout) error {
func (b *Bfter) Timeout(timeout *types.Timeout) error {
log.Debug("Receive Timeout", "timeout", timeout)

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

return nil
}
func (b *Bfter) SyncInfo(syncInfo *utils.SyncInfo) error {
func (b *Bfter) SyncInfo(syncInfo *types.SyncInfo) error {
log.Debug("Receive SyncInfo", "syncInfo", syncInfo)

verified, err := b.consensus.verifySyncInfo(b.blockChainReader, syncInfo)
Expand Down Expand Up @@ -147,11 +148,11 @@ func (b *Bfter) loop() {
return
case obj := <-b.broadcastCh:
switch v := obj.(type) {
case *utils.Vote:
case *types.Vote:
go b.broadcast.Vote(v)
case *utils.Timeout:
case *types.Timeout:
go b.broadcast.Timeout(v)
case *utils.SyncInfo:
case *types.SyncInfo:
go b.broadcast.SyncInfo(v)
default:
log.Error("Unknown message type received", "value", v)
Expand Down
67 changes: 34 additions & 33 deletions eth/bft/bft_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/engines/engine_v2"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/stretchr/testify/assert"
)

// make different votes based on Signatures
func makeVotes(n int) []utils.Vote {
var votes []utils.Vote
func makeVotes(n int) []types.Vote {
var votes []types.Vote
for i := 0; i < n; i++ {
votes = append(votes, utils.Vote{
ProposedBlockInfo: &utils.BlockInfo{},
votes = append(votes, types.Vote{
ProposedBlockInfo: &types.BlockInfo{},
Signature: []byte{byte(i)},
GapNumber: 0,
})
Expand Down Expand Up @@ -55,17 +56,17 @@ func TestSequentialVotes(t *testing.T) {
broadcastCounter := uint32(0)
targetVotes := 10

tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *utils.Vote) (bool, error) {
tester.bfter.consensus.verifyVote = func(chain consensus.ChainReader, vote *types.Vote) (bool, error) {
atomic.AddUint32(&verifyCounter, 1)
return true, nil
}

tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}

tester.bfter.broadcast.Vote = func(*utils.Vote) {
tester.bfter.broadcast.Vote = func(*types.Vote) {
atomic.AddUint32(&broadcastCounter, 1)
}

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

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

tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}
tester.bfter.broadcast.Vote = func(*utils.Vote) {
tester.bfter.broadcast.Vote = func(*types.Vote) {
atomic.AddUint32(&broadcastCounter, 1)
}

vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}}
vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}}
tester.bfter.Vote(&vote)

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

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

tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *utils.Vote) error {
tester.bfter.consensus.voteHandler = func(chain consensus.ChainReader, vote *types.Vote) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}
tester.bfter.broadcast.Vote = func(*utils.Vote) {
tester.bfter.broadcast.Vote = func(*types.Vote) {
atomic.AddUint32(&broadcastCounter, 1)
}

vote := utils.Vote{ProposedBlockInfo: &utils.BlockInfo{}}
vote := types.Vote{ProposedBlockInfo: &types.BlockInfo{}}
tester.bfter.Vote(&vote)

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

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

tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}
tester.bfter.broadcast.Timeout = func(*utils.Timeout) {
tester.bfter.broadcast.Timeout = func(*types.Timeout) {
atomic.AddUint32(&broadcastCounter, 1)
}

timeout := utils.Timeout{}
timeout := types.Timeout{}
tester.bfter.Timeout(&timeout)

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

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

tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *utils.SyncInfo) error {
tester.bfter.consensus.syncInfoHandler = func(chain consensus.ChainReader, syncInfo *types.SyncInfo) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}
tester.bfter.broadcast.SyncInfo = func(*utils.SyncInfo) {
tester.bfter.broadcast.SyncInfo = func(*types.SyncInfo) {
atomic.AddUint32(&broadcastCounter, 1)
}

syncInfo := utils.SyncInfo{}
syncInfo := types.SyncInfo{}
tester.bfter.SyncInfo(&syncInfo)

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

tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) {
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) {
atomic.AddUint32(&verifyCounter, 1)
return true, nil
}

tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
atomic.AddUint32(&handlerCounter, 1)
return nil
}

tester.bfter.broadcast.Timeout = func(*utils.Timeout) {
tester.bfter.broadcast.Timeout = func(*types.Timeout) {
atomic.AddUint32(&broadcastCounter, 1)
}

timeoutMsg := &utils.Timeout{}
timeoutMsg := &types.Timeout{}

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

tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *utils.Timeout) (bool, error) {
tester.bfter.consensus.verifyTimeout = func(consensus.ChainReader, *types.Timeout) (bool, error) {
return true, nil
}

tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *utils.Timeout) error {
tester.bfter.consensus.timeoutHandler = func(chain consensus.ChainReader, timeout *types.Timeout) error {
return &utils.ErrIncomingMessageRoundNotEqualCurrentRound{
Type: "timeout",
IncomingRound: utils.Round(1),
CurrentRound: utils.Round(2),
IncomingRound: types.Round(1),
CurrentRound: types.Round(2),
}
}

tester.bfter.broadcast.Timeout = func(*utils.Timeout) {}
tester.bfter.broadcast.Timeout = func(*types.Timeout) {}

timeoutMsg := &utils.Timeout{}
timeoutMsg := &types.Timeout{}

err := tester.bfter.Timeout(timeoutMsg)
assert.Equal(t, "timeout message round number: 1 does not match currentRound: 2", err.Error())
Expand Down
13 changes: 6 additions & 7 deletions eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/consensus/misc"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/types"
Expand Down Expand Up @@ -848,7 +847,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
pm.lendingpool.AddRemotes(txs)
}
case msg.Code == VoteMsg:
var vote utils.Vote
var vote types.Vote
if err := msg.Decode(&vote); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)
}
Expand All @@ -865,7 +864,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
}

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

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

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

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

// BroadcastSyncInfo will propagate a SyncInfo to all peers which are not known to
// already have the given SyncInfo.
func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *utils.SyncInfo) {
func (pm *ProtocolManager) BroadcastSyncInfo(syncInfo *types.SyncInfo) {
hash := syncInfo.Hash()
peers := pm.peers.PeersWithoutSyncInfo(hash)
if len(peers) > 0 {
Expand Down
7 changes: 3 additions & 4 deletions eth/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"time"

"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/p2p"
"github.com/XinFinOrg/XDPoSChain/rlp"
Expand Down Expand Up @@ -299,7 +298,7 @@ func (p *peer) SendReceiptsRLP(receipts []rlp.RawValue) error {
}
}

func (p *peer) SendVote(vote *utils.Vote) error {
func (p *peer) SendVote(vote *types.Vote) error {
p.knownVote.Add(vote.Hash())
if p.pairRw != nil {
return p2p.Send(p.pairRw, VoteMsg, vote)
Expand All @@ -313,7 +312,7 @@ func (p *peer) AsyncSendVote() {
}
*/
func (p *peer) SendTimeout(timeout *utils.Timeout) error {
func (p *peer) SendTimeout(timeout *types.Timeout) error {
p.knownTimeout.Add(timeout.Hash())
if p.pairRw != nil {
return p2p.Send(p.pairRw, TimeoutMsg, timeout)
Expand All @@ -327,7 +326,7 @@ func (p *peer) AsyncSendTimeout() {
}
*/
func (p *peer) SendSyncInfo(syncInfo *utils.SyncInfo) error {
func (p *peer) SendSyncInfo(syncInfo *types.SyncInfo) error {
p.knownSyncInfo.Add(syncInfo.Hash())
if p.pairRw != nil {
return p2p.Send(p.pairRw, SyncInfoMsg, syncInfo)
Expand Down

0 comments on commit ca336f6

Please sign in to comment.