A blockchain is a record of transactions. Everytime anyone sends a transaction to another person, the sender, receiver, and what was sent are all recorded. Eventually, these transactions get grouped together and are put inside of a block, which is then added to the blockchain. New blocks of transactions are continuously added over and over again. These blocks are chained together through cryptography, so that once a block is added to the blockchain, anyone can access any block and look up any transaction that has ever occurred on that blockchain, but none of the transactions in the block can be edited or removed.
A cryptocurrency is a digital currency that is paid to a person (called a miner) as a reward for adding blocks of transactions to the blockchain. Each blockchain has their own cryptocurrency for this purpose. There is also another type of cryptocurrency called an ERC20 token, which is discussed in detail in the smart contract section.
Every block has a unique number to identify it, called a hash value. This hash value is produced by something called a hash function, which you can think of as a giant blender that takes in any data and spits out a number (hash value). The miner takes in all the transaction data in the current block that he wants to add to the blockchain, and takes the previous block's (the last one that was updated to the blockchain) hash value, puts all of these in the blender (hash function), and gets a new hash value for the block he wants to add. (The very first block in a blockchain, called the genesis block, obviously won't have a previous block's hash value, so a value of 0 is usually given as a subsitutue). If someone changes a transaction in one of the previous blocks, the hash value for that block will also change, which will then change the hash value for the next block in the blockchain since it's hash value was dependent upon the now changed previous block's hash value. The hash values for the block after that will also change for the same reason, and so on. This makes it easy to detect that the blockchain has been tampered with, and it will be deemed invalid by the other nodes running the blockchain (nodes are explained below).
There are two types of blockchains: public and private. Private blockchains are run by an individual, corporation, or any other central authority. Public blockchains have no central authority. Anyone in the world can download an entire copy of a public blockchain and then start verifying if new blocks that miners are adding contain valid transactions. People who do this are called full nodes. Since there is no central authority in charge of these nodes, the blockchain is considered to be decentralized.
There are two main types of nodes: full nodes and miner nodes. Full nodes download a copy of the entire blockchain and validate all new transactions by verifying that they comply with the rules of the blockchain, which ensures the integrity of the network and protects it from malicious actors. Miner nodes put transactions in blocks and add them to the blockchain. They are rewarded with cryptocurrency for their work.
If I have made a transaction, but a miner hasn't added it to a block yet, is it still on the blockchain?No, your transaction is in whats called the memory pool. The memory pool is a limbo area where transactions wait until a miner puts them into a block and adds that block to the blockchain. In order to speed up this process, you can offer a fee to any miner that adds your transaction to a block. This fee is called a transaction fee. The higher the transaction fee you choose to pay, the faster your transaction will leave the mempool and be put into a block and added to the blockchain. Depending on the blockchain, if you decide to pay too little of a transaction fee, your transaction may never be added to a block by a miner.
How do miners tell other nodes that they have added a new block? Miners initially connect to nodes that already have many connections, called seed nodes. From there, the miner will try to connect with all the nodes that the seed node is connected to, and then all the nodes that those new nodes are connected to, and so on until the miner has connected to enough nodes to be able to participate in the network. Once the miner has connected to enough nodes, they can quickly communicate with those nodes when he has added a block to the blockchain, and wait for those nodes to verify that no mistakes have been made. If the nodes agree that the new block is valid, they will communicate it with all the nodes they are connected to, until the network has accepted the new block. The process then continues for the next block to be added.
What if two different miners try to add blocks with different transactions in them? How does the network know which updated blockchain to use? In order to determine which updated blockchain to use, blockchains use the longest chain rule, meaning that the chain with the most blocks added to it is the correct one. If two miners have added different blocks at roughly the same time, then these chains compete with more blocks added on by different miners until one chain has significantly more blocks added than the other. When this happens, the smaller one will stopp being used and the larger one will be recognized as the correct chain. Because of this, it is important for users to wait until more than a few blocks have been added to confirm that a transaction has gone through, otherwise it runs the risk of being added to a chain that will eventually be abandoned and running into the double spending problem.
The double spending problem occurs when two transactions take place, but neither of them have been added to the blockchain. Imagine you have 1 cryptocurrency and pay someone with it. After a bit of time a miner adds it to the blockchain. However, at the same time another miner has added a block to the blockchain with transactions that are different, and don't include yours. If this other miner's blockchain has more blocks added to it than the blockchain where your transaction was processed, it will become the longest chain, and the smaller blockchain will be abandoned. Since your transaction was never added onto the longest chain (just a shorter abandoned one), your transaction is technically still in the memory pool, so you still have control of that 1 cryptocurrency until a miner adds a block to the longest chain with your transaction inside it. If you quickly spend that cryptocurrency on something else, and if this transaction is updated to the longest chain before your first transaction, then when a miner tries to add your first transaction it will become invalid because your second transaction was added to the chain at an earlier time (Remember it doesn't matter which transaction occurred first, only which transaction is uploaded onto the longest chain first). Therefore you should always wait until several additional blocks have been added after your transaction has been confirmed to greatly increase the probability that the chain where your transaction was added will also be the longest chain.
What if some miners want to use a chain that isn't the longest chain? Although rare, this does happen and is called a hard fork. One of the chains is given a new name and considered an entirely new blockchain. The most famous example of this occurred in 2016 on the Ethereum network. The Ethereum chain split into two: Ethereum and Ethereum Classic.
What does a miner have to do in order to be allowed to add a block to the blockchain? As explained above, every miner is taking a certain amount of transaction from the memory pool into his new block as well as the previous blocks hash value into a blender (hash function), which spits out a number (a new hash value). Every blockchain has slightly different rules, but in general, a miner can only add a block if this hash value is a number smaller than some arbitrarily chosen number. When a combination of transactions and the previous blocks hash value are put into the hash function, the new hash value they produce will almost certanly be larger than the networks chosen cutoff value, so the miner is looking for an additional number, called a number once (nonce), that when added to the hash function, will produce a hash value that is small enough for the network to accept it.
To recap, the miner puts the previous blocks hash value, nonce, and a combination of transactions from the memory pool into a hash function that produces a hash value. If this hash value is too big, the miner tries again, using a different nonce. Eventually, the miner will find a nonce that creates a hash value small enough to be accepted by the network. This process of finding a specific nonce that when added to the hash function will produce a sufficiently small enough hash value is very time consuming, expensive, and requires a lot of computing power. It is known as proof of work.
The main reason for requiring miners to have a hash value smaller than some arbitrarily chosen number is to make it expensive, computationally and energy wise, to try to find the nonce that gets the correct hash value. Every second a miner is spending trying to find the nonce, they are losing money due to computational, power, and other costs. Because of this, for large blockchains like Bitcoin, it will be nearly impossible for any single person or entity to have control of more than 50% of the blockchain network (It would cost 10s or 100s of billions of dollars to achieve this). If any person or group of people were to have more than 50% of the networks computing power, the integrity of the blockchain would be compromised, so Proof of Work is extremely important to prevent this from happening.
Proof of Work (PoW) has two major issues. The first is energy consumption: PoW requires a significant amount of computational power, which in turn consumes a lot of energy. This has led to concerns about the environmental impact of PoW-based systems, as well as the cost of running the network.
The second is scalability: As the number of users on the network increases, so does the amount of computational power required to maintain the network. This can lead to scalability issues, as the network may become too slow to process transactions in a timely manner.
In order to address these issues, some blockchains like Ethereum have moved to Proof of Stake instead of Proof of Work. Proof of Stake works in the following way: The network selects a node (often referred to as a "validator") to add the next block to the blockchain. This validator must be willing to "stake" some of their cryptocurrency which will be taken away if they are found to have tampered with the blockchain, or tried to add invalid transactions. The more cryptocurrency a node stakes, the higher the chance that they will be selected to add the next block. When a node is selected to add a block, it must validate that the transactions in that block are valid, and then broadcast the block to the rest of the network. The other nodes in the network then double check that the transactions in the new block are valid, and, if it is valid, add it to the blockchain. The validator is then rewarded with cryptocurrency.
In order to send and receive transactions from a blockchain, you first need a private key which is just a random number between 0 and 2^256. From this private key, you can generate a public key. When someone wants to send you cryptocurrency, they lock the cryptocurrency using your public key and their private key (called their digital signature). Once they have locked the cryptocurrency to your public key, only your private key will be able to unlock it. Now with your private key you can lock it to someone elses public key in the same way and then only they will be able to unlock it, and so on. Please note that even though you sign the transaction with your private key, your private key will not be visible to anyone, and will be completely safe. The way that nodes will be able to verify that you signed the transaction without actually seeing your private key itself is through special cryptography and your public key, but they won't be able to derive your private key from from your public key.
An address is created by taking your public key and running it through a hash function. Although you could transfer your cryptocurrency to someone with just their public key, as explained above, there is a small risk that in the future, someone will discover a way to derive a private key from someone's public key by breaking something called elliptic curve cryptography (impossible as of 2023, and very unlikely to be the case). Just to be extra safe however, people wanted an extra layer of security, so they took their public key, ran it through a hash function, and made an address which, like public keys, can be used to transfer cryptocurrencies. In fact, the most common way to transfer cryptocurrencies on almost every blockchain is by using an address instead of a public key. If you never send cryptocurrency, but only receive it through an address, your public key will never actually be exposed. The public key is revealed only when you send cryptocurrency, because it is necessary to prove that the digital signature came from your private key, but if all you do is receive money then your public key will never be revealed and you'll have an extra layer of security to make sure your private key is safe via hashing even if elliptic curve cryptography is ever broken.
A wallet is where you can store multiple addresses (explained above). Think of it like a keychain that has as many addresses as you decide to create with it. Of course, it holds the public and private keys linked to these addresses as well.