-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jakub Sztandera <[email protected]>
- Loading branch information
Showing
3 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package f3 | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
|
||
"github.com/filecoin-project/go-f3/gpbft" | ||
) | ||
|
||
// zero value is valid | ||
type equivocationFilter struct { | ||
lk sync.Mutex | ||
currentInstance uint64 | ||
// sentMessages map unique message slot to its signature | ||
sentMessages map[equivocationKey][]byte | ||
} | ||
|
||
type equivocationKey struct { | ||
Sender gpbft.ActorID | ||
Round uint64 | ||
Phase gpbft.Phase | ||
} | ||
|
||
func (ef *equivocationFilter) formKey(m gpbft.GMessage) equivocationKey { | ||
return equivocationKey{ | ||
Sender: m.Sender, | ||
Round: m.Vote.Round, | ||
Phase: m.Vote.Phase, | ||
} | ||
} | ||
|
||
func (ef *equivocationFilter) Process(m *gpbft.GMessage) bool { | ||
ef.lk.Lock() | ||
defer ef.lk.Unlock() | ||
|
||
if m.Vote.Instance < ef.currentInstance { | ||
// disallow past instances | ||
log.Debugw("disallowing broadcast for past message", "sender", m.Sender, "instance", | ||
m.Vote.Instance, "currentInstance", ef.currentInstance) | ||
return false | ||
} | ||
// lazy initialization or moved onto new instance | ||
if ef.sentMessages == nil || m.Vote.Instance > ef.currentInstance { | ||
// bump to next instance | ||
ef.currentInstance = m.Vote.Instance | ||
ef.sentMessages = make(map[equivocationKey][]byte) | ||
} | ||
|
||
key := ef.formKey(*m) | ||
sig, ok := ef.sentMessages[key] | ||
if ok { | ||
log.Debugw("disallowing broadcast due to signature mismatch", "sender", m.Sender, | ||
"instance", m.Vote.Instance, "round", m.Vote.Round, "phase", m.Vote.Phase) | ||
return bytes.Equal(sig, m.Signature) | ||
} | ||
|
||
ef.sentMessages[key] = m.Signature | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package f3 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/filecoin-project/go-f3/gpbft" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEquivactionFilter_Process(t *testing.T) { | ||
ef := &equivocationFilter{} | ||
|
||
// Test case 1: First message should be processed | ||
msg1 := &gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Instance: 1, Round: 1, Phase: gpbft.Phase(1)}, | ||
Signature: []byte("signature1"), | ||
} | ||
assert.True(t, ef.Process(msg1), "First message should be processed") | ||
|
||
// Test case 2: Duplicate message with same signature should be processed | ||
msg2 := &gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Instance: 1, Round: 1, Phase: gpbft.Phase(1)}, | ||
Signature: []byte("signature1"), | ||
} | ||
assert.True(t, ef.Process(msg2), "Duplicate message with same signature should be processed") | ||
|
||
// Test case 3 Message with same key but different signature should not be processed | ||
msg3 := &gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Instance: 1, Round: 1, Phase: gpbft.Phase(1)}, | ||
Signature: []byte("signature2"), | ||
} | ||
assert.False(t, ef.Process(msg3), "Message with same key but different signature should not be processed") | ||
|
||
// Test case 4: Message with new instance should be processed | ||
msg4 := &gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Instance: 2, Round: 1, Phase: gpbft.Phase(1)}, | ||
Signature: []byte("signature3"), | ||
} | ||
assert.True(t, ef.Process(msg4), "Message with new instance should be processed") | ||
|
||
// Test case 5: Message with past instance should not be processed | ||
msg5 := &gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Instance: 1, Round: 1, Phase: gpbft.Phase(1)}, | ||
Signature: []byte("signature4"), | ||
} | ||
assert.False(t, ef.Process(msg5), "Message with past instance should not be processed") | ||
} | ||
|
||
func TestEquivactionFilter_formKey(t *testing.T) { | ||
ef := &equivocationFilter{} | ||
|
||
msg := gpbft.GMessage{ | ||
Sender: gpbft.ActorID(1), | ||
Vote: gpbft.Payload{Round: 1, Phase: gpbft.Phase(1)}, | ||
} | ||
|
||
expectedKey := equivocationKey{ | ||
Sender: gpbft.ActorID(1), | ||
Round: 1, | ||
Phase: gpbft.Phase(1), | ||
} | ||
|
||
assert.Equal(t, expectedKey, ef.formKey(msg), "Keys should match") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters