diff --git a/sync/handler_block_announce.go b/sync/handler_block_announce.go index fe066f85a..9c94d438d 100644 --- a/sync/handler_block_announce.go +++ b/sync/handler_block_announce.go @@ -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 diff --git a/sync/handler_block_announce_test.go b/sync/handler_block_announce_test.go index c57bd82bf..eb58ec6a2 100644 --- a/sync/handler_block_announce_test.go +++ b/sync/handler_block_announce_test.go @@ -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" ) @@ -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) }) @@ -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()) } diff --git a/sync/handler_blocks_request.go b/sync/handler_blocks_request.go index 4ce3adf8a..1bf27034c 100644 --- a/sync/handler_blocks_request.go +++ b/sync/handler_blocks_request.go @@ -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 { diff --git a/sync/handler_blocks_request_test.go b/sync/handler_blocks_request_test.go index 91383dab1..a86f572f0 100644 --- a/sync/handler_blocks_request_test.go +++ b/sync/handler_blocks_request_test.go @@ -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) diff --git a/sync/handler_hello.go b/sync/handler_hello.go index 6142ae371..73bdb0513 100644 --- a/sync/handler_hello.go +++ b/sync/handler_hello.go @@ -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, @@ -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) } @@ -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) @@ -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) diff --git a/sync/handler_hello_ack.go b/sync/handler_hello_ack.go index fe25306ef..b2cf84948 100644 --- a/sync/handler_hello_ack.go +++ b/sync/handler_hello_ack.go @@ -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 } diff --git a/sync/handler_query_proposal.go b/sync/handler_query_proposal.go index cd02e44a1..034a19f58 100644 --- a/sync/handler_query_proposal.go +++ b/sync/handler_query_proposal.go @@ -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) @@ -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 diff --git a/sync/handler_query_proposal_test.go b/sync/handler_query_proposal_test.go index e518e419b..e0a19eba6 100644 --- a/sync/handler_query_proposal_test.go +++ b/sync/handler_query_proposal_test.go @@ -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) } diff --git a/sync/handler_query_votes.go b/sync/handler_query_votes.go index decd4c077..3d31296ae 100644 --- a/sync/handler_query_votes.go +++ b/sync/handler_query_votes.go @@ -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) @@ -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 diff --git a/sync/handler_query_votes_test.go b/sync/handler_query_votes_test.go index 8f916ef62..76bce4ada 100644 --- a/sync/handler_query_votes_test.go +++ b/sync/handler_query_votes_test.go @@ -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) } diff --git a/sync/sync.go b/sync/sync.go index 3d37705c5..5df2ea460 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -372,29 +372,6 @@ func (sync *synchronizer) downloadBlocks(from uint32, onlyNodeNetwork bool) { }) } -// // peerIsInTheCommittee checks if the peer is a member of the committee -// // at the current height. -// func (sync *synchronizer) peerIsInTheCommittee(pid peer.ID) bool { -// p := sync.peerSet.GetPeer(pid) -// if !p.IsKnownOrTrusty() { -// return false -// } - -// for _, key := range p.ConsensusKeys { -// if sync.state.IsInCommittee(key.ValidatorAddress()) { -// return true -// } -// } - -// return false -// } - -// weAreInTheCommittee checks if one of the validators is a member of the committee -// at the current height. -func (sync *synchronizer) weAreInTheCommittee() bool { - return sync.consMgr.HasActiveInstance() -} - func (sync *synchronizer) tryCommitBlocks() error { height := sync.state.LastBlockHeight() + 1 for {