2021-11-15 17:39:55 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2021-11-22 21:20:31 +00:00
|
|
|
"context"
|
2021-11-15 17:39:55 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2021-11-22 21:20:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-12-01 03:35:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-05-02 18:32:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/consensus-types/interfaces"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-11-15 17:39:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-11-24 01:56:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/time/slots"
|
2021-11-15 17:39:55 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-12-15 20:42:20 +00:00
|
|
|
// AggregateReportingPeriod defines the number of epochs between aggregate reports.
|
2021-12-01 03:35:55 +00:00
|
|
|
const AggregateReportingPeriod = 5
|
|
|
|
|
2021-11-22 21:20:31 +00:00
|
|
|
// processBlock handles the cases when
|
2021-12-15 20:42:20 +00:00
|
|
|
// - A block was proposed by one of our tracked validators
|
|
|
|
// - An attestation by one of our tracked validators was included
|
|
|
|
// - An Exit by one of our validators was included
|
|
|
|
// - A Slashing by one of our tracked validators was included
|
|
|
|
// - A Sync Committee Contribution by one of our tracked validators was included
|
2022-05-02 18:32:37 +00:00
|
|
|
func (s *Service) processBlock(ctx context.Context, b interfaces.SignedBeaconBlock) {
|
2021-11-22 21:20:31 +00:00
|
|
|
if b == nil || b.Block() == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
blk := b.Block()
|
|
|
|
|
|
|
|
s.processSlashings(blk)
|
|
|
|
s.processExitsFromBlock(blk)
|
|
|
|
|
|
|
|
root, err := blk.HashTreeRoot()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Could not compute block's hash tree root")
|
|
|
|
return
|
|
|
|
}
|
2022-06-27 13:34:38 +00:00
|
|
|
st := s.config.StateGen.StateByRootIfCachedNoCopy(root)
|
|
|
|
if st == nil {
|
2021-11-22 21:20:31 +00:00
|
|
|
log.WithField("BeaconBlockRoot", fmt.Sprintf("%#x", bytesutil.Trunc(root[:]))).Debug(
|
|
|
|
"Skipping block collection due to state not found in cache")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-24 01:56:34 +00:00
|
|
|
currEpoch := slots.ToEpoch(blk.Slot())
|
|
|
|
s.RLock()
|
|
|
|
lastSyncedEpoch := s.lastSyncedEpoch
|
|
|
|
s.RUnlock()
|
|
|
|
|
|
|
|
if currEpoch != lastSyncedEpoch &&
|
|
|
|
slots.SyncCommitteePeriod(currEpoch) == slots.SyncCommitteePeriod(lastSyncedEpoch) {
|
2022-06-27 13:34:38 +00:00
|
|
|
s.updateSyncCommitteeTrackedVals(st)
|
2021-11-24 01:56:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:34:38 +00:00
|
|
|
s.processSyncAggregate(st, blk)
|
|
|
|
s.processProposedBlock(st, root, blk)
|
|
|
|
s.processAttestations(ctx, st, blk)
|
2021-12-01 03:35:55 +00:00
|
|
|
|
2021-12-01 17:14:08 +00:00
|
|
|
if blk.Slot()%(AggregateReportingPeriod*params.BeaconConfig().SlotsPerEpoch) == 0 {
|
2021-12-01 03:35:55 +00:00
|
|
|
s.logAggregatedPerformance()
|
|
|
|
}
|
2021-11-22 21:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 20:42:20 +00:00
|
|
|
// processProposedBlock logs when the beacon node observes a beacon block from a tracked validator.
|
2022-05-02 18:32:37 +00:00
|
|
|
func (s *Service) processProposedBlock(state state.BeaconState, root [32]byte, blk interfaces.BeaconBlock) {
|
2021-11-24 01:56:34 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
if s.trackedIndex(blk.ProposerIndex()) {
|
2021-11-22 21:20:31 +00:00
|
|
|
// update metrics
|
|
|
|
proposedSlotsCounter.WithLabelValues(fmt.Sprintf("%d", blk.ProposerIndex())).Inc()
|
|
|
|
|
|
|
|
// update the performance map
|
|
|
|
balance, err := state.BalanceAtIndex(blk.ProposerIndex())
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Could not get balance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
latestPerf := s.latestPerformance[blk.ProposerIndex()]
|
2021-11-24 01:56:34 +00:00
|
|
|
balanceChg := int64(balance - latestPerf.balance)
|
2021-11-22 21:20:31 +00:00
|
|
|
latestPerf.balanceChange = balanceChg
|
|
|
|
latestPerf.balance = balance
|
|
|
|
s.latestPerformance[blk.ProposerIndex()] = latestPerf
|
|
|
|
|
|
|
|
aggPerf := s.aggregatedPerformance[blk.ProposerIndex()]
|
|
|
|
aggPerf.totalProposedCount++
|
|
|
|
s.aggregatedPerformance[blk.ProposerIndex()] = aggPerf
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"ProposerIndex": blk.ProposerIndex(),
|
|
|
|
"Slot": blk.Slot(),
|
|
|
|
"Version": blk.Version(),
|
|
|
|
"ParentRoot": fmt.Sprintf("%#x", bytesutil.Trunc(blk.ParentRoot())),
|
|
|
|
"BlockRoot": fmt.Sprintf("%#x", bytesutil.Trunc(root[:])),
|
|
|
|
"NewBalance": balance,
|
|
|
|
"BalanceChange": balanceChg,
|
2021-12-15 20:42:20 +00:00
|
|
|
}).Info("Proposed beacon block was included")
|
2021-11-22 21:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 20:42:20 +00:00
|
|
|
// processSlashings logs the event when tracked validators was slashed
|
2022-05-02 18:32:37 +00:00
|
|
|
func (s *Service) processSlashings(blk interfaces.BeaconBlock) {
|
2021-11-24 01:56:34 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2021-11-15 17:39:55 +00:00
|
|
|
for _, slashing := range blk.Body().ProposerSlashings() {
|
|
|
|
idx := slashing.Header_1.Header.ProposerIndex
|
2021-11-24 01:56:34 +00:00
|
|
|
if s.trackedIndex(idx) {
|
2021-11-15 17:39:55 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"ProposerIndex": idx,
|
2021-12-03 20:18:43 +00:00
|
|
|
"Slot": blk.Slot(),
|
2021-11-15 17:39:55 +00:00
|
|
|
"SlashingSlot": slashing.Header_1.Header.Slot,
|
2021-12-15 20:42:20 +00:00
|
|
|
"BodyRoot1": fmt.Sprintf("%#x", bytesutil.Trunc(slashing.Header_1.Header.BodyRoot)),
|
|
|
|
"BodyRoot2": fmt.Sprintf("%#x", bytesutil.Trunc(slashing.Header_2.Header.BodyRoot)),
|
2021-11-15 17:39:55 +00:00
|
|
|
}).Info("Proposer slashing was included")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, slashing := range blk.Body().AttesterSlashings() {
|
|
|
|
for _, idx := range blocks.SlashableAttesterIndices(slashing) {
|
2021-11-24 01:56:34 +00:00
|
|
|
if s.trackedIndex(types.ValidatorIndex(idx)) {
|
2021-11-15 17:39:55 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2021-12-15 20:42:20 +00:00
|
|
|
"AttesterIndex": idx,
|
|
|
|
"BlockInclusionSlot": blk.Slot(),
|
|
|
|
"AttestationSlot1": slashing.Attestation_1.Data.Slot,
|
|
|
|
"BeaconBlockRoot1": fmt.Sprintf("%#x", bytesutil.Trunc(slashing.Attestation_1.Data.BeaconBlockRoot)),
|
|
|
|
"SourceEpoch1": slashing.Attestation_1.Data.Source.Epoch,
|
|
|
|
"TargetEpoch1": slashing.Attestation_1.Data.Target.Epoch,
|
|
|
|
"AttestationSlot2": slashing.Attestation_2.Data.Slot,
|
|
|
|
"BeaconBlockRoot2": fmt.Sprintf("%#x", bytesutil.Trunc(slashing.Attestation_2.Data.BeaconBlockRoot)),
|
|
|
|
"SourceEpoch2": slashing.Attestation_2.Data.Source.Epoch,
|
|
|
|
"TargetEpoch2": slashing.Attestation_2.Data.Target.Epoch,
|
2021-11-15 17:39:55 +00:00
|
|
|
}).Info("Attester slashing was included")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 03:35:55 +00:00
|
|
|
|
2021-12-15 20:42:20 +00:00
|
|
|
// logAggregatedPerformance logs the collected performance statistics since the start of the service.
|
2021-12-01 03:35:55 +00:00
|
|
|
func (s *Service) logAggregatedPerformance() {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
for idx, p := range s.aggregatedPerformance {
|
|
|
|
if p.totalAttestedCount == 0 || p.totalRequestedCount == 0 || p.startBalance == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
l, ok := s.latestPerformance[idx]
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
percentAtt := float64(p.totalAttestedCount) / float64(p.totalRequestedCount)
|
|
|
|
percentBal := float64(l.balance-p.startBalance) / float64(p.startBalance)
|
|
|
|
percentDistance := float64(p.totalDistance) / float64(p.totalAttestedCount)
|
|
|
|
percentCorrectSource := float64(p.totalCorrectSource) / float64(p.totalAttestedCount)
|
|
|
|
percentCorrectHead := float64(p.totalCorrectHead) / float64(p.totalAttestedCount)
|
|
|
|
percentCorrectTarget := float64(p.totalCorrectTarget) / float64(p.totalAttestedCount)
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"ValidatorIndex": idx,
|
|
|
|
"StartEpoch": p.startEpoch,
|
|
|
|
"StartBalance": p.startBalance,
|
|
|
|
"TotalRequested": p.totalRequestedCount,
|
|
|
|
"AttestationInclusion": fmt.Sprintf("%.2f%%", percentAtt*100),
|
|
|
|
"BalanceChangePct": fmt.Sprintf("%.2f%%", percentBal*100),
|
|
|
|
"CorrectlyVotedSourcePct": fmt.Sprintf("%.2f%%", percentCorrectSource*100),
|
|
|
|
"CorrectlyVotedTargetPct": fmt.Sprintf("%.2f%%", percentCorrectTarget*100),
|
|
|
|
"CorrectlyVotedHeadPct": fmt.Sprintf("%.2f%%", percentCorrectHead*100),
|
|
|
|
"AverageInclusionDistance": fmt.Sprintf("%.1f", percentDistance),
|
|
|
|
"TotalProposedBlocks": p.totalProposedCount,
|
|
|
|
"TotalAggregations": p.totalAggregations,
|
|
|
|
"TotalSyncContributions": p.totalSyncComitteeContributions,
|
|
|
|
}).Info("Aggregated performance since launch")
|
|
|
|
}
|
|
|
|
}
|