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

Add size_of const fns for upgradeable loader states #25131

Merged
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions account-decoder/src/parse_bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub fn parse_bpf_upgradeable_loader(
UpgradeableLoaderState::Uninitialized => BpfUpgradeableLoaderAccountType::Uninitialized,
UpgradeableLoaderState::Buffer { authority_address } => {
let offset = if authority_address.is_some() {
UpgradeableLoaderState::buffer_data_offset().unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

somehow if possible, keeping the offset terminology would probably be more readable at call-sites.
(i.e. not immediately clear why the caller should care about metadata or size of structs here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, you're definitely right. I think I would like to abstract this all away from the decoder in a follow up PR.

UpgradeableLoaderState::size_of_buffer_metadata()
} else {
// This case included for code completeness; in practice, a Buffer account will
// always have authority_address.is_some()
UpgradeableLoaderState::buffer_data_offset().unwrap()
UpgradeableLoaderState::size_of_buffer_metadata()
- serialized_size(&Pubkey::default()).unwrap() as usize
};
BpfUpgradeableLoaderAccountType::Buffer(UiBuffer {
Expand All @@ -42,9 +42,9 @@ pub fn parse_bpf_upgradeable_loader(
upgrade_authority_address,
} => {
let offset = if upgrade_authority_address.is_some() {
UpgradeableLoaderState::programdata_data_offset().unwrap()
UpgradeableLoaderState::size_of_programdata_metadata()
} else {
UpgradeableLoaderState::programdata_data_offset().unwrap()
UpgradeableLoaderState::size_of_programdata_metadata()
- serialized_size(&Pubkey::default()).unwrap() as usize
};
BpfUpgradeableLoaderAccountType::ProgramData(UiProgramData {
Expand Down
23 changes: 11 additions & 12 deletions cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ fn process_program_deploy(
program_len * 2
};
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(buffer_data_len)?,
UpgradeableLoaderState::size_of_programdata(buffer_data_len),
)?;

let result = if do_deploy {
Expand Down Expand Up @@ -1066,7 +1066,7 @@ fn process_write_buffer(
program_data.len()
};
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(buffer_data_len)?,
UpgradeableLoaderState::size_of_programdata(buffer_data_len),
)?;

let result = do_process_program_write_and_deploy(
Expand Down Expand Up @@ -1281,7 +1281,7 @@ fn get_programs(
.unwrap_or_else(|| "none".to_string()),
last_deploy_slot: slot,
data_len: programdata_account.data.len()
- UpgradeableLoaderState::programdata_data_offset()?,
- UpgradeableLoaderState::size_of_programdata_metadata(),
lamports: programdata_account.lamports,
use_lamports_unit,
});
Expand Down Expand Up @@ -1363,7 +1363,7 @@ fn process_show(
.unwrap_or_else(|| "none".to_string()),
last_deploy_slot: slot,
data_len: programdata_account.data.len()
- UpgradeableLoaderState::programdata_data_offset()?,
- UpgradeableLoaderState::size_of_programdata_metadata(),
lamports: programdata_account.lamports,
use_lamports_unit,
}))
Expand All @@ -1384,7 +1384,7 @@ fn process_show(
.map(|pubkey| pubkey.to_string())
.unwrap_or_else(|| "none".to_string()),
data_len: account.data.len()
- UpgradeableLoaderState::buffer_data_offset()?,
- UpgradeableLoaderState::size_of_buffer_metadata(),
lamports: account.lamports,
use_lamports_unit,
}))
Expand Down Expand Up @@ -1441,8 +1441,7 @@ fn process_dump(
if let Ok(UpgradeableLoaderState::ProgramData { .. }) =
programdata_account.state()
{
let offset =
UpgradeableLoaderState::programdata_data_offset().unwrap_or(0);
let offset = UpgradeableLoaderState::size_of_programdata_metadata();
let program_data = &programdata_account.data[offset..];
let mut f = File::create(output_location)?;
f.write_all(program_data)?;
Expand All @@ -1454,7 +1453,7 @@ fn process_dump(
Err(format!("Program {} has been closed", account_pubkey).into())
}
} else if let Ok(UpgradeableLoaderState::Buffer { .. }) = account.state() {
let offset = UpgradeableLoaderState::buffer_data_offset().unwrap_or(0);
let offset = UpgradeableLoaderState::size_of_buffer_metadata();
let program_data = &account.data[offset..];
let mut f = File::create(output_location)?;
f.write_all(program_data)?;
Expand Down Expand Up @@ -1758,7 +1757,7 @@ fn do_process_program_write_and_deploy(
buffer_pubkey,
&account,
if loader_id == &bpf_loader_upgradeable::id() {
UpgradeableLoaderState::buffer_len(buffer_data_len)?
UpgradeableLoaderState::size_of_buffer(buffer_data_len)
} else {
buffer_data_len
},
Expand Down Expand Up @@ -1848,7 +1847,7 @@ fn do_process_program_write_and_deploy(
buffer_pubkey,
&program_signers[1].pubkey(),
rpc_client.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::program_len()?,
UpgradeableLoaderState::size_of_program(),
)?,
programdata_len,
)?,
Expand Down Expand Up @@ -1911,7 +1910,7 @@ fn do_process_program_upgrade(
let loader_id = bpf_loader_upgradeable::id();
let data_len = program_data.len();
let minimum_balance = rpc_client.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(data_len)?,
UpgradeableLoaderState::size_of_programdata(data_len),
)?;

// Build messages to calculate fees
Expand All @@ -1930,7 +1929,7 @@ fn do_process_program_upgrade(
&config.signers[0].pubkey(),
&buffer_signer.pubkey(),
&account,
UpgradeableLoaderState::buffer_len(data_len)?,
UpgradeableLoaderState::size_of_buffer(data_len),
minimum_balance,
true,
)?
Expand Down
76 changes: 38 additions & 38 deletions cli/tests/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ fn test_cli_program_deploy_no_authority() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();
let minimum_balance_for_program = rpc_client
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::program_len().unwrap())
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_program())
.unwrap();
let upgrade_authority = Keypair::new();

Expand Down Expand Up @@ -252,12 +252,12 @@ fn test_cli_program_deploy_with_authority() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();
let minimum_balance_for_program = rpc_client
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::program_len().unwrap())
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_program())
.unwrap();
let upgrade_authority = Keypair::new();

Expand Down Expand Up @@ -316,7 +316,7 @@ fn test_cli_program_deploy_with_authority() {
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::programdata_data_offset().unwrap()..],
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -358,7 +358,7 @@ fn test_cli_program_deploy_with_authority() {
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::programdata_data_offset().unwrap()..],
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -391,7 +391,7 @@ fn test_cli_program_deploy_with_authority() {
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::programdata_data_offset().unwrap()..],
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -446,7 +446,7 @@ fn test_cli_program_deploy_with_authority() {
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!programdata_account.executable);
assert_eq!(
programdata_account.data[UpgradeableLoaderState::programdata_data_offset().unwrap()..],
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -591,12 +591,12 @@ fn test_cli_program_close_program() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_programdata = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();
let minimum_balance_for_program = rpc_client
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::program_len().unwrap())
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_program())
.unwrap();
let upgrade_authority = Keypair::new();

Expand Down Expand Up @@ -680,14 +680,14 @@ fn test_cli_program_write_buffer() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();
let minimum_balance_for_buffer_default = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();

let mut config = CliConfig::recent_for_tests();
Expand Down Expand Up @@ -730,7 +730,7 @@ fn test_cli_program_write_buffer() {
panic!("not a buffer account");
}
assert_eq!(
buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..],
buffer_account.data[UpgradeableLoaderState::size_of_buffer_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -767,7 +767,7 @@ fn test_cli_program_write_buffer() {
panic!("not a buffer account");
}
assert_eq!(
buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..],
buffer_account.data[UpgradeableLoaderState::size_of_buffer_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -829,7 +829,7 @@ fn test_cli_program_write_buffer() {
panic!("not a buffer account");
}
assert_eq!(
buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..],
buffer_account.data[UpgradeableLoaderState::size_of_buffer_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -864,7 +864,7 @@ fn test_cli_program_write_buffer() {
panic!("not a buffer account");
}
assert_eq!(
buffer_account.data[UpgradeableLoaderState::buffer_data_offset().unwrap()..],
buffer_account.data[UpgradeableLoaderState::size_of_buffer_metadata()..],
program_data[..]
);

Expand Down Expand Up @@ -1004,9 +1004,9 @@ fn test_cli_program_set_buffer_authority() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();

let mut config = CliConfig::recent_for_tests();
Expand Down Expand Up @@ -1119,9 +1119,9 @@ fn test_cli_program_mismatch_buffer_authority() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();

let mut config = CliConfig::recent_for_tests();
Expand Down Expand Up @@ -1212,9 +1212,9 @@ fn test_cli_program_show() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();

let mut config = CliConfig::recent_for_tests();
Expand Down Expand Up @@ -1399,9 +1399,9 @@ fn test_cli_program_dump() {
file.read_to_end(&mut program_data).unwrap();
let max_len = program_data.len();
let minimum_balance_for_buffer = rpc_client
.get_minimum_balance_for_rent_exemption(
UpgradeableLoaderState::programdata_len(max_len).unwrap(),
)
.get_minimum_balance_for_rent_exemption(UpgradeableLoaderState::size_of_programdata(
max_len,
))
.unwrap();

let mut config = CliConfig::recent_for_tests();
Expand Down
2 changes: 1 addition & 1 deletion program-test/tests/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn test_bpf_loader_upgradable_present() {
let upgrade_authority_keypair = Keypair::new();

let rent = banks_client.get_rent().await.unwrap();
let buffer_rent = rent.minimum_balance(UpgradeableLoaderState::programdata_len(1).unwrap());
let buffer_rent = rent.minimum_balance(UpgradeableLoaderState::size_of_programdata(1));

let create_buffer_instructions = bpf_loader_upgradeable::create_buffer(
&payer.pubkey(),
Expand Down
Loading