From 5e1900cc4811dab703c650268366d76fa753eb28 Mon Sep 17 00:00:00 2001
From: Giulio rebuffo <giulio.rebuffo@gmail.com>
Date: Sun, 24 Apr 2022 03:13:49 +0200
Subject: [PATCH] cleaned up forkchoices db insertions #3949

---
 core/rawdb/accessors_chain.go   | 64 ---------------------------------
 eth/stagedsync/stage_headers.go |  9 ++---
 2 files changed, 2 insertions(+), 71 deletions(-)

diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go
index 4c8efc4a31..b3fb1f53ff 100644
--- a/core/rawdb/accessors_chain.go
+++ b/core/rawdb/accessors_chain.go
@@ -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))
@@ -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))
diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go
index f6f8fbca10..f41e23148c 100644
--- a/eth/stagedsync/stage_headers.go
+++ b/eth/stagedsync/stage_headers.go
@@ -159,7 +159,6 @@ func finishHandlingForkChoice(
 	if err := rawdb.WriteHeadHeaderHash(tx, forkChoice.HeadBlockHash); err != nil {
 		return err
 	}
-	rawdb.WriteForkchoiceHead(tx, forkChoice.HeadBlockHash)
 
 	sendErrResponse := cfg.hd.GetPendingPayloadStatus() != (common.Hash{})
 
@@ -167,9 +166,7 @@ func finishHandlingForkChoice(
 	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{
@@ -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{
-- 
GitLab