Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added back PR 3806 #4382

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/rpcdaemon/commands/rpc_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"

"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/rpc"
)
Expand All @@ -26,6 +28,14 @@ func getBlockNumber(number rpc.BlockNumber, 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)
if err != nil {
return 0, fmt.Errorf("getting latest block number: %w", err)
Expand Down
70 changes: 70 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
func ReadHeaderRLP(db kv.Getter, hash common.Hash, number uint64) rlp.RawValue {
data, err := db.GetOne(kv.Headers, dbutils.HeaderKey(number, hash))
Expand Down
9 changes: 7 additions & 2 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ func safeAndFinalizedBlocksAreCanonical(
if err != nil {
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)
if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
Expand All @@ -223,7 +225,9 @@ func safeAndFinalizedBlocksAreCanonical(
if err != nil {
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)
if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
Expand Down Expand Up @@ -366,6 +370,7 @@ func finishHandlingForkChoice(
if err := rawdb.WriteHeadHeaderHash(tx, forkChoice.HeadBlockHash); err != nil {
return err
}
rawdb.WriteForkchoiceHead(tx, forkChoice.HeadBlockHash)

sendErrResponse := cfg.hd.GetPendingPayloadStatus() != (common.Hash{})
canonical, err := safeAndFinalizedBlocksAreCanonical(forkChoice, s, tx, cfg, sendErrResponse)
Expand Down