BSV
$68.02
Vol 58.5m
-0.77%
BTC
$96793
Vol 44283.72m
-0.88%
BCH
$508.01
Vol 946.74m
1.41%
LTC
$96.6
Vol 1373.21m
-0.97%
DOGE
$0.42
Vol 14351.68m
0.01%
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
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.

a screenshot of isBlockHeaderChainValid code
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¹.

a screenshot of BlockTimeBet code
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.

Watch: CoinGeek New York panel, Blockchain: The Future of Technology Building on Achievements of the Past

Recommended for you

David Case gets technical with Bitcoin masterclass coding sessions
Whether you're a coding pro or a novice, David Case's livestream sessions on the X platform are not to be...
November 21, 2024
NY Supreme Court’s ruling saves BTC miner Greenidge from closing
However, the judge also ruled that Greenidge must reapply for the permit and that the Department of Environmental Conservation has...
November 20, 2024
Advertisement
Advertisement
Advertisement