Blockchain technologies solved 2 problems:

1) immutability: data can't be altered

2) Distributed Ledger Technology: each node has a copy of the ledger, information is decentralized in order to avoid 'a single point of failure' (SPOF). Each node works together based on a consensus mechanism preventing malicious actors.

Immutability

In a blockchain all transactions are stored in a block.

Each block includes the cryptographic hash of the prior block in the chain, linking the two blocks, and also a cryptographic hash of all the transactions stored in the block.

The cryptographic hash, is a hash generated by a cryptographic hash function.

The cryptographic hash function is a one way function (non invertible function) that maps the content of a block to a number. Due to cryptography there is no way to derive (or reverse engineer) the original digital content from the hash, thus making it one way.

These 2 numbers, the cryptographic hash of the previous block and the cryptographic hash of all the confirmed transactions in the block, are stored in the block header of the new incoming block.

Mining

Miners solve puzzles with known partial inputs derived from the latest blockchain. Miners are given INPUT, for example 'starts with 11110000' and an OUTPUT TARGET that ends with '000000000'. If miners can solve the INPUT variable then they receive a financial incentive. Mining is expensive therefore miners need to have incentives.

  1. Download entire blockchain
  2. Verify incoming transactions and run smart contract code invoked by transactions
  3. Create a block
  4. Find a valid nonce
  5. Broadcast your block
  6. Profit

Nonce Randomised set of digits/numbers that used to verifiy that data has only been submitted once. As an authentication tool it makes sure a block has only been submitted once into the blockchain once.

Merkle Trees

Let's say we have four transactions. We run a hash function that allows us to create 4 unique hashes. The unique hashes are then run through another hash function which prodcues 2 new hashes based upon the twinning of the previous 4 hashes. We then do this again, running the two pairs through another hash function that results in 1 single hash, forming what is known as the root hash and forming a complete root tree. Merkle trees allow for any changes within a block to be verified by comparing the current state of the block to the original version.

Proof of Work (PoW) PoW was created in the 90s to combat spam but was not used extensively until 2009 to secure the blockchain. With PoW, miners compete against each other to complete transactions on the network and add new blocks to the blockchain. As mining popularity has grown so have mining costs which has led to vast amounts of electricity being used in 'mining farms'. Groups have formed to create mining pools; an aggregated method to share the cost of minining and the financial incentive. This leads to centralised behaviour rather than decentralised behaviour.

Proof of Stake (PoS) In PoS nodes are chosen randomly to validate the next block. It is not completely random as validators must put down a stake in order to complete the blockchain validity. In order to disincentivise fradulent behaviour, a validator's stake must be greater than the transaction fees. This leads to decentralised behaviour and reduces the risk of 51% attack. The more money validators put down as stake, the more likely they are to be chosen.

Asymmetric cryptography

Symmetric cryptography is like me wanting to send a document to a friend but my friend needs a password to open it. How do I share this password without someone seeing my details.

Assymetric cryptography is like having public mailbox. You put in your sensitive document, and only 1 person has a private key to open it. Two users, Bob and Alice, they both have private and public keys which are mathematically linked. Public keys can be used to encrypt and only private keys can be used to open the file. Bob and Alice exchange public keys, Alice enrcypts her document with Bob's public key, now Bob can open it with his private key. Not even Alice can open her document.

Ethereum Virtual Machine (EVM) BitCoin transformed the world, showing us we could use the blockchain for monetary transactions. Ethereum looked at the same technology and asked, what else, above and beyong cryptocurrency could we do with blockchain technology. EVM is an immutable, distrubuted ledger, Turing Complete*, running smart contracts. Ether is used to fund computation, reward miners, align incentives.

Gas and Fees Code is written and deployed all over the world, and users must pay to have code run on the platform. Different operations have different prices. For example to run a contract is 10 gas. People must bid how much money per gas/unit to have the contract run. Highest bidder wins. As developers we always want to think how much will it cost to run our code. The idea of gas is to avoid attacks on the network. We can not look at a piece of code and know if it will be in an infiite loop (halting problem) so we use gas. If run out of gas code doesnt execute, if paid too much gas, then gas refunded. Every opcode requires gas. You are paying miners for smart contrats on a distributed network that will give you a trustworthy output. Every transactions specifies

startgas - maxiumum quanity of gas it is willing to consume

gasprice- fee in ether it is willing to pay per unit of gas.

*Turing Complete refers to a machine that, given enough time and memory along with the necessary instructions, can solve any computational problem, no matter how complex. The term is normally used to describe modern programming languages as most of them are Turing Complete (C++, Python, JavaScript, etc.).