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

Provide ability to skip account initialization if it's already been created #852

Closed
armaniferrante opened this issue Oct 8, 2021 · 5 comments · Fixed by #906
Closed
Assignees

Comments

@armaniferrante
Copy link
Member

Currently the #[account(init)] keyword will always initalize an account via CPI to the system program. This means that if the account has already been created, then the instruction will fail. This is undesireable in the event you have two different users in two different apps performing the same action on the same new account, in which case they will both try to initialize and one will fail. Instead, it would be nice if both of them succeeded, and the second user simply didn't try to initialize.

One way of doing this would be to introduce a new #[account(init_if_needed)] keyword that simply skipped the initialization logic if the account already exists.

@davoclavo
Copy link
Contributor

I think this is a great idea!

And I would like to add something to the discussion: Could this be the default behavior of the current init keyword? Or is there a security vulnerability or significant performance degradation? Asking because if this keyword were added, I think I would use it exclusively in favor of init - but perhaps I am missing something obvious.

Regardless, I am going to take a look into the code to see what would be the best approach possible.

@armaniferrante
Copy link
Member Author

Using this as the default behavior is dangerous, e.g., the program might explicitly zero state or set to some starting value that shouldn't be set if it's already been initialized.

@perlish
Copy link

perlish commented Oct 9, 2021

its helpful but how to create more than one account with program ?

@armaniferrante
Copy link
Member Author

its helpful but how to create more than one account with program ?

This would check the data has been allocated and initialized. If it has not, i.e., zero length data and owned by the system program, then the new account would be initialized. This would support more than one account.

@acamill
Copy link
Contributor

acamill commented Oct 14, 2021

I vote for this

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 a pull request may close this issue.

4 participants