Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: updating sync tests #740

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
} else {
handler.logger.Info("responding block request message", "message", msg,
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
4 changes: 1 addition & 3 deletions sync/handler_blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,8 @@ func TestSyncing(t *testing.T) {
assert.Equal(t, uint32(0), syncAlice.state.LastBlockHeight())
assert.Equal(t, uint32(100), syncBob.state.LastBlockHeight())

// Alice receives a BlockAnnounce message and starts updating its blockchain
syncAlice.updateBlockchain()

// Perform block syncing
shouldPublishMessageWithThisType(t, networkBob, message.TypeBlocksRequest)
shouldPublishMessageWithThisType(t, networkAlice, message.TypeBlocksRequest)
shouldPublishMessageWithThisType(t, networkBob, message.TypeBlocksResponse) // 1-11
shouldPublishMessageWithThisType(t, networkBob, message.TypeBlocksResponse) // 12-22
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
Loading