Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions consensus/spos/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ func (wrk *Worker) ProcessReceivedMessage(message p2p.MessageP2P, fromConnectedP

err = wrk.consensusMessageValidator.checkConsensusMessageValidity(cnsMsg, message.Peer())
if err != nil {
wrk.processInvalidEquivalentMessage(msgType, cnsMsg.BlockHeaderHash)
return err
}

Expand Down Expand Up @@ -749,6 +750,19 @@ func (wrk *Worker) processEquivalentMessage(msgType consensus.MessageType, block
return nil
}

func (wrk *Worker) processInvalidEquivalentMessage(msgType consensus.MessageType, blockHeaderHash []byte) {
if !wrk.consensusService.IsMessageWithFinalInfo(msgType) {
return
}

hdrHash := string(blockHeaderHash)

wrk.mutEquivalentMessages.Lock()
defer wrk.mutEquivalentMessages.Unlock()

delete(wrk.equivalentMessages, hdrHash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is the proper way to do this.
Reason: what happens in this scenario?

  1. receive the correct message
  2. receive the same block final info but for some reason checkConsensusMessageValidity returns an error. We delete the equivalent message for the hdrHash
  3. we receive the correct message again and we re-broadcast it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed

}

// getEquivalentMessages returns a copy of the equivalent messages
func (wrk *Worker) getEquivalentMessages() map[string]uint64 {
wrk.mutEquivalentMessages.RLock()
Expand Down
34 changes: 34 additions & 0 deletions consensus/spos/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,24 @@ func TestWorker_ProcessReceivedMessageEquivalentMessageShouldReturnError(t *test
)
buffEquiv, _ := wrk.Marshalizer().Marshal(cnsMsgEquiv)

invalidCnsMsg := consensus.NewConsensusMessage(
[]byte("other block header hash"),
nil,
nil,
nil,
pubKey,
bytes.Repeat([]byte("a"), SignatureSize),
int(bls.MtBlockHeaderFinalInfo),
0,
[]byte("invalid chain id"),
[]byte("01"),
signature,
signature,
currentPid,
nil,
)
buffInvalidCnsMsg, _ := wrk.Marshalizer().Marshal(invalidCnsMsg)

err := wrk.ProcessReceivedMessage(
&p2pmocks.P2PMessageMock{
DataField: buff,
Expand Down Expand Up @@ -665,6 +683,22 @@ func TestWorker_ProcessReceivedMessageEquivalentMessageShouldReturnError(t *test
assert.Equal(t, 1, len(equivalentMessages))
assert.Equal(t, uint64(2), equivalentMessages[string(equivalentBlockHeaderHash)])

err = wrk.ProcessReceivedMessage(
&p2pmocks.P2PMessageMock{
DataField: buffInvalidCnsMsg,
PeerField: currentPid,
SignatureField: []byte("signatureEquiv"),
},
equivMsgFrom,
&p2pmocks.MessengerStub{},
)
assert.Error(t, err)

// same state as before, invalid message should have been dropped
equivalentMessages = wrk.GetEquivalentMessages()
assert.Equal(t, 1, len(equivalentMessages))
assert.Equal(t, uint64(2), equivalentMessages[string(equivalentBlockHeaderHash)])

wrk.RemoveAllEquivalentMessages()
equivalentMessages = wrk.GetEquivalentMessages()
assert.Equal(t, 0, len(equivalentMessages))
Expand Down