From 73ad9a67cdfac3eb11f8487fb18343b85d69e532 Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Wed, 20 Nov 2024 03:58:35 +0000 Subject: [PATCH 1/5] program: reorder lookup table close ops --- program/src/processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/program/src/processor.rs b/program/src/processor.rs index 83bf5ea..1014b88 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -537,8 +537,8 @@ fn process_close_lookup_table(program_id: &Pubkey, accounts: &[AccountInfo]) -> **recipient_info.try_borrow_mut_lamports()? = new_recipient_lamports; // Lookup tables are _not_ reassigned when closed. - **lookup_table_info.try_borrow_mut_lamports()? = 0; lookup_table_info.realloc(0, true)?; + **lookup_table_info.try_borrow_mut_lamports()? = 0; Ok(()) } From 1311a86bf42ecb9af2fa653f781270d13c49cacd Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Wed, 20 Nov 2024 03:59:33 +0000 Subject: [PATCH 2/5] program: add readonly check for lookup table on close --- program/src/processor.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/program/src/processor.rs b/program/src/processor.rs index 1014b88..17657ae 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -536,6 +536,10 @@ fn process_close_lookup_table(program_id: &Pubkey, accounts: &[AccountInfo]) -> .ok_or::(ProgramError::ArithmeticOverflow)?; **recipient_info.try_borrow_mut_lamports()? = new_recipient_lamports; + if !lookup_table_info.is_writable { + return Err(AddressLookupTableError::ReadonlyDataModified.into()); + } + // Lookup tables are _not_ reassigned when closed. lookup_table_info.realloc(0, true)?; **lookup_table_info.try_borrow_mut_lamports()? = 0; From c900ec523d45db1f7afb926c509f479ffb2fc940 Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Wed, 20 Nov 2024 04:00:30 +0000 Subject: [PATCH 3/5] program: add readonly check for recipient on close --- program/src/error.rs | 3 +++ program/src/processor.rs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/program/src/error.rs b/program/src/error.rs index 1d804c9..b4dbac0 100644 --- a/program/src/error.rs +++ b/program/src/error.rs @@ -29,6 +29,9 @@ pub enum AddressLookupTableError { /// Instruction modified data of a read-only account. #[error("Instruction modified data of a read-only account")] ReadonlyDataModified = 10, // Avoid collisions with System. + /// Instruction changed the balance of a read-only account. + #[error("Instruction changed the balance of a read-only account")] + ReadonlyLamportsChanged, } impl PrintProgramError for AddressLookupTableError { diff --git a/program/src/processor.rs b/program/src/processor.rs index 17657ae..9453680 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -534,6 +534,11 @@ fn process_close_lookup_table(program_id: &Pubkey, accounts: &[AccountInfo]) -> .lamports() .checked_add(recipient_info.lamports()) .ok_or::(ProgramError::ArithmeticOverflow)?; + + if !recipient_info.is_writable { + return Err(AddressLookupTableError::ReadonlyLamportsChanged.into()); + } + **recipient_info.try_borrow_mut_lamports()? = new_recipient_lamports; if !lookup_table_info.is_writable { From b687678bbf2b158ddf4cfb71026329f1598622a4 Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Wed, 20 Nov 2024 04:01:32 +0000 Subject: [PATCH 4/5] program: map sol_get_sysavr errs to `UnsupportedSysvar` --- program/src/processor.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/program/src/processor.rs b/program/src/processor.rs index 9453680..98d1824 100644 --- a/program/src/processor.rs +++ b/program/src/processor.rs @@ -43,7 +43,9 @@ fn get_lookup_table_status( Ok(LookupTableStatus::Deactivating { remaining_blocks: MAX_ENTRIES.saturating_add(1), }) - } else if let Some(slot_position) = SlotHashesSysvar::position(&deactivation_slot)? { + } else if let Some(slot_position) = SlotHashesSysvar::position(&deactivation_slot) + .map_err(|_| ProgramError::UnsupportedSysvar)? + { // Deactivation requires a cool-down period to give in-flight transactions // enough time to land and to remove indeterminism caused by transactions // loading addresses in the same slot when a table is closed. The @@ -143,7 +145,10 @@ fn process_create_lookup_table( } let derivation_slot = { - if SlotHashesSysvar::get(&untrusted_recent_slot)?.is_some() { + if SlotHashesSysvar::get(&untrusted_recent_slot) + .map_err(|_| ProgramError::UnsupportedSysvar)? + .is_some() + { Ok(untrusted_recent_slot) } else { msg!("{} is not a recent slot", untrusted_recent_slot); From c816d6cfd81cc721761a47a301fc4f66631da321 Mon Sep 17 00:00:00 2001 From: Joe Caulfield Date: Wed, 20 Nov 2024 05:29:03 +0000 Subject: [PATCH 5/5] check in new compute unit benchmark --- program/benches/compute_units.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/program/benches/compute_units.md b/program/benches/compute_units.md index 0a80f05..477e64d 100644 --- a/program/benches/compute_units.md +++ b/program/benches/compute_units.md @@ -1,3 +1,29 @@ +#### Compute Units: 2024-11-20 05:26:29.922433084 UTC + +| Name | CUs | Delta | +|------|------|-------| +| create_lookup_table | 10755 | -1 | +| freeze_lookup_table | 1518 | -- | +| extend_lookup_table_from_0_to_1 | 6226 | -1 | +| extend_lookup_table_from_0_to_10 | 8845 | -1 | +| extend_lookup_table_from_0_to_38 | 17081 | -1 | +| extend_lookup_table_from_1_to_2 | 6226 | -1 | +| extend_lookup_table_from_1_to_10 | 8551 | -1 | +| extend_lookup_table_from_1_to_39 | 17081 | -1 | +| extend_lookup_table_from_5_to_6 | 6226 | -1 | +| extend_lookup_table_from_5_to_15 | 8846 | -1 | +| extend_lookup_table_from_5_to_43 | 17081 | -1 | +| extend_lookup_table_from_25_to_26 | 6229 | -1 | +| extend_lookup_table_from_25_to_35 | 8848 | -1 | +| extend_lookup_table_from_25_to_63 | 17084 | -1 | +| extend_lookup_table_from_50_to_88 | 17087 | -1 | +| extend_lookup_table_from_100_to_138 | 17093 | -1 | +| extend_lookup_table_from_150_to_188 | 17100 | -1 | +| extend_lookup_table_from_200_to_238 | 17106 | -1 | +| extend_lookup_table_from_255_to_256 | 6258 | -1 | +| deactivate_lookup_table | 3152 | -- | +| close_lookup_table | 2234 | +7 | + #### Compute Units: 2024-11-13 16:51:07.839873 UTC | Name | CUs | Delta |