2022-04-27 18:01:59 +00:00
|
|
|
// This file contains all the functions to handle SSE events after they have been turned
|
|
|
|
// to the structs.
|
|
|
|
|
2022-04-27 14:28:42 +00:00
|
|
|
package beaconclient
|
|
|
|
|
2022-05-06 15:03:15 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-16 15:49:23 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
2022-05-06 15:03:15 +00:00
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
2022-05-16 15:49:23 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-05-06 15:03:15 +00:00
|
|
|
)
|
2022-04-27 14:28:42 +00:00
|
|
|
|
|
|
|
// This function will perform the necessary steps to handle a reorg.
|
2022-05-06 15:03:15 +00:00
|
|
|
func (bc *BeaconClient) handleReorg() {
|
2022-04-27 14:28:42 +00:00
|
|
|
log.Info("Starting to process reorgs.")
|
|
|
|
for {
|
|
|
|
reorg := <-bc.ReOrgTracking.ProcessCh
|
|
|
|
log.WithFields(log.Fields{"reorg": reorg}).Debug("Received a new reorg message.")
|
2022-05-12 13:52:13 +00:00
|
|
|
writeReorgs(bc.Db, reorg.Slot, reorg.NewHeadBlock, bc.Metrics)
|
2022-04-27 14:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function will handle the latest head event.
|
|
|
|
func (bc *BeaconClient) handleHead() {
|
|
|
|
log.Info("Starting to process head.")
|
2022-05-12 19:44:05 +00:00
|
|
|
errorSlots := 0
|
2022-04-27 14:28:42 +00:00
|
|
|
for {
|
2022-05-06 15:03:15 +00:00
|
|
|
head := <-bc.HeadTracking.ProcessCh
|
|
|
|
// Process all the work here.
|
|
|
|
slot, err := strconv.Atoi(head.Slot)
|
|
|
|
if err != nil {
|
|
|
|
bc.HeadTracking.ErrorCh <- &SseError{
|
|
|
|
err: fmt.Errorf("Unable to turn the slot from string to int: %s", head.Slot),
|
|
|
|
}
|
2022-05-12 19:44:05 +00:00
|
|
|
errorSlots = errorSlots + 1
|
|
|
|
continue
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-05-12 19:44:05 +00:00
|
|
|
if errorSlots != 0 && bc.PreviousSlot != 0 {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"lastProcessedSlot": bc.PreviousSlot,
|
|
|
|
"errorMessages": errorSlots,
|
|
|
|
}).Warn("We added slots to the knownGaps table because we got bad head messages.")
|
2022-05-16 18:10:43 +00:00
|
|
|
writeKnownGaps(bc.Db, bc.KnownGapTableIncrement, bc.PreviousSlot, bcSlotsPerEpoch+errorSlots, fmt.Errorf("Bad Head Messages"), "headProcessing", bc.Metrics)
|
2022-05-12 19:44:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 15:49:23 +00:00
|
|
|
log.WithFields(log.Fields{"head": head}).Debug("We are going to start processing the slot.")
|
|
|
|
|
|
|
|
go func(db sql.Database, serverAddress string, slot int, blockRoot string, stateRoot string, previousSlot int, previousBlockRoot string, metrics *BeaconClientMetrics, knownGapsTableIncrement int) {
|
|
|
|
errG := new(errgroup.Group)
|
|
|
|
errG.Go(func() error {
|
|
|
|
err = processHeadSlot(db, serverAddress, slot, blockRoot, stateRoot, previousSlot, previousBlockRoot, metrics, knownGapsTableIncrement)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err := errG.Wait(); err != nil {
|
|
|
|
loghelper.LogSlotError(strconv.Itoa(slot), err).Error("Unable to process a slot")
|
|
|
|
}
|
|
|
|
}(bc.Db, bc.ServerEndpoint, slot, head.Block, head.State, bc.PreviousSlot, bc.PreviousBlockRoot, bc.Metrics, bc.KnownGapTableIncrement)
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"head": head.Slot}).Debug("We finished calling processHeadSlot.")
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
// Update the previous block
|
|
|
|
bc.PreviousSlot = slot
|
|
|
|
bc.PreviousBlockRoot = head.Block
|
2022-04-27 14:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|