Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Tiny add_native_program bug fixes with cleanups#14042

Merged
ryoqun merged 2 commits intosolana-labs:masterfrom
ryoqun:simpler-capitalization-preps
Dec 11, 2020
Merged

Tiny add_native_program bug fixes with cleanups#14042
ryoqun merged 2 commits intosolana-labs:masterfrom
ryoqun:simpler-capitalization-preps

Conversation

@ryoqun
Copy link
Copy Markdown
Contributor

@ryoqun ryoqun commented Dec 10, 2020

Problem

the simpler capitalization pr got too big... #13884.

Also, this fixes a regression bug and a new bug introduced at #13403. In this sense, this pr is a follow up to #13403, #11750.

Especially, this is extractable.

Summary of Changes

So, extract it! And bunch of tiny renames and preparation changes for this.

There is no need for gating because add_native_program is seldom touched on the live cluster.

Comment thread runtime/src/bank.rs
}

fn inherit_sysvar_account_balance(&self, old_account: &Option<Account>) -> u64 {
fn inherit_specially_retained_account_balance(&self, old_account: &Option<Account>) -> u64 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

rename prep for #13884

Comment thread runtime/src/bank.rs
// flush the Stakes cache
account.lamports = 0;
self.store_account(&program_id, &account);
None
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

wrote a new test case specifically for this: (test_add_native_program_squatted_while_not_replacing)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bug description (re-post from https://github.com/solana-labs/solana/pull/13403/files#r535953548):

well this logic changes introduced a regression where we could be tricked into skipping to create native program because existing_genuine_program can be set to a bad account and not reset and we enters this match arm returning from this fn: https://github.com/solana-labs/solana/pull/13403/files#diff-ed47b4a0198313377e091bb3957bbbc63d937805426d1b2b6de39d0a50d32a0cR1760

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in other words, we shouldn't keep around fake account as Some(existing_genuine_program). It would make us skip native account creation (= must_replace = false) So, return None here.

Previously, it only cleared the bogus account...

Comment thread runtime/src/bank.rs
let account = native_loader::create_loadable_account(name);
let account = native_loader::create_loadable_account(
name,
self.inherit_specially_retained_account_balance(&existing_genuine_program),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this new inherit_specially_retained_account_balance call is justified by this: (test_add_native_program_inherited_cap_while_replacing)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bug description (re-post from #13403 (comment)):

Also, when replacing, we must consider the balance of existing program account otherwise we would cause capitalization mismatch if the replaced native program isn't 1 lamports.

Copy link
Copy Markdown
Contributor Author

@ryoqun ryoqun Dec 10, 2020

Choose a reason for hiding this comment

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

Also, I later noticed that we can meaninglessly send to sysvars, but not to executable accounts. So, this can't happen on the wild. But, addressing and adding tests should never hurt us not much (judging from the needed code changes in this pr). :)

Comment thread runtime/src/bank.rs
assert!(bank.stakes.read().unwrap().vote_accounts().is_empty());
assert!(bank.stakes.read().unwrap().stake_delegations().is_empty());
assert_eq!(bank.calculate_capitalization(), bank.capitalization());
assert_eq!(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

these additional assertions to a test written at #11750 exposes a regression introduced at #13403.

Comment thread runtime/src/bank.rs
let bank1 = Arc::new(Bank::new(&genesis_config));
bank1.update_sysvar_account(&dummy_clock_id, |optional_account| {
assert!(optional_account.is_none());
assert_capitalization_diff(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

best viewed with ignore whitespace mode in github

Comment thread runtime/src/bank.rs
},
|old, new| {
// creating new sysvar twice in a slot shouldn't increment capitalization twice
assert_eq!(old, new);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

for example, this and that (https://github.com/solana-labs/solana/pull/14042/files#diff-ed47b4a0198313377e091bb3957bbbc63d937805426d1b2b6de39d0a50d32a0cR10071) writing style differences are somewhat intentional for easier diff of upcoming #13884 .

Comment thread runtime/src/bank.rs
let existing_genuine_program = if let Some(mut account) = self.get_account(&program_id) {
// it's very unlikely to be squatted at program_id as non-system account because of burden to
// find victim's pubkey/hash. So, when account.owner is indeed native_loader's, it's
// safe to assume it's a genuine program.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added some explanation.

@ryoqun
Copy link
Copy Markdown
Contributor Author

ryoqun commented Dec 10, 2020

@CriesofCarrots Could you review this? thanks!

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 10, 2020

Codecov Report

Merging #14042 (1f84aaa) into master (f02d4cd) will increase coverage by 0.0%.
The diff coverage is 98.6%.

@@           Coverage Diff           @@
##           master   #14042   +/-   ##
=======================================
  Coverage    82.1%    82.1%           
=======================================
  Files         381      381           
  Lines       94076    94167   +91     
=======================================
+ Hits        77291    77379   +88     
- Misses      16785    16788    +3     

Copy link
Copy Markdown
Contributor

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

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

Nice cleanup!

@ryoqun ryoqun added the v1.4 label Dec 11, 2020
@ryoqun ryoqun merged commit 164b789 into solana-labs:master Dec 11, 2020
mergify Bot pushed a commit that referenced this pull request Dec 11, 2020
* Tiny add_native_program bug fixes with cleanups

* Fix typo

(cherry picked from commit 164b789)
mvines pushed a commit that referenced this pull request Dec 11, 2020
* Tiny add_native_program bug fixes with cleanups

* Fix typo

(cherry picked from commit 164b789)

Co-authored-by: Ryo Onodera <ryoqun@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants