<p>You can now get the target for the next block without all the transactions using this command...</p>
<pre><code>bitcoin-cli getblocktemplate &quot;{\&quot;rules\&quot;:[\&quot;segwit\&quot;]}&quot;
</code></pre>
<p>...which returns this relatively short datastructure that contains the target...</p>
<pre><code>{
  &quot;capabilities&quot;: [
    &quot;proposal&quot;
  ],
  &quot;version&quot;: 536870912,
  &quot;rules&quot;: [
    &quot;csv&quot;,
    &quot;!segwit&quot;,
    &quot;taproot&quot;
  ],
  &quot;vbavailable&quot;: {
  },
  &quot;vbrequired&quot;: 0,
  &quot;previousblockhash&quot;: &quot;0000000000000000000204567a1893c68560ac05decc823e264ee90ccc122e07&quot;,
  &quot;transactions&quot;: [
  ],
  &quot;coinbaseaux&quot;: {
  },
  &quot;coinbasevalue&quot;: 625000000,
  &quot;longpollid&quot;: &quot;0000000000000000000204567a1893c68560ac05decc823e264ee90ccc122e07434&quot;,
  &quot;target&quot;: &quot;0000000000000000000838300000000000000000000000000000000000000000&quot;,
  &quot;mintime&quot;: 1670613493,
  &quot;mutable&quot;: [
    &quot;time&quot;,
    &quot;transactions&quot;,
    &quot;prevblock&quot;
  ],
  &quot;noncerange&quot;: &quot;00000000ffffffff&quot;,
  &quot;sigoplimit&quot;: 80000,
  &quot;sizelimit&quot;: 4000000,
  &quot;weightlimit&quot;: 4000000,
  &quot;curtime&quot;: 1670616125,
  &quot;bits&quot;: &quot;17083830&quot;,
  &quot;height&quot;: 766672,
  &quot;default_witness_commitment&quot;: &quot;6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9&quot;
}
</code></pre>

CC BY-SA 4.0
2022-12-09T20:12:39.057Z
2022-12-09T20:12:39.057Z
bigjosh
<p>You can easly calculate that from the <code>difficulty</code></p>
<p>So just with the RPC call <code>getdifficulty</code> calculate this:</p>
<pre><code>target = targetmax / difficulty
</code></pre>
<p>Where <code>targetmax</code> = <code>26959535291011309493156476344723991336010898738574164086137773096960</code></p>
<p>target max is a fix number from the genesis block</p>
<p>If the hash value (Converted to decimal) is lower then this value, it's a valid Block</p>

CC BY-SA 4.0
2021-05-07T02:54:15.817Z
That reports the difficulty of the last block, not the *next* block.
Pieter Wuille
CC BY-SA 4.0
2021-05-07T00:28:13.097Z
2021-05-07T00:28:13.097Z
Hamita
<blockquote>
<p>Is there a fast way to determine the target for the next block from bitcoin-core over RPC?</p>
</blockquote>
<p>Not really, there's no special RPC command to have the future target.</p>
<p>The target is <strong>the number you need to get below to mine a block</strong>. The target <strong>adjusts every 2016 blocks</strong>. That's very important to take into account.</p>
<p>First, you have to get the last target block information by doing a <strong>getblockchaininfo</strong> RPC command. Then you will be able to access to 3 interesting JSON fields which are the <strong>best block height</strong> (blocks), the <strong>best block hash</strong> (bestblockhash) and the <strong>current difficulty</strong> (difficulty).</p>
<p>Secondly, you must calculate a modular operation which is :</p>
<p><a href="https://i.sstatic.net/mgBqy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/mgBqy.png" alt="enter image description here" /></a></p>
<p>There are 2 choices :</p>
<ul>
<li>If the result is <strong>different</strong> from 0, you will be able to use the same target for the next block ! You'll notice there's no field called &quot;target&quot; in JSON-RPC, so in this step you have to do :</li>
</ul>
<p><strong>target = targetmax / difficulty</strong></p>
<p>where <strong>targetmax = 0xffff0000000000000000000000000000000000000000000000000000</strong>
and represents a fix number from the genesis block</p>
<ul>
<li>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</li>
</ul>
<p>There's a code for the second choice. This is available : <a href="https://learnmeabitcoin.com/technical/target#code" rel="nofollow noreferrer">https://learnmeabitcoin.com/technical/target#code</a>.</p>
<p>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.</p>
<p>Tell me if you see a mistake by posting a comment, Loopite.</p>

CC BY-SA 4.0
2022-06-01T20:28:14.100Z
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
CC BY-SA 4.0
2022-06-01T10:17:28.067Z
2022-06-01T10:17:28.067Z
Loopite
<p>The <code>target</code> for the next block is available in <code>getblocktemplate</code> but I can not figure out if there is a way to get the <code>target</code> without also getting all of the pending transactions, which can take a very long time and generate lots of data.</p>
<p>You could also use <code>getbestblock</code> to get the top block and then use <code>getblockheader</code> to get the previous 2016 blocks and then do the math based on the times, but again this is very slow and data heavy.</p>
<p>Is there a <em>fast</em> way to determine the <code>target</code> for the <em>next</em> block from bitcoin-core over RPC?</p>

<bitcoin-core><json-rpc><miner-configuration>
How to quickly get the target of the next block to be mined from bitcoin-core over RPC?
bigjosh
# How to quickly get the target of the next block to be mined from bitcoin-core over RPC?

(Asked by: username_0 on 2021-05-06)

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?

**Tags:** bitcoin-core, json-rpc, miner-configuration


---

## 3 answers

### Answer by username_0 on 2022-12-09. Score: 0 | Accepted answer

You can now get the target for the next block without all the transactions using this command...

```
bitcoin-cli getblocktemplate "{\"rules\":[\"segwit\"]}"

```

...which returns this relatively short datastructure that contains the target...

```
{
  "capabilities": [
    "proposal"
  ],
  "version": 536870912,
  "rules": [
    "csv",
    "!segwit",
    "taproot"
  ],
  "vbavailable": {
  },
  "vbrequired": 0,
  "previousblockhash": "0000000000000000000204567a1893c68560ac05decc823e264ee90ccc122e07",
  "transactions": [
  ],
  "coinbaseaux": {
  },
  "coinbasevalue": 625000000,
  "longpollid": "0000000000000000000204567a1893c68560ac05decc823e264ee90ccc122e07434",
  "target": "0000000000000000000838300000000000000000000000000000000000000000",
  "mintime": 1670613493,
  "mutable": [
    "time",
    "transactions",
    "prevblock"
  ],
  "noncerange": "00000000ffffffff",
  "sigoplimit": 80000,
  "sizelimit": 4000000,
  "weightlimit": 4000000,
  "curtime": 1670616125,
  "bits": "17083830",
  "height": 766672,
  "default_witness_commitment": "6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9"
}

```


---

### Answer by username_1 on 2021-05-07. Score: 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

> **Comments:**
> **username_2**: That reports the difficulty of the last block, not the *next* block.  

---

### Answer by username_3 on 2022-06-01. Score: 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](https://i.sstatic.net/mgBqy.png)](https://i.sstatic.net/mgBqy.png)

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, username_3.

> **Comments:**
> **username_0**: 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!  

---

