-
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.
- Loading branch information
Showing
8 changed files
with
148 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,97 @@ | ||
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) Verify(newVote *types.VoteEnvelope, pendingBlockNumber uint64) { | ||
// 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 | ||
} | ||
m.curVotes[newVote.VoteAddress] = voteDataBuffer | ||
} | ||
voteDataBuffer := m.curVotes[newVote.VoteAddress] | ||
sourceNumber, targetNumber := newVote.Data.SourceNumber, newVote.Data.TargetNumber | ||
|
||
//Rule 1: A validator must not publish two distinct votes for the same height. | ||
if voteDataBuffer.Contains(targetNumber) { | ||
voteData, ok := voteDataBuffer.Get(targetNumber) | ||
if !ok { | ||
log.Error("Failed to get voteData info from LRU cache.") | ||
return | ||
} | ||
log.Warn("violate rule1", "VoteAddress", common.Bytes2Hex(newVote.VoteAddress[:]), "voteExisted", voteData.(*types.VoteData), "newVote", newVote) | ||
violateRule1Counter.Inc(1) | ||
// prepare message for slashing | ||
} | ||
|
||
//Rule 2: A validator must not vote within the span of its other votes. | ||
blockNumber := sourceNumber + 1 | ||
if blockNumber+maliciousVoteSlashScope < pendingBlockNumber { | ||
blockNumber = pendingBlockNumber - maliciousVoteSlashScope | ||
} | ||
for ; blockNumber < pendingBlockNumber; blockNumber++ { | ||
if voteDataBuffer.Contains(blockNumber) { | ||
voteData, ok := voteDataBuffer.Get(blockNumber) | ||
if !ok { | ||
log.Error("Failed to get voteData info from LRU cache.") | ||
return | ||
} | ||
if voteData.(*types.VoteData).SourceNumber > sourceNumber { | ||
log.Warn("violate rule2", "VoteAddress", common.Bytes2Hex(newVote.VoteAddress[:]), "voteExisted", voteData.(*types.VoteData), "newVote", newVote) | ||
violateRule2Counter.Inc(1) | ||
// prepare message for slashing | ||
} | ||
} | ||
} | ||
for blockNumber := targetNumber + 1; 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.") | ||
return | ||
} | ||
if voteData.(*types.VoteData).SourceNumber < sourceNumber { | ||
log.Warn("violate rule2", "VoteAddress", common.Bytes2Hex(newVote.VoteAddress[:]), "voteExisted", voteData.(*types.VoteData), "newVote", newVote) | ||
violateRule2Counter.Inc(1) | ||
// no need to prepare message for slashing | ||
} | ||
} | ||
} | ||
|
||
// for simplicity, Just override even if the targetNumber has existed. | ||
voteDataBuffer.Add(newVote.Data.TargetNumber, newVote.Data) | ||
} |
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
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