Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions docs/conceptual/core/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,94 @@
title: Storage
---

* Key Value Database, RocksDB
Substrate uses a simple key-value data store implemented as a database-backed,
modified Merkle tree.

* Trie abstraction
## Key-Value Database

* Runtime Storage abstraction
Substrate implements its storage database with [RocksDB](https://rocksdb.org/),
a persistent key-value store for fast storage environments.

This is used for all the components of Substrate that require persistent
storage, such as:

- Substrate clients
- Substrate light-clients
- Off-chain workers

## Trie Abstraction

One advantage of using a simple key-value store is that you are able to easily
abstract storage structures on top of it.

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.

Tries allow efficient storing and sharing of the historical block state. The
trie root is a representation of the data within the trie; that is, two tries
with different data will always have different roots. Thus, two blockchain nodes
can easily verify that they have the same state by simply comparing their trie
roots.

Accessing 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.

All trie nodes 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. We do not use [reference
counting](http://en.wikipedia.org/wiki/Reference_counting) for performance
reasons.

### State Trie

Substrate based chains have a single main trie, called the state trie, whose
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](https://substrate.dev/rustdocs/master/substrate_state_db/index.html) that
maintains the trie state with references counted in memory for all that is
non-canonical.

### Child Trie

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

Child tries are identical to the main state trie, except that a child trie's
root is stored and updated as a node 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 the complete node state when it includes child tries.

Child tries are useful when you want your own independent trie with a separate
root hash that you can use to verify the specific content in that trie.
Subsections of a trie do not have a root-hash-like representation that satisfy
these needs automatically; thus a child trie is used instead.

## Runtime Storage API

The Substrate's [Support
module](https://substrate.dev/rustdocs/master/srml_support/index.html) provides
utilities to generate unique, deterministic keys for your runtime module storage
items. These storage items are placed in the state trie and are accessible by
querying the trie by key.

## Next Steps

### Learn More

- Learn how to add [storage items](development/module/storage.md) into your
Substrate runtime modules.

### Examples

- View an example of creating [child tries](TODO) in your Substrate runtime module.

### References

- Visit the reference docs for
[`paritytech/trie`](https://substrate.dev/rustdocs/master/trie_db/trait.Trie.html).
2 changes: 1 addition & 1 deletion docs/development/module/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TODO

### Examples

TODO
* View this example to see how you can use a `double_map` to act as a `killable` single-map.

### References

Expand Down