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

refactor: Combine contract accesses and deployments into ContractUpdates #12326

Merged
merged 26 commits into from
Oct 30, 2024

Conversation

tayfunelmas
Copy link
Contributor

@tayfunelmas tayfunelmas commented Oct 28, 2024

The propagation of contract accesses (calls) and deployments are done together. Thus, introducing a wrapper called ContractUpdates that contains the accesses and deployments and this is passed through the call stack from the chunk application up to the point we send the messages.

Also address a TODO to change BTreeSet to HashSet to contain the code hashes.

We do NOT change the representation of code hashes in database and network messages, but only the internal data structures.

@tayfunelmas tayfunelmas marked this pull request as ready for review October 28, 2024 13:38
@tayfunelmas tayfunelmas requested a review from a team as a code owner October 28, 2024 13:38
Copy link

codecov bot commented Oct 28, 2024

Codecov Report

Attention: Patch coverage is 58.92857% with 23 lines in your changes missing coverage. Please review.

Project coverage is 71.26%. Comparing base (daa67a3) to head (f97d0e0).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...src/stateless_validation/state_witness_producer.rs 67.85% 4 Missing and 5 partials ⚠️
.../src/stateless_validation/contract_distribution.rs 0.00% 9 Missing ⚠️
...idation/partial_witness/partial_witness_tracker.rs 0.00% 3 Missing ⚠️
...alidation/partial_witness/partial_witness_actor.rs 0.00% 1 Missing ⚠️
runtime/runtime/src/lib.rs 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #12326       +/-   ##
===========================================
+ Coverage   38.11%   71.26%   +33.15%     
===========================================
  Files         837      838        +1     
  Lines      168711   169346      +635     
  Branches   168711   169346      +635     
===========================================
+ Hits        64298   120686    +56388     
+ Misses     100525    43414    -57111     
- Partials     3888     5246     +1358     
Flag Coverage Δ
backward-compatibility 0.16% <0.00%> (?)
db-migration 0.16% <0.00%> (?)
genesis-check 1.23% <0.00%> (?)
integration-tests 39.04% <58.92%> (+0.93%) ⬆️
linux 70.65% <55.35%> (+32.54%) ⬆️
linux-nightly 70.84% <58.92%> (?)
macos 50.39% <55.35%> (?)
pytests 1.54% <0.00%> (?)
sanity-checks 1.35% <0.00%> (?)
unittests 64.20% <55.35%> (?)
upgradability 0.21% <0.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@shreyan-gupta shreyan-gupta left a comment

Choose a reason for hiding this comment

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

Looks good!

) {
if let Some(partial_storage) = partial_storage {
let ContractUpdates { contract_accesses, contract_deploys } = contract_updates;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the interaction layer with store, verifying whether it's okay to not have contract_accesses and contract_deploys sorted?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just checked, I think we are storing vec here, so should be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not rely on sorted-ness of the hashes (eg. not keeping a hash of the list etc.) so using HashSet should be fine. We store the hashes in the DB as Vector but the storage is temporary and again does not depend on the sortedness.

Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, do you know the reason to store hashes as vectors? Is it to save space?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah two considerations, first I started using vector all over the place in the first PR and did not want to change the protocol and DB structures. Second, I thought vec would store them more compactly and we turn them into hashset when processing anyways, thus did not want to deal with a migration.

/// Code-hashes of contracts deployed while applying the previous chunk.
pub(crate) contract_deploys: BTreeSet<CodeHash>,
/// Contracts accessed and deployed while applying the chunk.
pub contract_updates: ContractUpdates,
Copy link
Contributor

Choose a reason for hiding this comment

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

pub(crate)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done (will send a commit)

@@ -422,13 +415,15 @@ impl Client {
/// Sends the contract accesses to the same chunk validators
/// (except for the chunk producers that track the same shard),
/// which will receive the state witness for the new chunk.
fn send_contract_accesses_to_chunk_validators(
fn send_contract_updates_to_validators(
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like we are still only sending contract_accesses and not contract_updates. Do we want to rename this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am planning to also send the deployments, but yeah I can rename in the later PRs. Reverted the naming change here.

Copy link
Contributor

@Trisfald Trisfald left a comment

Choose a reason for hiding this comment

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

🚀

) {
if let Some(partial_storage) = partial_storage {
let ContractUpdates { contract_accesses, contract_deploys } = contract_updates;
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, do you know the reason to store hashes as vectors? Is it to save space?

@@ -43,7 +43,7 @@ enum AccessedContractsState {
Unknown,
/// Received `ChunkContractAccesses` and sent `ContractCodeRequest`,
/// waiting for response from the chunk producer.
Requested { contract_hashes: BTreeSet<CodeHash>, requested_at: Instant },
Requested { contract_hashes: HashSet<CodeHash>, requested_at: Instant },
Copy link
Contributor

Choose a reason for hiding this comment

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

What're the pros and cons of BTreeSet vs HashSet here? Why are we using one vs the other? Would Vec also work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for set vs vector is to indicate using the type that the items are unique.

HashSet is known to be faster than BTreeSet and also since we do not need order/sortedness, thus I wanted to replace it with HashSet.

@tayfunelmas tayfunelmas added this pull request to the merge queue Oct 30, 2024
@tayfunelmas tayfunelmas removed this pull request from the merge queue due to a manual request Oct 30, 2024
@tayfunelmas tayfunelmas added this pull request to the merge queue Oct 30, 2024
Merged via the queue into near:master with commit 1e41b11 Oct 30, 2024
29 checks passed
@tayfunelmas tayfunelmas deleted the contract-dist-refactor branch October 30, 2024 09:57
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.

4 participants