The check for whether a block is pruned or not is sufficiently obscure that it deserves a macro. It is also used in 2 places, with more coming, e.g. #10757 (turns out it was a move, not an addition).
refactoring: add a method for determining if a block is pruned or not #13259
pull kallewoof wants to merge 1 commits into bitcoin:master from kallewoof:block-pruned-macro changing 3 files +8 −2-
kallewoof commented at 7:31 AM on May 17, 2018: member
- kallewoof renamed this:
validation: add a macro for determining if a block is pruned or not
refactoring: add a macro for determining if a block is pruned or not
on May 17, 2018 - kallewoof force-pushed on May 17, 2018
-
in src/rest.cpp:220 in e340c9c35c outdated
216 | @@ -217,7 +217,7 @@ static bool rest_block(HTTPRequest* req, 217 | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); 218 | } 219 | 220 | - if (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0) 221 | + if (BLOCK_PRUNED(pblockindex)
Empact commented at 7:33 AM on May 17, 2018:Missing paren.
kallewoof commented at 7:34 AM on May 17, 2018:Heh. Fixed.
kallewoof force-pushed on May 17, 2018Empact commented at 7:47 AM on May 17, 2018: memberThis is a different change but for simplification IMO there's room for a
HasData()or similar method onCBlockIndex, in place of->nStatus & BLOCK_HAVE_DATA. There'd be some ~30 callers.kallewoof commented at 7:53 AM on May 17, 2018: memberNot sure
pindex->nStatus & BLOCK_HAVE_DATAis obscure enough to warrant a new method, but if it's inline it might be okay, yeah..jonasschnelli commented at 7:55 AM on May 17, 2018: contributorUnsure. I'm worries about the readability of the code as well as undetected
nullptraccesses in future implementations.kallewoof commented at 8:21 AM on May 17, 2018: member@jonasschnelli The code that is replaced has to do that check already (null block index), so not sure what the concern is.
Also, I think this helps with readability. I think the "is this block pruned? Oh let's just throw in a
(fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0)and check" is a lot more error prone that "oh let's just throw in aBLOCK_PRUNED(pblockindex)".laanwj added the label Refactoring on May 17, 2018laanwj commented at 10:18 AM on May 17, 2018: memberIf you insist on factoring this out, I'd prefer an inline function instead of a macro. Macros are mostly an ugly C legacy (and have some well-known issues, such as that
pblockindexis evaluated multiple times).kallewoof force-pushed on May 17, 2018kallewoof commented at 10:55 AM on May 17, 2018: memberI'm not insisting anything, but I think it would help readability since the copy-pasted if case is rather hairy.
Replaced macro with an inline method in
CBlockIndex.practicalswift commented at 11:03 AM on May 17, 2018: contributorConcept ACK:
IsPruned()is betterEmpact commented at 4:12 PM on May 17, 2018: memberConcept ACK. I would leave the fHavePruned check at the call sites though for separation of concerns. Looks like he fHavePruned check is a feature flipper / optimization,
IsPrunedshould always be false if it is: https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L4500kallewoof renamed this:refactoring: add a macro for determining if a block is pruned or not
refactoring: add a method for determining if a block is pruned or not
on May 19, 2018GreatSock commented at 12:51 PM on May 19, 2018: contributorpromag commented at 2:19 PM on May 19, 2018: memberutACK a0e2648.
in src/chain.h:343 in a0e2648534 outdated
338 | @@ -337,6 +339,11 @@ class CBlockIndex 339 | return ((nStatus & BLOCK_VALID_MASK) >= nUpTo); 340 | } 341 | 342 | + //! Check whether the block associated with this index entry is pruned or not. 343 | + inline bool IsPruned() const {
sipa commented at 6:56 PM on May 19, 2018:Adding this to the
chainmodule introduces a cyclic dependency betweenchainandvalidation(as fHavePruned is defined there). There's no need for this to be aCBlockIndexmember as it only uses public fields, so I would suggest making it a function invalidationinstead.
kallewoof commented at 3:48 AM on May 20, 2018:Good point. I moved it (back) to
validation.h.kallewoof force-pushed on May 20, 2018kallewoof force-pushed on May 20, 2018in src/validation.h:499 in 0f80c21ba9 outdated
494 | @@ -495,4 +495,9 @@ bool DumpMempool(); 495 | /** Load the mempool from disk. */ 496 | bool LoadMempool(); 497 | 498 | +//! Check whether the block associated with this index entry is pruned or not. 499 | +inline bool IsBlockPruned(const CBlockIndex* pblockindex) {
promag commented at 3:40 PM on May 21, 2018:nit,
{on newline.
kallewoof commented at 11:32 PM on May 21, 2018:Fixed.
kallewoof force-pushed on May 21, 2018Empact commented at 11:55 PM on May 21, 2018: memberutACK dff0cf5
laanwj commented at 6:29 PM on May 29, 2018: memberNeeds rebase.
laanwj assigned laanwj on May 29, 2018refactor: add a function for determining if a block is pruned or not e9a1881b90kallewoof force-pushed on May 30, 2018kallewoof commented at 3:24 AM on May 30, 2018: memberRebased.
Empact commented at 7:57 AM on June 8, 2018: membernit:
GetBlockCheckedandIsBlockPrunedshould probably both assert on the index not being null.Empact commented at 8:17 AM on June 8, 2018: memberAlso occurs to me that
GetBlockCheckedand the functionality inrest_blockare identical except for the error handling. You could makeGetBlockCheckedsuitable for both by returning the error in the form ofstd::optional<std::string>boost::optional<std::string>(because C++11). That would remove the need for this function because there would be no reuse.kallewoof commented at 10:42 AM on June 8, 2018: memberThat sounds like a good idea, but it means having to make
GetBlockCheckedpublic, and requires changing it around. There's also no unified way of saying what the error is, in case there are non-NOT_FOUNDtype errors in the future.laanwj merged this on Jun 8, 2018laanwj closed this on Jun 8, 2018laanwj referenced this in commit 121cbaacc2 on Jun 8, 2018kallewoof deleted the branch on Jun 9, 2018jasonbcox referenced this in commit a8257058a3 on Dec 6, 2019PastaPastaPasta referenced this in commit b2c3237b1c on Jun 17, 2020PastaPastaPasta referenced this in commit 000b2794a5 on Jul 2, 2020jonspock referenced this in commit a1c4ed3e23 on Oct 2, 2020jonspock referenced this in commit fa4cc2da53 on Oct 5, 2020jonspock referenced this in commit c404d98a00 on Oct 10, 2020MarcoFalke locked this on Sep 8, 2021
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 18:15 UTC
More mirrored repositories can be found on mirror.b10c.me