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 Dec 18, 2024
1 parent d04fd3f commit 1b2a4c7
Show file tree
Hide file tree
Showing 296 changed files with 8,931 additions and 4,834 deletions.
2 changes: 1 addition & 1 deletion src/0/source/changes.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c537f0b
index 00000000..c537f0b6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
Expand Down
22 changes: 10 additions & 12 deletions src/1/source/changes.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..31cbe7d
index 00000000..31cbe7df
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,3624 @@
Expand Down Expand Up @@ -3630,7 +3630,7 @@ index 0000000..31cbe7d
+]
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..293d4f2
index 00000000..293d4f2a
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,26 @@
Expand Down Expand Up @@ -3662,7 +3662,7 @@ index 0000000..293d4f2
+try-runtime = []
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..cf9aa25
index 00000000..cf9aa251
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,24 @@
Expand Down Expand Up @@ -3692,7 +3692,7 @@ index 0000000..cf9aa25
+wrap_comments = true
diff --git a/src/impls.rs b/src/impls.rs
new file mode 100644
index 0000000..03abf99
index 00000000..03abf99e
--- /dev/null
+++ b/src/impls.rs
@@ -0,0 +1,9 @@
Expand All @@ -3707,7 +3707,7 @@ index 0000000..03abf99
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..ae8a09b
index 00000000..ae8a09bc
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,38 @@
Expand Down Expand Up @@ -3751,10 +3751,10 @@ index 0000000..ae8a09b
+}
diff --git a/src/tests.rs b/src/tests.rs
new file mode 100644
index 0000000..6d271d5
index 00000000..7e6f34da
--- /dev/null
+++ b/src/tests.rs
@@ -0,0 +1,117 @@
@@ -0,0 +1,115 @@
+// Tests for the Kitties Pallet.
+//
+// Normally this file would be split into two parts: `mock.rs` and `tests.rs`.
Expand Down Expand Up @@ -3806,15 +3806,15 @@ index 0000000..6d271d5
+
+ /// System: Mandatory system pallet that should always be included in a FRAME runtime.
+ #[runtime::pallet_index(0)]
+ pub type System = frame_system::Pallet<Runtime>;
+ pub type System = frame_system::Pallet<TestRuntime>;
+
+ /// PalletBalances: Manages your blockchain's native currency. (i.e. DOT on Polkadot)
+ #[runtime::pallet_index(1)]
+ pub type PalletBalances = pallet_balances::Pallet<Runtime>;
+ pub type PalletBalances = pallet_balances::Pallet<TestRuntime>;
+
+ /// PalletKitties: The pallet you are building in this tutorial!
+ #[runtime::pallet_index(2)]
+ pub type PalletKitties = pallet_kitties::Pallet<Runtime>;
+ pub type PalletKitties = pallet_kitties::Pallet<TestRuntime>;
+}
+
+// Normally `System` would have many more configurations, but you can see that we use some macro
Expand Down Expand Up @@ -3865,8 +3865,6 @@ index 0000000..6d271d5
+fn system_and_balances_work() {
+ // This test will just sanity check that we can access `System` and `PalletBalances`.
+ new_test_ext().execute_with(|| {
+ // We often need to set `System` to block 1 so that we can see events.
+ System::set_block_number(1);
+ // We often need to add some balance to a user to test features which needs tokens.
+ assert_ok!(PalletBalances::mint_into(&ALICE, 100));
+ assert_ok!(PalletBalances::mint_into(&BOB, 100));
Expand Down
8 changes: 3 additions & 5 deletions src/1/source/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ mod runtime {

/// System: Mandatory system pallet that should always be included in a FRAME runtime.
#[runtime::pallet_index(0)]
pub type System = frame_system::Pallet<Runtime>;
pub type System = frame_system::Pallet<TestRuntime>;

/// PalletBalances: Manages your blockchain's native currency. (i.e. DOT on Polkadot)
#[runtime::pallet_index(1)]
pub type PalletBalances = pallet_balances::Pallet<Runtime>;
pub type PalletBalances = pallet_balances::Pallet<TestRuntime>;

/// PalletKitties: The pallet you are building in this tutorial!
#[runtime::pallet_index(2)]
pub type PalletKitties = pallet_kitties::Pallet<Runtime>;
pub type PalletKitties = pallet_kitties::Pallet<TestRuntime>;
}

// Normally `System` would have many more configurations, but you can see that we use some macro
Expand Down Expand Up @@ -108,8 +108,6 @@ fn starting_template_is_sane() {
fn system_and_balances_work() {
// This test will just sanity check that we can access `System` and `PalletBalances`.
new_test_ext().execute_with(|| {
// We often need to set `System` to block 1 so that we can see events.
System::set_block_number(1);
// We often need to add some balance to a user to test features which needs tokens.
assert_ok!(PalletBalances::mint_into(&ALICE, 100));
assert_ok!(PalletBalances::mint_into(&BOB, 100));
Expand Down
34 changes: 1 addition & 33 deletions src/10/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@

<div class="content-row">
<div class="content-col">
<div class="content-section">

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

</div>
<div class="content-col">

<div class="tab">
<button class="maintab tablinks active" onclick="switchMainTab(event, 'Source')">Source</button>
<button class="maintab tablinks" onclick="switchMainTab(event, 'Diff')">Diff</button>
</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, 'changes.diff')" data-id="changes.diff">changes.diff</button>
</div>
<div id="changes.diff" class="difftab tabcontent active" data-id="changes.diff">

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

</div>

</div>

</div>
</div>
65 changes: 3 additions & 62 deletions src/10/source/README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,5 @@
# Blockchain Storage
# Storage Basics

Blockchains use a Merkle Trie structure to store data. The Merkle Trie provides two important properties for blockchains:
Now that we have covered the basics of Pallets and gone through all of the template code, we can start writing some code ourselves.

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^256 different possible outputs. This is 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.
In this section you will learn the basics of creating and using storage in your Pallet, including creating and using storage values and storage maps.
48 changes: 48 additions & 0 deletions src/10/source/changes.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
diff --git a/src/lib.rs b/src/lib.rs
index a52896ed..ae8a09bc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,7 +18,6 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

- /* 🚧 TODO 🚧: Learn about Pallet Events. */
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
diff --git a/src/tests.rs b/src/tests.rs
index 434ce723..59889339 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -29,7 +29,6 @@ type Block = frame_system::mocking::MockBlock<TestRuntime>;
const ALICE: u64 = 1;
const BOB: u64 = 2;

-/* 🚧 TODO 🚧: Learn about constructing a runtime. */
#[runtime]
mod runtime {
#[runtime::derive(
@@ -61,7 +60,6 @@ mod runtime {
pub type PalletKitties = pallet_kitties::Pallet<TestRuntime>;
}

-/* 🚧 TODO 🚧: Learn about configuring a pallet. */
// Normally `System` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
@@ -84,7 +82,6 @@ impl pallet_kitties::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
}

-/* 🚧 TODO 🚧: Learn about test externalities. */
// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
// It simulates the blockchain database backend for our tests.
// If you forget to include this and try to access your Pallet storage, you will get an error like:
@@ -127,7 +124,6 @@ fn create_kitty_checks_signed() {
})
}

-/* 🚧 TODO 🚧: Learn about writing tests. */
#[test]
fn create_kitty_emits_event() {
new_test_ext().execute_with(|| {
8 changes: 3 additions & 5 deletions src/10/source/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ mod runtime {

/// System: Mandatory system pallet that should always be included in a FRAME runtime.
#[runtime::pallet_index(0)]
pub type System = frame_system::Pallet<Runtime>;
pub type System = frame_system::Pallet<TestRuntime>;

/// PalletBalances: Manages your blockchain's native currency. (i.e. DOT on Polkadot)
#[runtime::pallet_index(1)]
pub type PalletBalances = pallet_balances::Pallet<Runtime>;
pub type PalletBalances = pallet_balances::Pallet<TestRuntime>;

/// PalletKitties: The pallet you are building in this tutorial!
#[runtime::pallet_index(2)]
pub type PalletKitties = pallet_kitties::Pallet<Runtime>;
pub type PalletKitties = pallet_kitties::Pallet<TestRuntime>;
}

// Normally `System` would have many more configurations, but you can see that we use some macro
Expand Down Expand Up @@ -108,8 +108,6 @@ fn starting_template_is_sane() {
fn system_and_balances_work() {
// This test will just sanity check that we can access `System` and `PalletBalances`.
new_test_ext().execute_with(|| {
// We often need to set `System` to block 1 so that we can see events.
System::set_block_number(1);
// We often need to add some balance to a user to test features which needs tokens.
assert_ok!(PalletBalances::mint_into(&ALICE, 100));
assert_ok!(PalletBalances::mint_into(&BOB, 100));
Expand Down
61 changes: 7 additions & 54 deletions src/11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +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>
<button class="subtab tablinks file-solution file-modified" onclick="switchSubTab(event, 'src/tests.rs')" data-id="src/tests.rs">src/tests.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="solution/src/tests.rs" class="subtab tabcontent" data-id="src/tests.rs">

```rust
{{#include ./solution/src/tests.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
Loading

0 comments on commit 1b2a4c7

Please sign in to comment.