Exploring the Blockchain

Joe Buza

What is a Blockchain?

Distributed database that is used to maintain a list of records, called blocks, which are linked and secured using cryptography.

What is a block?

ID

Previous Hash

Timestamp

Data

Nonce

Which block is it.  Genesis block #0

When the block was added

Hash of the previous block

Information stored in the block

Number of iterations to find a valid block

Genesis Block

How is a block mined?

  1. Look at the latest block on the blockchain

  2. Get the Index and increment by 1

  3. Get the latest block's hash and store as previous hash

  4. Create data

  5. Calculate Hash

  6. Calculate Nonce

Demo

How is hash calculated?




  SHA256(index + previousHash + timestamp + data + nonce)


Proof of work

  • Not just any hash value is valid.

  • A valid hash has to pass a requirement, called Difficulty.

Difficulty

  • This is the minimum requirement for a valid hash.

  • A valid hash has at least four leading 0’s. (demo)




function isValidHashDifficulty(hash, difficulty) {
  for (var i = 0, b = hash.length; i < b; i ++) {
      if (hash[i] !== '0') {
          break;
      }
  }
  return i >= difficulty;
}

What is a nonce?

  • Number of iterations used to find a valid hash

  • This process is called mining



let nonce = 0;
let hash;
let input;

while(!isValidHashDifficulty(hash)) {     
  nonce = nonce + 1;
  input = index + previousHash + timestamp + data + nonce;
  hash = CryptoJS.SHA256(input)
}

What's the point?

  • Mining and POW keep the blockchain immutable.

  • This is what makes the blockchain hack proof.

Resources

Questions?