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

Fix DB serialization order for Ord types #4619

Merged
merged 18 commits into from
Jan 10, 2024
Merged

Fix DB serialization order for Ord types #4619

merged 18 commits into from
Jan 10, 2024

Conversation

Leo-Besancon
Copy link
Collaborator

@Leo-Besancon Leo-Besancon commented Jan 9, 2024

An error prone behaviour in our code is that when serializing numbers (or slots) for the keys of the DB, we use varint serializer. As a result, the ordering of such numbers is not garantied in the DB's ordering (for instance when the number is around a power of two).

This is e.g. related to the following issue and associated PR: #4101 and #4104

In this PR, we will replace every instance of such serialization (and deserialization).

TODO:

  • List every DB key and check whether it is impacted
  • Fix serialization of the keys in the DB
  • Fix deserialiazation of the keys in the DB (also in the is_db_key_value_valid) check function!)

Potentially impacted files list. For each file, list the potentially impacted keys:

Impacted keys:

  • Critical:
    • All the keys that are prefixed by a cycle (pos_final_state.rs)
    • Deferred credits key (contains a slot - pos_final_state.rs)
  • Non critical instances that we won't change in this PR (to avoid too many unnecessary changes that could introduce other problems):
    • In the AsyncPool, the keys in the DB are AsyncMessageId that are Ord and partly encoded as Varint. We do not (currently) use this Ord, but may in the future).
    • Executed Denunciations are indexed by DenunciationIndexed that are Ord and partly encoded as Varint.. We do not (currently) use this Ord, but may in the future)
    • MIP Info related keys, but we don't iter on them in the DB, we query all of them at start.

OTHER FACTORS

  • make sure network ban times are 1 day
  • update version to MAIN.2.0
  • update package versions
  • add validated Genesis files
    • wait for validation
    • update files
    • update readme

@damip
Copy link
Member

damip commented Jan 9, 2024

For slots that are used in a db key: use Slot::to_bytes_key() and Slot::from_bytes_key()

For integers, use to_be_bytes() and from_be_bytes()

@AurelienFT
Copy link
Contributor

My part is done.

@damip
Copy link
Member

damip commented Jan 9, 2024

My independent analysis (I went through pos state):

https://github.com/massalabs/massa/pull/4620/files

@Eitu33
Copy link
Contributor

Eitu33 commented Jan 9, 2024

final state and ledger db done

@Leo-Besancon
Copy link
Collaborator Author

I checked every change made in https://github.com/massalabs/massa/pull/4620/files , this PR seems to take into account the same ser/deser.

AurelienFT
AurelienFT previously approved these changes Jan 9, 2024
Copy link

codecov bot commented Jan 9, 2024

Codecov Report

Attention: 30 lines in your changes are missing coverage. Please review.

Comparison is base (5697687) 71.78% compared to head (d491cac) 71.78%.

❗ Current head d491cac differs from pull request most recent head 9505422. Consider uploading reports for the commit 9505422 to get more accurate results

Files Patch % Lines
massa-pos-exports/src/pos_final_state.rs 80.99% 23 Missing ⚠️
massa-execution-worker/src/execution.rs 69.23% 4 Missing ⚠️
...ssa-execution-worker/src/speculative_roll_state.rs 77.77% 2 Missing ⚠️
massa-node/src/main.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4619   +/-   ##
=======================================
  Coverage   71.78%   71.78%           
=======================================
  Files         291      291           
  Lines       51642    51642           
=======================================
  Hits        37069    37069           
  Misses      14573    14573           

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

@damip
Copy link
Member

damip commented Jan 9, 2024

All those variables on slices are stressing me out a bit.
Here is a less error-prone approach:

fn buf_to_array_ctr<F: Fn(&[u8; N]) -> V, V, const N: usize>(
    buf: &[u8],
    ctr: F,
) -> Option<(&[u8], V)> {
    Some((&buf[N..], ctr(&buf.get(..N)?.try_into().ok()?)))
}

Then you can do:

let (rest_key, slot) =
    buf_to_array_ctr(rest_key, Slot::from_bytes_key).expect("error");

let (rest_key, cycle) =
    buf_to_array_ctr(rest_key, |v| u64::from_be_bytes(*v)).expect("error");

AurelienFT
AurelienFT previously approved these changes Jan 10, 2024
@Leo-Besancon Leo-Besancon dismissed stale reviews from AurelienFT and sydhds via 0f7919f January 10, 2024 09:13
AurelienFT
AurelienFT previously approved these changes Jan 10, 2024
bilboquet
bilboquet previously approved these changes Jan 10, 2024
sydhds
sydhds previously approved these changes Jan 10, 2024
damip
damip previously approved these changes Jan 10, 2024
@damip
Copy link
Member

damip commented Jan 10, 2024

Only the genesis files update remains. Waiting for external approval

@Leo-Besancon Leo-Besancon dismissed stale reviews from damip, sydhds, bilboquet, and AurelienFT via 9505422 January 10, 2024 13:06
@damip damip self-requested a review January 10, 2024 13:13
damip
damip previously approved these changes Jan 10, 2024
@Leo-Besancon Leo-Besancon added this pull request to the merge queue Jan 10, 2024
@damip damip removed this pull request from the merge queue due to a manual request Jan 10, 2024
@damip damip self-requested a review January 10, 2024 14:34
@damip damip enabled auto-merge January 10, 2024 14:38
@damip damip added this pull request to the merge queue Jan 10, 2024
Merged via the queue into main with commit 4dbac57 Jan 10, 2024
11 checks passed
@sydhds sydhds deleted the fix_db_number_ordering branch January 16, 2024 11:07
@Leo-Besancon Leo-Besancon mentioned this pull request Apr 23, 2024
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.

6 participants