From e9a1881b90704c6708cfba79d2208debbd4476d0 Mon Sep 17 00:00:00 2001
From: Karl-Johan Alm <karljohan-alm@garage.co.jp>
Date: Thu, 17 May 2018 16:30:00 +0900
Subject: [PATCH] refactor: add a function for determining if a block is pruned
 or not

---
 src/rest.cpp           | 2 +-
 src/rpc/blockchain.cpp | 2 +-
 src/validation.h       | 6 ++++++
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/rest.cpp b/src/rest.cpp
index ffa75c241..a5f164497 100644
--- a/src/rest.cpp
+++ b/src/rest.cpp
@@ -217,7 +217,7 @@ static bool rest_block(HTTPRequest* req,
             return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
         }
 
-        if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)
+        if (IsBlockPruned(pblockindex))
             return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
 
         if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 24fb522e6..ea9eb3eca 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -742,7 +742,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
 static CBlock GetBlockChecked(const CBlockIndex* pblockindex)
 {
     CBlock block;
-    if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) {
+    if (IsBlockPruned(pblockindex)) {
         throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
     }
 
diff --git a/src/validation.h b/src/validation.h
index b5ab10786..04f5b6cb8 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -497,4 +497,10 @@ bool DumpMempool();
 /** Load the mempool from disk. */
 bool LoadMempool();
 
+//! Check whether the block associated with this index entry is pruned or not.
+inline bool IsBlockPruned(const CBlockIndex* pblockindex)
+{
+    return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
+}
+
 #endif // BITCOIN_VALIDATION_H