Access Blockchain Data from Bitcoin Smart Contracts: Part 2

In the last post, we introduced a way to access a block and transactions within it with minimal trust. We extend it to include a chain of blocks in this article. Using the technique, we develop a simple betting contract based on block time. We also show another way to deter faking blocks.

A Chain of 3 Blocks

A Chain of Blocks

As shown below, isBlockHeaderValid() validates a chain of blocks, instead of a single block as we did in the last post. We reuse function isBlockHeaderValid() from existing code to validate each individual block, at Line 8. In addition, we hash a block header at Line 12 and ensure the hash matches the prevBlockHash field in the next block header at Line 14. That is, these two blocks are chained together.

Blockchain Contract

A Case Study: Betting on Block Time

On average, it take 10 minutes to mine a block on Bitcoin. Alice and Bob want to bet on how long it takes to mine a specific block. They each lock up some bitcoins in the following smart contract contained in a transaction. After the transaction is broadcasted, it will be mined into a block. If the block takes less than 10 minutes to mine, Alice wins and takes all the coins locked up; otherwise, Bob wins. We use the difference between the block’s timestamp (the 4th field in the header) and the timestamp of its preceding block as the block time¹.

BlockTimeBet Contract

As in the BlockchainPRNG contract from last post, we use the OP_PUSH_TX technique to get the txid of the transaction containing the betting contract. Line 20 verifies the chain of block headers is legitimate and Line 23 verifies the betting transaction is in it. Line 26 calculates the block time, which is used to determine the winner at Line 29.

Deter Fake Blocks

In the last post, we introduced the blockchainTarget parameter to control the difficulty of an acceptable block header. We can also require multiple blocks built on top of a block, to make it even more difficult to import a fake block outside of the Bitcoin public blockchain. The more blocks piled on the block, the more expensive to fake it. This is similar to the typical 6 confirmations needed when using Bitcoin to purchase goods. In the betting contract above, we only need to change N to 7 to ensure the transaction containing the contract has 6 confirmations.

static const int N = 7;

[1] Bitcoin block timestamping is not accurate and usually cannot be used to measure time interval of 10 minutes. But as long as it is random and hard to predict, the betting contract is fine since it relies on the randomness of block time, not its precision.




sCrypt Inc ( is a company with a mission to provide integrated on-chain smart contracting solutions using the original BitCoin Protocol on BSV

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium


Arken Finance — Contributor Grants Program

Binance Listed MIR in the Innovation Zone

How Infrastructure As A Service (IaaS) Meets Blockchain


Blockchain technology set to hold citizens Identity?


Our Latest Investment Partner — Huobi Ventures

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


sCrypt Inc ( is a company with a mission to provide integrated on-chain smart contracting solutions using the original BitCoin Protocol on BSV

More from Medium

Cattle IoT and Blockchain in Botswana

Bitcoin Transactions

Welcoming Efficient Frontier’s new Head of DeFi Engineering, Roman Kagan

A Permanent Web of Linked Data: Arweave