-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add malicious vote monitor (#1597)
- Loading branch information
Showing
9 changed files
with
347 additions
and
12 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
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
File renamed without changes.
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,85 @@ | ||
package monitor | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/metrics" | ||
lru "github.com/hashicorp/golang-lru" | ||
) | ||
|
||
// follow define in core/vote | ||
const ( | ||
maxSizeOfRecentEntry = 512 | ||
maliciousVoteSlashScope = 256 | ||
upperLimitOfVoteBlockNumber = 11 | ||
) | ||
|
||
var ( | ||
violateRule1Counter = metrics.NewRegisteredCounter("monitor/maliciousVote/violateRule1", nil) | ||
violateRule2Counter = metrics.NewRegisteredCounter("monitor/maliciousVote/violateRule2", nil) | ||
) | ||
|
||
// two purposes | ||
// 1. monitor whether there are bugs in the voting mechanism, so add metrics to observe it. | ||
// 2. do malicious vote slashing. TODO | ||
type MaliciousVoteMonitor struct { | ||
curVotes map[types.BLSPublicKey]*lru.Cache | ||
} | ||
|
||
func NewMaliciousVoteMonitor() *MaliciousVoteMonitor { | ||
return &MaliciousVoteMonitor{ | ||
curVotes: make(map[types.BLSPublicKey]*lru.Cache, 21), // mainnet config | ||
} | ||
} | ||
|
||
func (m *MaliciousVoteMonitor) ConflictDetect(newVote *types.VoteEnvelope, pendingBlockNumber uint64) bool { | ||
// get votes for specified VoteAddress | ||
if _, ok := m.curVotes[newVote.VoteAddress]; !ok { | ||
voteDataBuffer, err := lru.New(maxSizeOfRecentEntry) | ||
if err != nil { | ||
log.Error("MaliciousVoteMonitor new lru failed", "err", err) | ||
return false | ||
} | ||
m.curVotes[newVote.VoteAddress] = voteDataBuffer | ||
} | ||
voteDataBuffer := m.curVotes[newVote.VoteAddress] | ||
sourceNumber, targetNumber := newVote.Data.SourceNumber, newVote.Data.TargetNumber | ||
|
||
//Basic check | ||
// refer to https://github.com/bnb-chain/bsc-genesis-contract/blob/master/contracts/SlashIndicator.sol#LL207C4-L207C4 | ||
if !(targetNumber+maliciousVoteSlashScope > pendingBlockNumber) { | ||
return false | ||
} | ||
|
||
// UnderRules check | ||
blockNumber := sourceNumber + 1 | ||
if !(blockNumber+maliciousVoteSlashScope > pendingBlockNumber) { | ||
blockNumber = pendingBlockNumber - maliciousVoteSlashScope + 1 | ||
} | ||
for ; blockNumber <= pendingBlockNumber+upperLimitOfVoteBlockNumber; blockNumber++ { | ||
if voteDataBuffer.Contains(blockNumber) { | ||
voteData, ok := voteDataBuffer.Get(blockNumber) | ||
if !ok { | ||
log.Error("Failed to get voteData info from LRU cache.") | ||
continue | ||
} | ||
if blockNumber == targetNumber { | ||
log.Warn("violate rule1", "VoteAddress", common.Bytes2Hex(newVote.VoteAddress[:]), "voteExisted", voteData.(*types.VoteData), "newVote", newVote.Data) | ||
violateRule1Counter.Inc(1) | ||
// prepare message for slashing | ||
return true | ||
} else if (blockNumber < targetNumber && voteData.(*types.VoteData).SourceNumber > sourceNumber) || | ||
(blockNumber > targetNumber && voteData.(*types.VoteData).SourceNumber < sourceNumber) { | ||
log.Warn("violate rule2", "VoteAddress", common.Bytes2Hex(newVote.VoteAddress[:]), "voteExisted", voteData.(*types.VoteData), "newVote", newVote.Data) | ||
violateRule2Counter.Inc(1) | ||
// prepare message for slashing | ||
return true | ||
} | ||
} | ||
} | ||
|
||
// for simplicity, Just override even if the targetNumber has existed. | ||
voteDataBuffer.Add(newVote.Data.TargetNumber, newVote.Data) | ||
return false | ||
} |
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,210 @@ | ||
package monitor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMaliciousVoteMonitor(t *testing.T) { | ||
//log.Root().SetHandler(log.StdoutHandler) | ||
// case 1, different voteAddress | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
voteAddress[0] = 4 | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
} | ||
|
||
// case 2, target number not in maliciousVoteSlashScope | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - maliciousVoteSlashScope - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
} | ||
|
||
// case 3, violate rule1 | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: uint64(0), | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
} | ||
|
||
// case 4, violate rule2, vote with smaller range first | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 4, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 2, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 3, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
} | ||
|
||
// case 5, violate rule2, vote with larger range first | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 2, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 3, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 4, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, true, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
} | ||
|
||
// case 6, normal case | ||
{ | ||
maliciousVoteMonitor := NewMaliciousVoteMonitor() | ||
pendingBlockNumber := uint64(1000) | ||
voteAddrBytes := common.Hex2BytesFixed("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", types.BLSPublicKeyLength) | ||
voteAddress := types.BLSPublicKey{} | ||
copy(voteAddress[:], voteAddrBytes[:]) | ||
vote1 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 4, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 3, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote1, pendingBlockNumber)) | ||
vote2 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 3, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 2, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote2, pendingBlockNumber)) | ||
vote3 := &types.VoteEnvelope{ | ||
VoteAddress: voteAddress, | ||
Signature: types.BLSSignature{}, | ||
Data: &types.VoteData{ | ||
SourceNumber: pendingBlockNumber - 2, | ||
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))), | ||
TargetNumber: pendingBlockNumber - 1, | ||
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(2)))), | ||
}, | ||
} | ||
assert.Equal(t, false, maliciousVoteMonitor.ConflictDetect(vote3, pendingBlockNumber)) | ||
} | ||
} |
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
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
Oops, something went wrong.