-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d04fd3f
commit 1b2a4c7
Showing
296 changed files
with
8,931 additions
and
4,834 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(|| { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.