6a1e57656c636f6d6520746f20426c6f636b636861696e20416e616c79736973

Month: March 2023

Nonce Exhaustion

I’m currently working through data stored on the Bitcoin blockchain for a future post. In doing so, I was surprised by the amount of seemingly random or meaningless data within Coinbase transactions. The obvious reason for this may be a type of encoding I am not familiar with, but these encodings often exhibit patterns which weren’t observed in many instances.

I researched potential reasons for this seemingly random data and I found in Andreas Antonopoulos’ excellent book ‘Mastering Bitcoin’ a discussion of Bitcoin’s nonce being insufficient to facilitate modern hash rates in combination with Bitcoin’s timestamp only being to 1 second precision.


The nonce is part of the Bitcoin block header which is intended to be modified by miners when mining to create a hash lower than the difficulty target. This is stored within the Bitcoin block header as a 4-byte (32 bit) value. 4-bytes allow for 4,294,967,296 unique combinations. Therefore a hash rate of ~4.3GH/s could exhaust the nonce possibilities within the 1 second before the timestamp could be updated.

In the early days of Bitcoin mining using CPUs hash rates were typically measured in MH/s (for example bitcoin.it reports an Intel Core i7 3930K – 6 core, 12 threads – achieving 66.6MH/s, or around 0.07GH/s) and were not close to exhausting the nonce possibilities within 1 second (it would take just over a minute). The move to GPU (Graphic Processing Units) did improve hash rates significantly, but they typically were still measured in MH/s. My trusty AMD Radeon 5850 I mined my first bitcoin on is reported to be around 430MH/s (~0.4GH/s) which from memory is about right. This would mean exhausting the 4-byte nonce would take around 10 seconds).

Then came ASICs (Application Specific Integrated Circuits). These specifically designed chips quickly pushed the GH/s barrier. Bitmain’s Antminer S1, first introduced in 2013, was advertised as achieving 180GH/s which would exhaust the 4-byte nonce in approximately 20ms. If the nonce was the only element which the miner could change, the miner would idle 98% of the time waiting for the next second to ‘tick over’.

Even my Gekkoscience Compac which is equipped with a single Bitmain BM1384 ASIC chip achieved around 11GH/s whilst only being powered by a USB port would be capable of exhausting the nonce in around 400ms.

For reference, Bitmain’s highest performance miner currently listed on their website is S19 Pro+ Hyd which is advertised as achieving 191TH/s (191000GH/s), over 1000 times quicker than the original S1, or over 17000 times quicker than my Gekkoscience Compac (although using 1750 times the power!).

To accommodate increasing hash rates beyond 4.3GH/s miners had to find other elements of the block to change to generate a different candidate hash value to compare to the target. Within the Block Header are the following six elements:

NameSize (Bytes)Description
Version4The version of block validation rules used
Previous Block Header Hash32The hash of the previous block header
Merkle Root Hash32The hash of the all transactions within the candidate block
Time4The time the of the candidate block
Nbits4The difficulty target
Nonce4A modifiable field used to achieve a hash below the difficulty target

The Previous Block Header Hash and the nBits fields cannot be modified as the former would break the connection with the previous block whilst the latter would rule the block invalid as the target wouldn’t pass validation. The remainder are, however, modifiable to varying degrees. The Time and Version fields need to meet certain acceptance criteria and therefore the full range of values is not available to the miner.

This therefore leaves limited scope to accommodate the high hash rates of modern ASIC miners and ultimately leaves the Merkle Rook Hash to be changed. This cannot be modified directly (as the hash would not match the transactions present in the block), but the transactions can be modified to ensure the hash changes. An easy method of changing the transactions, without adding extra fees, is the modification of the Coinbase transaction essentially using the unlocking script field (ScriptSig) as an extra nonce, explaining the seemingly random data observed.

SegWit Adoption

In the previous post I touched upon SegWit and how it was used to fit almost 4MB of data within the 1MB block size limit present in Bitcoin.


SegWit was introduced to the Bitcoin network in 2017 and removed (segregated) the witness data from the transactions’ inputs and moved them to a separate structure within the blockchain. This had the effect of reducing the block size requirements for transactions (and therefore the often the transaction costs) and protecting against transaction malleability.

The move also brought in a means to increase the storage potential as seen in the previous post relating to ordinals of around 3.9MB being stored on chain. This is due to a move from bytes to Work Units (wu) for calculating the size. Since the SegWit update, blocks are limited to 4 million weight units, with 1 byte in a legacy transaction being 4wu, but 1 byte in a SegWit transaction being 1wu.

Native SegWit addresses can be identified as starting bc1 on the mainnet, or tb1 for the test net. P2SH (Pay to Script Hash) addresses can also be used for SegWit transactions which start with ‘3’.

The adoption of SegWit for transactions appears pretty rapid, perhaps not unexpected given the savings made in fees by users. In 2018, approximately 1/3rd of transactions used SegWit. By 2019 this was around 43% and by 2022 approximately 87% of transactions used SegWit. Impressively, in 2023 SegWit usage appears to continue to rise with 86.5% of transactions using SegWit.


The table below summarises the quantity and percentage of SegWit transactions each year.

YearNo of
SegWit

Transactions
No of Legacy
Transaction
s
Total No of
Transactions
Percentage
SegWit
Percentage
Normal
2009032708327080100.00
201001853051853050100.00
20110190176519017650100.00
20120845305084530500100.00
2013019643241196432410100.00
2014025263720252637200100.00
2015045674023456740230100.00
2016082626623826266230100.00
201733742151006890141040632293.2496.76
201826599771547958658139563632.6867.32
2019510861026869754511978364742.6557.35
2020551791305737436811255349849.0250.98
202163919480338769739779645365.3634.64
202277594859155115199310637883.3416.66
20231444662522544831670110886.5013.50

Bitcoin Block Size

In my previous post (Bitcoin – Transactions Per Second (TPS)) I used data from Bitcoin Visuals’ website (bitcoinvisuals.com) to identify the average Bitcoin transaction size in recent years.

Since writing that post I have found that the Bitcoin RPC server responds to the ‘getblockstats‘ command. The response includes various parameters including:

  • ‘avgtxsize’ – the mean average transaction size within the requested block
  • ‘maxtxsize’ – the maximum transaction size within the requested block
  • ‘mediantxsize’ – the median average transaction size within the requested block
  • ‘mintxsize’ – the minimum transaction size within the requested block
  • ‘swtotal_size’ – the total size of all segwit transactions within the requested blockchain
  • ‘total_size’ – the total size of all non-coinbase transactions

By using this command we can easily identify the typical size of transactions on the Bitcoin network.


The (mean) average transaction size since the genesis block is around 590 bytes, and the (mean) average of the block median transaction sizes is around 265 bytes suggesting that relatively few very large transactions are responsible for skewing the mean average.

Without additional information as to each transaction’s size which, as far as I’m currently aware, would require querying each individual transaction it is difficult to identify these outliers. The ‘getblockstats‘ command does, however, expose the maximum transaction size for each block.

Earlier this year, on 1st February 2023, the Bitcoin network saw the largest ever single transaction in block ‘774628’. This transaction was a whopping 3938383 bytes (3.9MB) occupying the majority of the available block space. Some may have noticed that this exceeds the 1MB block size limit, however, this was a SegWit transaction therefore its contribution to the block size is not counted in the same manner.

Further investigation of this transaction found that it was related to Bitcoin ordinals and the reason for it being so large is the entire JFIF image is embedded within the witness data. The ordinal can be seen here.


As an aside, the transaction was large enough to crash Bitcoin Core’s ‘decoderawtransaction’ command. Fortunately, some of the online decoders (but not all!) managed to decode the transaction.


A further 2 transactions have been over 1000000 bytes, both in February 2023. These were blocks 776884 and 777945 and the large transactions were both related to ordinals(776884 and 777945).

Before 2023, the largest transaction was in 2015 when block 364292 contained a transaction just under 1MB in size. This appears to be very large due to the number of addresses (5569) all sending 1000 satoshis to a single Bitcoin address (transaction hash bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08).


Getting back on track, the table below provides a summary of the average block sizes.

Year2009201020112012201320142015
Average TX Size10.13140.13445.42476.81526.36638.45794.79
Year2017201820192020202120222023 (partial)
Average TX Size622.29808.31533.21690.68867.07853.391309.37

This would give an approximate average TPS over the past few years of between 1.95 to 3.13 (2017 to 2022) based on the mean average transaction size, and each block being the target 10 minutes.