Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockchain basics #71

Merged
merged 13 commits into from
Apr 9, 2024
2 changes: 1 addition & 1 deletion content/authors/nader-dabit.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"role": "Director of developer relations",
"avatarUrl": "https://res.cloudinary.com/droqoz7lg/image/upload/v1708620482/authors/auth2_yriysu.jpg",
"company": "Avara"
}
}
2 changes: 1 addition & 1 deletion content/authors/richard-gottleber.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"role": "Developer relations",
"avatarUrl": "https://res.cloudinary.com/droqoz7lg/image/upload/v1708620479/authors/auth1_rwkb2f.jpg",
"company": "Chainlink"
}
}
2 changes: 1 addition & 1 deletion content/courses/advanced-foundry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,4 @@
]
}
]
}
}
113 changes: 90 additions & 23 deletions courses/blockchain-basics/1-basics/10-blockchain-fundamentals/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,120 @@
title: High Level Blockchain Fundamentals
---

You can follow along with this section of the course here.
_Follow along with this video:_

---

### Preface

I'll start by saying, you've done great getting his far, if at first some of these concepts are hard to grasp, things will get better with experience as we move through the course and you're exposed to real world examples.

I definitely would recommend going back and reviewing the parts that you don't quite get and asking questions in the [**discussions tab**](https://github.com/Cyfrin/foundry-full-course-f23/discussions) of the GitHub repository.

Now that we know all the cryptography pieces, how the blockchain actually works, how our signatures work and how everything sticks together, let's talk a little bit about how this works in actuality and what's really going on.

It's important to note that many of the concepts we've covered and will cover are going to pertain to Ethereum, or the EVM ecosystem. Each specific blockchain however, may have their own nuances and intricies to watch out for. Trust that the overarching concepts will all be the same, but keep an eye out for the specific criteria that may very from chain to chain, how blocktime is handled, or which hashing algorithm is used for example.

### Traditional Networks vs Blockchain

Traditionally, when you run an application be it a website or something that connects to a server you are interacting with a centralized entity. This is the opposite of what you may recall from our distributed blockchain example, in that the server is controlled and run by a single centralized group.

Blockchains, as we saw, run on a network of independent nodes. In our previous example, each of the `Peers` was representative of an independent `node` operator. The term `node` typically refers to a single instance of a decentralized system, Peer A would be a `node`. This network, this combination of these nodes interacting with each other is what creates a blockchain. What makes these networks so potent, is that anybody can join. All anyone needs is a little bit a hardward and you can participate in securing a blockchain network. You could go to GitHub and start operating a node in a few seconds!

In the traditional world applications are run by centralized entities and if that entity goes down or is malicious or decides that they want to shut off - they just can. They're the ones that control everything.

Blockchains, by contrast, don't have this problem. If one node or one entity that runs several nodes goes down, since there are so many other independent nodes running, it doesn't matter, the blockchain and the system will persist so long as there is at least one node always running. Luckily for us, the most popular chains like Bitcoin and ethereum have thousands and thousands of nodes. Malicious nodes are kicked from the network, or even punished in some cases. Majority rules when it comes to the blockchain.

This gives blockchains this incredibly potent immutability trait where nothing can be changed or corrupted so in essence we can think of a blockchain as a decentralized database. In the case of Ethereum it has an extra additional feature where it also can do computation in a decentralized manner now.

### Consensus

Let's talk consensus. This includes `Proof of Work` and `Proof of Stake`. You've probably heard these terms before and they're really important to how these blockchains work.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof of work and stake are not consensus, they are part of consensus.

Bitcoin's consensus mechanism is nakamoto consensus, with PoW being the "Sybil resistance mechanism"

Consensus is made up of 2 components:

  1. Sybil resistance mechanism
  2. Chain selection rule

Nakamoto Consensus:

  1. PoW
  2. Longest chain rule

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see you fix it below. Nevermind.


The `mining` feature of our previous blockchain example was an example of `Proof of Work`

`Proof of Work` and `Proof of Stake` fall under this umbrella of `consensus` and `consensus` is a really important topic when it comes to blockchains.

> `Consensus` is defined as the mechanism used to reach an agreement on the state or a single value on the blockchain especially in a decentralized system.

## Understanding Cryptography and Blockchain
Very roughly, a consensus protocol in a blockchain or decentralized system can be broken down into two pieces a chain selection algorithm and a sybil resistance mechanism. Mining, or Proof of Work, is a sybil resistance mechanism. This is what Bitcoin currently uses.

In our previous discussions, we have covered basic concepts of cryptography and elements of blockchain. Now, let's discuss how these concepts translate into real-world applications. It is important to bear in mind that varying blockchains utilize different algorithms and criteria, so there might be minute variations in the implementation, but the core principles remain consistent.
`Proof of Work` is known as a sybil resistance mechanism because it defines a way to figure out who is the block author or which node did the work to mine a block. Sybil resistance is a blockchain's ability to defend against users creating a large number of pseudo-anonymous identities to gain a disproportionately advantageous influence over said system.

In the traditional sense, when we interact with an application or a server, such as a website, we are essentially engaging with a centralized entity. Contrarily, as we've seen, a blockchain operates within a network of independent nodes, all managed by individual users running blockchain software.
As mentioned, there are two primary types of sybil resistance:

In the realm of blockchain, the term `node` takes on a special significance, emerging as the heartbeat of the decentralized system. Imagine it this way - each `node` represents an individual user's server, pulsating with the rhythmic cadence of blockchain technology. When these nodes sync and engage with each other, they weave together an intricate and robust blockchain network. The real magic, however, lies in its democratic essence. In this decentralized universe, anyone armed with the right hardware and software can join the network, embodying the true spirit of decentralization. This is not just a technological concept; it's a silent revolution celebrating inclusivity and accessibility.
- Proof of Work
- Proof of Stake

For those eager to participate, Websites like GitHub offer the opportunity to set up your own Ethereum node in a matter of seconds!
We'll look a little closer at `Proof of Work` first.

## Blockchain: A Decentralized Powerhouse Resilient to Disruptions
### Proof of Work

The primary advantage of blockchain technology is its resilience to disruptions. Here's the reason: traditional online systems run by centralized entities are vulnerable. If they shut down due to a variety of reasons (like being hacked or due to internal issues), their services are interrupted.
Proof of work is a system of sybil resistance used in many blockchains, in its essence a miner needs to go through a very computationally heavy process (mining) to find the block's answer. As a result, it doesn't matter how many additional nodes you're running, each node is obligated to do this work in order to receive a reward. The playing field is kept fair.

On the other hand, blockchains are decentralized, and the chances of all nodes shutting down simultaneously are extremely low. So, even if one or more nodes fail, the system continues to operate unabated, as long as there is at least one functioning node. This inherent backup feature makes blockchain an incredibly resilient system. Popular chains like Bitcoin and Ethereum consist of thousands of nodes which makes them even more resistant to disruptions.
> **Note:** Some blockchains may make their riddle or their block answer intentionally hard, or intentionally easy to adjust the block time - which is the average time it takes to mine a block. Blocktime is proportional to how difficult these algorithms are.

## The Consensus Protocols: Proof of Work and Proof of Stake
Proof of Work needs to be combined with a `chain selection rule` to create `consensus`.

<img src="/blockchain-basics/09-blockchain-fundamentals/blockchain-fundamentals.jpg" style="width: 100%; height: auto;" alt="block fee">
A `chain selection rule` is implemented as a means to determine which blockchain is the _real_ blockchain. Bitcoin (and prior to the merge, Ethereum), both use something called `Nakomoto Consensus`. This is a combination of Proof of Work (Etherum has since switched to Proof of Stake) and the `longest chain rule`.

Now that we've reviewed some fundamentals, let's move on to two key concepts you may have heard about: 'Proof of Work' and 'Proof of Stake'. These concepts are crucial to understanding how blockchains work.
In the `longest chain rule`, the decentralized network decides that whichever chain has the most number of blocks will be the valid, or _real_ blockchain. When we saw `block confirmations` in Etherscan earlier, this was representing the number of blocks ahead of our transaction in the longest chain.

Proof of work and proof of stake fall under the umbrella of consensus. Consensus is a critical topic when it comes to blockchains because it is used to reach an agreement on the state or a single value on the blockchain, especially in a decentralized system.
> You'll sometimes hear people use **Proof of Work** to describe a consensus mechanism, but it's a little bit inaccurate, it's really the combination of sybil resistance _and_ chain selection that create consensus

In the majority of blockchains, the consensus protocol can be broken down into two constituent parts; a chain selection algorithm and a civil resistance mechanism. We'll touch on the main characteristics of each mechanism and then cover in more detail how Proof of Stake forms an evolved alternative to the electricity-hungry Proof of Work.
`Proof of Work` also serves as a means to determine who receives transaction fees as we discussed earlier. These transaction fees are paid by whomever initiates the transaction. In a Proof of Work system, every node is competing against eachother to solve the block problem first. The first node to solve the problem gets paid the transaction fees accumulated in the block they mine. In addition to this, miners are also paid a `block reward`, the `block reward` is given by the blockchain itself.

### Proof of Work: Deciphering the Consensus Protocol
> If you've previously heard of the Bitcoin Halving - this is the concept of the block reward being cut in half roughly every 4 years.

As already discussed, Proof of Work is a civil resistance mechanism, a way to avert potential Sybil attacks. A Sybil attack is when a user creates numerous pseudonymous identities aiming to gain a disproportionately influential sway over the system. In the Proof of Work environment, such an attack is difficult to execute. As Sybil resistance is inherent in the mechanism, irrespective of how many aliases an attacker creates, every identity must undertake the highly resource-intense process of mining to find the answer to the blockchain's puzzle.
Block rewards are in the blockchains native currency - Bitcoin = BTC, Ethereum = ETH. This effectively increases the amount of that cryptocurrency in circulation.

The Proof of Work mechanism also interacts with the consensus protocol's other key component: the chain selection rule. With this, the decentralized network decides that the longest chain - i.e., the one with the highest number of blocks - will be the authoritative chain.
### Blockchain Attacks

### Consensus and Scalability Issues
There are two major types of attacks that exist in the blockchain space.

One key compromise with Proof of Work is the substantial demands it puts on electricity, rendering it environmentally unfriendly. This has spurred the development of more eco-friendly protocols, such as Proof of Stake. This alternative consensus protocol follows a different sybil resistance mechanism: rather than expending substantial computational resources to mine blocks, in Proof of Stake, nodes or "validators" instead stake collateral as a surety they will behave honestly.
- Sybil Attack - When a user creates a number of pseudo-anonymous accounts to try to influence a network.
- 51% attack - Occurs when a single entity possesses both the longest chain and majority network control. This would allow the entity to `fork` the chain and bring the network onto the entities record of events, effectively allowing them to validate anything.

However, another significant issue requiring attention is scalability. As the number of transactions exceeds the amount of block space, latency and high transaction costs, or "gas fees", can become a hindrance.
Blockchains are very democratic. The bigger a blockchain is, the more decentralized, the more secure it becomes.

### Layer 1 and Layer 2 Scaling Solutions
I encourage you to look into running a node yourself to increase the security of the network!

Blockchain developers have devised two key options in response to this limitation:
Proof of Work does come with drawbacks. For example, Proof of Work consumes a LOT of electricity. When you have thousands of nodes all working as hard as they can to solve a block problem the energy consumption is HUGE and as such, so is the potential environmental impact.

With the above in mind, many protocols are choosing the shift to a different consensus mechanism that is more environmentally friendly. The most popular of which is...

### Proof of Stake

In contrast to trying to solve a block problem, Proof of Stake nodes put up some collateral that they are going to behave honestly aka they `stake`. If a node is found to be misbehaving, it's stake is slashed. This serves as a very effective sybil resistance mechanism because for each account, the validator needs to put up more stake and misbehaving risks losing all that collateral.

> In a Proof of Stake system, `miners` are known as `validators`. They aren't actually mining blocks, they're validating other nodes.

Unlike in Proof of Work, where each node is racing to solve the block problem first, in Proof of Stake, validators are pseudo-randomly chosen to propose the next block and other nodes will validate it.

Proof of Stake of course comes with its own Pros and Cons.

Pros:

- great sybil resistance mechanism
- great for the environment, much less energy

Cons:

- seen as less decentralized due to upfront staking costs

This raises the question of _how decentralized is decentralized enough?_ and I think I need to leave that to the community to decide.

### Layer 1 and Layer 2

I want to briefly touch on the concepts of Layer 1 and Layer 2 networks here as well.

1. `Layer 1` solutions: This refers to base layer blockchain implementations like Bitcoin or Ethereum.
2. `Layer 2` solutions: These are applications added on top of a layer one, like [Chainlink](https://chain.link/) or [Arbitrum](https://arbitrum.io/).

Options like Arbitrum, for instance, use a "roll-up" approach where transactions are processed in bulk and then rolled up into a Layer 1 blockchain. This increases the effective capacity of a Layer 1 blockchain, allowing it to absorb more transactions, effectively easing the scalability issue.
Layer 2s like Arbitrum and Optimism are special in that they're trying to solve the problem of scalability. These protocols leverage something called `rollups`. We won't go too deep, but the idea is that the protocols bundle their transactions to be processed by a Layer 1.

### Wrap Up

This overview was huge. Amazing work, you now have a fundamental understanding of how blockchains work, how to interact with them and why they're so secure and empowering.

Lets bring it all home in the final part!
39 changes: 7 additions & 32 deletions courses/blockchain-basics/1-basics/11-basics-completed/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,15 @@
title: Congratulations!
---

If you reached this section you are awesome!! You can follow along with this section of the course here.
_Follow along with this video:_

---

You've learnt so much and should be incredibly proud you've come this far. Give yourself a pat on the back and celebrate this victory.

## Demystifying Blockchain: From Basics to Code

As we wrap up our series on blockchain basics and elaborate further with insightful blockchain explainers, we not only aim to offer a seamless transition into blockchain-related fields but also make it enjoyable and interesting. With the knowledge you've garnered, you are now equipped to explore the world of blockchains more competently and confidently.

By marching this far, you should not only be proud of your accomplishments but also be excited about the journey ahead. It is indeed commendable that you took upon yourself to understand the complexities of blockchain technology.

<img src="/blockchain-basics/Final-Congrats/quote.png" style="width: 100%; height: auto;" alt="block fee">

## Venturing into The Coding Aspect

Now that you've grasped a lot of the basics and fundamental concepts of blockchain, it's time to delve into the coding aspect. This is where the more practical aspects of blockchain technology come into focus. The transition from theoretical insights to practical applications can be a thrilling journey, especially when we're talking about something as ground-breaking as blockchain.

### Learning Solidity Basics

Solidity is a statically-typed programming language designed for implementing smart contracts on Ethereum-based blockchain platforms. To put it simply, if blockchain was a car, Solidity would be the engine that drives it, or more specifically, the language in which the engine's instructions are written.

Our next section will introduce you to the solidity fundamentals. This will equip you with the necessary skills to start coding in Solidity and offer an in-depth understanding of how smart contracts work under the hood.

At this juncture, it is appropriate to revisit your achievements. After all, learning is a continual process and every milestone deserves a celebration.

#### Give yourself a virtual high-five for your fantastic progress. If you've found our content helpful, we'd love to hear more about your journey. You can reach out to us in the GitHub discussions!

The switch from learning blockchain concepts to actually applying them might be stark, yet it's exhilarating. It signals the progression from rudimentary understanding, to applying that knowledge, and finally, creating something new and valuable. And let's face it, in today's tech-savvy world, knowing how to code is a power in itself.

So congratulations are in order! By seeking to learn Solidity and understanding how to interact with blockchains, you’re standing at the gateway of endless potential. Get ready to unlock new opportunities in the world of technology, as you step into the exciting realm of blockchain coding. Remember, every code you write brings us one step closer to a decentralized world. Happy coding!

Your next Chapter is to learn the:
Your next steps are going to be an opportunity to jump right into the code and learn the basics of Solidity. The next sections will give you all the tools you need to start building on the blockchain and developing your own protocols and applications.

<a href="/solidity/remix/1-simple-storage" style="color: blue; text-decoration: underline;">
<button style="background-color: white; color: #f4899c; border: 2px solid white; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; transition: border 0.3s ease; border-radius: 5px;" onmouseover="this.style.borderColor='#f4899c';" onmouseout="this.style.borderColor='white';">Solidity Basics</button>
</a>
Blockchain Basics NFT Challenge 👀

<!-- TODO: make this a big button a general style -->
[Blockchain Basics (Arb)](https://arbiscan.io/address/0x2e99A4CA9c85383ccB54769adB3837437e296479)
[Blockchain Basics (Sepolia)](https://sepolia.etherscan.io/address/0x25056312685339b49e1d1C5a0b72Ff9eff13AF77)
Loading