2

The target for the next block is available in getblocktemplate but I can not figure out if there is a way to get the target without also getting all of the pending transactions, which can take a very long time and generate lots of data.

You could also use getbestblock to get the top block and then use getblockheader to get the previous 2016 blocks and then do the math based on the times, but again this is very slow and data heavy.

Is there a fast way to determine the target for the next block from bitcoin-core over RPC?

2 Answers 2

0

You can easly calculate that from the difficulty

So just with the RPC call getdifficulty calculate this:

target = targetmax / difficulty

Where targetmax = 26959535291011309493156476344723991336010898738574164086137773096960

target max is a fix number from the genesis block

If the hash value (Converted to decimal) is lower then this value, it's a valid Block

1
  • 4
    That reports the difficulty of the last block, not the next block. May 7, 2021 at 2:54
0

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 :

enter image description here

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.

1
  • Yes, trivial when there is not a difficulty adjustment but looking for a general solution that does not require reading the previous 2016 block headers. Obviously Bitcoin core knows this number (it needs it to verify the next block) but sadly does not seem to make it available efficiently. Thanks!
    – bigjosh
    Jun 1 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.