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

MTP: Extension node is a lie! #703

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

MTP: Extension node is a lie! #703

wants to merge 1 commit into from

Conversation

chfast
Copy link
Member

@chfast chfast commented Sep 13, 2023

MPT: Remove explicit extended node kind

Notice that the formally specified extended node in
the Merkle Patricia Trie always leads to a branch node.
Therefore, we can treat branch nodes as having potentially non-empty
"extended path" and remove the explicit extended node kind.
This significantly simplifies the implementation.

@codecov
Copy link

codecov bot commented Sep 25, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.29%. Comparing base (b1f68a3) to head (2314743).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #703   +/-   ##
=======================================
  Coverage   94.29%   94.29%           
=======================================
  Files         143      143           
  Lines       16145    16110   -35     
=======================================
- Hits        15224    15191   -33     
+ Misses        921      919    -2     
Flag Coverage Δ
eof_execution_spec_tests 16.47% <100.00%> (-0.16%) ⬇️
ethereum_tests 26.76% <100.00%> (-0.15%) ⬇️
ethereum_tests_silkpre 18.52% <100.00%> (-0.16%) ⬇️
execution_spec_tests 17.55% <100.00%> (-0.17%) ⬇️
unittests 89.72% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
test/state/mpt.cpp 100.00% <100.00%> (+1.81%) ⬆️

@chfast chfast force-pushed the mpt_branch branch 2 times, most recently from fcb7222 to 75ec69a Compare December 20, 2023 10:03
@chfast chfast changed the base branch from master to mpt/path_mismatch December 20, 2023 10:08
@chfast chfast marked this pull request as ready for review December 20, 2023 11:27
@chfast chfast force-pushed the mpt/path_mismatch branch 5 times, most recently from 42f8279 to ce0bca3 Compare January 2, 2024 18:58
@chfast chfast self-assigned this Jan 5, 2024
@chfast chfast force-pushed the mpt/path_mismatch branch 2 times, most recently from b4727c9 to f6bd4c1 Compare January 5, 2024 21:30
Base automatically changed from mpt/path_mismatch to master January 5, 2024 21:56
@chfast chfast force-pushed the mpt_branch branch 2 times, most recently from 11af3f5 to 60c37ca Compare January 6, 2024 20:37
@chfast
Copy link
Member Author

chfast commented Jan 6, 2024

After extracting refactoring to #751, this is finally ready for review.

test/state/mpt.cpp Outdated Show resolved Hide resolved
test/state/mpt.cpp Outdated Show resolved Hide resolved
Comment on lines 162 to 168
const Path extended_path{m_path.begin(), this_idx_it};
const auto this_idx = *this_idx_it;
m_path = Path{this_idx_it + 1, m_path.end()}; // shorten this path, invalidates this_idx_it
*this = branch(extended_path, this_idx, std::make_unique<MPTNode>(std::move(*this)),
*insert_idx_it, leaf(insert_tail, std::move(value)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we have a method to copy node with children, and try to simplify like:

Suggested change
const Path extended_path{m_path.begin(), this_idx_it};
const auto this_idx = *this_idx_it;
m_path = Path{this_idx_it + 1, m_path.end()}; // shorten this path, invalidates this_idx_it
*this = branch(extended_path, this_idx, std::make_unique<MPTNode>(std::move(*this)),
*insert_idx_it, leaf(insert_tail, std::move(value)));
// The original node must be pushed down.
const Path common_path{m_path.begin(), this_idx_it};
const Path this_node_tail{this_idx_it + 1, m_path.end()};
auto this_node = copy_node(m_kind, this_node_tail, std::move(m_children), std::move(m_value)); // this_idx_it is not invalidated
auto new_leaf = leaf(insert_tail, std::move(value));
*this = branch(common_path, *this_idx_it, std::move(this_node), *insert_idx_it, std::move(new_leaf));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need special method. The move constructor can do this. I believe I tried this but the effect was more code similarly confusing. I'm still thinking about some refactoring here (e.g. removing named constructors leaf() and branch()) but wanted to minimize the diff this time.

Notice that the formally specified extended node in
the Merkle Patricia Trie always leads to a branch node.
Therefore, we can treat branch nodes as having potentially non-empty
"extended path" and remove the explicit extended node kind.
This significantly simplifies the implementation.
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.

2 participants