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

Simplified account details for on-chain accounts (only delta remains) #530

Closed
wants to merge 4 commits into from

Conversation

polydez
Copy link
Contributor

@polydez polydez commented Mar 20, 2024

Related to issue: #524

We decided to use just AccountDelta for on-chain accounts, even for new ones. In this case, all values must be provided.

Limitations:

  1. Code is supported only for new accounts. Code updates are still not supported.
  2. Storage for new on-chain accounts may have only simple layout (set of values created from updated_values). Maps and arrays are not supported.

I think, we also should discuss these limitations. Do we really want to move in that direction? It seems, we would need to add more and more special cases for delta processing for new/existing accounts so this will lead to even worse code than it was before simplification.

@polydez polydez requested review from hackaugusto and bobbinth March 20, 2024 08:01
@polydez polydez marked this pull request as ready for review March 20, 2024 12:08
@polydez polydez self-assigned this Mar 20, 2024
@bobbinth
Copy link
Contributor

As I mentioned in #524, I think we should probably put this on hold for now.

But as far as this PR goes, we'd also need to update deserialization code for AccountStorageDelta as it fail to deserialize "immutable" slot updates.

@polydez polydez force-pushed the polydez-simplify-account-details branch from beea8af to dc072b2 Compare April 16, 2024 16:29
@polydez polydez requested a review from igamigo April 18, 2024 05:09
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Thank you! Looks good. Not a full review yet - but I left some comments inline.

CHANGELOG.md Outdated
@@ -2,6 +2,8 @@

## 0.3.0 (TBD)

* [BREAKING] Simplified account details for on-chain accounts, now only delta provided
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: let's add a PR number to this line (similar to how we do it for other changes below).

Comment on lines 27 to 32
pub struct AccountDelta {
storage: AccountStorageDelta,
vault: AccountVaultDelta,
code: Option<AccountCode>,
nonce: Option<Felt>,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also update comments for this struct.

@@ -31,7 +31,11 @@ impl AccountStorageDelta {
/// - The number of cleared or updated items is greater than 255.
/// - Any of cleared or updated items are at slot 255 (i.e., immutable slot).
/// - Any of the cleared or updated items is referenced more than once (e.g., updated twice).
pub fn validate(&self) -> Result<(), AccountDeltaError> {
pub fn validate(&self, is_new_account: bool) -> Result<(), AccountDeltaError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's update the comments above to include this new error condition.

Comment on lines +136 to +138
if nonce != Some(ZERO) {
return Err(AccountDeltaError::IncorrectNonceForNewAccount(nonce));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is correct: nonce for a new account would start out as zero, but after the update it would not be zero. So, the nonce in the delta would not be zero. We could potentially force the nonce for the first update to be one - but I'm not sure that's a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make sure deserialization code here works correctly. I think this will currently fail for new accounts as for new accounts we need to set storage layout in MAX_MUTABLE_STORAGE_SLOT_IDX but deserialization code would not allow this.

@@ -28,7 +28,11 @@ impl AccountVaultDelta {
/// - The number of removed assets is greater than [u16::MAX].
/// - The same asset was added more than once, removed more than once, or both added and
/// removed.
pub fn validate(&self) -> Result<(), AccountDeltaError> {
pub fn validate(&self, is_new_account: bool) -> Result<(), AccountDeltaError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's update the comments above describing the new failure condition.

Comment on lines +74 to +75
/// Creates and returns a new account constructed from the specified ID and delta.
pub fn from_delta(id: AccountId, delta: &AccountDelta) -> Result<Self, AccountError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably return an error from here if the delta is not for new account.

Comment on lines +78 to +88
let storage = AccountStorage::new(
delta
.storage()
.updated_items
.iter()
.map(|&(index, value)| SlotItem {
index,
slot: StorageSlot::new_value(value),
})
.collect(),
)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be fine for now - but we'll also need to handle other storage slot types (e.g., storage map which are implemented in #521). So, I wonder if it would be better to encapsulate this in the AccountStorageDelta - e.g., by implementing TryFrom<AccountStorageDelta> for <AccountStorage>.

@polydez
Copy link
Contributor Author

polydez commented Apr 18, 2024

@bobbinth thank you!
@bobbinth @hackaugusto, what do you think, are we moving the right direction with substituting account/delta with just delta? It seems, that delta doesn't fit well for new accounts. It's possible that eventually we will need to rollback to our previous approach.

# Conflicts:
#	CHANGELOG.md
#	objects/src/transaction/proven_tx.rs
@bobbinth
Copy link
Contributor

@bobbinth @hackaugusto, how do you think, are we moving the right direction with substituting account/delta with just delta? It seems, that delta doesn't fit well for new accounts. It's possible that eventually we will need to rollback to our previous approach.

Yeah - I was wondering the same thing. It seems like we are introducing a lot of code to handle the special case of AccountDelta being for a new account, and maybe having new vs. existing cases separated the way we have now is cleaner approach.

If we do keep the things the way they are now, we should probably rename AccountDetails into AccountUpdateDetails, and maybe define it as:

pub enum AccountUpdateDetails {
    None,
    NewAccount(Account),
    Delta(AccountDelta),
}

Another potential thing we might want to do is to define a new struct for AccountUpdate - something like:

pub struct AccountUpdate {
    init_hash: Diget,
    final_hash: Digest,
    details: AccountUpdateDetails,
}

And then ProvenTransaction could look like:

pub struct ProvenTransaction {
    id: TransactionId,
    account_id: AccountId,
    account_update: AccountUpdate,
    input_notes: InputNotes<Nullifier>,
    output_notes: OutputNotes,
    block_ref: Digest,
    proof: ExecutionProof,
}

@polydez
Copy link
Contributor Author

polydez commented Apr 19, 2024

@bobbinth thank you!
@hackaugusto what do you think?

@hackaugusto
Copy link
Contributor

@hackaugusto what do you think?

I don't really understand why the special cases are needed. But if you and @bobbinth think the change is worse, totally fine with me to not do it.

@bobbinth
Copy link
Contributor

I don't really understand why the special cases are needed.

There are a couple of reasons. First is validation of the delta. For example, for new accounts, the vault delta should not have any removed_assets. Second reason is about how we instantiate the account. For example, we need to have a special constructor (e.g., Account::from_delta(account_id, account_delta)) since we can't just create a blank account and apply delta to it.

If we forego special cases during validation, that would probably simplify things enough to make it worth it. But I'm not sure if that's a good idea.

@polydez
Copy link
Contributor Author

polydez commented Apr 22, 2024

Closed in favour of #618

@polydez polydez closed this Apr 22, 2024
@hackaugusto hackaugusto deleted the polydez-simplify-account-details branch April 22, 2024 15:30
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