Skip to content

Commit

Permalink
Simplify double signer evidence (#1812)
Browse files Browse the repository at this point in the history
* Simplification

* Suppress linting error
  • Loading branch information
Stefan-Ethernal committed Aug 15, 2023
1 parent c0dcc0e commit 49bc267
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
30 changes: 8 additions & 22 deletions consensus/polybft/slashing/double_signing_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,11 @@ var (
type SenderMessagesMap map[types.Address][]*ibftProto.Message
type MessagesMap map[uint64]map[uint64]SenderMessagesMap

type DoubleSignEvidences []*DoubleSignEvidence
type DoubleSigners []types.Address

//nolint:godox
// TODO RLP serialize/deserialize methods, Equals method for validation, etc.

type DoubleSignEvidence struct {
signer types.Address
messages []*ibftProto.Message
}

func newDoubleSignEvidence(signer types.Address,
messages []*ibftProto.Message) *DoubleSignEvidence {
return &DoubleSignEvidence{signer: signer, messages: messages}
}

type Messages struct {
content MessagesMap
mux sync.RWMutex
Expand Down Expand Up @@ -93,7 +84,7 @@ var _ DoubleSigningTracker = (*DoubleSigningTrackerImpl)(nil)

type DoubleSigningTracker interface {
Handle(msg *ibftProto.Message)
GetEvidences(height uint64) []*DoubleSignEvidence
GetDoubleSigners(height uint64) DoubleSigners
PruneMsgsUntil(height uint64)
PostBlock(req *common.PostBlockRequest) error
}
Expand Down Expand Up @@ -171,9 +162,9 @@ func (t *DoubleSigningTrackerImpl) PruneMsgsUntil(height uint64) {
}
}

// GetEvidences returns double signing evidences for the given height
func (t *DoubleSigningTrackerImpl) GetEvidences(height uint64) []*DoubleSignEvidence {
evidences := []*DoubleSignEvidence{}
// GetEvidences returns double signers for the given height
func (t *DoubleSigningTrackerImpl) GetDoubleSigners(height uint64) DoubleSigners {
doubleSigners := make(DoubleSigners, 0)

for _, msgType := range t.msgsTypes {
msgs := t.resolveMessagesStorage(msgType)
Expand All @@ -194,19 +185,14 @@ func (t *DoubleSigningTrackerImpl) GetEvidences(height uint64) []*DoubleSignEvid
continue
}

evidence := newDoubleSignEvidence(address, []*ibftProto.Message{msgs[0]})
evidences = append(evidences, evidence)

for _, msg := range msgs[1:] {
evidence.messages = append(evidence.messages, msg)
}
doubleSigners = append(doubleSigners, address)
}
}

msgs.mux.Unlock()
}

return evidences
return doubleSigners
}

// PostBlock is used to populate all known validators
Expand Down
21 changes: 6 additions & 15 deletions consensus/polybft/slashing/double_signing_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func TestDoubleSigningTracker_PruneMsgsUntil(t *testing.T) {
}
}

func TestDoubleSigningTracker_GetEvidences(t *testing.T) {
func TestDoubleSigningTracker_GetDoubleSigners(t *testing.T) {
t.Parallel()

const (
Expand All @@ -354,6 +354,7 @@ func TestDoubleSigningTracker_GetEvidences(t *testing.T) {
require.NoError(t, err)

key := wallet.NewKey(acc)
senderAddr := types.Address(key.Address())
view := &ibftProto.View{Height: 6, Round: 2}
tracker, err := NewDoubleSigningTracker(hclog.NewNullLogger(), &dummyValidatorsProvider{accounts: []*wallet.Account{acc}})
require.NoError(t, err)
Expand All @@ -375,21 +376,11 @@ func TestDoubleSigningTracker_GetEvidences(t *testing.T) {
tracker.Handle(msg)
}

evidences := tracker.GetEvidences(view.Height)
require.Len(t, evidences, 2)
doubleSigners := tracker.GetDoubleSigners(view.Height)
require.Len(t, doubleSigners, 2)

prepareEvidence := evidences[0]
require.Len(t, prepareEvidence.messages, len(prepareMessages))

commitEvidence := evidences[1]
require.Len(t, commitEvidence.messages, len(commitMessages))

for i, msg := range prepareMessages {
require.Equal(t, msg, prepareEvidence.messages[i])
}

for i, msg := range commitMessages {
require.Equal(t, msg, commitEvidence.messages[i])
for _, doubleSigner := range doubleSigners {
require.Equal(t, senderAddr, doubleSigner)
}
}

Expand Down

0 comments on commit 49bc267

Please sign in to comment.