2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-05-06 15:03:15 +00:00
|
|
|
// This file will keep track of all the code needed to process a slot.
|
|
|
|
// To process a slot, it should have all the necessary data needed to write it to the DB.
|
|
|
|
// But not actually write it.
|
|
|
|
|
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
2022-05-17 20:05:15 +00:00
|
|
|
"context"
|
2022-05-06 15:03:15 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-06-15 15:49:30 +00:00
|
|
|
"time"
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-06-06 13:02:43 +00:00
|
|
|
"github.com/jackc/pgx/v4"
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-09 21:32:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/database/sql"
|
|
|
|
"github.com/vulcanize/ipld-eth-beacon-indexer/pkg/loghelper"
|
2022-05-17 20:05:15 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-05-06 15:03:15 +00:00
|
|
|
)
|
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
type SlotProcessingDetails struct {
|
|
|
|
Context context.Context // A context generic context with multiple uses.
|
|
|
|
ServerEndpoint string // What is the endpoint of the beacon server.
|
|
|
|
Db sql.Database // Database object used for reads and writes.
|
|
|
|
Metrics *BeaconClientMetrics // An object used to keep track of certain BeaconClient Metrics.
|
|
|
|
KnownGapTableIncrement int // The max number of slots within a single known_gaps table entry.
|
|
|
|
CheckDb bool // Should we check the DB to see if the slot exists before processing it?
|
|
|
|
PerformBeaconStateProcessing bool // Should we process BeaconStates?
|
|
|
|
ProcessBeaconBlockProcessing bool // Should we process BeaconBlocks?
|
|
|
|
|
|
|
|
StartingSlot int // If we're performing head tracking. What is the first slot we processed.
|
|
|
|
PreviousSlot int // Whats the previous slot we processed
|
|
|
|
PreviousBlockRoot string // Whats the previous block root, used to check the next blocks parent.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *BeaconClient) SlotProcessingDetails() SlotProcessingDetails {
|
|
|
|
return SlotProcessingDetails{
|
|
|
|
Context: bc.Context,
|
|
|
|
ServerEndpoint: bc.ServerEndpoint,
|
|
|
|
Db: bc.Db,
|
|
|
|
Metrics: bc.Metrics,
|
|
|
|
|
|
|
|
CheckDb: bc.CheckDb,
|
|
|
|
ProcessBeaconBlockProcessing: bc.ProcessBeaconBlockProcessing,
|
|
|
|
PerformBeaconStateProcessing: bc.PerformBeaconStateProcessing,
|
|
|
|
|
|
|
|
KnownGapTableIncrement: bc.KnownGapTableIncrement,
|
|
|
|
StartingSlot: bc.StartingSlot,
|
|
|
|
PreviousSlot: bc.PreviousSlot,
|
|
|
|
PreviousBlockRoot: bc.PreviousBlockRoot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 15:03:15 +00:00
|
|
|
type ProcessSlot struct {
|
|
|
|
// Generic
|
|
|
|
|
2022-06-15 15:49:30 +00:00
|
|
|
Slot int // The slot number.
|
|
|
|
Epoch int // The epoch number.
|
|
|
|
BlockRoot string // The hex encoded string of the BlockRoot.
|
|
|
|
StateRoot string // The hex encoded string of the StateRoot.
|
|
|
|
ParentBlockRoot string // The hex encoded string of the parent block.
|
|
|
|
Status string // The status of the block
|
|
|
|
HeadOrHistoric string // Is this the head or a historic slot. This is critical when trying to analyze errors and skipped slots.
|
|
|
|
Db sql.Database // The DB object used to write to the DB.
|
|
|
|
Metrics *BeaconClientMetrics // An object to keep track of the beaconclient metrics
|
|
|
|
PerformanceMetrics PerformanceMetrics // An object to keep track of performance metrics.
|
2022-05-06 15:03:15 +00:00
|
|
|
// BeaconBlock
|
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
SszSignedBeaconBlock []byte // The entire SSZ encoded SignedBeaconBlock
|
|
|
|
FullSignedBeaconBlock *SignedBeaconBlock // The unmarshaled BeaconState object, the unmarshalling could have errors.
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
// BeaconState
|
2022-09-15 21:17:59 +00:00
|
|
|
FullBeaconState *BeaconState // The unmarshaled BeaconState object, the unmarshalling could have errors.
|
|
|
|
SszBeaconState []byte // The entire SSZ encoded BeaconState
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
// DB Write objects
|
|
|
|
DbSlotsModel *DbSlots // The model being written to the slots table.
|
2022-06-09 21:32:46 +00:00
|
|
|
DbSignedBeaconBlockModel *DbSignedBeaconBlock // The model being written to the signed_block table.
|
|
|
|
DbBeaconState *DbBeaconState // The model being written to the state table.
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 15:49:30 +00:00
|
|
|
type PerformanceMetrics struct {
|
|
|
|
BeaconNodeBlockRetrievalTime time.Duration // How long it took to get the BeaconBlock from the Beacon Node.
|
|
|
|
BeaconNodeStateRetrievalTime time.Duration // How long it took to get the BeaconState from the Beacon Node.
|
|
|
|
ParseBeaconObjectForHash time.Duration // How long it took to get some information from the beacon objects.
|
|
|
|
CheckDbPreProcessing time.Duration // How long it takes to check the DB before processing a block.
|
|
|
|
CreateDbWriteObject time.Duration // How long it takes to create a DB write object.
|
|
|
|
TransactSlotOnly time.Duration // How long it takes to transact the slot information only.
|
|
|
|
CheckReorg time.Duration // How long it takes to check for Reorgs
|
|
|
|
CommitTransaction time.Duration // How long it takes to commit the final transaction.
|
|
|
|
TotalDbTransaction time.Duration // How long it takes from start to committing the entire DB transaction.
|
|
|
|
TotalProcessing time.Duration // How long it took to process the entire slot.
|
|
|
|
}
|
|
|
|
|
2022-05-06 15:03:15 +00:00
|
|
|
// This function will do all the work to process the slot and write it to the DB.
|
2022-05-24 20:18:55 +00:00
|
|
|
// It will return the error and error process. The error process is used for providing reach detail to the
|
|
|
|
// known_gaps table.
|
2022-09-15 21:17:59 +00:00
|
|
|
func processFullSlot(
|
|
|
|
ctx context.Context,
|
|
|
|
slot int,
|
|
|
|
blockRoot string,
|
|
|
|
stateRoot string,
|
|
|
|
previousSlot int,
|
|
|
|
previousBlockRoot string,
|
|
|
|
knownGapsTableIncrement int,
|
|
|
|
headOrHistoric string,
|
|
|
|
spd *SlotProcessingDetails) (error, string) {
|
2022-06-09 21:32:46 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ""
|
|
|
|
default:
|
2022-06-15 15:49:30 +00:00
|
|
|
totalStart := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
ps := &ProcessSlot{
|
|
|
|
Slot: slot,
|
|
|
|
BlockRoot: blockRoot,
|
|
|
|
StateRoot: stateRoot,
|
|
|
|
HeadOrHistoric: headOrHistoric,
|
2022-09-15 21:17:59 +00:00
|
|
|
Db: spd.Db,
|
|
|
|
Metrics: spd.Metrics,
|
2022-06-15 15:49:30 +00:00
|
|
|
PerformanceMetrics: PerformanceMetrics{
|
|
|
|
BeaconNodeBlockRetrievalTime: 0,
|
|
|
|
BeaconNodeStateRetrievalTime: 0,
|
|
|
|
ParseBeaconObjectForHash: 0,
|
|
|
|
CheckDbPreProcessing: 0,
|
|
|
|
CreateDbWriteObject: 0,
|
|
|
|
TransactSlotOnly: 0,
|
|
|
|
CheckReorg: 0,
|
|
|
|
CommitTransaction: 0,
|
|
|
|
TotalDbTransaction: 0,
|
|
|
|
TotalProcessing: 0,
|
|
|
|
},
|
2022-06-09 21:32:46 +00:00
|
|
|
}
|
2022-05-17 20:05:15 +00:00
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
g, _ := errgroup.WithContext(context.Background())
|
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
if spd.PerformBeaconStateProcessing {
|
|
|
|
// Get the BeaconState.
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
start := time.Now()
|
|
|
|
err := ps.getBeaconState(spd.ServerEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ps.PerformanceMetrics.BeaconNodeStateRetrievalTime = time.Since(start)
|
|
|
|
return nil
|
2022-06-09 21:32:46 +00:00
|
|
|
}
|
2022-09-15 21:17:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if spd.ProcessBeaconBlockProcessing {
|
|
|
|
// Get the SignedBeaconBlock.
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
start := time.Now()
|
|
|
|
err := ps.getSignedBeaconBlock(spd.ServerEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ps.PerformanceMetrics.BeaconNodeBlockRetrievalTime = time.Since(start)
|
|
|
|
return nil
|
2022-06-09 21:32:46 +00:00
|
|
|
}
|
2022-09-15 21:17:59 +00:00
|
|
|
})
|
|
|
|
}
|
2022-06-09 21:32:46 +00:00
|
|
|
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err, "processSlot"
|
2022-05-17 20:05:15 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-06-15 15:49:30 +00:00
|
|
|
parseBeaconTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
finalBlockRoot, finalStateRoot, finalEth1BlockHash, err := ps.provideFinalHash()
|
2022-05-17 20:05:15 +00:00
|
|
|
if err != nil {
|
2022-06-09 21:32:46 +00:00
|
|
|
return err, "CalculateBlockRoot"
|
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.ParseBeaconObjectForHash = time.Since(parseBeaconTime)
|
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
if spd.CheckDb {
|
2022-06-15 15:49:30 +00:00
|
|
|
checkDbTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
inDb, err := IsSlotInDb(ctx, ps.Db, strconv.Itoa(ps.Slot), finalBlockRoot, finalStateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err, "checkDb"
|
|
|
|
}
|
|
|
|
if inDb {
|
|
|
|
log.WithField("slot", slot).Info("Slot already in the DB.")
|
|
|
|
return nil, ""
|
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.CheckDbPreProcessing = time.Since(checkDbTime)
|
2022-05-17 20:05:15 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
// Get this object ready to write
|
2022-06-15 15:49:30 +00:00
|
|
|
createDbWriteTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
dw, err := ps.createWriteObjects(finalBlockRoot, finalStateRoot, finalEth1BlockHash)
|
2022-06-06 13:02:43 +00:00
|
|
|
if err != nil {
|
2022-06-09 21:32:46 +00:00
|
|
|
return err, "blockRoot"
|
2022-06-06 13:02:43 +00:00
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.CreateDbWriteObject = time.Since(createDbWriteTime)
|
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
// Write the object to the DB.
|
2022-06-15 15:49:30 +00:00
|
|
|
dbFullTransactionTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
defer func() {
|
|
|
|
err := dw.Tx.Rollback(dw.Ctx)
|
|
|
|
if err != nil && err != pgx.ErrTxClosed {
|
|
|
|
loghelper.LogError(err).Error("We were unable to Rollback a transaction")
|
|
|
|
}
|
|
|
|
}()
|
2022-06-15 15:49:30 +00:00
|
|
|
|
|
|
|
transactionTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
err = dw.transactFullSlot()
|
|
|
|
if err != nil {
|
|
|
|
return err, "processSlot"
|
2022-06-06 13:02:43 +00:00
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.TransactSlotOnly = time.Since(transactionTime)
|
2022-06-06 13:02:43 +00:00
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
// Handle any reorgs or skipped slots.
|
2022-06-15 15:49:30 +00:00
|
|
|
reorgTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
headOrHistoric = strings.ToLower(headOrHistoric)
|
|
|
|
if headOrHistoric != "head" && headOrHistoric != "historic" {
|
2022-09-03 04:22:44 +00:00
|
|
|
return fmt.Errorf("headOrHistoric must be either historic or head"), ""
|
2022-06-09 21:32:46 +00:00
|
|
|
}
|
|
|
|
if ps.HeadOrHistoric == "head" && previousSlot != 0 && previousBlockRoot != "" && ps.Status != "skipped" {
|
|
|
|
ps.checkPreviousSlot(dw.Tx, dw.Ctx, previousSlot, previousBlockRoot, knownGapsTableIncrement)
|
2022-06-06 13:02:43 +00:00
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.CheckReorg = time.Since(reorgTime)
|
2022-05-12 13:52:13 +00:00
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
// Commit the transaction
|
2022-06-15 15:49:30 +00:00
|
|
|
commitTime := time.Now()
|
2022-06-09 21:32:46 +00:00
|
|
|
if err = dw.Tx.Commit(dw.Ctx); err != nil {
|
|
|
|
return err, "transactionCommit"
|
|
|
|
}
|
2022-06-15 15:49:30 +00:00
|
|
|
ps.PerformanceMetrics.CommitTransaction = time.Since(commitTime)
|
|
|
|
|
|
|
|
// Total metric capture time.
|
|
|
|
ps.PerformanceMetrics.TotalDbTransaction = time.Since(dbFullTransactionTime)
|
|
|
|
ps.PerformanceMetrics.TotalProcessing = time.Since(totalStart)
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"slot": slot,
|
|
|
|
"performanceMetrics": fmt.Sprintf("%+v\n", ps.PerformanceMetrics),
|
|
|
|
}).Debug("Performance Metric output!")
|
2022-06-06 13:02:43 +00:00
|
|
|
|
2022-06-09 21:32:46 +00:00
|
|
|
return nil, ""
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a slot that is at head. A wrapper function for calling `handleFullSlot`.
|
2022-09-15 21:17:59 +00:00
|
|
|
func processHeadSlot(slot int, blockRoot string, stateRoot string, spd SlotProcessingDetails) {
|
|
|
|
// Get the knownGaps at startUp
|
|
|
|
if spd.PreviousSlot == 0 && spd.PreviousBlockRoot == "" {
|
|
|
|
writeStartUpGaps(spd.Db, spd.KnownGapTableIncrement, slot, spd.Metrics)
|
2022-05-24 20:18:55 +00:00
|
|
|
}
|
2022-09-15 21:17:59 +00:00
|
|
|
// TODO(telackey): Why context.Background()?
|
|
|
|
err, errReason := processFullSlot(context.Background(), slot, blockRoot, stateRoot,
|
|
|
|
spd.PreviousSlot, spd.PreviousBlockRoot, spd.KnownGapTableIncrement, "head", &spd)
|
2022-05-24 20:18:55 +00:00
|
|
|
if err != nil {
|
2022-09-15 21:17:59 +00:00
|
|
|
writeKnownGaps(spd.Db, spd.KnownGapTableIncrement, slot, slot, err, errReason, spd.Metrics)
|
2022-05-24 20:18:55 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a historic slot. A wrapper function for calling `handleFullSlot`.
|
2022-09-15 21:17:59 +00:00
|
|
|
func handleHistoricSlot(ctx context.Context, slot int, spd SlotProcessingDetails) (error, string) {
|
|
|
|
return processFullSlot(ctx, slot, "", "", 0, "",
|
|
|
|
1, "historic", &spd)
|
2022-05-24 20:18:55 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
|
|
|
// Update the SszSignedBeaconBlock and FullSignedBeaconBlock object with their respective values.
|
2022-09-03 04:22:44 +00:00
|
|
|
func (ps *ProcessSlot) getSignedBeaconBlock(serverAddress string) error {
|
2022-05-06 15:03:15 +00:00
|
|
|
var blockIdentifier string // Used to query the block
|
|
|
|
if ps.BlockRoot != "" {
|
|
|
|
blockIdentifier = ps.BlockRoot
|
|
|
|
} else {
|
2022-05-24 20:18:55 +00:00
|
|
|
blockIdentifier = strconv.Itoa(ps.Slot)
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-09-03 04:22:44 +00:00
|
|
|
|
2022-09-07 18:38:58 +00:00
|
|
|
blockEndpoint := serverAddress + BcBlockQueryEndpoint + blockIdentifier
|
|
|
|
sszSignedBeaconBlock, rc, err := querySsz(blockEndpoint, strconv.Itoa(ps.Slot))
|
|
|
|
|
|
|
|
if err != nil || rc != 200 {
|
2022-05-06 15:03:15 +00:00
|
|
|
loghelper.LogSlotError(strconv.Itoa(ps.Slot), err).Error("Unable to properly query the slot.")
|
2022-09-15 21:17:59 +00:00
|
|
|
ps.FullSignedBeaconBlock = nil
|
2022-09-07 18:38:58 +00:00
|
|
|
ps.SszSignedBeaconBlock = []byte{}
|
|
|
|
ps.ParentBlockRoot = ""
|
|
|
|
ps.Status = "skipped"
|
|
|
|
return nil
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:38:58 +00:00
|
|
|
var signedBeaconBlock SignedBeaconBlock
|
|
|
|
err = signedBeaconBlock.UnmarshalSSZ(sszSignedBeaconBlock)
|
2022-09-03 04:22:44 +00:00
|
|
|
if err != nil {
|
2022-09-07 18:38:58 +00:00
|
|
|
loghelper.LogSlotError(strconv.Itoa(ps.Slot), err).Error("Unable to unmarshal SignedBeaconBlock for slot.")
|
2022-09-15 21:17:59 +00:00
|
|
|
ps.FullSignedBeaconBlock = nil
|
2022-05-13 12:48:31 +00:00
|
|
|
ps.SszSignedBeaconBlock = []byte{}
|
|
|
|
ps.ParentBlockRoot = ""
|
|
|
|
ps.Status = "skipped"
|
|
|
|
return nil
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
ps.FullSignedBeaconBlock = &signedBeaconBlock
|
2022-09-03 04:22:44 +00:00
|
|
|
ps.SszSignedBeaconBlock = sszSignedBeaconBlock
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-09-08 05:03:37 +00:00
|
|
|
ps.ParentBlockRoot = toHex(ps.FullSignedBeaconBlock.Block().ParentRoot())
|
2022-05-06 15:03:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the SszBeaconState and FullBeaconState object with their respective values.
|
2022-09-03 04:22:44 +00:00
|
|
|
func (ps *ProcessSlot) getBeaconState(serverEndpoint string) error {
|
2022-05-06 15:03:15 +00:00
|
|
|
var stateIdentifier string // Used to query the state
|
|
|
|
if ps.StateRoot != "" {
|
|
|
|
stateIdentifier = ps.StateRoot
|
|
|
|
} else {
|
2022-05-24 20:18:55 +00:00
|
|
|
stateIdentifier = strconv.Itoa(ps.Slot)
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 18:38:58 +00:00
|
|
|
stateEndpoint := serverEndpoint + BcStateQueryEndpoint + stateIdentifier
|
|
|
|
sszBeaconState, _, err := querySsz(stateEndpoint, strconv.Itoa(ps.Slot))
|
2022-05-17 20:05:15 +00:00
|
|
|
if err != nil {
|
2022-09-03 04:22:44 +00:00
|
|
|
loghelper.LogSlotError(strconv.Itoa(ps.Slot), err).Error("Unable to properly query the BeaconState.")
|
|
|
|
return err
|
2022-05-17 20:05:15 +00:00
|
|
|
}
|
2022-09-03 04:22:44 +00:00
|
|
|
|
2022-09-07 18:38:58 +00:00
|
|
|
var beaconState BeaconState
|
|
|
|
err = beaconState.UnmarshalSSZ(sszBeaconState)
|
2022-05-06 15:03:15 +00:00
|
|
|
if err != nil {
|
2022-09-07 18:38:58 +00:00
|
|
|
loghelper.LogSlotError(strconv.Itoa(ps.Slot), err).Error("Unable to unmarshal the BeaconState.")
|
2022-06-09 21:32:46 +00:00
|
|
|
return err
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-09-03 04:22:44 +00:00
|
|
|
|
2022-09-15 21:17:59 +00:00
|
|
|
ps.FullBeaconState = &beaconState
|
2022-09-03 04:22:44 +00:00
|
|
|
ps.SszBeaconState = sszBeaconState
|
2022-05-06 15:03:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure that the previous block we processed is the parent of the current block.
|
2022-06-06 13:02:43 +00:00
|
|
|
func (ps *ProcessSlot) checkPreviousSlot(tx sql.Tx, ctx context.Context, previousSlot int, previousBlockRoot string, knownGapsTableIncrement int) {
|
2022-09-15 21:17:59 +00:00
|
|
|
if nil == ps.FullSignedBeaconBlock {
|
|
|
|
log.Debug("Can't check previous slot, no current slot.")
|
|
|
|
return
|
|
|
|
}
|
2022-09-08 05:03:37 +00:00
|
|
|
parentRoot := toHex(ps.FullSignedBeaconBlock.Block().ParentRoot())
|
2022-09-07 18:38:58 +00:00
|
|
|
slot := int(ps.FullBeaconState.Slot())
|
2022-06-15 15:49:30 +00:00
|
|
|
if previousSlot == slot {
|
2022-05-06 15:03:15 +00:00
|
|
|
log.WithFields(log.Fields{
|
2022-06-15 15:49:30 +00:00
|
|
|
"slot": slot,
|
2022-05-06 15:03:15 +00:00
|
|
|
"fork": true,
|
|
|
|
}).Warn("A fork occurred! The previous slot and current slot match.")
|
2022-06-06 13:02:43 +00:00
|
|
|
transactReorgs(tx, ctx, strconv.Itoa(ps.Slot), ps.BlockRoot, ps.Metrics)
|
2022-06-15 15:49:30 +00:00
|
|
|
} else if previousSlot > slot {
|
2022-05-24 20:18:55 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"previousSlot": previousSlot,
|
2022-06-15 15:49:30 +00:00
|
|
|
"curSlot": slot,
|
2022-05-24 20:18:55 +00:00
|
|
|
}).Warn("We noticed the previous slot is greater than the current slot.")
|
2022-06-15 15:49:30 +00:00
|
|
|
} else if previousSlot+1 != slot {
|
2022-05-06 15:03:15 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"previousSlot": previousSlot,
|
2022-06-15 15:49:30 +00:00
|
|
|
"currentSlot": slot,
|
2022-05-06 15:03:15 +00:00
|
|
|
}).Error("We skipped a few slots.")
|
2022-09-03 04:22:44 +00:00
|
|
|
transactKnownGaps(tx, ctx, knownGapsTableIncrement, previousSlot+1, slot-1, fmt.Errorf("gaps during head processing"), "headGaps", ps.Metrics)
|
2022-05-06 15:03:15 +00:00
|
|
|
} else if previousBlockRoot != parentRoot {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"previousBlockRoot": previousBlockRoot,
|
|
|
|
"currentBlockParent": parentRoot,
|
|
|
|
}).Error("The previousBlockRoot does not match the current blocks parent, an unprocessed fork might have occurred.")
|
2022-06-06 13:02:43 +00:00
|
|
|
transactReorgs(tx, ctx, strconv.Itoa(previousSlot), parentRoot, ps.Metrics)
|
2022-05-06 15:03:15 +00:00
|
|
|
} else {
|
|
|
|
log.Debug("Previous Slot and Current Slot are one distance from each other.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transforms all the raw data into DB models that can be written to the DB.
|
2022-06-06 13:02:43 +00:00
|
|
|
func (ps *ProcessSlot) createWriteObjects(blockRoot, stateRoot, eth1BlockHash string) (*DatabaseWriter, error) {
|
|
|
|
var status string
|
|
|
|
if ps.Status != "" {
|
|
|
|
status = ps.Status
|
|
|
|
} else {
|
|
|
|
status = "proposed"
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:49:30 +00:00
|
|
|
dw, err := CreateDatabaseWrite(ps.Db, ps.Slot, stateRoot, blockRoot, ps.ParentBlockRoot, eth1BlockHash, status, &ps.SszSignedBeaconBlock, &ps.SszBeaconState, ps.Metrics)
|
2022-06-06 13:02:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return dw, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function will return the final blockRoot, stateRoot, and eth1BlockHash that will be
|
|
|
|
// used to write to a DB
|
|
|
|
func (ps *ProcessSlot) provideFinalHash() (string, string, string, error) {
|
2022-05-06 15:03:15 +00:00
|
|
|
var (
|
2022-05-13 12:48:31 +00:00
|
|
|
stateRoot string
|
|
|
|
blockRoot string
|
|
|
|
eth1BlockHash string
|
2022-05-06 15:03:15 +00:00
|
|
|
)
|
2022-05-13 12:48:31 +00:00
|
|
|
if ps.Status == "skipped" {
|
|
|
|
stateRoot = ""
|
|
|
|
blockRoot = ""
|
|
|
|
eth1BlockHash = ""
|
2022-05-06 15:03:15 +00:00
|
|
|
} else {
|
2022-05-13 12:48:31 +00:00
|
|
|
if ps.StateRoot != "" {
|
|
|
|
stateRoot = ps.StateRoot
|
|
|
|
} else {
|
2022-09-15 21:17:59 +00:00
|
|
|
if nil != ps.FullSignedBeaconBlock {
|
|
|
|
stateRoot = toHex(ps.FullSignedBeaconBlock.Block().StateRoot())
|
|
|
|
log.Debug("BeaconBlock StateRoot: ", stateRoot)
|
|
|
|
} else {
|
|
|
|
log.Debug("BeaconBlock StateRoot: <nil beacon block>")
|
|
|
|
}
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
|
2022-05-13 12:48:31 +00:00
|
|
|
if ps.BlockRoot != "" {
|
|
|
|
blockRoot = ps.BlockRoot
|
|
|
|
} else {
|
2022-09-15 21:17:59 +00:00
|
|
|
if nil != ps.FullSignedBeaconBlock {
|
|
|
|
rawBlockRoot := ps.FullSignedBeaconBlock.Block().HashTreeRoot()
|
|
|
|
blockRoot = toHex(rawBlockRoot)
|
|
|
|
log.WithFields(log.Fields{"blockRoot": blockRoot}).Debug("Block Root from ssz")
|
|
|
|
} else {
|
|
|
|
log.Debug("BeaconBlock HashTreeRoot: <nil beacon block>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nil != ps.FullSignedBeaconBlock {
|
|
|
|
eth1BlockHash = toHex(ps.FullSignedBeaconBlock.Block().Body().Eth1Data().BlockHash)
|
2022-05-13 12:48:31 +00:00
|
|
|
}
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-06-06 13:02:43 +00:00
|
|
|
return blockRoot, stateRoot, eth1BlockHash, nil
|
2022-05-06 15:03:15 +00:00
|
|
|
}
|
2022-09-03 04:22:44 +00:00
|
|
|
|
2022-09-08 05:03:37 +00:00
|
|
|
func toHex(r [32]byte) string {
|
2022-09-03 04:22:44 +00:00
|
|
|
return "0x" + hex.EncodeToString(r[:])
|
|
|
|
}
|