Skip to content

Commit

Permalink
test: updating sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Oct 9, 2023
1 parent 53b540e commit 8ad9b01
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 186 deletions.
4 changes: 0 additions & 4 deletions sync/handler_block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func (handler *blockAnnounceHandler) ParseMessage(m message.Message, initiator p
}

func (handler *blockAnnounceHandler) PrepareBundle(m message.Message) *bundle.Bundle {
if !handler.weAreInTheCommittee() {
handler.logger.Debug("sending BlockAnnounce ignored. We are not in the committee")
return nil
}
bdl := bundle.NewBundle(handler.SelfID(), m)

return bdl
Expand Down
46 changes: 5 additions & 41 deletions sync/handler_block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/sync/services"
"github.com/pactus-project/pactus/types/certificate"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,17 +18,9 @@ func TestParsingBlockAnnounceMessages(t *testing.T) {
blk2, cert2 := td.GenerateTestBlock(lastHeight + 2)
msg2 := message.NewBlockAnnounceMessage(blk2, cert2)

pub, _ := td.RandBLSKeyPair()
td.addPeer(t, pub, pid, services.New(services.Network))

t.Run("Receiving new block announce message, without committing previous block", func(t *testing.T) {
assert.NoError(t, td.receivingNewMessage(td.sync, msg2, pid))

msg1 := td.shouldPublishMessageWithThisType(t, td.network, message.TypeBlocksRequest)
assert.Equal(t, msg1.Message.(*message.BlocksRequestMessage).From, lastHeight+1)

peer := td.sync.peerSet.GetPeer(pid)
assert.Equal(t, peer.Height, lastHeight+2)
assert.Equal(t, td.sync.state.LastBlockHeight(), lastHeight)
})

Expand All @@ -41,39 +31,13 @@ func TestParsingBlockAnnounceMessages(t *testing.T) {
})
}

func TestInvalidBlockAnnounce(t *testing.T) {
td := setup(t, nil)

pid := td.RandPeerID()
lastHeight := td.state.LastBlockHeight()
blk, _ := td.GenerateTestBlock(lastHeight + 1)
invCert := certificate.NewCertificate(lastHeight+1, 0, nil, nil, nil)
msg := message.NewBlockAnnounceMessage(blk, invCert)

err := td.receivingNewMessage(td.sync, msg, pid)
assert.Error(t, err)
}

func TestBroadcastingBlockAnnounceMessages(t *testing.T) {
td := setup(t, nil)

td.state.CommitTestBlocks(21)
blk, _ := td.state.CommittedBlock(td.state.LastBlockHeight()).ToBlock()
msg := message.NewBlockAnnounceMessage(
blk, td.state.LastCertificate())

t.Run("Not in the committee, should not broadcast block announce message", func(t *testing.T) {
td.sync.broadcast(msg)

td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeBlockAnnounce)
})
blk, cert := td.GenerateTestBlock(td.RandHeight())
msg := message.NewBlockAnnounceMessage(blk, cert)
td.sync.broadcast(msg)

td.addPeerToCommittee(t, td.sync.SelfID(), td.sync.valKeys[0].PublicKey())

t.Run("In the committee, should broadcast block announce message", func(t *testing.T) {
td.sync.broadcast(msg)

msg1 := td.shouldPublishMessageWithThisType(t, td.network, message.TypeBlockAnnounce)
assert.Equal(t, msg1.Message.(*message.BlockAnnounceMessage).Certificate.Height(), msg.Certificate.Height())
})
msg1 := td.shouldPublishMessageWithThisType(t, td.network, message.TypeBlockAnnounce)
assert.Equal(t, msg1.Message.(*message.BlockAnnounceMessage).Certificate.Height(), msg.Certificate.Height())
}
2 changes: 1 addition & 1 deletion sync/handler_blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (handler *blocksRequestHandler) PrepareBundle(m message.Message) *bundle.Bu

func (handler *blocksRequestHandler) respond(msg *message.BlocksResponseMessage, to peer.ID) error {
if msg.ResponseCode == message.ResponseCodeRejected {
handler.logger.Error("rejecting block request message", "message", msg,
handler.logger.Debug("rejecting block request message", "message", msg,
"to", to, "reason", msg.Reason)
handler.network.CloseConnection(to)
} else {
Expand Down
2 changes: 1 addition & 1 deletion sync/handler_blocks_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestLatestBlocksRequestMessages(t *testing.T) {
})
})

t.Run("Respond error", func(t *testing.T) {
t.Run("Send error", func(t *testing.T) {
td.network.SendError = fmt.Errorf("send error")

msg := message.NewBlocksRequestMessage(sid, 1, 2)
Expand Down
19 changes: 11 additions & 8 deletions sync/handler_hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func (handler *helloHandler) ParseMessage(m message.Message, initiator peer.ID)
msg := m.(*message.HelloMessage)
handler.logger.Trace("parsing Hello message", "message", msg)

handler.logger.Debug("updating peer info",
"pid", msg.PeerID,
"moniker", msg.Moniker,
"services", msg.Services)

handler.peerSet.UpdateInfo(initiator,
msg.Moniker,
msg.Agent,
Expand All @@ -34,14 +39,16 @@ func (handler *helloHandler) ParseMessage(m message.Message, initiator peer.ID)

if msg.PeerID != initiator {
response := message.NewHelloAckMessage(message.ResponseCodeRejected,
fmt.Sprintf("peer ID is not matched, expected: %v, got: %v", msg.PeerID, initiator))
fmt.Sprintf("peer ID is not matched, expected: %v, got: %v",
msg.PeerID, initiator))

return handler.acknowledge(response, initiator)
}

if msg.GenesisHash != handler.state.Genesis().Hash() {
response := message.NewHelloAckMessage(message.ResponseCodeRejected,
fmt.Sprintf("peer ID is not matched, expected: %v, got: %v", msg.PeerID, initiator))
fmt.Sprintf("invalid genesis hash, expected: %v, got: %v",
handler.state.Genesis().Hash(), msg.GenesisHash))

return handler.acknowledge(response, initiator)
}
Expand All @@ -53,11 +60,6 @@ func (handler *helloHandler) ParseMessage(m message.Message, initiator peer.ID)
return handler.acknowledge(response, initiator)
}

handler.logger.Debug("updating peer info",
"pid", initiator,
"moniker", msg.Moniker,
"services", msg.Services)

handler.peerSet.UpdateHeight(initiator, msg.Height, msg.BlockHash)
handler.peerSet.UpdateStatus(initiator, peerset.StatusCodeConnected)

Expand All @@ -75,8 +77,9 @@ func (handler *helloHandler) acknowledge(msg *message.HelloAckMessage, to peer.I
if msg.ResponseCode == message.ResponseCodeRejected {
handler.peerSet.UpdateStatus(to, peerset.StatusCodeBanned)

handler.logger.Warn("rejecting hello message", "message", msg,
handler.logger.Debug("rejecting hello message", "message", msg,
"to", to, "reason", msg.Reason)
handler.network.CloseConnection(to)
} else {
handler.logger.Info("acknowledging hello message", "message", msg,
"to", to)
Expand Down
2 changes: 2 additions & 0 deletions sync/handler_hello_ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (handler *helloAckHandler) ParseMessage(m message.Message, initiator peer.I
handler.logger.Debug("hello message acknowledged",
"from", initiator)

handler.updateBlockchain()

return nil
}

Expand Down
12 changes: 2 additions & 10 deletions sync/handler_query_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ func newQueryProposalHandler(sync *synchronizer) messageHandler {
}
}

func (handler *queryProposalHandler) ParseMessage(m message.Message, initiator peer.ID) error {
func (handler *queryProposalHandler) ParseMessage(m message.Message, _ peer.ID) error {
msg := m.(*message.QueryProposalMessage)
handler.logger.Trace("parsing QueryProposal message", "message", msg, "initiator", initiator)
handler.logger.Trace("parsing QueryProposal message", "message", msg)

height, _ := handler.consMgr.HeightRound()
if msg.Height == height {
// TODO: this should be refactored
// if !handler.peerIsInTheCommittee(initiator) {
// return errors.Errorf(errors.ErrInvalidMessage, "peers is not in the committee")
// }
prop := handler.consMgr.Proposal()
if prop != nil {
response := message.NewProposalMessage(prop)
Expand All @@ -37,10 +33,6 @@ func (handler *queryProposalHandler) ParseMessage(m message.Message, initiator p
}

func (handler *queryProposalHandler) PrepareBundle(m message.Message) *bundle.Bundle {
if !handler.weAreInTheCommittee() {
handler.logger.Debug("sending QueryProposal ignored. We are not in the committee")
return nil
}
bdl := bundle.NewBundle(handler.SelfID(), m)

return bdl
Expand Down
72 changes: 27 additions & 45 deletions sync/handler_query_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,46 @@ import (
"testing"

"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/stretchr/testify/assert"
)

// func TestParsingQueryProposalMessages(t *testing.T) {
// td := setup(t, nil)

// consensusHeight, _ := td.consMgr.HeightRound()
// prop, _ := td.GenerateTestProposal(consensusHeight, 0)
// pid := td.RandPeerID()
// td.consMgr.SetProposal(prop)

// t.Run("Not in the committee, should not respond to the query proposal message", func(t *testing.T) {
// msg := message.NewQueryProposalMessage(consensusHeight)

// assert.Error(t, td.receivingNewMessage(td.sync, msg, pid))
// })
func TestParsingQueryProposalMessages(t *testing.T) {
td := setup(t, nil)

// td.addPeerToCommittee(t, pid, nil)
consensusHeight, _ := td.consMgr.HeightRound()
prop, _ := td.GenerateTestProposal(consensusHeight, 0)
pid := td.RandPeerID()
td.consMgr.SetProposal(prop)

// t.Run("In the committee, but not the same height", func(t *testing.T) {
// msg := message.NewQueryProposalMessage(consensusHeight + 1)
// assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
t.Run("not the same height", func(t *testing.T) {
msg := message.NewQueryProposalMessage(consensusHeight + 1)
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

// td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeProposal)
// })
// t.Run("In the committee, should respond to the query proposal message", func(t *testing.T) {
// msg := message.NewQueryProposalMessage(consensusHeight)
// assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeProposal)
})
t.Run("should respond to the query proposal message", func(t *testing.T) {
msg := message.NewQueryProposalMessage(consensusHeight)
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

// bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeProposal)
// assert.Equal(t, bdl.Message.(*message.ProposalMessage).Proposal.Hash(), prop.Hash())
// })
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeProposal)
assert.Equal(t, bdl.Message.(*message.ProposalMessage).Proposal.Hash(), prop.Hash())
})

// t.Run("In the committee, but doesn't have the proposal", func(t *testing.T) {
// td.consMocks[0].CurProposal = nil
// msg := message.NewQueryProposalMessage(consensusHeight)
// assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
t.Run("doesn't have the proposal", func(t *testing.T) {
td.consMocks[0].CurProposal = nil
msg := message.NewQueryProposalMessage(consensusHeight)
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

// td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeProposal)
// })
// }
td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeProposal)
})
}

func TestBroadcastingQueryProposalMessages(t *testing.T) {
td := setup(t, nil)

consensusHeight := td.state.LastBlockHeight() + 1
msg := message.NewQueryProposalMessage(consensusHeight)
td.sync.broadcast(msg)

t.Run("Not in the committee, should not send query proposal message", func(t *testing.T) {
td.sync.broadcast(msg)

td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeQueryProposal)
})

td.addPeerToCommittee(t, td.sync.SelfID(), td.sync.valKeys[0].PublicKey())

t.Run("In the committee, should send query proposal message", func(t *testing.T) {
td.sync.broadcast(msg)

td.shouldPublishMessageWithThisType(t, td.network, message.TypeQueryProposal)
})
td.shouldPublishMessageWithThisType(t, td.network, message.TypeQueryProposal)
}
12 changes: 2 additions & 10 deletions sync/handler_query_votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ func newQueryVotesHandler(sync *synchronizer) messageHandler {
}
}

func (handler *queryVotesHandler) ParseMessage(m message.Message, initiator peer.ID) error {
func (handler *queryVotesHandler) ParseMessage(m message.Message, _ peer.ID) error {
msg := m.(*message.QueryVotesMessage)
handler.logger.Trace("parsing QueryVotes message", "message", msg, "initiator", initiator)
handler.logger.Trace("parsing QueryVotes message", "message", msg)

height, _ := handler.consMgr.HeightRound()
if msg.Height == height {
// TODO: this should be refactored
// if !handler.peerIsInTheCommittee(initiator) {
// return errors.Errorf(errors.ErrInvalidMessage, "peers is not in the committee")
// }
v := handler.consMgr.PickRandomVote(msg.Round)
if v != nil {
response := message.NewVoteMessage(v)
Expand All @@ -37,10 +33,6 @@ func (handler *queryVotesHandler) ParseMessage(m message.Message, initiator peer
}

func (handler *queryVotesHandler) PrepareBundle(m message.Message) *bundle.Bundle {
if !handler.weAreInTheCommittee() {
handler.logger.Debug("sending QueryVotes ignored. We are not in the committee")
return nil
}
bdl := bundle.NewBundle(handler.SelfID(), m)

return bdl
Expand Down
64 changes: 21 additions & 43 deletions sync/handler_query_votes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,39 @@ import (
"testing"

"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/stretchr/testify/assert"
)

// func TestParsingQueryVotesMessages(t *testing.T) {
// td := setup(t, nil)

// consensusHeight, _ := td.consMgr.HeightRound()
// v1, _ := td.GenerateTestPrecommitVote(consensusHeight, 0)
// td.consMgr.AddVote(v1)
// pid := td.RandPeerID()
// msg := message.NewQueryVotesMessage(consensusHeight, 1)

// t.Run("Not known peer, should not respond to the query vote message", func(t *testing.T) {
// assert.Error(t, td.receivingNewMessage(td.sync, msg, pid))
// })

// pub, _ := td.RandBLSKeyPair()
// td.addPeer(t, pub, pid, services.New(services.None))

// t.Run("Not in the committee, should not respond to the query vote message", func(t *testing.T) {
// assert.Error(t, td.receivingNewMessage(td.sync, msg, pid))
// })
func TestParsingQueryVotesMessages(t *testing.T) {
td := setup(t, nil)

// td.addPeerToCommittee(t, pid, nil)
consensusHeight, _ := td.consMgr.HeightRound()
v1, _ := td.GenerateTestPrecommitVote(consensusHeight, 0)
td.consMgr.AddVote(v1)
pid := td.RandPeerID()
msg := message.NewQueryVotesMessage(consensusHeight, 1)

// t.Run("In the committee, should respond to the query vote message", func(t *testing.T) {
// assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
t.Run("should respond to the query votes message", func(t *testing.T) {
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

// bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeVote)
// assert.Equal(t, bdl.Message.(*message.VoteMessage).Vote.Hash(), v1.Hash())
// })
bdl := td.shouldPublishMessageWithThisType(t, td.network, message.TypeVote)
assert.Equal(t, bdl.Message.(*message.VoteMessage).Vote.Hash(), v1.Hash())
})

// t.Run("In the committee, but doesn't have the vote", func(t *testing.T) {
// msg := message.NewQueryVotesMessage(consensusHeight+1, 1)
// assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))
t.Run("doesn't have any votes", func(t *testing.T) {
msg := message.NewQueryVotesMessage(consensusHeight+1, 1)
assert.NoError(t, td.receivingNewMessage(td.sync, msg, pid))

// td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeVote)
// })
// }
td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeVote)
})
}

func TestBroadcastingQueryVotesMessages(t *testing.T) {
td := setup(t, nil)

consensusHeight := td.state.LastBlockHeight() + 1
msg := message.NewQueryVotesMessage(consensusHeight, 1)
td.sync.broadcast(msg)

t.Run("Not in the committee, should not send query vote message", func(t *testing.T) {
td.sync.broadcast(msg)

td.shouldNotPublishMessageWithThisType(t, td.network, message.TypeQueryVotes)
})

td.addPeerToCommittee(t, td.sync.SelfID(), td.sync.valKeys[0].PublicKey())
t.Run("In the committee, should send query vote message", func(t *testing.T) {
td.sync.broadcast(msg)

td.shouldPublishMessageWithThisType(t, td.network, message.TypeQueryVotes)
})
td.shouldPublishMessageWithThisType(t, td.network, message.TypeQueryVotes)
}
Loading

0 comments on commit 8ad9b01

Please sign in to comment.