Skip to content

Commit

Permalink
AddressLoader: allow for non-owned MessageAddressTableLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
apfitzge committed Aug 14, 2024
1 parent aa2d351 commit 54ba97b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
20 changes: 10 additions & 10 deletions accounts-db/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
account::{AccountSharedData, ReadableAccount},
address_lookup_table::{self, error::AddressLookupError, state::AddressLookupTable},
clock::{BankId, Slot},
message::v0::{LoadedAddresses, MessageAddressTableLookup},
message::{v0::LoadedAddresses, MessageAddressTableLookupRef},
pubkey::Pubkey,
slot_hashes::SlotHashes,
transaction::{Result, SanitizedTransaction},
Expand Down Expand Up @@ -82,12 +82,12 @@ impl Accounts {
pub fn load_lookup_table_addresses(
&self,
ancestors: &Ancestors,
address_table_lookup: &MessageAddressTableLookup,
address_table_lookup: MessageAddressTableLookupRef,
slot_hashes: &SlotHashes,
) -> std::result::Result<LoadedAddresses, AddressLookupError> {
let table_account = self
.accounts_db
.load_with_fixed_root(ancestors, &address_table_lookup.account_key)
.load_with_fixed_root(ancestors, address_table_lookup.account_key)
.map(|(account, _rent)| account)
.ok_or(AddressLookupError::LookupTableAccountNotFound)?;

Expand All @@ -99,12 +99,12 @@ impl Accounts {
Ok(LoadedAddresses {
writable: lookup_table.lookup(
current_slot,
&address_table_lookup.writable_indexes,
address_table_lookup.writable_indexes,
slot_hashes,
)?,
readonly: lookup_table.lookup(
current_slot,
&address_table_lookup.readonly_indexes,
address_table_lookup.readonly_indexes,
slot_hashes,
)?,
})
Expand Down Expand Up @@ -611,7 +611,7 @@ mod tests {
address_lookup_table::state::LookupTableMeta,
hash::Hash,
instruction::CompiledInstruction,
message::{Message, MessageHeader},
message::{v0::MessageAddressTableLookup, Message, MessageHeader},
native_loader,
signature::{signers::Signers, Keypair, Signer},
transaction::{Transaction, TransactionError, MAX_TX_ACCOUNT_LOCKS},
Expand Down Expand Up @@ -708,7 +708,7 @@ mod tests {
assert_eq!(
accounts.load_lookup_table_addresses(
&ancestors,
&address_table_lookup,
MessageAddressTableLookupRef::from(&address_table_lookup),
&SlotHashes::default(),
),
Err(AddressLookupError::LookupTableAccountNotFound),
Expand All @@ -735,7 +735,7 @@ mod tests {
assert_eq!(
accounts.load_lookup_table_addresses(
&ancestors,
&address_table_lookup,
MessageAddressTableLookupRef::from(&address_table_lookup),
&SlotHashes::default(),
),
Err(AddressLookupError::InvalidAccountOwner),
Expand All @@ -762,7 +762,7 @@ mod tests {
assert_eq!(
accounts.load_lookup_table_addresses(
&ancestors,
&address_table_lookup,
MessageAddressTableLookupRef::from(&address_table_lookup),
&SlotHashes::default(),
),
Err(AddressLookupError::InvalidAccountData),
Expand Down Expand Up @@ -801,7 +801,7 @@ mod tests {
assert_eq!(
accounts.load_lookup_table_addresses(
&ancestors,
&address_table_lookup,
MessageAddressTableLookupRef::from(&address_table_lookup),
&SlotHashes::default(),
),
Ok(LoadedAddresses {
Expand Down
10 changes: 3 additions & 7 deletions runtime/src/bank/address_lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use {
super::Bank,
solana_sdk::{
address_lookup_table::error::AddressLookupError,
message::{
v0::{LoadedAddresses, MessageAddressTableLookup},
AddressLoaderError,
},
message::{v0::LoadedAddresses, AddressLoaderError, MessageAddressTableLookupRef},
transaction::AddressLoader,
},
};
Expand All @@ -22,9 +19,9 @@ fn into_address_loader_error(err: AddressLookupError) -> AddressLoaderError {
}

impl AddressLoader for &Bank {
fn load_addresses(
fn load_addresses_ref<'a>(
self,
address_table_lookups: &[MessageAddressTableLookup],
address_table_lookups: impl Iterator<Item = MessageAddressTableLookupRef<'a>>,
) -> Result<LoadedAddresses, AddressLoaderError> {
let slot_hashes = self
.transaction_processor
Expand All @@ -33,7 +30,6 @@ impl AddressLoader for &Bank {
.map_err(|_| AddressLoaderError::SlotHashesSysvarNotFound)?;

address_table_lookups
.iter()
.map(|address_table_lookup| {
self.rc
.accounts
Expand Down
29 changes: 27 additions & 2 deletions sdk/program/src/message/address_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
super::v0::{LoadedAddresses, MessageAddressTableLookup},
crate::pubkey::Pubkey,
thiserror::Error,
};

Expand Down Expand Up @@ -30,10 +31,34 @@ pub enum AddressLoaderError {
InvalidLookupIndex,
}

/// A non-owning version of `MessageAddressTableLookup`.
pub struct MessageAddressTableLookupRef<'a> {
pub account_key: &'a Pubkey,
pub writable_indexes: &'a [u8],
pub readonly_indexes: &'a [u8],
}

impl<'a> From<&'a MessageAddressTableLookup> for MessageAddressTableLookupRef<'a> {
fn from(lookup: &'a MessageAddressTableLookup) -> Self {
Self {
account_key: &lookup.account_key,
writable_indexes: &lookup.writable_indexes,
readonly_indexes: &lookup.readonly_indexes,
}
}
}

pub trait AddressLoader: Clone {
fn load_addresses(
self,
lookups: &[MessageAddressTableLookup],
) -> Result<LoadedAddresses, AddressLoaderError> {
self.load_addresses_ref(lookups.iter().map(MessageAddressTableLookupRef::from))
}

fn load_addresses_ref<'a>(
self,
lookups: impl Iterator<Item = MessageAddressTableLookupRef<'a>>,
) -> Result<LoadedAddresses, AddressLoaderError>;
}

Expand All @@ -44,9 +69,9 @@ pub enum SimpleAddressLoader {
}

impl AddressLoader for SimpleAddressLoader {
fn load_addresses(
fn load_addresses_ref<'a>(
self,
_lookups: &[MessageAddressTableLookup],
_lookups: impl Iterator<Item = MessageAddressTableLookupRef<'a>>,
) -> Result<LoadedAddresses, AddressLoaderError> {
match self {
Self::Disabled => Err(AddressLoaderError::Disabled),
Expand Down
9 changes: 5 additions & 4 deletions svm/tests/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use {
hash::Hash,
instruction::{AccountMeta, CompiledInstruction},
message::{
v0::{self, LoadedAddresses, MessageAddressTableLookup},
AddressLoader, AddressLoaderError, Message, MessageHeader, VersionedMessage,
v0::{self, LoadedAddresses},
AddressLoader, AddressLoaderError, Message, MessageAddressTableLookupRef,
MessageHeader, VersionedMessage,
},
pubkey::Pubkey,
reserved_account_keys::ReservedAccountKeys,
Expand Down Expand Up @@ -48,9 +49,9 @@ struct MockLoader {}

// This implementation is only necessary if one is using account table lookups.
impl AddressLoader for MockLoader {
fn load_addresses(
fn load_addresses_ref<'a>(
self,
_lookups: &[MessageAddressTableLookup],
_lookups: impl Iterator<Item = MessageAddressTableLookupRef<'a>>,
) -> Result<LoadedAddresses, AddressLoaderError> {
Ok(LoadedAddresses {
writable: vec![],
Expand Down

0 comments on commit 54ba97b

Please sign in to comment.