Getting your Trinity Audio player ready...
|
This post was first published on Medium.
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 flowchart](https://coingeek.com/wp-content/uploads/2021/12/Access-blockchain-data-from-Bitcoin-smart-contracts-Part-2-image-1.png)
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.
![a screenshot of isBlockHeaderChainValid code](https://coingeek.com/wp-content/uploads/2021/12/Access-blockchain-data-from-Bitcoin-smart-contracts-Part-2-image-2.png)
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¹.
![a screenshot of BlockTimeBet code](https://coingeek.com/wp-content/uploads/2021/12/Access-blockchain-data-from-Bitcoin-smart-contracts-Part-2-image-3.png)
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.
Watch: CoinGeek New York panel, Blockchain: The Future of Technology Building on Achievements of the Past
New to blockchain? Check out CoinGeek’s Blockchain for Beginners section, the ultimate resource guide to learn more about blockchain technology.