-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Feature Request: Clearer error when handling Account
& ProgramAccount
#1015
Comments
@armaniferrante @fanatid /// Allows you to check if a given account is initialized by system program call or not
pub trait AccountInitialized<'info>: ToAccountInfo<'info> {
fn initialized(&self) -> bool {
let account_info = self.to_account_info();
account_info.lamports() == 0 && system_program::ID.eq(account_info.owner)
}
fn not_initialized(&self) -> bool {
!self.initialized()
}
} Since this will be my first contribution to the project, let me know if this conflicts with any accepted practice. |
Interesting, can you provide an example of where the current implementation will fail or be incorrect? |
@fanatid I didn't quite understand what kind of incorrect implementation you are talking about 😅. Just instead of adding two if account_info.lamports() == 0 && system_program::ID.eq(account_info.owner) {
return Err(ErrorCode::AccountNotInitialized.into());
} Slightly harder if account.not_initialized() {
return Err(ErrorCode::AccountNotInitialized.into());
} and add all needed context about solana account initialization with all links in rustdoc of this trait method. But the first option will also solve the issue and it seems to me it will also improve the code base! So if you prefer not to complicate things and add simple |
I'm just not sure that I understand everything correctly. Are you saying that right now everything works correctly but we need an additional check for better errors, right? Or without check account_info.lamports() == 0 && system_program::ID.eq(account_info.owner) we have security vulnerability? |
Since non-initialized accounts do not have allocated memory, we cannot initialize any of them (I have not tried it, I am guided by the documentation, you can correct me if you are wrong). Therefore, I do not see a vulnerability here. And the original issue is only about the decomposition of the error into two more understandable and intuitive errors. |
I do not know the exact behavior. Can you leave links to the documentation? |
I also made the manual check, for an empty account, the following condition is correct: assert_eq!(ctx.accounts.not_initialized.try_borrow_data().unwrap().len(), 0); |
For
Account
andProgramAccount
, before checking ownership - do a check for existence. An important difference between transferring a non-existent account (belonging to the system program and withlamports == 0
) an account with really incorrect ownership.It is suggested to add an error that the account was expected to be already initialized:
This is an important clarification, since ownership is not the most intuitive marker of existence.
The text was updated successfully, but these errors were encountered: