Skip to content

Conversation

@kallewoof
Copy link
Contributor

@kallewoof kallewoof commented May 17, 2018

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).

@kallewoof kallewoof changed the title 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 May 17, 2018
@kallewoof kallewoof force-pushed the block-pruned-macro branch from 5887dca to e340c9c Compare May 17, 2018 07:32
src/rest.cpp Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing paren.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. Fixed.

@kallewoof kallewoof force-pushed the block-pruned-macro branch from e340c9c to 87a4c78 Compare May 17, 2018 07:34
@Empact
Copy link
Contributor

Empact commented May 17, 2018

This is a different change but for simplification IMO there's room for a HasData() or similar method on CBlockIndex, in place of ->nStatus & BLOCK_HAVE_DATA. There'd be some ~30 callers.

@kallewoof
Copy link
Contributor Author

kallewoof commented May 17, 2018

Not sure pindex->nStatus & BLOCK_HAVE_DATA is obscure enough to warrant a new method, but if it's inline it might be okay, yeah..

@jonasschnelli
Copy link
Contributor

Unsure. I'm worries about the readability of the code as well as undetected nullptr accesses in future implementations.

@kallewoof
Copy link
Contributor Author

kallewoof commented May 17, 2018

@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 a BLOCK_PRUNED(pblockindex)".

@laanwj
Copy link
Member

laanwj commented May 17, 2018

If 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 pblockindex is evaluated multiple times).

@kallewoof kallewoof force-pushed the block-pruned-macro branch from 87a4c78 to a0e2648 Compare May 17, 2018 10:53
@kallewoof
Copy link
Contributor Author

I'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
Copy link
Contributor

practicalswift commented May 17, 2018

Concept ACK: IsPruned() is better

@Empact
Copy link
Contributor

Empact commented May 17, 2018

Concept 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, IsPruned should always be false if it is: https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L4500

@kallewoof
Copy link
Contributor Author

@Empact I think it's okay to leave it in. It has very small impact and I think it's fine even if people check fHavePruned externally as well.

@kallewoof kallewoof changed the title 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 May 19, 2018
@GreatSock
Copy link
Contributor

utACK a0e2648

@promag
Copy link
Contributor

promag commented May 19, 2018

utACK a0e2648.

src/chain.h Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this to the chain module introduces a cyclic dependency between chain and validation (as fHavePruned is defined there). There's no need for this to be a CBlockIndex member as it only uses public fields, so I would suggest making it a function in validation instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I moved it (back) to validation.h.

@kallewoof kallewoof force-pushed the block-pruned-macro branch 2 times, most recently from 5d6f03d to 0f80c21 Compare May 20, 2018 03:59
src/validation.h Outdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, { on newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@kallewoof kallewoof force-pushed the block-pruned-macro branch from 0f80c21 to dff0cf5 Compare May 21, 2018 23:32
@Empact
Copy link
Contributor

Empact commented May 21, 2018

utACK dff0cf5

@laanwj
Copy link
Member

laanwj commented May 29, 2018

Needs rebase.

@laanwj laanwj self-assigned this May 29, 2018
@kallewoof kallewoof force-pushed the block-pruned-macro branch from dff0cf5 to e9a1881 Compare May 30, 2018 03:24
@kallewoof
Copy link
Contributor Author

Rebased.

@Empact
Copy link
Contributor

Empact commented Jun 8, 2018

nit: GetBlockChecked and IsBlockPruned should probably both assert on the index not being null.

@Empact
Copy link
Contributor

Empact commented Jun 8, 2018

Also occurs to me that GetBlockChecked and the functionality in rest_block are identical except for the error handling. You could make GetBlockChecked suitable for both by returning the error in the form of std::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
Copy link
Contributor Author

That sounds like a good idea, but it means having to make GetBlockChecked public, and requires changing it around. There's also no unified way of saying what the error is, in case there are non-NOT_FOUND type errors in the future.

@laanwj laanwj merged commit e9a1881 into bitcoin:master Jun 8, 2018
laanwj added a commit that referenced this pull request Jun 8, 2018
… pruned or not

e9a1881 refactor: add a function for determining if a block is pruned or not (Karl-Johan Alm)

Pull request description:

  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).

Tree-SHA512: b9aeb60663e1d1196df5371d5aa00b32ff5d4cdea6a77e4b566f28115cce09570c18e45e4b81a4033f67c4135c8e32c027f67bae3b75c2ea4564285578a3f4dd
@kallewoof kallewoof deleted the block-pruned-macro branch June 9, 2018 01:53
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 6, 2019
Summary:
"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)."

This is a backport from Core PR13259: bitcoin/bitcoin#13259

Test Plan:
  ninja check
  ./test/functional/test_runner.py

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D4624
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 2, 2020
…lock is pruned or not

e9a1881 refactor: add a function for determining if a block is pruned or not (Karl-Johan Alm)

Pull request description:

  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. bitcoin#10757~~ (turns out it was a move, not an addition).

Tree-SHA512: b9aeb60663e1d1196df5371d5aa00b32ff5d4cdea6a77e4b566f28115cce09570c18e45e4b81a4033f67c4135c8e32c027f67bae3b75c2ea4564285578a3f4dd
jonspock pushed a commit to jonspock/devault that referenced this pull request Oct 2, 2020
Summary:
"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)."

This is a backport from Core PR13259: bitcoin/bitcoin#13259

Test Plan:
  ninja check
  ./test/functional/test_runner.py

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D4624
jonspock pushed a commit to jonspock/devault that referenced this pull request Oct 5, 2020
Summary:
"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)."

This is a backport from Core PR13259: bitcoin/bitcoin#13259

Test Plan:
  ninja check
  ./test/functional/test_runner.py

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D4624
jonspock pushed a commit to devaultcrypto/devault that referenced this pull request Oct 10, 2020
Summary:
"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)."

This is a backport from Core PR13259: bitcoin/bitcoin#13259

Test Plan:
  ninja check
  ./test/functional/test_runner.py

Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, nakihito, jasonbcox

Subscribers: jasonbcox

Differential Revision: https://reviews.bitcoinabc.org/D4624
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants