BSV
$80.55
Vol 84.56m
-4.61%
BTC
$98545
Vol 159435.25m
-4.2%
BCH
$596.98
Vol 1193.22m
-2.48%
LTC
$134.72
Vol 2720.6m
3.53%
DOGE
$0.43
Vol 11034.45m
-0.23%
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

Palo Alto Networks breach: Blockchain key to cybersecurity resilience
Following the breach reports, Palo Alto Networks revealed that its Next Generation Firewalls had been targeted in cyberattacks exploiting two...
December 5, 2024
Swiss council opposes capital Bern’s motion to study BTC mining
Legislators in Bern passed a motion to assess how BTC block reward mining can repurpose excess energy, but the governing...
December 5, 2024
Advertisement
Advertisement
Advertisement