2019-08-24 17:51:00 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2020-04-29 16:57:04 +00:00
|
|
|
"encoding/hex"
|
2020-01-27 21:48:16 +00:00
|
|
|
"fmt"
|
2021-02-15 16:33:38 +00:00
|
|
|
"time"
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2022-06-15 10:03:48 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
2023-04-25 15:07:24 +00:00
|
|
|
consensus_types "github.com/prysmaticlabs/prysm/v4/consensus-types"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
|
|
|
prysmTime "github.com/prysmaticlabs/prysm/v4/time"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2019-08-24 17:51:00 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("prefix", "blockchain")
|
|
|
|
|
|
|
|
// logs state transition related data every slot.
|
2023-02-09 09:23:32 +00:00
|
|
|
func logStateTransitionData(b interfaces.ReadOnlyBeaconBlock) error {
|
2021-08-09 20:05:46 +00:00
|
|
|
log := log.WithField("slot", b.Slot())
|
2021-05-26 16:19:54 +00:00
|
|
|
if len(b.Body().Attestations()) > 0 {
|
|
|
|
log = log.WithField("attestations", len(b.Body().Attestations()))
|
2021-02-18 19:08:27 +00:00
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
if len(b.Body().Deposits()) > 0 {
|
|
|
|
log = log.WithField("deposits", len(b.Body().Deposits()))
|
2021-02-18 19:08:27 +00:00
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
if len(b.Body().AttesterSlashings()) > 0 {
|
|
|
|
log = log.WithField("attesterSlashings", len(b.Body().AttesterSlashings()))
|
2021-02-18 19:08:27 +00:00
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
if len(b.Body().ProposerSlashings()) > 0 {
|
|
|
|
log = log.WithField("proposerSlashings", len(b.Body().ProposerSlashings()))
|
2021-02-18 19:08:27 +00:00
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
if len(b.Body().VoluntaryExits()) > 0 {
|
|
|
|
log = log.WithField("voluntaryExits", len(b.Body().VoluntaryExits()))
|
2021-02-18 19:08:27 +00:00
|
|
|
}
|
2023-01-04 03:47:20 +00:00
|
|
|
if b.Version() >= version.Altair {
|
2021-08-26 05:01:09 +00:00
|
|
|
agg, err := b.Body().SyncAggregate()
|
2022-03-15 14:02:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-26 05:01:09 +00:00
|
|
|
}
|
2022-03-15 14:02:39 +00:00
|
|
|
log = log.WithField("syncBitsCount", agg.SyncCommitteeBits.Count())
|
|
|
|
}
|
2023-01-04 03:47:20 +00:00
|
|
|
if b.Version() >= version.Bellatrix {
|
2022-07-13 01:49:38 +00:00
|
|
|
p, err := b.Body().Execution()
|
2022-03-15 14:02:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 01:49:38 +00:00
|
|
|
log = log.WithField("payloadHash", fmt.Sprintf("%#x", bytesutil.Trunc(p.BlockHash())))
|
|
|
|
txs, err := p.Transactions()
|
2022-08-31 12:38:44 +00:00
|
|
|
switch {
|
2023-05-23 09:38:52 +00:00
|
|
|
case errors.Is(err, consensus_types.ErrUnsupportedField):
|
2022-08-31 12:38:44 +00:00
|
|
|
case err != nil:
|
2022-07-13 01:49:38 +00:00
|
|
|
return err
|
2022-08-31 12:38:44 +00:00
|
|
|
default:
|
|
|
|
log = log.WithField("txCount", len(txs))
|
2022-08-31 22:05:50 +00:00
|
|
|
txsPerSlotCount.Set(float64(len(txs)))
|
2022-07-13 01:49:38 +00:00
|
|
|
}
|
2021-08-26 05:01:09 +00:00
|
|
|
}
|
2023-08-11 21:43:58 +00:00
|
|
|
if b.Version() >= version.Deneb {
|
|
|
|
kzgs, err := b.Body().BlobKzgCommitments()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to get blob KZG commitments")
|
|
|
|
} else if len(kzgs) > 0 {
|
2023-09-11 20:46:06 +00:00
|
|
|
log = log.WithField("kzgCommitmentCount", len(kzgs))
|
2023-08-11 21:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 19:08:27 +00:00
|
|
|
log.Info("Finished applying state transition")
|
2022-03-15 14:02:39 +00:00
|
|
|
return nil
|
2019-08-24 17:51:00 +00:00
|
|
|
}
|
2020-01-27 21:48:16 +00:00
|
|
|
|
2023-12-14 22:46:48 +00:00
|
|
|
func logBlockSyncStatus(block interfaces.ReadOnlyBeaconBlock, blockRoot [32]byte, justified, finalized *ethpb.Checkpoint, receivedTime time.Time, genesisTime uint64, daWaitedTime time.Duration) error {
|
2021-10-01 20:17:57 +00:00
|
|
|
startTime, err := slots.ToTime(genesisTime, block.Slot())
|
2021-02-10 17:24:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-22 16:57:48 +00:00
|
|
|
level := log.Logger.GetLevel()
|
|
|
|
if level >= logrus.DebugLevel {
|
2022-09-06 14:30:16 +00:00
|
|
|
parentRoot := block.ParentRoot()
|
2023-07-25 21:57:01 +00:00
|
|
|
lf := logrus.Fields{
|
2022-06-30 19:02:23 +00:00
|
|
|
"slot": block.Slot(),
|
|
|
|
"slotInEpoch": block.Slot() % params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
"block": fmt.Sprintf("0x%s...", hex.EncodeToString(blockRoot[:])[:8]),
|
|
|
|
"epoch": slots.ToEpoch(block.Slot()),
|
|
|
|
"justifiedEpoch": justified.Epoch,
|
|
|
|
"justifiedRoot": fmt.Sprintf("0x%s...", hex.EncodeToString(justified.Root)[:8]),
|
|
|
|
"finalizedEpoch": finalized.Epoch,
|
|
|
|
"finalizedRoot": fmt.Sprintf("0x%s...", hex.EncodeToString(finalized.Root)[:8]),
|
2022-09-06 14:30:16 +00:00
|
|
|
"parentRoot": fmt.Sprintf("0x%s...", hex.EncodeToString(parentRoot[:])[:8]),
|
2022-06-30 19:02:23 +00:00
|
|
|
"version": version.String(block.Version()),
|
|
|
|
"sinceSlotStartTime": prysmTime.Now().Sub(startTime),
|
2023-12-14 22:46:48 +00:00
|
|
|
"chainServiceProcessedTime": prysmTime.Now().Sub(receivedTime) - daWaitedTime,
|
2022-12-13 23:13:49 +00:00
|
|
|
"deposits": len(block.Body().Deposits()),
|
2023-07-25 21:57:01 +00:00
|
|
|
}
|
|
|
|
log.WithFields(lf).Debug("Synced new block")
|
2022-06-30 19:02:23 +00:00
|
|
|
} else {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"slot": block.Slot(),
|
|
|
|
"block": fmt.Sprintf("0x%s...", hex.EncodeToString(blockRoot[:])[:8]),
|
|
|
|
"finalizedEpoch": finalized.Epoch,
|
|
|
|
"finalizedRoot": fmt.Sprintf("0x%s...", hex.EncodeToString(finalized.Root)[:8]),
|
|
|
|
"epoch": slots.ToEpoch(block.Slot()),
|
|
|
|
}).Info("Synced new block")
|
2022-05-22 16:57:48 +00:00
|
|
|
}
|
2021-02-10 17:24:40 +00:00
|
|
|
return nil
|
2020-04-29 16:57:04 +00:00
|
|
|
}
|
2022-06-15 10:03:48 +00:00
|
|
|
|
|
|
|
// logs payload related data every slot.
|
2023-02-09 09:23:32 +00:00
|
|
|
func logPayload(block interfaces.ReadOnlyBeaconBlock) error {
|
2022-06-15 10:03:48 +00:00
|
|
|
isExecutionBlk, err := blocks.IsExecutionBlock(block.Body())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not determine if block is execution block")
|
|
|
|
}
|
|
|
|
if !isExecutionBlk {
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-13 01:49:38 +00:00
|
|
|
payload, err := block.Body().Execution()
|
2022-06-15 10:03:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 01:49:38 +00:00
|
|
|
if payload.GasLimit() == 0 {
|
2022-06-15 10:03:48 +00:00
|
|
|
return errors.New("gas limit should not be 0")
|
|
|
|
}
|
2022-07-13 01:49:38 +00:00
|
|
|
gasUtilized := float64(payload.GasUsed()) / float64(payload.GasLimit())
|
2022-12-02 06:04:45 +00:00
|
|
|
fields := logrus.Fields{
|
2022-07-13 01:49:38 +00:00
|
|
|
"blockHash": fmt.Sprintf("%#x", bytesutil.Trunc(payload.BlockHash())),
|
|
|
|
"parentHash": fmt.Sprintf("%#x", bytesutil.Trunc(payload.ParentHash())),
|
2023-06-14 19:55:33 +00:00
|
|
|
"blockNumber": payload.BlockNumber(),
|
2022-06-15 10:03:48 +00:00
|
|
|
"gasUtilized": fmt.Sprintf("%.2f", gasUtilized),
|
2022-12-02 06:04:45 +00:00
|
|
|
}
|
|
|
|
if block.Version() >= version.Capella {
|
|
|
|
withdrawals, err := payload.Withdrawals()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not get withdrawals")
|
|
|
|
}
|
|
|
|
fields["withdrawals"] = len(withdrawals)
|
|
|
|
changes, err := block.Body().BLSToExecutionChanges()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not get BLSToExecutionChanges")
|
|
|
|
}
|
|
|
|
fields["blsToExecutionChanges"] = len(changes)
|
|
|
|
}
|
|
|
|
log.WithFields(fields).Debug("Synced new payload")
|
2022-06-15 10:03:48 +00:00
|
|
|
return nil
|
|
|
|
}
|