Skip to content

Commit

Permalink
Add coverage for message helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Jul 23, 2022
1 parent 2b86d60 commit 16fac25
Show file tree
Hide file tree
Showing 3 changed files with 716 additions and 24 deletions.
13 changes: 0 additions & 13 deletions core/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1814,19 +1814,6 @@ func TestIBFT_ValidPC(t *testing.T) {
func TestIBFT_ValidateProposal(t *testing.T) {
t.Parallel()

/*
Payload: &proto.Message_PreprepareData{
PreprepareData: &proto.PrePrepareMessage{
Proposal: nil,
ProposalHash: nil,
Certificate: &proto.RoundChangeCertificate{
RoundChangeMessages: testCase.roundChangeMessages,
},
},
},
*/

t.Run("proposer is not valid", func(t *testing.T) {
t.Parallel()

Expand Down
64 changes: 53 additions & 11 deletions messages/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import (

// ExtractCommittedSeals extracts the committed seals from the passed in messages
func ExtractCommittedSeals(commitMessages []*proto.Message) [][]byte {
committedSeals := make([][]byte, len(commitMessages))
committedSeals := make([][]byte, 0)

for index, commitMessage := range commitMessages {
committedSeals[index] = ExtractCommittedSeal(commitMessage)
for _, commitMessage := range commitMessages {
if commitMessage.Type != proto.MessageType_COMMIT {
continue
}

committedSeals = append(committedSeals, ExtractCommittedSeal(commitMessage))
}

return committedSeals
Expand All @@ -25,49 +29,82 @@ func ExtractCommittedSeal(commitMessage *proto.Message) []byte {

// ExtractCommitHash extracts the commit proposal hash from the passed in message
func ExtractCommitHash(commitMessage *proto.Message) []byte {
if commitMessage.Type != proto.MessageType_COMMIT {
return nil
}

commitData, _ := commitMessage.Payload.(*proto.Message_CommitData)

return commitData.CommitData.ProposalHash
}

// ExtractProposal extracts the proposal from the passed in message
func ExtractProposal(proposalMessage *proto.Message) []byte {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}

preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)

return preprepareData.PreprepareData.Proposal
}

// ExtractProposalHash extracts the proposal hash from the passed in message
func ExtractProposalHash(proposalMessage *proto.Message) []byte {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}

preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)

return preprepareData.PreprepareData.ProposalHash
}

// ExtractRoundChangeCertificate extracts the RCC from the passed in message
func ExtractRoundChangeCertificate(proposalMessage *proto.Message) *proto.RoundChangeCertificate {
if proposalMessage.Type != proto.MessageType_PREPREPARE {
return nil
}

preprepareData, _ := proposalMessage.Payload.(*proto.Message_PreprepareData)

return preprepareData.PreprepareData.Certificate
}

// ExtractPrepareHash extracts the prepare proposal hash from the passed in message
func ExtractPrepareHash(prepareMessage *proto.Message) []byte {
if prepareMessage.Type != proto.MessageType_PREPARE {
return nil
}

prepareData, _ := prepareMessage.Payload.(*proto.Message_PrepareData)

return prepareData.PrepareData.ProposalHash
}

// ExtractLatestPC extracts the latest PC from the passed in message
func ExtractLatestPC(roundChangeMessage *proto.Message) *proto.PreparedCertificate {
if roundChangeMessage.Type != proto.MessageType_ROUND_CHANGE {
return nil
}

rcData, _ := roundChangeMessage.Payload.(*proto.Message_RoundChangeData)

return rcData.RoundChangeData.LatestPreparedCertificate
}

// ExtractLastPreparedProposedBlock extracts the latest prepared proposed block from the passed in message
func ExtractLastPreparedProposedBlock(roundChangeMessage *proto.Message) []byte {
if roundChangeMessage.Type != proto.MessageType_ROUND_CHANGE {
return nil
}

rcData, _ := roundChangeMessage.Payload.(*proto.Message_RoundChangeData)

return rcData.RoundChangeData.LastPreparedProposedBlock
}

// HasUniqueSenders checks if the messages have unique senders
func HasUniqueSenders(messages []*proto.Message) bool {
if len(messages) < 1 {
return false
Expand All @@ -87,6 +124,7 @@ func HasUniqueSenders(messages []*proto.Message) bool {
return true
}

// HaveSameProposalHash checks if the messages have the same proposal hash
func HaveSameProposalHash(messages []*proto.Message) bool {
if len(messages) < 1 {
return false
Expand All @@ -99,15 +137,9 @@ func HaveSameProposalHash(messages []*proto.Message) bool {

switch message.Type {
case proto.MessageType_PREPREPARE:
ppData, _ := message.Payload.(*proto.Message_PreprepareData)
payload := ppData.PreprepareData

extractedHash = payload.ProposalHash
extractedHash = ExtractProposalHash(message)
case proto.MessageType_PREPARE:
pData, _ := message.Payload.(*proto.Message_PrepareData)
payload := pData.PrepareData

extractedHash = payload.ProposalHash
extractedHash = ExtractPrepareHash(message)
default:
return false
}
Expand All @@ -124,7 +156,12 @@ func HaveSameProposalHash(messages []*proto.Message) bool {
return true
}

// AllHaveLowerRound checks if all messages have the same round
func AllHaveLowerRound(messages []*proto.Message, round uint64) bool {
if len(messages) < 1 {
return false
}

for _, message := range messages {
if message.View.Round >= round {
return false
Expand All @@ -134,7 +171,12 @@ func AllHaveLowerRound(messages []*proto.Message, round uint64) bool {
return true
}

// AllHaveSameHeight checks if all messages have the same height
func AllHaveSameHeight(messages []*proto.Message, height uint64) bool {
if len(messages) < 1 {
return false
}

for _, message := range messages {
if message.View.Height != height {
return false
Expand Down
Loading

0 comments on commit 16fac25

Please sign in to comment.