Skip to content

Commit

Permalink
PruneMsgsUntil unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Jul 31, 2023
1 parent 3156c1f commit 6659ec8
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 103 deletions.
146 changes: 43 additions & 103 deletions consensus/polybft/slashing/double_signing_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/0xPolygon/polygon-edge/types"
)

func TestDoubleSigningTracker_Handle(t *testing.T) {
func TestDoubleSigningTracker_Handle_SingleSender(t *testing.T) {
t.Parallel()

acc, err := wallet.GenerateAccount()
Expand All @@ -30,19 +30,15 @@ func TestDoubleSigningTracker_Handle(t *testing.T) {
tracker.Handle(prePrepareMsg)
tracker.Handle(prepareMsg)

prePrepareMsgs := tracker.preprepare.getSenderMsgsLocked(prePrepareView, types.Address(key.Address()))
require.Len(t, prePrepareMsgs, 1)
sender := types.Address(key.Address())

prePrepareMsgs := tracker.preprepare.getSenderMsgsLocked(prePrepareView, sender)
assertSenderMessageMapsSize(t, tracker, 1, 0, 0, 0, prePrepareView, sender)
require.Equal(t, prePrepareMsg, prePrepareMsgs[0])
require.Empty(t, tracker.prepare.getSenderMsgsLocked(prePrepareView, types.Address(key.Address())))
require.Empty(t, tracker.commit.getSenderMsgsLocked(prePrepareView, types.Address(key.Address())))
require.Empty(t, tracker.roundChange.getSenderMsgsLocked(prePrepareView, types.Address(key.Address())))

prepareMsgs := tracker.prepare.getSenderMsgsLocked(view, types.Address(key.Address()))
require.Len(t, prepareMsgs, 1)
prepareMsgs := tracker.prepare.getSenderMsgsLocked(view, sender)
assertSenderMessageMapsSize(t, tracker, 0, 1, 0, 0, view, sender)
require.Equal(t, prepareMsg, prepareMsgs[0])
require.Empty(t, tracker.preprepare.getSenderMsgsLocked(view, types.Address(key.Address())))
require.Empty(t, tracker.commit.getSenderMsgsLocked(view, types.Address(key.Address())))
require.Empty(t, tracker.roundChange.getSenderMsgsLocked(view, types.Address(key.Address())))

view.Round++
prepareMsg = buildPrepareMessage(t, view, key, proposalHash)
Expand All @@ -54,18 +50,16 @@ func TestDoubleSigningTracker_Handle(t *testing.T) {
tracker.Handle(commitMsg)
tracker.Handle(roundChangeMsg)

prepareMsgs = tracker.prepare.getSenderMsgsLocked(view, types.Address(key.Address()))
commitMsgs := tracker.commit.getSenderMsgsLocked(view, types.Address(key.Address()))
roundChangeMsgs := tracker.roundChange.getSenderMsgsLocked(view, types.Address(key.Address()))
prepareMsgs = tracker.prepare.getSenderMsgsLocked(view, sender)
commitMsgs := tracker.commit.getSenderMsgsLocked(view, sender)
roundChangeMsgs := tracker.roundChange.getSenderMsgsLocked(view, sender)

assertSenderMessageMapsSize(t, tracker, 0, 2, 1, 1, view, sender)

require.Len(t, prepareMsgs, 2)
require.Equal(t, prepareMsg, prepareMsgs[0])
require.Equal(t, prepareMsg, prepareMsgs[1])
require.Len(t, commitMsgs, 1)
require.Equal(t, commitMsg, commitMsgs[0])
require.Len(t, roundChangeMsgs, 1)
require.Equal(t, roundChangeMsg, roundChangeMsgs[0])
require.Empty(t, tracker.preprepare.getSenderMsgsLocked(view, types.Address(key.Address())))
}

func TestDoubleSigningTracker_validateMessage(t *testing.T) {
Expand Down Expand Up @@ -168,95 +162,41 @@ func TestDoubleSigningTracker_validateMessage(t *testing.T) {
}
}

func buildPrePrepareMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()

prePrepareMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_PREPREPARE,
Payload: &ibftProto.Message_PreprepareData{
PreprepareData: &ibftProto.PrePrepareMessage{
Proposal: &ibftProto.Proposal{
RawProposal: proposalHash.Bytes(),
Round: 1,
},
ProposalHash: proposalHash.Bytes(),
Certificate: &ibftProto.RoundChangeCertificate{},
},
},
}

prePrepareMsg, err := key.SignIBFTMessage(prePrepareMsg)
require.NoError(t, err)

return prePrepareMsg
}

func buildPrepareMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()

prepareMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_PREPARE,
Payload: &ibftProto.Message_PrepareData{
PrepareData: &ibftProto.PrepareMessage{
ProposalHash: proposalHash.Bytes(),
},
},
}

prepareMsg, err := key.SignIBFTMessage(prepareMsg)
require.NoError(t, err)

return prepareMsg
}

func buildCommitMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()
func TestDoubleSigningTracker_PruneMsgsUntil(t *testing.T) {
t.Parallel()

seal, err := key.Sign(proposalHash.Bytes())
acc, err := wallet.GenerateAccount()
require.NoError(t, err)

commitMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_COMMIT,
Payload: &ibftProto.Message_CommitData{
CommitData: &ibftProto.CommitMessage{
ProposalHash: proposalHash.Bytes(),
CommittedSeal: seal,
},
},
}

commitMsg, err = key.SignIBFTMessage(commitMsg)
require.NoError(t, err)
key := wallet.NewKey(acc)

return commitMsg
}
proposalHash := types.StringToHash("dummy proposal")
view := &ibftProto.View{Height: 1, Round: 1}

func buildRoundChangeMessage(t *testing.T, view *ibftProto.View, key *wallet.Key) *ibftProto.Message {
t.Helper()

roundChangeMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_ROUND_CHANGE,
Payload: &ibftProto.Message_RoundChangeData{
RoundChangeData: &ibftProto.RoundChangeMessage{
LastPreparedProposal: &ibftProto.Proposal{},
LatestPreparedCertificate: &ibftProto.PreparedCertificate{},
},
},
tracker := NewDoubleSigningTracker(hclog.NewNullLogger())
tracker.Handle(buildPrePrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildPrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildCommitMessage(t, view, key, proposalHash))

view.Height = 3
tracker.Handle(buildPrePrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildPrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildCommitMessage(t, view, key, proposalHash))

view = &ibftProto.View{Height: 5, Round: 1}
tracker.Handle(buildPrePrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildPrepareMessage(t, view, key, proposalHash))
tracker.Handle(buildCommitMessage(t, view, key, proposalHash))

view.Round = 4
tracker.Handle(buildCommitMessage(t, view, key, proposalHash))

tracker.PruneMsgsUntil(5)
sender := types.Address(key.Address())

Check failure on line 195 in consensus/polybft/slashing/double_signing_tracker_test.go

View workflow job for this annotation

GitHub Actions / golangci_lint

assignments should only be cuddled with other assignments (wsl)

Check failure on line 195 in consensus/polybft/slashing/double_signing_tracker_test.go

View workflow job for this annotation

GitHub Actions / golangci_lint

assignments should only be cuddled with other assignments (wsl)
for height := uint64(0); height < view.Height; height++ {

Check failure on line 196 in consensus/polybft/slashing/double_signing_tracker_test.go

View workflow job for this annotation

GitHub Actions / golangci_lint

only one cuddle assignment allowed before for statement (wsl)

Check failure on line 196 in consensus/polybft/slashing/double_signing_tracker_test.go

View workflow job for this annotation

GitHub Actions / golangci_lint

only one cuddle assignment allowed before for statement (wsl)
for round := uint64(0); round < view.Round; round++ {
currentView := &ibftProto.View{Height: height, Round: round}
assertSenderMessageMapsSize(t, tracker, 0, 0, 0, 0, currentView, sender)
}
}

roundChangeMsg, err := key.SignIBFTMessage(roundChangeMsg)
require.NoError(t, err)

return roundChangeMsg
}
120 changes: 120 additions & 0 deletions consensus/polybft/slashing/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package slashing

import (
"testing"

ibftProto "github.com/0xPolygon/go-ibft/messages/proto"
"github.com/stretchr/testify/require"

"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/types"
)

func buildPrePrepareMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()

prePrepareMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_PREPREPARE,
Payload: &ibftProto.Message_PreprepareData{
PreprepareData: &ibftProto.PrePrepareMessage{
Proposal: &ibftProto.Proposal{
RawProposal: proposalHash.Bytes(),
Round: 1,
},
ProposalHash: proposalHash.Bytes(),
Certificate: &ibftProto.RoundChangeCertificate{},
},
},
}

prePrepareMsg, err := key.SignIBFTMessage(prePrepareMsg)
require.NoError(t, err)

return prePrepareMsg
}

func buildPrepareMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()

prepareMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_PREPARE,
Payload: &ibftProto.Message_PrepareData{
PrepareData: &ibftProto.PrepareMessage{
ProposalHash: proposalHash.Bytes(),
},
},
}

prepareMsg, err := key.SignIBFTMessage(prepareMsg)
require.NoError(t, err)

return prepareMsg
}

func buildCommitMessage(t *testing.T, view *ibftProto.View,
key *wallet.Key, proposalHash types.Hash) *ibftProto.Message {
t.Helper()

seal, err := key.Sign(proposalHash.Bytes())
require.NoError(t, err)

commitMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_COMMIT,
Payload: &ibftProto.Message_CommitData{
CommitData: &ibftProto.CommitMessage{
ProposalHash: proposalHash.Bytes(),
CommittedSeal: seal,
},
},
}

commitMsg, err = key.SignIBFTMessage(commitMsg)
require.NoError(t, err)

return commitMsg
}

func buildRoundChangeMessage(t *testing.T, view *ibftProto.View, key *wallet.Key) *ibftProto.Message {
t.Helper()

roundChangeMsg := &ibftProto.Message{
View: view,
From: key.Address().Bytes(),
Type: ibftProto.MessageType_ROUND_CHANGE,
Payload: &ibftProto.Message_RoundChangeData{
RoundChangeData: &ibftProto.RoundChangeMessage{
LastPreparedProposal: &ibftProto.Proposal{},
LatestPreparedCertificate: &ibftProto.PreparedCertificate{},
},
},
}

roundChangeMsg, err := key.SignIBFTMessage(roundChangeMsg)
require.NoError(t, err)

return roundChangeMsg
}

func assertSenderMessageMapsSize(t *testing.T, tracker *DoubleSigningTrackerImpl,
prePrepareCount, prepareCount, commitCount, roundChangeCount int,
view *ibftProto.View, sender types.Address) {
t.Helper()

prePrepareMsgs := tracker.preprepare.getSenderMsgsLocked(view, sender)
prepareMsgs := tracker.prepare.getSenderMsgsLocked(view, sender)
commitMsgs := tracker.commit.getSenderMsgsLocked(view, sender)
roundChangeMsgs := tracker.roundChange.getSenderMsgsLocked(view, sender)

require.Len(t, prePrepareMsgs, prePrepareCount)
require.Len(t, prepareMsgs, prepareCount)
require.Len(t, commitMsgs, commitCount)
require.Len(t, roundChangeMsgs, roundChangeCount)
}

0 comments on commit 6659ec8

Please sign in to comment.