dashpay / dash

Dash - Reinventing Cryptocurrency
https://www.dash.org
MIT License
1.49k stars 1.19k forks source link

refactor: move Get*Index to rpc/index_util.cpp, const-ify functions and arguments, add lock annotations and some minor housekeeping #6083

Closed kwvg closed 1 week ago

kwvg commented 1 week ago

Additional Information

This pull request is motivated by bitcoin#22371, which gets rid of the pblocktree global.

The sole usage of pblocktree introduced by Dash is for managing our {address, spent, timestamp} indexes with most of invocations within BlockManager or CChainState, granting them internal access to pblocktree (now m_block_tree_db). The sole exception being Get*Index, that relies on accessing the global and has no direct internal access.

This pull request aims to refactor code associated with Get*Index with the eventual aim of moving gaining access to the global out of the function. Get*Index is called exclusively called through RPC, which makes giving it access to ChainstateManager quite easy, which makes switching from the global to accessing it through ChainstateManager when backporting bitcoin#22371 a drop-in replacement.

Alongside that, the surrounding code has been given some TLC:

Breaking Changes

None expected.

Checklist:

PastaPastaPasta commented 1 week ago

I'm still confused by all the added cs_mains. You say

Add lock annotations for accessing pblocktree (while already protected by ::cs_main, said protection is currently not enforced but will be once moved into BlockManager in the backport)

and I don't see where cs_main locks are being acquired in develop for these cases. Can you help me figure that out?

github-actions[bot] commented 1 week ago

This pull request has conflicts, please rebase.

kwvg commented 1 week ago

I don't see where cs_main locks are being acquired in develop for these cases. Can you help me figure that out?

@PastaPastaPasta

pblocktree is commented in Dash Core as being protected by ::cs_main in develop (source) but this isn't enforced (lack of EXCLUSIVE_LOCKS_REQUIRED).

In Bitcoin Core, the usage of pblocktree is mostly limited to functions like LoadBlockIndex{DB}, which are protected by ::cs_main (source, source) and FlushStateToDisk, which locks ::cs_main (source), Dash Core differs in that it utilizes pblocktree to manage its indexes (which for the most part are covered by the locks mentioned above) but also uses it in RPC code (via Get*Index, which aren't).

The lack of explicit annotations meant that the safety assumed from the annotations given to functions utilizing pblocktree doesn't extend to RPC calls as no annotations are present there at all.

The annotation is made explicit in https://github.com/bitcoin/bitcoin/pull/22371 (source, renamed as m_block_tree_db), which is part of a series of backports currently undergoing testing. In preparation of those series of backports, this pull request was opened to add explicit annotations (with added cleanups).