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:
- 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 ?
- block.blockhash(block.number-1) - Is there a structure with the name block and blockhash one of its data members ?