Smart Contract and Random Number

It is really awesome that Ethereum support easy-deployment of smart contract by individual developers! The language / script is using called Solidity.

However, unlike coding in ordinary environment, it is running in blockchain network, so we don't have a function simply to seed and generate numbers, as all outputs from blockchain network are deterministic.

Blockhash

This is an approach for games which are merely for fun or with a relatively small bet, as its number generation algorithm is transparent and shown in the smart contract and also its inputs, like blockhash (an unique long text for labelling a block) and block number (completely deterministic). Normally, blockhash is unpredictable but if the bet is extremely large or worthy, miners can choose to whether publish a new block to the network, so they can have some degrees of control in blockhash generation for result manipulation. Here is a simple:

contract random {
    /* Generates a random number from 0 to 100 based on the last block hash */
    function randomGen(uint seed) constant returns (uint randomNumber) {
        return(uint(sha3(block.blockhash(block.number-1), seed ))%100);
    }

    /* generates a number from 0 to 2^n based on the last n blocks */
    function multiBlockRandomGen(uint seed, uint size)
      constant returns (uint randomNumber) {
        uint n = 0;
        for (uint i = 0; i < size; i++){
            if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0)
                n += 2**i;
        }
        return n;
    }
}

Oraclize

This is a technology developed by a FinTech startup and its usages are not only limited for random number generation but also off-chain data feeding. They treat blockchain as an isolated world andOraclize is a bridge between web2 and Ethereum's world. Below figure illustrates the concept:




The concept is interesting and it has mechanism to provide proof which ensures the source data from external parties not being illegally trampled while travelling to the destination. 

However, after a series of testings, it really takes time to get a random number from external sources, like random.org, at least taking several block times, and also consuming significant amount of gas too!

In addition, the same issue persists, if the bet is very large enough and worthy, on-demand result from a centralized source can become trustless. 






留言

這個網誌中的熱門文章

Blockchain-based Provably Fair Betting

Adding Vanil tokens to your wallet