Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: add doublesign counter #2419

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"math/big"
"math/rand"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -80,6 +81,7 @@
verifyVoteAttestationErrorCounter = metrics.NewRegisteredCounter("parlia/verifyVoteAttestation/error", nil)
updateAttestationErrorCounter = metrics.NewRegisteredCounter("parlia/updateAttestation/error", nil)
validVotesfromSelfCounter = metrics.NewRegisteredCounter("parlia/VerifyVote/self", nil)
doubleSignCounter = metrics.NewRegisteredCounter("parlia/doublesign", nil)

systemContracts = map[common.Address]bool{
common.HexToAddress(systemcontracts.ValidatorContract): true,
Expand Down Expand Up @@ -216,8 +218,11 @@
genesisHash common.Hash
db ethdb.Database // Database to store and retrieve snapshot checkpoints

recentSnaps *lru.ARCCache // Snapshots for recent block to speed up
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
recentSnaps *lru.ARCCache // Snapshots for recent block to speed up
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining
recentHeaders *lru.ARCCache //
// Recent headers to check for double signing: key includes block number and miner. value is the block header
// If same key's value already exists for different block header roots then double sign is detected

signer types.Signer

Expand Down Expand Up @@ -263,6 +268,10 @@
if err != nil {
panic(err)
}
recentHeaders, err := lru.NewARC(inMemorySignatures)
zzzckck marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}
vABIBeforeLuban, err := abi.JSON(strings.NewReader(validatorSetABIBeforeLuban))
if err != nil {
panic(err)
Expand All @@ -286,6 +295,7 @@
db: db,
ethAPI: ethAPI,
recentSnaps: recentSnaps,
recentHeaders: recentHeaders,
signatures: signatures,
validatorSetABIBeforeLuban: vABIBeforeLuban,
validatorSetABI: vABI,
Expand Down Expand Up @@ -813,6 +823,16 @@
return errUnauthorizedValidator(signer.String())
}

// check for double sign & add to cache
key := proposalKey(*header)

value, ok := p.recentHeaders.Get(key)
if ok {
doubleSignCounter.Inc(1)
zzzckck marked this conversation as resolved.
Show resolved Hide resolved
} else {
p.recentHeaders.Add(key, value)
}

if snap.SignRecently(signer) {
return errRecentlySigned
}
Expand Down Expand Up @@ -2011,3 +2031,17 @@
}
return msg.Gas() - returnGas, err
}

// proposalKey build a key which is a combination of the block number and the proposer address.
func proposalKey(header types.Header) string {

Check failure on line 2036 in consensus/parlia/parlia.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

unnecessary leading newline (whitespace)
zzzckck marked this conversation as resolved.
Show resolved Hide resolved

slotKey := uintToString(header.Number.Uint64())
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
proposerIndexKey := uintToString(header.Coinbase.Big().Uint64())
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved

return slotKey + ":" + proposerIndexKey
}

// Turns a uint64 value to a string representation.
func uintToString(val uint64) string {
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
return strconv.FormatUint(val, 10)
}
Loading