added back PR 3806 (#4382)

This commit is contained in:
Enrique Jose Avila Asapche 2022-06-07 10:51:28 +01:00 committed by GitHub
parent e90bc39e04
commit a1f4472f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 2 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages" "github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/rpc"
) )
@ -26,6 +28,14 @@ func getBlockNumber(number rpc.BlockNumber, tx kv.Tx) (uint64, error) {
} }
func getLatestBlockNumber(tx kv.Tx) (uint64, error) { func getLatestBlockNumber(tx kv.Tx) (uint64, error) {
forkchoiceHeadHash := rawdb.ReadForkchoiceHead(tx)
if forkchoiceHeadHash != (common.Hash{}) {
forkchoiceHeadNum := rawdb.ReadHeaderNumber(tx, forkchoiceHeadHash)
if forkchoiceHeadNum != nil {
return *forkchoiceHeadNum, nil
}
}
blockNum, err := stages.GetStageProgress(tx, stages.Execution) blockNum, err := stages.GetStageProgress(tx, stages.Execution)
if err != nil { if err != nil {
return 0, fmt.Errorf("getting latest block number: %w", err) return 0, fmt.Errorf("getting latest block number: %w", err)

View File

@ -145,6 +145,76 @@ func WriteHeadBlockHash(db kv.Putter, hash common.Hash) {
} }
} }
// DeleteHeaderNumber removes hash->number mapping.
func DeleteHeaderNumber(db kv.Deleter, hash common.Hash) {
if err := db.Delete(kv.HeaderNumber, hash[:], nil); err != nil {
log.Crit("Failed to delete hash mapping", "err", err)
}
}
// ReadForkchoiceHead retrieves headBlockHash from the last Engine API forkChoiceUpdated.
func ReadForkchoiceHead(db kv.Getter) common.Hash {
data, err := db.GetOne(kv.LastForkchoice, []byte("headBlockHash"))
if err != nil {
log.Error("ReadForkchoiceHead failed", "err", err)
}
if len(data) == 0 {
return common.Hash{}
}
return common.BytesToHash(data)
}
// WriteForkchoiceHead stores headBlockHash from the last Engine API forkChoiceUpdated.
func WriteForkchoiceHead(db kv.Putter, hash common.Hash) {
if err := db.Put(kv.LastForkchoice, []byte("headBlockHash"), hash[:]); err != nil {
log.Crit("Failed to store head block hash", "err", err)
}
}
// ReadForkchoiceSafe retrieves safeBlockHash from the last Engine API forkChoiceUpdated.
func ReadForkchoiceSafe(db kv.Getter) common.Hash {
data, err := db.GetOne(kv.LastForkchoice, []byte("safeBlockHash"))
if err != nil {
log.Error("ReadForkchoiceSafe failed", "err", err)
return common.Hash{}
}
if len(data) == 0 {
return common.Hash{}
}
return common.BytesToHash(data)
}
// WriteForkchoiceSafe stores safeBlockHash from the last Engine API forkChoiceUpdated.
func WriteForkchoiceSafe(db kv.Putter, hash common.Hash) {
if err := db.Put(kv.LastForkchoice, []byte("safeBlockHash"), hash[:]); err != nil {
log.Crit("Failed to store safe block hash", "err", err)
}
}
// ReadForkchoiceFinalized retrieves finalizedBlockHash from the last Engine API forkChoiceUpdated.
func ReadForkchoiceFinalized(db kv.Getter) common.Hash {
data, err := db.GetOne(kv.LastForkchoice, []byte("finalizedBlockHash"))
if err != nil {
log.Error("ReadForkchoiceFinalize failed", "err", err)
return common.Hash{}
}
if len(data) == 0 {
return common.Hash{}
}
return common.BytesToHash(data)
}
// WriteForkchoiceFinalized stores finalizedBlockHash from the last Engine API forkChoiceUpdated.
func WriteForkchoiceFinalized(db kv.Putter, hash common.Hash) {
if err := db.Put(kv.LastForkchoice, []byte("finalizedBlockHash"), hash[:]); err != nil {
log.Crit("Failed to safe finalized block hash", "err", err)
}
}
// ReadHeaderRLP retrieves a block header in its raw RLP database encoding. // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
func ReadHeaderRLP(db kv.Getter, hash common.Hash, number uint64) rlp.RawValue { func ReadHeaderRLP(db kv.Getter, hash common.Hash, number uint64) rlp.RawValue {
data, err := db.GetOne(kv.Headers, dbutils.HeaderKey(number, hash)) data, err := db.GetOne(kv.Headers, dbutils.HeaderKey(number, hash))

View File

@ -207,7 +207,9 @@ func safeAndFinalizedBlocksAreCanonical(
if err != nil { if err != nil {
return false, err return false, err
} }
if !safeIsCanonical { if safeIsCanonical {
rawdb.WriteForkchoiceSafe(tx, forkChoice.SafeBlockHash)
} else {
log.Warn(fmt.Sprintf("[%s] Non-canonical SafeBlockHash", s.LogPrefix()), "forkChoice", forkChoice) log.Warn(fmt.Sprintf("[%s] Non-canonical SafeBlockHash", s.LogPrefix()), "forkChoice", forkChoice)
if sendErrResponse { if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{ cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
@ -223,7 +225,9 @@ func safeAndFinalizedBlocksAreCanonical(
if err != nil { if err != nil {
return false, err return false, err
} }
if !finalizedIsCanonical { if finalizedIsCanonical {
rawdb.WriteForkchoiceFinalized(tx, forkChoice.FinalizedBlockHash)
} else {
log.Warn(fmt.Sprintf("[%s] Non-canonical FinalizedBlockHash", s.LogPrefix()), "forkChoice", forkChoice) log.Warn(fmt.Sprintf("[%s] Non-canonical FinalizedBlockHash", s.LogPrefix()), "forkChoice", forkChoice)
if sendErrResponse { if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{ cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
@ -366,6 +370,7 @@ func finishHandlingForkChoice(
if err := rawdb.WriteHeadHeaderHash(tx, forkChoice.HeadBlockHash); err != nil { if err := rawdb.WriteHeadHeaderHash(tx, forkChoice.HeadBlockHash); err != nil {
return err return err
} }
rawdb.WriteForkchoiceHead(tx, forkChoice.HeadBlockHash)
sendErrResponse := cfg.hd.GetPendingPayloadStatus() != (common.Hash{}) sendErrResponse := cfg.hd.GetPendingPayloadStatus() != (common.Hash{})
canonical, err := safeAndFinalizedBlocksAreCanonical(forkChoice, s, tx, cfg, sendErrResponse) canonical, err := safeAndFinalizedBlocksAreCanonical(forkChoice, s, tx, cfg, sendErrResponse)