From 9fbfba17b66ecc30234f042ed68f392c5a082d74 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 20 Nov 2022 13:35:00 +0200 Subject: [PATCH] =?UTF-8?q?Compare=20blue=20score=20with=20selected=20tip?= =?UTF-8?q?=20when=20checking=20if=20a=20pruning=20point=E2=80=A6=20(#2169?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Compare blue score with selected tip when checking if a pruning point proof is needed * Don't redeclare err Co-authored-by: Michael Sutton --- app/protocol/flows/v5/blockrelay/ibd.go | 18 ++++++++++-------- .../v5/blockrelay/ibd_with_headers_proof.go | 15 ++++++++++----- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/protocol/flows/v5/blockrelay/ibd.go b/app/protocol/flows/v5/blockrelay/ibd.go index 5e1e5a13d0..7438c201ee 100644 --- a/app/protocol/flows/v5/blockrelay/ibd.go +++ b/app/protocol/flows/v5/blockrelay/ibd.go @@ -1,6 +1,7 @@ package blockrelay import ( + "fmt" "github.com/kaspanet/kaspad/app/appmessage" "github.com/kaspanet/kaspad/app/protocol/common" peerpkg "github.com/kaspanet/kaspad/app/protocol/peer" @@ -70,16 +71,17 @@ func (flow *handleIBDFlow) runIBDIfNotRunning(block *externalapi.DomainBlock) er } isFinishedSuccessfully := false + var err error defer func() { flow.UnsetIBDRunning() - flow.logIBDFinished(isFinishedSuccessfully) + flow.logIBDFinished(isFinishedSuccessfully, err) }() relayBlockHash := consensushashing.BlockHash(block) - log.Debugf("IBD started with peer %s and relayBlockHash %s", flow.peer, relayBlockHash) - log.Debugf("Syncing blocks up to %s", relayBlockHash) - log.Debugf("Trying to find highest known syncer chain block from peer %s with relay hash %s", flow.peer, relayBlockHash) + log.Infof("IBD started with peer %s and relayBlockHash %s", flow.peer, relayBlockHash) + log.Infof("Syncing blocks up to %s", relayBlockHash) + log.Infof("Trying to find highest known syncer chain block from peer %s with relay hash %s", flow.peer, relayBlockHash) syncerHeaderSelectedTipHash, highestKnownSyncerChainHash, err := flow.negotiateMissingSyncerChainSegment() if err != nil { @@ -98,7 +100,7 @@ func (flow *handleIBDFlow) runIBDIfNotRunning(block *externalapi.DomainBlock) er if shouldDownloadHeadersProof { log.Infof("Starting IBD with headers proof") - err := flow.ibdWithHeadersProof(syncerHeaderSelectedTipHash, relayBlockHash, block.Header.DAAScore()) + err = flow.ibdWithHeadersProof(syncerHeaderSelectedTipHash, relayBlockHash, block.Header.DAAScore()) if err != nil { return err } @@ -265,7 +267,7 @@ func (flow *handleIBDFlow) negotiateMissingSyncerChainSegment() (*externalapi.Do } } - log.Debugf("Found highest known syncer chain block %s from peer %s", + log.Infof("Found highest known syncer chain block %s from peer %s", highestKnownSyncerChainHash, flow.peer) return syncerHeaderSelectedTipHash, highestKnownSyncerChainHash, nil @@ -280,10 +282,10 @@ func (flow *handleIBDFlow) isGenesisVirtualSelectedParent() (bool, error) { return virtualSelectedParent.Equal(flow.Config().NetParams().GenesisHash), nil } -func (flow *handleIBDFlow) logIBDFinished(isFinishedSuccessfully bool) { +func (flow *handleIBDFlow) logIBDFinished(isFinishedSuccessfully bool, err error) { successString := "successfully" if !isFinishedSuccessfully { - successString = "(interrupted)" + successString = fmt.Sprintf("(interrupted: %s)", err) } log.Infof("IBD with peer %s finished %s", flow.peer, successString) } diff --git a/app/protocol/flows/v5/blockrelay/ibd_with_headers_proof.go b/app/protocol/flows/v5/blockrelay/ibd_with_headers_proof.go index 2be9bb3418..2ad059a0e0 100644 --- a/app/protocol/flows/v5/blockrelay/ibd_with_headers_proof.go +++ b/app/protocol/flows/v5/blockrelay/ibd_with_headers_proof.go @@ -55,7 +55,12 @@ func (flow *handleIBDFlow) shouldSyncAndShouldDownloadHeadersProof( var highestSharedBlockFound, isPruningPointInSharedBlockChain bool if highestKnownSyncerChainHash != nil { - highestSharedBlockFound = true + blockInfo, err := flow.Domain().Consensus().GetBlockInfo(highestKnownSyncerChainHash) + if err != nil { + return false, false, err + } + + highestSharedBlockFound = blockInfo.HasBody() pruningPoint, err := flow.Domain().Consensus().PruningPoint() if err != nil { return false, false, err @@ -87,21 +92,21 @@ func (flow *handleIBDFlow) shouldSyncAndShouldDownloadHeadersProof( } func (flow *handleIBDFlow) checkIfHighHashHasMoreBlueWorkThanSelectedTipAndPruningDepthMoreBlueScore(relayBlock *externalapi.DomainBlock) (bool, error) { - headersSelectedTip, err := flow.Domain().Consensus().GetHeadersSelectedTip() + virtualSelectedParent, err := flow.Domain().Consensus().GetVirtualSelectedParent() if err != nil { return false, err } - headersSelectedTipInfo, err := flow.Domain().Consensus().GetBlockInfo(headersSelectedTip) + virtualSelectedTipInfo, err := flow.Domain().Consensus().GetBlockInfo(virtualSelectedParent) if err != nil { return false, err } - if relayBlock.Header.BlueScore() < headersSelectedTipInfo.BlueScore+flow.Config().NetParams().PruningDepth() { + if relayBlock.Header.BlueScore() < virtualSelectedTipInfo.BlueScore+flow.Config().NetParams().PruningDepth() { return false, nil } - return relayBlock.Header.BlueWork().Cmp(headersSelectedTipInfo.BlueWork) > 0, nil + return relayBlock.Header.BlueWork().Cmp(virtualSelectedTipInfo.BlueWork) > 0, nil } func (flow *handleIBDFlow) syncAndValidatePruningPointProof() (*externalapi.DomainHash, error) {