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

Implement MPT #1235

Merged
merged 13 commits into from
Jul 30, 2020
Merged

Implement MPT #1235

merged 13 commits into from
Jul 30, 2020

Conversation

fyrchik
Copy link
Contributor

@fyrchik fyrchik commented Jul 27, 2020

Close #851 .
P2P/Consensus/RPC are not ported.

The only difference is that storage keys and values now are serialized in a straight-forward way.

@codecov
Copy link

codecov bot commented Jul 27, 2020

Codecov Report

Merging #1235 into master will increase coverage by 0.76%.
The diff coverage is 85.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1235      +/-   ##
==========================================
+ Coverage   66.74%   67.50%   +0.76%     
==========================================
  Files         200      210      +10     
  Lines       17159    17900     +741     
==========================================
+ Hits        11452    12083     +631     
- Misses       5092     5173      +81     
- Partials      615      644      +29     
Impacted Files Coverage Δ
pkg/core/prometheus.go 85.71% <0.00%> (-14.29%) ⬇️
pkg/core/storage/store.go 42.30% <ø> (ø)
pkg/core/blockchain.go 67.52% <49.41%> (-2.00%) ⬇️
pkg/core/dao/dao.go 70.91% <67.27%> (-1.25%) ⬇️
pkg/core/state/mpt_root.go 77.77% <77.77%> (ø)
pkg/core/mpt/hash.go 80.48% <80.48%> (ø)
pkg/core/mpt/branch.go 88.57% <88.57%> (ø)
pkg/core/mpt/base.go 93.10% <93.10%> (ø)
pkg/core/mpt/trie.go 93.69% <93.69%> (ø)
pkg/core/mpt/leaf.go 96.29% <96.29%> (ø)
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9adb3a0...e305a65. Read the comment docs.

config/protocol.unit_testnet.yml Outdated Show resolved Hide resolved
fyrchik and others added 13 commits July 30, 2020 12:42
MPT is a trie with a branching factor = 16, i.e. it consists of sequences in
16-element alphabet.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Because there is no distinct type field in JSONized nodes, distinction
is made via payload itself, thus all unmarshaling is done via
NodeObject.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Because trie size is rather big, it can't be stored in memory.
Thus some form of caching should also be implemented. To avoid
marshaling/unmarshaling of items which are close to root and are used
very frequenly we can save them across the persists.
This commit implements pruning items at the specified depth,
replacing them by hash nodes.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
It drastically reduces the number of allocations and hash calculations.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
It's just a waste of time.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
It should always be invalidated if something changes in the `next` (below the
extension node).

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This was differing from C# notion of PrevHash. It's not a previous root, but
rather a hash of the previous serialized MPTRoot structure (that is to be
signed by CNs).

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
We need to compact our in-memory MPT from time to time, otherwise it quickly
fills up all available memory. This raises two obvious quesions --- when to do
that and to what level do that.

As for 'when', I think it's quite easy to use our regular persistence interval
as an anchor (and it also frees up some memory), but we can't do that in the
persistence routine itself because of synchronization issues (adding some
synchronization primitives would add some cost that I'd also like to avoid),
so do it indirectly by comparing persisted and current height in `storeBlock`.

Choosing proper level is another problem, but if we're to roughly estimate one
full branch node to use 1K of memory (usually it's way less than that) then we
can easily store 1K of these nodes and that gives us a depth of 10 for our
trie.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
@fyrchik fyrchik merged commit f435604 into master Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement MPT
3 participants