2023-04-08 01:01:10 +00:00
|
|
|
package forkchoice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-27 09:15:51 +00:00
|
|
|
"time"
|
2023-05-14 22:12:24 +00:00
|
|
|
|
2024-01-08 16:13:25 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2023-05-14 22:12:24 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
|
2023-05-13 21:44:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/cache"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
|
2023-09-27 09:15:51 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2023-04-08 01:01:10 +00:00
|
|
|
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
)
|
|
|
|
|
2023-09-27 09:15:51 +00:00
|
|
|
// OnAttestation processes incoming attestations.
|
2024-01-08 16:13:25 +00:00
|
|
|
func (f *ForkChoiceStore) OnAttestation(attestation *solid.Attestation, fromBlock bool, insert bool) error {
|
2023-04-08 01:01:10 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
2023-09-29 21:42:07 +00:00
|
|
|
f.headHash = libcommon.Hash{}
|
2023-05-14 22:12:24 +00:00
|
|
|
data := attestation.AttestantionData()
|
2023-04-08 01:01:10 +00:00
|
|
|
if err := f.validateOnAttestation(attestation, fromBlock); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-27 09:15:51 +00:00
|
|
|
// Schedule for later processing.
|
|
|
|
if f.Slot() < attestation.AttestantionData().Slot()+1 {
|
|
|
|
f.scheduleAttestationForLaterProcessing(attestation, fromBlock)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
target := data.Target()
|
2023-06-08 13:52:09 +00:00
|
|
|
if cachedIndicies, ok := cache.LoadAttestatingIndicies(&data, attestation.AggregationBits()); ok {
|
2023-05-11 22:47:26 +00:00
|
|
|
f.processAttestingIndicies(attestation, cachedIndicies)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
targetState, err := f.getCheckpointState(target)
|
2023-04-08 01:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Verify attestation signature.
|
|
|
|
if targetState == nil {
|
|
|
|
return fmt.Errorf("target state does not exist")
|
|
|
|
}
|
2023-05-03 08:51:39 +00:00
|
|
|
// Now we need to find the attesting indicies.
|
2023-05-14 22:12:24 +00:00
|
|
|
attestationIndicies, err := targetState.getAttestingIndicies(&data, attestation.AggregationBits())
|
2023-04-08 01:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fromBlock {
|
2023-05-03 08:51:39 +00:00
|
|
|
indexedAttestation := state.GetIndexedAttestation(attestation, attestationIndicies)
|
2023-04-08 01:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:51:39 +00:00
|
|
|
valid, err := targetState.isValidIndexedAttestation(indexedAttestation)
|
2023-04-08 01:01:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("invalid attestation")
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 09:15:51 +00:00
|
|
|
cache.StoreAttestation(&data, attestation.AggregationBits(), attestationIndicies)
|
2023-04-08 01:01:10 +00:00
|
|
|
// Lastly update latest messages.
|
2023-05-11 22:47:26 +00:00
|
|
|
f.processAttestingIndicies(attestation, attestationIndicies)
|
2024-01-08 16:13:25 +00:00
|
|
|
if !fromBlock && insert {
|
|
|
|
// Add to the pool when verified.
|
|
|
|
f.operationsPool.AttestationsPool.Insert(attestation.Signature(), attestation)
|
|
|
|
}
|
2023-05-11 22:47:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 16:13:25 +00:00
|
|
|
func (f *ForkChoiceStore) OnAggregateAndProof(aggregateAndProof *cltypes.SignedAggregateAndProof, test bool) error {
|
|
|
|
slot := aggregateAndProof.Message.Aggregate.AttestantionData().Slot()
|
|
|
|
selectionProof := aggregateAndProof.Message.SelectionProof
|
|
|
|
committeeIndex := aggregateAndProof.Message.Aggregate.AttestantionData().ValidatorIndex()
|
|
|
|
epoch := state.GetEpochAtSlot(f.beaconCfg, slot)
|
|
|
|
|
|
|
|
target := aggregateAndProof.Message.Aggregate.AttestantionData().Target()
|
|
|
|
targetState, err := f.getCheckpointState(target)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
activeIndicies := targetState.getActiveIndicies(epoch)
|
|
|
|
activeIndiciesLength := uint64(len(activeIndicies))
|
|
|
|
|
|
|
|
count := targetState.committeeCount(epoch, activeIndiciesLength) * f.beaconCfg.SlotsPerEpoch
|
|
|
|
start := (activeIndiciesLength * committeeIndex) / count
|
|
|
|
end := (activeIndiciesLength * (committeeIndex + 1)) / count
|
|
|
|
committeeLength := end - start
|
|
|
|
if !state.IsAggregator(f.beaconCfg, committeeLength, slot, committeeIndex, selectionProof) {
|
|
|
|
log.Warn("invalid aggregate and proof")
|
|
|
|
return fmt.Errorf("invalid aggregate and proof")
|
|
|
|
}
|
|
|
|
return f.OnAttestation(aggregateAndProof.Message.Aggregate, false, false)
|
|
|
|
}
|
|
|
|
|
2023-09-27 09:15:51 +00:00
|
|
|
// scheduleAttestationForLaterProcessing scheudules an attestation for later processing
|
2024-01-08 16:13:25 +00:00
|
|
|
func (f *ForkChoiceStore) scheduleAttestationForLaterProcessing(attestation *solid.Attestation, insert bool) {
|
2023-09-27 09:15:51 +00:00
|
|
|
go func() {
|
|
|
|
logInterval := time.NewTicker(50 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-f.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-logInterval.C:
|
|
|
|
if f.Slot() < attestation.AttestantionData().Slot()+1 {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-08 16:13:25 +00:00
|
|
|
if err := f.OnAttestation(attestation, false, insert); err != nil {
|
|
|
|
log.Debug("could not process scheduled attestation", "reason", err)
|
2023-09-27 09:15:51 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-01-06 20:49:23 +00:00
|
|
|
func (f *ForkChoiceStore) setLatestMessage(index uint64, message LatestMessage) {
|
|
|
|
if index >= uint64(len(f.latestMessages)) {
|
|
|
|
if index >= uint64(cap(f.latestMessages)) {
|
|
|
|
tmp := make([]LatestMessage, index+1, index*2)
|
|
|
|
copy(tmp, f.latestMessages)
|
|
|
|
f.latestMessages = tmp
|
|
|
|
}
|
|
|
|
f.latestMessages = f.latestMessages[:index+1]
|
|
|
|
}
|
|
|
|
f.latestMessages[index] = message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkChoiceStore) getLatestMessage(validatorIndex uint64) (LatestMessage, bool) {
|
|
|
|
if validatorIndex >= uint64(len(f.latestMessages)) || f.latestMessages[validatorIndex] == (LatestMessage{}) {
|
|
|
|
return LatestMessage{}, false
|
|
|
|
}
|
|
|
|
return f.latestMessages[validatorIndex], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkChoiceStore) isUnequivocating(validatorIndex uint64) bool {
|
|
|
|
// f.equivocatingIndicies is a bitlist
|
|
|
|
index := int(validatorIndex) / 8
|
|
|
|
if index >= len(f.equivocatingIndicies) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
subIndex := int(validatorIndex) % 8
|
|
|
|
return f.equivocatingIndicies[index]&(1<<uint(subIndex)) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ForkChoiceStore) setUnequivocating(validatorIndex uint64) {
|
|
|
|
index := int(validatorIndex) / 8
|
|
|
|
if index >= len(f.equivocatingIndicies) {
|
|
|
|
if index >= cap(f.equivocatingIndicies) {
|
|
|
|
tmp := make([]byte, index+1, index*2)
|
|
|
|
copy(tmp, f.equivocatingIndicies)
|
|
|
|
f.equivocatingIndicies = tmp
|
|
|
|
}
|
|
|
|
f.equivocatingIndicies = f.equivocatingIndicies[:index+1]
|
|
|
|
}
|
|
|
|
subIndex := int(validatorIndex) % 8
|
|
|
|
f.equivocatingIndicies[index] |= 1 << uint(subIndex)
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:12:24 +00:00
|
|
|
func (f *ForkChoiceStore) processAttestingIndicies(attestation *solid.Attestation, indicies []uint64) {
|
|
|
|
beaconBlockRoot := attestation.AttestantionData().BeaconBlockRoot()
|
|
|
|
target := attestation.AttestantionData().Target()
|
2023-05-11 22:47:26 +00:00
|
|
|
|
|
|
|
for _, index := range indicies {
|
2024-01-06 20:49:23 +00:00
|
|
|
if f.isUnequivocating(index) {
|
2023-04-08 01:01:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-01-06 20:49:23 +00:00
|
|
|
validatorMessage, has := f.getLatestMessage(index)
|
2023-05-14 22:12:24 +00:00
|
|
|
if !has || target.Epoch() > validatorMessage.Epoch {
|
2024-01-06 20:49:23 +00:00
|
|
|
f.setLatestMessage(index, LatestMessage{
|
2023-05-14 22:12:24 +00:00
|
|
|
Epoch: target.Epoch(),
|
2023-04-08 01:01:10 +00:00
|
|
|
Root: beaconBlockRoot,
|
2024-01-06 20:49:23 +00:00
|
|
|
})
|
2023-04-08 01:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:12:24 +00:00
|
|
|
func (f *ForkChoiceStore) validateOnAttestation(attestation *solid.Attestation, fromBlock bool) error {
|
|
|
|
target := attestation.AttestantionData().Target()
|
2023-04-08 01:01:10 +00:00
|
|
|
|
|
|
|
if !fromBlock {
|
|
|
|
if err := f.validateTargetEpochAgainstCurrentTime(attestation); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
if target.Epoch() != f.computeEpochAtSlot(attestation.AttestantionData().Slot()) {
|
2023-04-08 01:01:10 +00:00
|
|
|
return fmt.Errorf("mismatching target epoch with slot data")
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
if _, has := f.forkGraph.GetHeader(target.BlockRoot()); !has {
|
2023-04-08 01:01:10 +00:00
|
|
|
return fmt.Errorf("target root is missing")
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
if blockHeader, has := f.forkGraph.GetHeader(attestation.AttestantionData().BeaconBlockRoot()); !has || blockHeader.Slot > attestation.AttestantionData().Slot() {
|
2023-04-08 01:01:10 +00:00
|
|
|
return fmt.Errorf("bad attestation data")
|
|
|
|
}
|
|
|
|
// LMD vote must be consistent with FFG vote target
|
2023-05-14 22:12:24 +00:00
|
|
|
targetSlot := f.computeStartSlotAtEpoch(target.Epoch())
|
|
|
|
ancestorRoot := f.Ancestor(attestation.AttestantionData().BeaconBlockRoot(), targetSlot)
|
2023-04-08 01:01:10 +00:00
|
|
|
if ancestorRoot == (libcommon.Hash{}) {
|
|
|
|
return fmt.Errorf("could not retrieve ancestor")
|
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
if ancestorRoot != target.BlockRoot() {
|
2023-04-08 01:01:10 +00:00
|
|
|
return fmt.Errorf("ancestor root mismatches with target")
|
|
|
|
}
|
2023-09-27 09:15:51 +00:00
|
|
|
|
2023-04-08 01:01:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:12:24 +00:00
|
|
|
func (f *ForkChoiceStore) validateTargetEpochAgainstCurrentTime(attestation *solid.Attestation) error {
|
|
|
|
target := attestation.AttestantionData().Target()
|
2023-04-08 01:01:10 +00:00
|
|
|
// Attestations must be from the current or previous epoch
|
|
|
|
currentEpoch := f.computeEpochAtSlot(f.Slot())
|
|
|
|
// Use GENESIS_EPOCH for previous when genesis to avoid underflow
|
|
|
|
previousEpoch := currentEpoch - 1
|
2023-10-21 21:10:58 +00:00
|
|
|
if currentEpoch <= f.beaconCfg.GenesisEpoch {
|
|
|
|
previousEpoch = f.beaconCfg.GenesisEpoch
|
2023-04-08 01:01:10 +00:00
|
|
|
}
|
2023-05-14 22:12:24 +00:00
|
|
|
if target.Epoch() == currentEpoch || target.Epoch() == previousEpoch {
|
2023-04-08 01:01:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("verification of attestation against current time failed")
|
|
|
|
}
|