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

Conceptual: Substrate Storage #255

Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
368c7bc
Update storage.md
shawntabrizi Sep 24, 2019
ac6d78b
skeleton of storage doc
shawntabrizi Sep 24, 2019
09fb5dd
typo
shawntabrizi Sep 24, 2019
7b8b959
Update storage.md
shawntabrizi Sep 24, 2019
97e85f5
Integrate feedback
shawntabrizi Sep 26, 2019
402bba0
fixes
shawntabrizi Sep 27, 2019
89c720c
clarify kind of node
shawntabrizi Sep 27, 2019
d77a40b
Clarify
shawntabrizi Sep 27, 2019
999d39f
typo, fix line width
shawntabrizi Sep 27, 2019
68a8592
fixes
shawntabrizi Sep 27, 2019
5bbead7
replace misleading data
shawntabrizi Sep 27, 2019
ec323ac
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
5813b88
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
d6d7292
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
1c5993e
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
ab8caad
update from feedback
shawntabrizi Sep 28, 2019
e3ceff2
Merge branch 'shawntabrizi-storage-doc' of https://github.com/shawnta…
shawntabrizi Sep 28, 2019
f4570ee
Add a section on why to use child tries
shawntabrizi Sep 28, 2019
9db4f54
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
23f725b
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
59f529a
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
07026a3
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
9240c48
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
2cc0216
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
9cb7da5
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
df67541
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
96fee72
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
330d8a1
Update docs/conceptual/core/storage.md
shawntabrizi Sep 28, 2019
72550f5
Final fixes
shawntabrizi Sep 28, 2019
2018486
add todo
shawntabrizi Sep 28, 2019
0e33445
Merge branch 'source' into shawntabrizi-storage-doc
shawntabrizi Sep 29, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/conceptual/core/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ One advantage of using a simple key-value store is that you are able to easily a

Substrate uses a Base-16 Modified Merkle Patricia tree ("trie") from [`paritytech/trie`](https://github.com/paritytech/trie) to provide a trie structure whose contents can be modified and whose root hash is recalculated efficiently.

### Main Trie
Tries are important tool for blockchains because the allows for efficient storing and sharing of historical of block state. You don't have a state trie per block, but a trie hash that will point to the nodes from previous block states. However, accessing data to trie data is costly. Each read operation takes O(log N) time, where N is the number of elements stored in the trie. To mitigate this, we use a key value cache.
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved

Substrate has a single main trie whose storage root hash is placed in the block header. This is used to easily verify the state of the blockchain and provide a basis for light clients to verify proofs.
All trie node are stored in RocksDB and part of the trie state can get pruned, i.e. a key-value pair can be deleted from the storage when it is out of pruning range for non archive nodes. Trie nodes are also encoded to allow for any key type, while still be able to avoid key collision. We do not use [reference counting](http://en.wikipedia.org/wiki/Reference_counting) for performance reasons.
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved

### State Trie

Substrate has a single main trie, called the state trie, whose changing root hash is placed in each block header. This is used to easily verify the state of the blockchain and provide a basis for light clients to verify proofs.

This trie only stores content for the canonical chain, not forks. There is a separate `state_db` layer that maintain the trie state with references counted in memory for all that is non canonical. See JournalDB.

### Child Trie

Substrate also provides an API to generate new child tries with their own root hash that can be used in the runtime.

The root hash of these child tries are automatically stored in the main trie to maintain easy verification of the complete node state.
Child tries are identical to main state trie, except their root is stored and updated in the main trie instead of the block header. Since their headers are a part of the main state trie, it is still easy to verify of the complete node state when it includes child tries.

## Runtime Storage API

Expand Down