From 08b6dc74eb6e39dbc3218e100963f82bab6f54fc Mon Sep 17 00:00:00 2001 From: msutton Date: Wed, 29 Jun 2022 11:38:49 +0300 Subject: [PATCH] check if blocks exist --- .../v5/blockrelay/handle_request_headers.go | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/protocol/flows/v5/blockrelay/handle_request_headers.go b/app/protocol/flows/v5/blockrelay/handle_request_headers.go index c85fdc5cb..e7a379bf0 100644 --- a/app/protocol/flows/v5/blockrelay/handle_request_headers.go +++ b/app/protocol/flows/v5/blockrelay/handle_request_headers.go @@ -46,7 +46,31 @@ func (flow *handleRequestHeadersFlow) start() error { } log.Debugf("Received requestHeaders with lowHash: %s, highHash: %s", lowHash, highHash) - isLowSelectedAncestorOfHigh, err := flow.Domain().Consensus().IsInSelectedParentChainOf(lowHash, highHash) + consensus := flow.Domain().Consensus() + + lowHashInfo, err := consensus.GetBlockInfo(lowHash) + if err != nil { + return err + } + if !lowHashInfo.Exists { + return protocolerrors.Errorf(true, "Block %s does not exist", lowHash) + } + if lowHashInfo.BlockStatus == externalapi.StatusInvalid { + return protocolerrors.Errorf(true, "Block %s is invalid", lowHash) + } + + highHashInfo, err := consensus.GetBlockInfo(highHash) + if err != nil { + return err + } + if !highHashInfo.Exists { + return protocolerrors.Errorf(true, "Block %s does not exist", highHash) + } + if highHashInfo.BlockStatus == externalapi.StatusInvalid { + return protocolerrors.Errorf(true, "Block %s is invalid", highHash) + } + + isLowSelectedAncestorOfHigh, err := consensus.IsInSelectedParentChainOf(lowHash, highHash) if err != nil { return err } @@ -62,7 +86,7 @@ func (flow *handleRequestHeadersFlow) start() error { // in order to avoid locking the consensus for too long // maxBlocks MUST be >= MergeSetSizeLimit + 1 const maxBlocks = 1 << 10 - blockHashes, _, err := flow.Domain().Consensus().GetHashesBetween(lowHash, highHash, maxBlocks) + blockHashes, _, err := consensus.GetHashesBetween(lowHash, highHash, maxBlocks) if err != nil { return err } @@ -70,7 +94,7 @@ func (flow *handleRequestHeadersFlow) start() error { blockHeaders := make([]*appmessage.MsgBlockHeader, len(blockHashes)) for i, blockHash := range blockHashes { - blockHeader, err := flow.Domain().Consensus().GetBlockHeader(blockHash) + blockHeader, err := consensus.GetBlockHeader(blockHash) if err != nil { return err }