From 11e936d10902511ddbb8745c4ce19e249d91f407 Mon Sep 17 00:00:00 2001 From: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com> Date: Sun, 8 Dec 2019 14:05:56 +0200 Subject: [PATCH] [NOD-493] Make GetBlock return an appropriate error if the hash is known to be of an invalid block. (#520) --- blockdag/dag.go | 12 ++++++++++++ btcjson/jsonrpcerr.go | 1 + server/rpc/handle_get_block.go | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/blockdag/dag.go b/blockdag/dag.go index 9c7753cfc..a6606ad6e 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -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. diff --git a/btcjson/jsonrpcerr.go b/btcjson/jsonrpcerr.go index 6e83c4b1a..862b01335 100644 --- a/btcjson/jsonrpcerr.go +++ b/btcjson/jsonrpcerr.go @@ -66,6 +66,7 @@ const ( ErrRPCRawTxString RPCErrorCode = -32602 ErrRPCDecodeHexString RPCErrorCode = -22 ErrRPCOrphanBlock RPCErrorCode = -6 + ErrRPCBlockInvalid RPCErrorCode = -5 ) // Errors that are specific to btcd. diff --git a/server/rpc/handle_get_block.go b/server/rpc/handle_get_block.go index ae6644735..96eb55382 100644 --- a/server/rpc/handle_get_block.go +++ b/server/rpc/handle_get_block.go @@ -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{