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

Commit

Permalink
Prevent lookup tables from being closed during deactivation slot
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Jan 2, 2022
1 parent d6ec103 commit 3dd6657
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
35 changes: 17 additions & 18 deletions programs/address-lookup-table/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,26 +419,25 @@ impl Processor {
if lookup_table.meta.authority != Some(*authority_account.unsigned_key()) {
return Err(InstructionError::IncorrectAuthority);
}
if lookup_table.meta.deactivation_slot == Slot::MAX {
ic_msg!(invoke_context, "Lookup table is not deactivated");
return Err(InstructionError::InvalidArgument);
}

// Assert that the deactivation slot is no longer recent 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. This enforced delay has a side
// effect of not allowing lookup tables to be recreated at the same derived address
// because tables must be created at an address derived from a recent slot.
let clock: Clock = invoke_context.get_sysvar(&clock::id())?;
let slot_hashes: SlotHashes = invoke_context.get_sysvar(&slot_hashes::id())?;
if let Some(position) = slot_hashes.position(&lookup_table.meta.deactivation_slot) {
let expiration = MAX_ENTRIES.saturating_sub(position);
ic_msg!(
invoke_context,
"Table cannot be closed until its derivation slot expires in {} blocks",
expiration
);
return Err(InstructionError::InvalidArgument);
}

match lookup_table.meta.status(clock.slot, &slot_hashes) {
LookupTableStatus::Activated => {
ic_msg!(invoke_context, "Lookup table is not deactivated");
Err(InstructionError::InvalidArgument)
}
LookupTableStatus::Deactivating(expiration) => {
ic_msg!(
invoke_context,
"Table cannot be closed until it's fully deactivated in {} blocks",
expiration
);
Err(InstructionError::InvalidArgument)
}
LookupTableStatus::Deactivated => Ok(()),
}?;

drop(lookup_table_account_ref);

Expand Down
47 changes: 46 additions & 1 deletion programs/address-lookup-table/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use {
serde::{Deserialize, Serialize},
solana_frozen_abi_macro::{AbiEnumVisitor, AbiExample},
solana_sdk::{clock::Slot, instruction::InstructionError, pubkey::Pubkey},
solana_sdk::{
clock::Slot,
instruction::InstructionError,
pubkey::Pubkey,
slot_hashes::{SlotHashes, MAX_ENTRIES},
},
std::borrow::Cow,
};

Expand All @@ -21,6 +26,13 @@ pub enum ProgramState {
LookupTable(LookupTableMeta),
}

/// Activation status of a lookup table
pub enum LookupTableStatus {
Activated,
Deactivating { remaining_blocks: usize },
Deactivated,
}

/// Address lookup table metadata
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, AbiExample)]
pub struct LookupTableMeta {
Expand Down Expand Up @@ -61,6 +73,39 @@ impl LookupTableMeta {
..LookupTableMeta::default()
}
}

pub fn is_active(&self, current_slot: Slot, slot_hashes: &SlotHashes) -> bool {
match self.status(current_slot, slot_hashes) {
LookupTableStatus::Activated => true,
LookupTableStatus::Deactivating { .. } => true,
LookupTableStatus::Deactivated => false,
}
}

pub fn status(&self, current_slot: Slot, slot_hashes: &SlotHashes) -> LookupTableStatus {
if self.deactivation_slot == Slot::MAX {
LookupTableStatus::Activated
} else if self.deactivation_slot == current_slot {
LookupTableStatus::Deactivating {
remaining_blocks: MAX_ENTRIES,
}
} else if let Some(slot_hash_position) = slot_hashes.position(&self.deactivation_slot) {
// 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 cool-down period is
// equivalent to the amount of time it takes for a slot to be removed from the
// slot hash list.
//
// By using the slot hash to enforce the cool-down, there is a side effect
// of not allowing lookup tables to be recreated at the same derived address
// because tables must be created at an address derived from a recent slot.
LookupTableStatus::Deactivating {
remaining_blocks: MAX_ENTRIES.saturating_sub(position),
}
} else {
LookupTableStatus::Deactivated
}
}
}

#[derive(Debug, PartialEq, Clone, AbiExample)]
Expand Down

0 comments on commit 3dd6657

Please sign in to comment.