Skip to content

Conversation

promag
Copy link
Contributor

@promag promag commented Jan 11, 2018

Motivated by #11913 (comment), this pull makes blockToJSON and blockheaderToJSON free of cs_main locks.

Locking cs_main was required to access chainActive in order to check if the block was in the chain and to retrieve the next block index.

With the this approach, CBlockIndex::GetAncestor() is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Generally wouldn't bother too much reducing cs_main scope when its really a trivial amount of time running with cs_main held, though I agree in the context of #11913 it makes sense to not lock cs_main and then unlock, then re-lock it there.

{
return GetDifficulty(chainActive, blockindex);
next = tip->GetAncestor(blockindex->nHeight + 1);
if (next && next->pprev == blockindex) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? You can just do a GetAncestor for blockindex->nHeight here, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This way you get next of blockindex.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohoh, sorry, missed its use in blockheaderToJSON.

Copy link
Contributor

Choose a reason for hiding this comment

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

This confused me for a minute too. Maybe could rename function to something like ComputeNextBlockAndDepth to mention the next part.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do, WDYT about returning std::pair<int, const CBlockIndex*>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can add a comment there too explaning the height + 1 and ->pprev == blockindex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to ComputeNextBlockAndDepth as per @ryanofsky suggestion.

@@ -1185,20 +1182,21 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)

LOCK(cs_main);

const CBlockIndex* tip = chainActive.Tip();
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to change these unless you're gonna actually kill the cs_main held for the whole time, I'd think, no (and little reason to, its not like its being held for an extended period)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This moves up tip variable declared below.

@promag
Copy link
Contributor Author

promag commented Jan 11, 2018

Best reviewed commit by commit

@promag promag force-pushed the 2018-01-blocktojson branch from f88df2e to 8ca0981 Compare January 11, 2018 22:23
Copy link
Contributor Author

@promag promag left a comment

Choose a reason for hiding this comment

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

Changed the order in the signature and forgot to change callers.

@promag promag force-pushed the 2018-01-blocktojson branch from 8ca0981 to f6d175d Compare January 11, 2018 22:43
@promag
Copy link
Contributor Author

promag commented Jan 11, 2018

Fixed @TheBlueMatt and @jimpo comments, thanks.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK f6d175ddc97a0d2b2614b44ddb9efbebf8d6eec5

{
return GetDifficulty(chainActive, blockindex);
next = tip->GetAncestor(blockindex->nHeight + 1);
if (next && next->pprev == blockindex) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This confused me for a minute too. Maybe could rename function to something like ComputeNextBlockAndDepth to mention the next part.

@promag promag force-pushed the 2018-01-blocktojson branch 2 times, most recently from 1e6f7f7 to 2925d3b Compare February 26, 2018 16:23
@promag
Copy link
Contributor Author

promag commented Feb 26, 2018

Rebased mainly due to recent change from push_back(Pair()) to pushKV().

Also reworded to replace prefix [rpc] to rpc: as per @laanwj suggestion.

@maflcko
Copy link
Member

maflcko commented Mar 19, 2018

Needs rebase if still relevant

@promag promag force-pushed the 2018-01-blocktojson branch from 2925d3b to 1a6b13c Compare March 19, 2018 13:50
@promag
Copy link
Contributor Author

promag commented Mar 19, 2018

Rebased.

@MarcoFalke like @TheBlueMatt said above:

in the context of #11913 it makes sense to not lock cs_main and then unlock, then re-lock it there.

I'd say at least let's wait for that.

@jnewbery
Copy link
Contributor

jnewbery commented Apr 3, 2018

needs rebase

@promag
Copy link
Contributor Author

promag commented Apr 3, 2018

Rebased.

@promag promag force-pushed the 2018-01-blocktojson branch from 1a6b13c to 77d51a6 Compare April 3, 2018 22:13
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK 77d51a68108e282856d9894d41e8a600dba78dd8. No changes since last review other than rebase and removing std::move.

@promag promag force-pushed the 2018-01-blocktojson branch from 77d51a6 to e20f745 Compare April 4, 2018 22:38
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK e20f745d9b279de9e43994505731658f0f3582fa, just whitespace fix since last review

Copy link
Contributor

@jimpo jimpo left a comment

Choose a reason for hiding this comment

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

I think you can stop passing tip to GetDifficulty in most places.

@promag promag force-pushed the 2018-01-blocktojson branch from e20f745 to 00755e5 Compare May 18, 2018 00:39
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK b9f226b only change since previous review is simplifying GetDifficulty in the second commit.

{
return 1.0;
}
assert(blockindex);
Copy link
Member

Choose a reason for hiding this comment

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

In rpc code, assert should be replaced with throw JSONRPCError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is a usage error, should be a programatic error?

Copy link
Member

Choose a reason for hiding this comment

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

I guess you could avoid the assert by passing in a const reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure but out of scope here and I'm happy to submit that refactor in a separate PR.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 21, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@maflcko
Copy link
Member

maflcko commented Jan 3, 2019

utACK b9f226b

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Jan 4, 2019
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
@maflcko maflcko merged commit b9f226b into bitcoin:master Jan 4, 2019
@promag promag deleted the 2018-01-blocktojson branch January 4, 2019 11:55
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 29, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 29, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 29, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Jun 1, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Jun 1, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Jun 1, 2021
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 14, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 24, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 24, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Aug 24, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 24, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 24, 2021
…ockheaderToJSON

b9f226b rpc: Remove cs_main lock from blockToJSON and blockHeaderToJSON (João Barbosa)
343b98c rpc: Specify chain tip instead of chain in GetDifficulty (João Barbosa)
54dc13b rpc: Fix SoftForkMajorityDesc and SoftForkDesc signatures (João Barbosa)

Pull request description:

  Motivated by bitcoin#11913 (comment), this pull makes `blockToJSON` and `blockheaderToJSON` free of `cs_main` locks.

  Locking `cs_main` was required to access `chainActive` in order to check if the block was in the chain and to retrieve the next block index.

  With the this approach, `CBlockIndex::GetAncestor()` is used in a way to check if the block belongs to the specified chain tip and, at the same time, get the next block index.

Tree-SHA512: a6720ace0182c19033bbed1a404f729d793574db8ab16e0966ffe412145611e32c30aaab02975d225df6d439d7b9ef2070e732b16137a902b0293c8cddfeb85f
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 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.

10 participants