Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: Remove cs_main lock from blockToJSON and blockheaderToJSON #12151

Merged
merged 3 commits into from Jan 4, 2019

Conversation

@promag
Copy link
Member

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

@promag promag force-pushed the 2018-01-blocktojson branch from a40e35a to b1950e4 Jan 11, 2018
@promag promag force-pushed the 2018-01-blocktojson branch 3 times, most recently from 1aeee56 to f88df2e Jan 11, 2018
Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

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

@TheBlueMatt TheBlueMatt Jan 11, 2018

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

Copy link
Member Author

@promag promag Jan 11, 2018

This way you get next of blockindex.

Copy link
Contributor

@TheBlueMatt TheBlueMatt Jan 11, 2018

Ohoh, sorry, missed its use in blockheaderToJSON.

Copy link
Contributor

@ryanofsky ryanofsky Feb 5, 2018

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

Copy link
Member Author

@promag promag Feb 5, 2018

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

Copy link
Member Author

@promag promag Feb 5, 2018

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

Copy link
Member Author

@promag promag Feb 26, 2018

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

@TheBlueMatt TheBlueMatt Jan 11, 2018

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
Member Author

@promag promag Jan 11, 2018

This moves up tip variable declared below.

@promag
Copy link
Member Author

@promag promag commented Jan 11, 2018

Best reviewed commit by commit

src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
@promag promag force-pushed the 2018-01-blocktojson branch from f88df2e to 8ca0981 Jan 11, 2018
Copy link
Member Author

@promag promag left a comment

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

src/rest.cpp Outdated Show resolved Hide resolved
src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
@promag promag force-pushed the 2018-01-blocktojson branch from 8ca0981 to f6d175d Jan 11, 2018
@promag
Copy link
Member Author

@promag promag commented Jan 11, 2018

Fixed @TheBlueMatt and @jimpo comments, thanks.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

utACK f6d175ddc97a0d2b2614b44ddb9efbebf8d6eec5

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

@ryanofsky ryanofsky Feb 5, 2018

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 Feb 26, 2018
@promag
Copy link
Member Author

@promag 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.

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Mar 19, 2018

Needs rebase if still relevant

src/rest.cpp Outdated Show resolved Hide resolved
@promag promag force-pushed the 2018-01-blocktojson branch from 2925d3b to 1a6b13c Mar 19, 2018
@promag
Copy link
Member Author

@promag 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
Member

@jnewbery jnewbery commented Apr 3, 2018

needs rebase

@promag
Copy link
Member Author

@promag promag commented Apr 3, 2018

Rebased.

@promag promag force-pushed the 2018-01-blocktojson branch from 1a6b13c to 77d51a6 Apr 3, 2018
Copy link
Contributor

@ryanofsky ryanofsky left a comment

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

src/rest.cpp Outdated Show resolved Hide resolved
@promag promag force-pushed the 2018-01-blocktojson branch from 77d51a6 to e20f745 Apr 4, 2018
Copy link
Contributor

@ryanofsky ryanofsky left a comment

utACK e20f745d9b279de9e43994505731658f0f3582fa, just whitespace fix since last review

Copy link
Contributor

@jimpo jimpo left a comment

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

src/rpc/blockchain.cpp Outdated Show resolved Hide resolved
@promag promag force-pushed the 2018-01-blocktojson branch from e20f745 to 00755e5 May 18, 2018
@DrahtBot DrahtBot mentioned this pull request Sep 10, 2018
Copy link
Contributor

@ryanofsky ryanofsky left a comment

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

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

@MarcoFalke MarcoFalke Sep 14, 2018

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

Copy link
Member Author

@promag promag Jan 3, 2019

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

Copy link
Member

@MarcoFalke MarcoFalke Jan 3, 2019

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

Copy link
Member Author

@promag promag Jan 3, 2019

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

@DrahtBot
Copy link
Contributor

@DrahtBot 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.

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Jan 3, 2019

utACK b9f226b

MarcoFalke added a commit to MarcoFalke/bitcoin-core that referenced this issue 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
@MarcoFalke MarcoFalke merged commit b9f226b into bitcoin:master Jan 4, 2019
2 checks passed
@promag promag deleted the 2018-01-blocktojson branch Jan 4, 2019
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Apr 29, 2021
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Apr 29, 2021
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Apr 29, 2021
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Jun 1, 2021
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Jun 1, 2021
LarryRuane added a commit to LarryRuane/zcash that referenced this issue Jun 1, 2021
Munkybooty added a commit to Munkybooty/dash that referenced this issue 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 added a commit to Munkybooty/dash that referenced this issue 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 added a commit to Munkybooty/dash that referenced this issue 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 added a commit to UdjinM6/dash that referenced this issue 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 added a commit to Munkybooty/dash that referenced this issue 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 added a commit to Munkybooty/dash that referenced this issue 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

10 participants