Access Blockchain Data from Bitcoin Smart Contracts: Part 4

Get Transaction Count in a Block

4 min readNov 17, 2021


The total number of transactions in a block is an important piece of information. We show how to obtain it without a trusted third party, which is considered impossible previously.


Let us denote the length of a Merkle path as n. The number of leaves, i.e., number of transactions in a block, is between 2^(n-1) + 1 and 2^n. To see this, a perfect binary tree¹ of height n has exactly 2^n leaves. The length of a Merkle path is the same as the height of the Merkle tree.

A Perfect Tree of Height 2

Exact Number

Find the Last Transaction

In the last post, we accessed the first coinbase transaction in a block. If we could also access the last transaction, we can deduce the total number of transactions. We can identify the first/leftmost transaction by verifying all the nodes on its Merkle path are on the right branch. It is tempting to find the last/rightmost transaction similarly by requiring all Merkle path nodes to be on the left branch. Unfortunately, this may not be always true.

If we have a perfect Merkle tree, all nodes on the Merkle path of the last transaction are indeed on the left branch, as shown below.

Colored Merkle Path for the Last Transaction

However, this is not the case when the tree is not perfect. For instance, the following tree has 5 leaves and the Merkle path for the last transaction consists of all nodes colored orange. Two of them lie on the right branch, only the top one on the left.

Merkle Path for the First and Last Transaction Colored in Red and Orange, Respectively

To overcome this issue, we notice that when there is an odd number of nodes in any single layer of the Merkle tree, the last node is duplicated. That implies, if any node on the Merkle path of the last transaction is on the right branch, it must be copied from the current left branch, as the graph below shows.

The following code implements the algorithm directly.

Blockchain Contract

From Merkle Path to Transaction Index

To derive a transaction’s index, we follow its Merkle path from root to the leaf representing it. When a node on the path is on the left branch, we go right (i.e., binary 1); otherwise, we go left (binary 0). It is easy to see that we can get a binary representation of its index this way. In the example below, we apply this rule to the last transaction, and we go all the way to the right and get 111 in binary, which is exactly its index 7 in decimal.

A Tree with All Leaves Indexed in Binary

This code is shown below:

Blockchain Contract

Get the Exact Transaction Count

Finally we can combine the two functions into blockTxCount(), which returns the exact number of transactions in a block.

Blockchain Contract


Once we can access the number of transactions in a block, we can use it to build smart contracts deemed impossible before. We list only a few examples below:

  • Place a bounty contract that only pays out when a block contains over 1 million transactions, to sponsor a stress test.
  • Combining with timing information obtained before, either timestamp in a block header or block height, transactions per second (TPS) can be calculated faithfully and used inside a contract. For example, a contract that only unlocks if TPS reaches 100,000.

We look forward to seeing what kinds of exciting new contracts you can come up with.


This article is inspired by shilch’s work.

[1] A perfect binary tree is a tree in which every internal node has exactly two child nodes and all the leaf nodes are at the same level.




sCrypt ( is a company with a mission to provide integrated on-chain smart contracting solutions on Bitcoin Satoshi Vision