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

New implementation for pre-verified hashes #606

Merged
merged 4 commits into from
Mar 16, 2022
Merged

Conversation

mriccobene
Copy link
Member

This PR changes the implementation of pre-verified hashes to allow fast compilation.

Comment on lines 57 to 58
private:
static std::map<uint64_t, PreverifiedHashes> per_chain; // chain-id based access to the instances
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't get the point of this. Why a map is needed ?
Afaik there is no way we can use preverified hashes from two (or more) different chains

Copy link
Contributor

Choose a reason for hiding this comment

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

On a wider perspective I don't get the point of the whole PreverifiedHashes struct.
Why not simply a pair<BlockNum,std::setevmc::bytes32> as const member of WorkingChain which is the only object using it ? Basically preverified hashes should be treated pretty much as ConsensusEngine: once is loaded it stays the same for all the duration of Silkworm instance.
Unless there are specific reasons I'd get rid of this struct

Copy link
Member Author

@mriccobene mriccobene Mar 8, 2022

Choose a reason for hiding this comment

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

The PreverifiedHashes struct has 2 fields as std::pair<>, yes, but the first makes the code clearer:

  • preverified_hashes_->height is more clear than preverified_hashes_.first
  • preverified_hashes_->contains(...) is more clear than preverified_hashes_.second.find(...) ...

Also it is open to other needs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Regarding the map, I managed to remove it maintaining the possibility to use or not pre-verified hashes (e.g. with a command line switch).

struct PreverifiedHashes {
std::set<evmc::bytes32> hashes; // Set of hashes of headers that are known to belong to canonical chain
uint64_t height{0}; // Block height corresponding to the highest preverified header
uint64_t height{0}; // Block height corresponding to the highest pre-verified header

[[nodiscard]] bool contains(const evmc::bytes32& hash) const { return hashes.find(hash) != hashes.end(); }

static PreverifiedHashes none; // A void set of hashes that can be used to turn-off pre-verified hashes usage and
Copy link
Contributor

Choose a reason for hiding this comment

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

Why define a static empty set of itself ?

Copy link
Member Author

@mriccobene mriccobene Mar 8, 2022

Choose a reason for hiding this comment

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

The WorkingChain must be able to run without pre-verified hashes. So by default it uses PreverifiedHashes::none that is a clear way to state this, IMO

Comment on lines 46 to 57
const PreverifiedHashes& PreverifiedHashes::load(uint64_t chain_id) {
PreverifiedHashes& result = per_chain[chain_id]; // insert if not present

if (chain_id == 1 && result.height == 0) {
load_preverified_hashes(result,
preverified_hashes_mainnet_data,
sizeof_preverified_hashes_mainnet_data,
preverified_hashes_mainnet_height);
}

return result;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not make much sense to me:

  • result.height == 0 seem to me a guard for chain_id presence in per_chain map and a not-loaded flag
  • Hashes should be loaded at struct construction and never reloaded again imho

Copy link
Member Author

Choose a reason for hiding this comment

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

The actual code is flexible enough to let us to decide in the future to load or not pre-verified hashes using a command line switch. Also the code make tests simple.

The code in the function is very simple IMO anyway I propose you to focus on the toolbox to find a way to insert the chain_id in the generated files so that here the code becomes generic respect to the chain.

Copy link
Member Author

Choose a reason for hiding this comment

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

This was also simplified in the last commit.

@codecov
Copy link

codecov bot commented Mar 8, 2022

Codecov Report

Merging #606 (a7c11a3) into master (7daf5b8) will decrease coverage by 0.00%.
The diff coverage is 87.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #606      +/-   ##
==========================================
- Coverage   81.58%   81.57%   -0.01%     
==========================================
  Files         169      169              
  Lines       13768    13768              
==========================================
- Hits        11232    11231       -1     
- Misses       2536     2537       +1     
Impacted Files Coverage Δ
...lkworm/downloader/internals/preverified_hashes.hpp 100.00% <ø> (ø)
...de/silkworm/downloader/internals/working_chain.cpp 66.15% <87.50%> (ø)
core/silkworm/state/in_memory_state.cpp 93.65% <0.00%> (-1.47%) ⬇️
core/silkworm/crypto/sha-256.c 51.24% <0.00%> (-0.50%) ⬇️
core/silkworm/execution/evm.cpp 96.90% <0.00%> (+0.82%) ⬆️

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 7daf5b8...a7c11a3. Read the comment docs.

@mriccobene mriccobene merged commit 446d1e3 into master Mar 16, 2022
@mriccobene mriccobene deleted the preverified-hashes-2 branch March 16, 2022 17:05
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.

3 participants