Skip to content

Commit

Permalink
mdBook generated from gitorial
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Aug 7, 2024
1 parent 0c5201b commit 7182b99
Show file tree
Hide file tree
Showing 228 changed files with 7,231 additions and 3,399 deletions.
52 changes: 7 additions & 45 deletions src/10/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,32 @@
<div class="content-row">
<div class="content-col">

{{#include ./template/README.md}}
{{#include ./source/README.md}}

</div>

<div class="content-col">

<div class="tab">
<button class="maintab tablinks active" onclick="switchMainTab(event, 'Template')">Template</button>
<button class="maintab tablinks" onclick="switchMainTab(event, 'Solution')">Solution</button>
<button class="maintab tablinks active" onclick="switchMainTab(event, 'Source')">Source</button>
<button class="maintab tablinks" onclick="switchMainTab(event, 'Diff')">Diff</button>
</div>

<div id="Template" class="maintab tabcontent active">

<div class="tab">
<button class="subtab tablinks file-template file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="template/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./template/src/lib.rs}}
```

</div>



</div>

<div id="Solution" class="maintab tabcontent">

<div class="tab">
<button class="subtab tablinks file-solution file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="solution/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./solution/src/lib.rs}}
```

</div>

<div id="Source" class="maintab tabcontent active">

No files edited in this step.

</div>

<div id="Diff" class="maintab tabcontent">


<div class="tab">
<button class="difftab tablinks active" onclick="switchDiff(event, 'template.diff')" data-id="template.diff">template.diff</button>
<button class="difftab tablinks" onclick="switchDiff(event, 'solution.diff')" data-id="solution.diff">solution.diff</button>
</div>
<div id="template.diff" class="difftab tabcontent active" data-id="template.diff">

```diff
{{#include ./template/template.diff}}
```

<button class="difftab tablinks active" onclick="switchDiff(event, 'changes.diff')" data-id="changes.diff">changes.diff</button>
</div>
<div id="solution.diff" class="difftab tabcontent" data-id="solution.diff">
<div id="changes.diff" class="difftab tabcontent active" data-id="changes.diff">

```diff
{{#include ./solution/solution.diff}}
{{#include ./source/changes.diff}}
```

</div>
Expand Down
18 changes: 0 additions & 18 deletions src/10/solution/solution.diff

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions src/10/source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Blockchain Storage

Blockchains use a Merkle Trie structure to store data. The Merkle Trie provides two important properties for blockchains:

1. Allows the whole database to be represented by a single fingerprint, which can easily be compared to other nodes.
2. Allows the creation of lightweight proofs, proving that specific data exists in the database.

This comes at the cost of additional complexity reading and writing data to the blockchain.

Let's learn about Merkle Tries in more detail.

## Hash Functions

Hash functions are an important tool throughout blockchain development.

A hash function takes an arbitrary sized input and returns a fixed-size string of bytes.

This output, usually called a hash, is unique to each unique input. Even a small change to the input creates a dramatic change to the output.

Hash functions have several key properties:

- Deterministic: The same input always produces the same output.
- Pre-image Resistant: It is difficult to derive the original input from its hash value.
- Collision Resistant: It’s hard to find two different inputs that produce the same hash output.

These properties make hash functions key for ensuring data integrity and uniqueness in blockchain technology.

## Hash Fingerprint

Due to the properties of a Hash, it is often referred to as a fingerprint.

For context, a 32-byte hash has 2^32 different possible outputs. This nearly as many atoms as there are in the whole universe!

This uniqueness property helps blockchain nodes come to consensus with one another.

Rather than needing to compare all the data in their blockchain database with one another, they can simply share the hash of that database, and know in a single small comparison if all data in that database is the same.

Remember, if there were any small differences between their databases, even just one bit in a multi-terabyte database being different, the resulting hash would dramatically change, and they would know their databases are not the same.

## Merkle Trie

A merkle trie is a data structure which is constructed using a hash function.

Rather than hashing the whole database into a single hash, we create a tree of hashes.

For example, we take pairs of data, combine them, then hash them to generate a new output. Then we take pairs of hashes, combine them, then hash them to generate another new output.

We can repeat this process until we are left with a single hash called the "root hash". This process literally creates a tree of hashes.

Just like before, we can use a single hash to represent the integrity of all data underneath it, but now we can efficiently represent specific pieces of data in the database using the path down the trie to that data.

It is called a merkle "trie" because the trie data structure is used to reduce the amount of redundant data stored in the tree.

### Complexity

The reason we go into this much detail about merkle tries is that they increase the complexity in reading and writing to the blockchain database.

Whereas reading and writing to a database could be considered `O(1)`, a merklized database has read and write complexity of `O(log N)`, where `N` is the total number of items stored in the database.

This additional complexity means that designing storage for a blockchain is an extremely important and sensitive operation.

The primary advantage of using a merkle trie is that proving specific data exists inside the database is much more efficient! Whereas you would normally need to share the whole database to prove that some data exists, with a merklized database, you only need to share `O(log N)` amount of data. This is very important to support light clients.

In this next section, and throughout the tutorial, we will start to explore some of those decisions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions src/10/solution/src/lib.rs → src/10/source/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::storage]
pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down
28 changes: 0 additions & 28 deletions src/10/template/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions src/10/template/src/impls.rs

This file was deleted.

43 changes: 0 additions & 43 deletions src/10/template/src/lib.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/10/template/template.diff

This file was deleted.

12 changes: 6 additions & 6 deletions src/11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
<div id="Template" class="maintab tabcontent active">

<div class="tab">
<button class="subtab tablinks file-template file-modified active" onclick="switchSubTab(event, 'src/impls.rs')" data-id="src/impls.rs">src/impls.rs</button>
<button class="subtab tablinks file-template file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="template/src/impls.rs" class="subtab tabcontent active" data-id="src/impls.rs">
<div id="template/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./template/src/impls.rs}}
{{#include ./template/src/lib.rs}}
```

</div>
Expand All @@ -34,12 +34,12 @@
<div id="Solution" class="maintab tabcontent">

<div class="tab">
<button class="subtab tablinks file-solution file-modified active" onclick="switchSubTab(event, 'src/impls.rs')" data-id="src/impls.rs">src/impls.rs</button>
<button class="subtab tablinks file-solution file-modified active" onclick="switchSubTab(event, 'src/lib.rs')" data-id="src/lib.rs">src/lib.rs</button>
</div>
<div id="solution/src/impls.rs" class="subtab tabcontent active" data-id="src/impls.rs">
<div id="solution/src/lib.rs" class="subtab tabcontent active" data-id="src/lib.rs">

```rust
{{#include ./solution/src/impls.rs}}
{{#include ./solution/src/lib.rs}}
```

</div>
Expand Down
36 changes: 17 additions & 19 deletions src/11/solution/solution.diff
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
diff --git a/src/impls.rs b/src/impls.rs
index b396f98..9739330 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -3,12 +3,9 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
- /* 🚧 TODO 🚧:
- - `get` the `current_count` of kitties.
- - `unwrap_or` set the count to `0`.
- - Create `new_count` by adding one to the `current_count`.
- - `set` the `new_count` of kitties.
- */
+ let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
+ let new_count = current_count + 1;
+ CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
diff --git a/src/lib.rs b/src/lib.rs
index ef50270..ebaf109 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -17,11 +17,8 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

- /* 🚧 TODO 🚧:
- - Create a new `StorageValue` named `CountForKitties`.
- - `CountForKitties` should be generic over `<T: Config>`.
- - Set `Value` to `u32` to store that type.
- */
+ #[pallet::storage]
+ pub(super) type CountForKitties<T: Config> = StorageValue<Value = u32>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
3 changes: 0 additions & 3 deletions src/11/solution/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use frame::prelude::*;

impl<T: Config> Pallet<T> {
pub fn mint(owner: T::AccountId) -> DispatchResult {
let current_count: u32 = CountForKitties::<T>::get().unwrap_or(0);
let new_count = current_count + 1;
CountForKitties::<T>::set(Some(new_count));
Self::deposit_event(Event::<T>::Created { owner });
Ok(())
}
Expand Down
Loading

0 comments on commit 7182b99

Please sign in to comment.