current position:Home>Accessing blockchain data from scrypt smart contract (2)

Accessing blockchain data from scrypt smart contract (2)

2022-02-02 10:49:01 freedomhero

stay Last article in , We introduce a method of accessing blocks and transactions with minimal trust .
In this paper , We extend it to include multiple blocks . Use the technology , We developed a simple betting contract based on block time . We also show another way to prevent forged blocks .

 Insert picture description here

One by 3 A sequence of blocks

A sequence of blocks

As shown below ,isBlockHeaderValid() Validate a block sequence , Instead of the single block we did in the last article . We are the first 8 Line reuses functions in existing code isBlockHeaderValid() To verify each individual block . Besides , We are the first 12 The row hashes the block header , And make sure that the hash is the same as 14 In the next block header of the line prevBlockHash Field matching .

// is a chain of block headers valid
static function isBlockHeaderChainValid(static const int N, BlockHeader[N] headers, int blockchainTarget) : bool {
    bool res = true;

    loop (N) : i {
        if (res) {
            // each block is valid
            if (!isBlockHeaderValid(headers[i], blockchainTarget))
                res = false;

            if (i > 0) {
                Sha256 prevBlockHash = blockHeaderHash(headers[i - 1]);
                // blocks are chained
                if (prevBlockHash != headers[i].prevBlockHash)
                    res = false; 

    return res;
Blockchain Source code

in other words , Verify that the two blocks are linked together .

A case study : Bet on the block time

On average, , Generating a block on bitcoin requires 10 minute .Alice and Bob Want to bet on how long it will take to dig a particular block . Each of them has locked in some... In a transaction that includes the following smart contracts BSV. After the transaction broadcast , Package the process into a block . If the generation time of the block is less than 10 minute , be Alice Win and take all locked BSV; otherwise , Bob won . We use the timestamp of the block ( The... In the header 4 A field ) The difference between the timestamp of its previous block is taken as the out of block time ¹.

// bet on block time: how long it takes to mine the block containing the bet transaction
contract BlockTimeBet {
    // only 2 is needed; 7 means the transaction has 6 confirmations
    static const int N = 7;
    // 10 minutes in seconds
    static const int AVG_BLOCK_TIME = 600;
    // maximal target for any block to be considered valid
    int blockchainTarget;

    PubKey alice;
    PubKey bob;

    // header[1] is the block containing the contract tx
    public function main(BlockHeader[N] headers, MerkleProof merkleproof, Sig sig, SigHashPreimage txPreimage) {

        // get id of previous tx
        Sha256 prevTxid = Sha256(SigHash.outpoint(txPreimage)[:32]);

        // validate a chain of block headers
        require(Blockchain.isBlockHeaderChainValid(N, headers, this.blockchainTarget));

        // verify previous tx is in block with index 1
        require(Blockchain.txInBlock(prevTxid, headers[1], merkleproof));

        // block time is the time difference between this block and last
        int blockTime = headers[1].time - headers[0].time;

        // Alice wins if block is mined within 10 mins; otherwise Bob wins
        PubKey winner = blockTime < AVG_BLOCK_TIME ? this.alice : this.bob;
        require(checkSig(sig, winner));
BlockTimeBet Contract source code

Similar to the previous article BlockchainPRNG The contract is the same , We use OP_PUSH_TX technology To get the information of the transaction containing the betting contract txid . The first 20 Line to verify whether the block header chain is legal , The first 23 The bank verifies that the betting transaction is in it . The first 26 The block time is calculated from the row , Used in 29 OK, determine the winner .

Identify false blocks

In the last article , We introduced blockchainTarget Parameters to control the difficulty of acceptable block headers . We can also ask to build multiple blocks on top of one block , This makes it more difficult to import fake block data . The more blocks required , The higher the cost of counterfeiting it . This is similar to what you need to buy goods with bitcoin 6 Second confirmation . In the betting contract above , We just need to put N Change it to 7 This ensures that the transaction containing the contract has 6 Confirmations .


[1]: Bitcoin block timestamp is not accurate , Usually cannot be used to measure 10 A minute interval . But as long as it's random and unpredictable , Just bet on the contract , Because it depends on the randomness of block time , Not its accuracy .

copyright notice
author[freedomhero],Please bring the original link to reprint, thank you.

Random recommended