Replace the function-local statics in getblocktemplate with a
CachedBlockTemplate struct and a GetNewOrCachedBlockTemplate() helper,
both local to rpc/mining.cpp. The cache is still process-wide, but it is
now GUARDED_BY(::cs_main), so its synchronization is checked.
GetNewOrCachedBlockTemplate() keeps the existing rules: the template is
rebuilt when the tip changes, or when the mempool changed and the cached
template is more than 5 seconds old. A failed rebuild leaves the cached
template unset so that the next call tries again.
This prepares for moving the cache into BlockTemplateManager in the next
commit. While the code is still next to the original, it also:
- Drops the static CBlockIndex pointer. pindexPrev is looked up from the
template's hashPrevBlock, so it cannot outlive the ChainstateManager
that owns it.
- Reads the active chain tip inside GetNewOrCachedBlockTemplate() and
compares it with the template's hashPrevBlock, so the cache cannot
pair a template with a tip it does not build on. The caller no longer
needs to re-read the tip after longpolling.
- Builds the longpollid from hashPrevBlock, so it always names the block
the template builds on.
- Replaces the deprecated GetTime() with the equivalent
Now<NodeSeconds>().
- Names the 5 second rebuild interval GBT_TEMPLATE_REBUILD_INTERVAL.