[NOD-493] Make GetBlock return an appropriate error if the hash is known to be of an invalid block. (#520)

This commit is contained in:
stasatdaglabs 2019-12-08 14:05:56 +02:00 committed by Dan Aharoni
parent 9adb105e37
commit 11e936d109
3 changed files with 21 additions and 0 deletions

View File

@ -192,6 +192,18 @@ func (dag *BlockDAG) IsKnownOrphan(hash *daghash.Hash) bool {
return exists
}
// IsKnownInvalid returns whether the passed hash is known to be an invalid block.
// Note that if the block is not found this method will return false.
//
// This function is safe for concurrent access.
func (dag *BlockDAG) IsKnownInvalid(hash *daghash.Hash) bool {
node := dag.index.LookupNode(hash)
if node == nil {
return false
}
return dag.index.NodeStatus(node).KnownInvalid()
}
// GetOrphanMissingAncestorHashes returns all of the missing parents in the orphan's sub-DAG
//
// This function is safe for concurrent access.

View File

@ -66,6 +66,7 @@ const (
ErrRPCRawTxString RPCErrorCode = -32602
ErrRPCDecodeHexString RPCErrorCode = -22
ErrRPCOrphanBlock RPCErrorCode = -6
ErrRPCBlockInvalid RPCErrorCode = -5
)
// Errors that are specific to btcd.

View File

@ -23,6 +23,14 @@ func handleGetBlock(s *Server, cmd interface{}, closeChan <-chan struct{}) (inte
return nil, rpcDecodeHexError(c.Hash)
}
// Return an appropriate error if the block is known to be invalid
if s.cfg.DAG.IsKnownInvalid(hash) {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCBlockInvalid,
Message: "Block is known to be invalid",
}
}
// Return an appropriate error if the block is an orphan
if s.cfg.DAG.IsKnownOrphan(hash) {
return nil, &btcjson.RPCError{