2

As per my current understanding PoW algorithm is to ensure that only the decided number of blocks are made in a time frame and we adjust difficulty accordingly. I'm a beginner in Solidity and Ethereum and i want to understand each line of this code, please help me to analyze it

bytes32 public currentChallenge;                         // The coin starts with a challenge
uint public timeOfLastProof;                             // Variable to keep track of when rewards were given

Line 1: currentChallenge is a variable of type byte32 ( sequence of 32 bytes, 256 bits in total )

Line 2:uint is a datatype for currency amount and dates ( in unix time )

  uint public difficulty = 10**32;                         // Difficulty starts reasonably low

Line 3: What does 10**32 does ? I have an idea that it is related to number of bits but not crystal clear about what it does

    function proofOfWork(uint nonce){
    bytes8 n = bytes8(sha3(nonce, currentChallenge));    // Generate a random hash based on input
    if (n < bytes8(difficulty)) throw;                   // Check if it's under the difficulty

Line 4: proofOfWork accepts nonce ( any random number ) as input, which I'll be manually entering in my Ethereum Wallet.

Line 5: We create a random hash by concatenating nonce and currentChallenge and store it in n

Line 6: If the computation by current nonce is not able to match the difficulty then do not proceed

        uint timeSinceLastProof = (now - timeOfLastProof);  // Calculate time since last reward was given
    if (timeSinceLastProof <  5 seconds) throw;         // Rewards cannot be given too quickly
    balanceOf[msg.sender] += timeSinceLastProof / 60 seconds;  // The reward to the winner grows by the minute

Line 7 and Line 8 : Can we compare time just by adding unit(seconds) just like in Line 8:, how does it work ?

difficulty = difficulty * 10 minutes / timeSinceLastProof + 1;  // Adjusts the difficulty

Line 9: This is based on unitary method, if blocks are being made in more than 10 minutes then lower the difficulty , else increase it. Again same doubt as in Line 3 what's happening on multiplying ( * ) , why are we adding 1 ?

    timeOfLastProof = now;                              // Reset the counter
    currentChallenge = sha3(nonce, currentChallenge, block.blockhash(block.number-1));  // Save a hash that will be used as the next proof

Line 10 and Line 11 : Update timeOfLastProof and currentChallenge. Getting inside the parameter 3 in sha3 block.blockhash(block.number-1), I have asked this in another thread too , and i got to know that:

  1. blockhash(block.number-1) gives blockhash ( a random number) of the previous block, and we are taking the previous block specifically to maintain the chain. Is this right ?
  2. block.blockhash(block.number-1) - Is there a structure with the name block and blockhash one of its data members ?
  • Line 2. 10**32 is 10 to the power of 32, commonly written as 10^32, or 100000000000000000000000000000000 (32 zeroes). – The Officious BokkyPooBah Jul 20 '16 at 11:40
3

The main purpose of any Proof of ... system is to pick a random node to confirm that nobody cheated in the last block(s). And it does so in a decentralized way, i.e. no central random generator.

Line 3: What does 10**32 does ?

It is 10 to the power of 32. Your hashcode must be lesser than this number to win the lottery and be the miner of the block.

Line 7 and Line 8 : Can we compare time just by adding unit(seconds) just like in Line 8:, how does it work ?

They are just constants, see it as multiplications: http://solidity.readthedocs.io/en/latest/units-and-global-variables.html?

10 * minutes is only a way to say 10 * 60 so 10 minutes in seconds

Line 9: This is based on unitary method, if blocks are being made in more than 10 minutes then lower the difficulty , else increase it. Again same doubt as in Line 3 what's happening on multiplying ( * ) , why are we adding 1 ?

timeSinceLastProof then guarantees that the more time passes the smaller the difficulty. +1 to ensure no division by zero is possible

block.blockhash(block.number-1) - Is there a structure with the name block and blockhash one of its data members ?

Correct: http://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=blockhash#block-and-transaction-properties

blockhash(block.number-1) gives blockhash ( a random number) of the previous block, and we are taking the previous block specifically to maintain the chain. Is this right ?

No this is because current block number is the actual block and does not have a hash yet, it is not mined... so you have to take the last one.

  • added further response – Roland Kofler Jul 20 '16 at 11:50
  • Thanks a lot, your input is really helpful. One more thing in the first line of function where we are creating a random hash, any reason for using bytes8 ? – Ayush Aggarwal Jul 20 '16 at 11:58
  • Good question can't see why ATM, maybe you make a question out of it? – Roland Kofler Jul 20 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.