Is there a fast way to determine the target for the next block from bitcoin-core over RPC?
Not really, there's no special RPC command to have the future target.
The target is the number you need to get below to mine a block. The target adjusts every 2016 blocks. That's very important to take into account.
First, you have to get the last target block information by doing a getblockchaininfo RPC command. Then you will be able to access to 3 interesting JSON fields which are the best block height (blocks), the best block hash (bestblockhash) and the current difficulty (difficulty).
Secondly, you must calculate a modular operation which is :
There are 2 choices :
- If the result is different from 0, you will be able to use the same target for the next block ! You'll notice there's no field called "target" in JSON-RPC, so in this step you have to do :
target = targetmax / difficulty
where targetmax = 0xffff0000000000000000000000000000000000000000000000000000
and represents a fix number from the genesis block
- If the result is 0, you shall calculate the new target thanks to the difficulty field : work out the ratio of the actual time against the expected time. Note : blocks represent the number of blocks in the blockchain not the last block height. The lastBlockHeight = blocks - 1
There's a code for the second choice. This is available : https://learnmeabitcoin.com/technical/target#code.
I think the second step is a little bit complicated. Note that the first step is most often called (the easier) and that the second will be used sometimes.
Tell me if you see a mistake by posting a comment, Loopite.