-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
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 Regardless, I am going to take a look into the code to see what would be the best approach possible. |
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. |
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. |
I vote for this |
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.The text was updated successfully, but these errors were encountered: