Skip to content

Commit

Permalink
cleaned up forkchoices db insertions #3949
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Apr 24, 2022
1 parent 3ad25c9 commit 5e1900c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 71 deletions.
64 changes: 0 additions & 64 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ func WriteHeaderNumber(db kv.Putter, hash common.Hash, number uint64) error {
return nil
}

// 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 to number mapping", "err", err)
}
}

// ReadHeadHeaderHash retrieves the hash of the current canonical head header.
func ReadHeadHeaderHash(db kv.Getter) common.Hash {
data, err := db.GetOne(kv.HeadHeaderKey, []byte(kv.HeadHeaderKey))
Expand Down Expand Up @@ -149,63 +142,6 @@ func WriteHeadBlockHash(db kv.Putter, hash common.Hash) {
}
}

// 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.Bytes()); err != nil {
log.Crit("Failed to store last headBlockHash", "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)
}
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.Bytes()); err != nil {
log.Crit("Failed to store last safeBlockHash", "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("ReadForkchoiceFinalized failed", "err", err)
}
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.Bytes()); err != nil {
log.Crit("Failed to store last finalizedBlockHash", "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: 2 additions & 7 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,14 @@ func finishHandlingForkChoice(
if err := rawdb.WriteHeadHeaderHash(tx, forkChoice.HeadBlockHash); err != nil {
return err
}
rawdb.WriteForkchoiceHead(tx, forkChoice.HeadBlockHash)

sendErrResponse := cfg.hd.GetPendingPayloadStatus() != (common.Hash{})

safeIsCanonical, err := rawdb.IsCanonicalHash(tx, forkChoice.SafeBlockHash)
if err != nil {
return err
}
if safeIsCanonical {
rawdb.WriteForkchoiceSafe(tx, forkChoice.SafeBlockHash)
} else {
if !safeIsCanonical {
log.Warn(fmt.Sprintf("[%s] Non-canonical SafeBlockHash", s.LogPrefix()), "forkChoice", forkChoice)
if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
Expand All @@ -184,9 +181,7 @@ func finishHandlingForkChoice(
if err != nil {
return err
}
if finalizedIsCanonical {
rawdb.WriteForkchoiceFinalized(tx, forkChoice.FinalizedBlockHash)
} else {
if !finalizedIsCanonical {
log.Warn(fmt.Sprintf("[%s] Non-canonical FinalizedBlockHash", s.LogPrefix()), "forkChoice", forkChoice)
if sendErrResponse {
cfg.hd.PayloadStatusCh <- privateapi.PayloadStatus{
Expand Down

0 comments on commit 5e1900c

Please sign in to comment.