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

sdk: implement AccountSharedData::is_default() #3289

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sdk/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ impl AccountSharedData {
Arc::strong_count(&self.data) > 1
}

pub fn is_default(&self) -> bool {
self.lamports == 0
&& self.owner == Pubkey::default()
&& !self.executable
&& self.rent_epoch == 0
&& self.data.is_empty()
}

pub fn reserve(&mut self, additional: usize) {
if let Some(data) = Arc::get_mut(&mut self.data) {
data.reserve(additional)
Expand Down Expand Up @@ -968,6 +976,38 @@ pub mod tests {
);
}

#[test]
fn test_default() {
// affirmative cases
assert!(AccountSharedData::default().is_default());

let account = AccountSharedData::new(0, 0, &Pubkey::default());
assert!(account.is_default());

let account = AccountSharedData::create(0, vec![], Pubkey::default(), false, 0);
assert!(account.is_default());

let account: AccountSharedData = Account::default().into();
assert!(account.is_default());

// negative cases
// since abi is frozen, we dont need to derive Arbitrary to guard against new fields
let account = AccountSharedData::create(1, vec![], Pubkey::default(), false, 0);
assert!(!account.is_default());

let account = AccountSharedData::create(0, vec![0], Pubkey::default(), false, 0);
assert!(!account.is_default());

let account = AccountSharedData::create(0, vec![], Pubkey::new_unique(), false, 0);
assert!(!account.is_default());

let account = AccountSharedData::create(0, vec![], Pubkey::default(), true, 0);
assert!(!account.is_default());

let account = AccountSharedData::create(0, vec![], Pubkey::default(), false, 1);
assert!(!account.is_default());
}

#[test]
fn test_account_add_sub_lamports() {
let key = Pubkey::new_unique();
Expand Down
Loading