From c3ed07ca4a50d237f6a8118ed52f1d03bb66e0f5 Mon Sep 17 00:00:00 2001 From: Enrique Jose Avila Asapche <eavilaasapche@gmail.com> Date: Tue, 7 Dec 2021 16:36:12 +0000 Subject: [PATCH] Implementing basic unwind process for Proof-of-stake (#3083) * initial commmit * gofmt * flipped if statement * flipped if statement (forgot to save) * passing unwinding to status ch * added unwinding constant * took out unwinding * added statusCh * added a return statement and a second part to the if statement * checking for parent hash * checking for parent hash * changed check to check for parentHash * staged wrong file * changed header check to parentHash check * channged blockhash name to a better name * changed it to check for header.Hash() * changed name of blocknumber to blockHash * changed bad block to common.Hash{} --- eth/stagedsync/stage_headers.go | 22 ++++++++++++++++++++++ turbo/stages/mock_sentry.go | 1 + turbo/stages/stageloop.go | 1 + 3 files changed, 24 insertions(+) diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go index 4e1749776a..f77f340758 100644 --- a/eth/stagedsync/stage_headers.go +++ b/eth/stagedsync/stage_headers.go @@ -18,6 +18,7 @@ import ( "github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/eth/stagedsync/stages" + "github.com/ledgerwatch/erigon/ethdb/privateapi" "github.com/ledgerwatch/erigon/p2p/enode" "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/rlp" @@ -30,6 +31,7 @@ import ( type HeadersCfg struct { db kv.RwDB hd *headerdownload.HeaderDownload + statusCh chan privateapi.ExecutionStatus chainConfig params.ChainConfig headerReqSend func(context.Context, *headerdownload.HeaderRequest) (enode.ID, bool) announceNewHashes func(context.Context, []headerdownload.Announce) @@ -45,6 +47,7 @@ type HeadersCfg struct { func StageHeadersCfg( db kv.RwDB, headerDownload *headerdownload.HeaderDownload, + statusCh chan privateapi.ExecutionStatus, chainConfig params.ChainConfig, headerReqSend func(context.Context, *headerdownload.HeaderRequest) (enode.ID, bool), announceNewHashes func(context.Context, []headerdownload.Announce), @@ -59,6 +62,7 @@ func StageHeadersCfg( return HeadersCfg{ db: db, hd: headerDownload, + statusCh: statusCh, chainConfig: chainConfig, headerReqSend: headerReqSend, announceNewHashes: announceNewHashes, @@ -127,7 +131,25 @@ func HeadersDownward( log.Info("Waiting for payloads...") header := <-cfg.reverseDownloadCh *cfg.waitingPosHeaders = false + + defer tx.Commit() + + headerNumber := header.Number.Uint64() + + blockHash, err := rawdb.ReadCanonicalHash(tx, headerNumber) + if err != nil { + return err + } + // Do we need to unwind? (TODO) + if s.BlockNumber >= headerNumber && header.Hash() != blockHash { + u.UnwindTo(headerNumber-1, common.Hash{}) + cfg.statusCh <- privateapi.ExecutionStatus{ + HeadHash: header.ParentHash, + Status: privateapi.Syncing, + } + return nil + } // Write current payload rawdb.WriteHeader(tx, &header) diff --git a/turbo/stages/mock_sentry.go b/turbo/stages/mock_sentry.go index 8eeeb6eff7..87856cbcac 100644 --- a/turbo/stages/mock_sentry.go +++ b/turbo/stages/mock_sentry.go @@ -275,6 +275,7 @@ func MockWithEverything(t *testing.T, gspec *core.Genesis, key *ecdsa.PrivateKey stagedsync.DefaultStages(mock.Ctx, prune, stagedsync.StageHeadersCfg( mock.DB, mock.downloader.Hd, + make(chan privateapi.ExecutionStatus), *mock.ChainConfig, sendHeaderRequest, propagateNewBlockHashes, diff --git a/turbo/stages/stageloop.go b/turbo/stages/stageloop.go index 81309b0139..3dcf4d0bf1 100644 --- a/turbo/stages/stageloop.go +++ b/turbo/stages/stageloop.go @@ -244,6 +244,7 @@ func NewStagedSync( stagedsync.DefaultStages(ctx, cfg.Prune, stagedsync.StageHeadersCfg( db, controlServer.Hd, + statusCh, *controlServer.ChainConfig, controlServer.SendHeaderRequest, controlServer.PropagateNewBlockHashes, -- GitLab