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

feat: account serialization overhaul #12794

Merged
merged 3 commits into from
Jan 27, 2025
Merged

feat: account serialization overhaul #12794

merged 3 commits into from
Jan 27, 2025

Conversation

pugachAG
Copy link
Contributor

@pugachAG pugachAG commented Jan 24, 2025

Change Account to be enum and use different representations for serde and borsh ser/deser:

  • SerdeAccount is introduced for serde to maintain backward and forward compatibility. Previously Account struct was directly annotated to support serde, we are opting out of this.
  • BorshVersionedAccount is introduced for borsh serialization. Accounts in old format are serialized directly as AccountV1. Note that we continue serializing in old format when possible to avoid 16 bytes overhead of sentinel padding.

Account::SERIALIZATION_SENTINEL hack is preserved for borsh serialization as I could not find a better way to handle backward compatibility.

This is part of #12716, in a separate PR global contracts related fields will be added as part of Account.

@pugachAG pugachAG requested a review from staffik January 24, 2025 13:03
@pugachAG pugachAG force-pushed the rework-account-ser branch 5 times, most recently from ee8d576 to 2fb8991 Compare January 25, 2025 18:24
Copy link

codecov bot commented Jan 25, 2025

Codecov Report

Attention: Patch coverage is 90.44586% with 15 lines in your changes missing coverage. Please review.

Project coverage is 70.45%. Comparing base (6618737) to head (6e7e6c0).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
core/primitives-core/src/account.rs 90.44% 11 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12794      +/-   ##
==========================================
- Coverage   70.46%   70.45%   -0.01%     
==========================================
  Files         847      847              
  Lines      175079   174994      -85     
  Branches   175079   174994      -85     
==========================================
- Hits       123369   123296      -73     
+ Misses      46451    46445       -6     
+ Partials     5259     5253       -6     
Flag Coverage Δ
backward-compatibility 0.16% <0.00%> (-0.01%) ⬇️
db-migration 0.16% <0.00%> (-0.01%) ⬇️
genesis-check 1.41% <26.36%> (+<0.01%) ⬆️
linux 70.06% <76.43%> (+0.02%) ⬆️
linux-nightly 70.09% <90.44%> (-0.01%) ⬇️
pytests 1.70% <26.36%> (+<0.01%) ⬆️
sanity-checks 1.52% <26.36%> (+0.01%) ⬆️
unittests 70.29% <90.44%> (-0.01%) ⬇️
upgradability 0.20% <0.00%> (-0.01%) ⬇️

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.

@pugachAG pugachAG requested a review from stedfn January 25, 2025 18:42
@pugachAG pugachAG marked this pull request as ready for review January 25, 2025 18:43
@pugachAG pugachAG requested a review from a team as a code owner January 25, 2025 18:43
Copy link
Contributor

@staffik staffik left a comment

Choose a reason for hiding this comment

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

Nice work!

pub fn set_permanent_storage_bytes(&mut self, permanent_storage_bytes: StorageUsage) {
self.permanent_storage_bytes = permanent_storage_bytes;
match self {
Self::V1(v1) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about this, because permanent storage bytes can only be set for V2 accounts and at the time of their creation. But anyway, plan to remove NEP-491 so feel free to leave it as it is.

permanent_storage_bytes,
code_hash: self.code_hash(),
storage_usage: self.storage_usage(),
version: version,
Copy link
Contributor

Choose a reason for hiding this comment

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

mini nit: just version

})
let versioned_account = BorshVersionedAccount::deserialize_reader(rd)?;
let account = match versioned_account {
BorshVersionedAccount::V2(account_v1) => Account::V2(account_v1),
Copy link
Contributor

Choose a reason for hiding this comment

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

should not be V2(account_v2) ?

return Err(serde::de::Error::custom(
"permanent storage bytes positive amount exists for account version older than V2",
"permanent storage bytes exists for V1 account",
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the err message is a bit unclear

Suggested change
"permanent storage bytes exists for V1 account",
"permanent storage bytes should not be set for V1 account",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, thanks!

@pugachAG pugachAG enabled auto-merge January 27, 2025 20:47
@pugachAG pugachAG added this pull request to the merge queue Jan 27, 2025
Merged via the queue into master with commit 5b17d59 Jan 27, 2025
29 checks passed
@pugachAG pugachAG deleted the rework-account-ser branch January 27, 2025 21:42
@staffik staffik mentioned this pull request Jan 28, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jan 30, 2025
There is ongoing work on [global
contracts](#12716) that addresses
the problem near/NEPs#491 was originally
designed to solve. NEP-491 code is quite messy and has been around for
about a year. We no longer see external interest in this feature, but if
the need arises in the future, we can always restore the code.

Reverts most of (because some part is used by
#12794):
- #9600
- #10701

Also, #11005 can be closed.
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