From b4350a2522e3d7756450f221f003fd130662bc34 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Thu, 17 Mar 2022 08:21:07 +0800 Subject: [PATCH] Make solana-address-lookup-table-program crate bpf compatible (#23700) --- Cargo.lock | 1 + programs/address-lookup-table/Cargo.toml | 5 ++- programs/address-lookup-table/src/error.rs | 34 +++++++++++++++++++ .../address-lookup-table/src/instruction.rs | 2 +- programs/address-lookup-table/src/lib.rs | 4 ++- programs/address-lookup-table/src/state.rs | 4 +-- programs/bpf/Cargo.lock | 2 ++ programs/bpf/rust/dep_crate/Cargo.toml | 2 ++ runtime/src/accounts.rs | 7 ++-- runtime/src/bank/address_lookup_table.rs | 5 ++- sdk/src/transaction/error.rs | 30 ---------------- 11 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 programs/address-lookup-table/src/error.rs diff --git a/Cargo.lock b/Cargo.lock index 47b7b34f4..4cc72f6cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4287,6 +4287,7 @@ dependencies = [ "serde", "solana-frozen-abi 1.10.3", "solana-frozen-abi-macro 1.10.3", + "solana-program 1.10.3", "solana-program-runtime", "solana-sdk", "thiserror", diff --git a/programs/address-lookup-table/Cargo.toml b/programs/address-lookup-table/Cargo.toml index f3378eed6..8ff141867 100644 --- a/programs/address-lookup-table/Cargo.toml +++ b/programs/address-lookup-table/Cargo.toml @@ -18,9 +18,12 @@ num-traits = "0.2" serde = { version = "1.0.136", features = ["derive"] } solana-frozen-abi = { path = "../../frozen-abi", version = "=1.10.3" } solana-frozen-abi-macro = { path = "../../frozen-abi/macro", version = "=1.10.3" } +solana-program = { path = "../../sdk/program", version = "=1.10.3" } +thiserror = "1.0" + +[target.'cfg(not(target_arch = "bpf"))'.dependencies] solana-program-runtime = { path = "../../program-runtime", version = "=1.10.3" } solana-sdk = { path = "../../sdk", version = "=1.10.3" } -thiserror = "1.0" [build-dependencies] rustc_version = "0.4" diff --git a/programs/address-lookup-table/src/error.rs b/programs/address-lookup-table/src/error.rs new file mode 100644 index 000000000..d0a063ec4 --- /dev/null +++ b/programs/address-lookup-table/src/error.rs @@ -0,0 +1,34 @@ +#[cfg(not(target_arch = "bpf"))] +use solana_sdk::transaction::TransactionError; +use thiserror::Error; + +#[derive(Debug, Error, PartialEq, Eq, Clone)] +pub enum AddressLookupError { + /// Attempted to lookup addresses from a table that does not exist + #[error("Attempted to lookup addresses from a table that does not exist")] + LookupTableAccountNotFound, + + /// Attempted to lookup addresses from an account owned by the wrong program + #[error("Attempted to lookup addresses from an account owned by the wrong program")] + InvalidAccountOwner, + + /// Attempted to lookup addresses from an invalid account + #[error("Attempted to lookup addresses from an invalid account")] + InvalidAccountData, + + /// Address lookup contains an invalid index + #[error("Address lookup contains an invalid index")] + InvalidLookupIndex, +} + +#[cfg(not(target_arch = "bpf"))] +impl From for TransactionError { + fn from(err: AddressLookupError) -> Self { + match err { + AddressLookupError::LookupTableAccountNotFound => Self::AddressLookupTableNotFound, + AddressLookupError::InvalidAccountOwner => Self::InvalidAddressLookupTableOwner, + AddressLookupError::InvalidAccountData => Self::InvalidAddressLookupTableData, + AddressLookupError::InvalidLookupIndex => Self::InvalidAddressLookupTableIndex, + } + } +} diff --git a/programs/address-lookup-table/src/instruction.rs b/programs/address-lookup-table/src/instruction.rs index 0777f40fa..80a6ddb7a 100644 --- a/programs/address-lookup-table/src/instruction.rs +++ b/programs/address-lookup-table/src/instruction.rs @@ -1,7 +1,7 @@ use { crate::id, serde::{Deserialize, Serialize}, - solana_sdk::{ + solana_program::{ clock::Slot, instruction::{AccountMeta, Instruction}, pubkey::Pubkey, diff --git a/programs/address-lookup-table/src/lib.rs b/programs/address-lookup-table/src/lib.rs index 11433e64c..8a4ecad48 100644 --- a/programs/address-lookup-table/src/lib.rs +++ b/programs/address-lookup-table/src/lib.rs @@ -2,9 +2,11 @@ #![cfg_attr(RUSTC_WITH_SPECIALIZATION, feature(specialization))] #![cfg_attr(RUSTC_NEEDS_PROC_MACRO_HYGIENE, feature(proc_macro_hygiene))] -use solana_sdk::declare_id; +use solana_program::declare_id; +pub mod error; pub mod instruction; +#[cfg(not(target_arch = "bpf"))] pub mod processor; pub mod state; diff --git a/programs/address-lookup-table/src/state.rs b/programs/address-lookup-table/src/state.rs index 4768cd563..52a1b30e9 100644 --- a/programs/address-lookup-table/src/state.rs +++ b/programs/address-lookup-table/src/state.rs @@ -1,12 +1,12 @@ use { + crate::error::AddressLookupError, serde::{Deserialize, Serialize}, solana_frozen_abi_macro::{AbiEnumVisitor, AbiExample}, - solana_sdk::{ + solana_program::{ clock::Slot, instruction::InstructionError, pubkey::Pubkey, slot_hashes::{SlotHashes, MAX_ENTRIES}, - transaction::AddressLookupError, }, std::borrow::Cow, }; diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index be5ded95e..9f9e649b2 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -2763,6 +2763,7 @@ dependencies = [ "serde", "solana-frozen-abi 1.10.3", "solana-frozen-abi-macro 1.10.3", + "solana-program 1.10.3", "solana-program-runtime", "solana-sdk", "thiserror", @@ -2901,6 +2902,7 @@ name = "solana-bpf-rust-dep-crate" version = "1.10.3" dependencies = [ "byteorder 1.4.3", + "solana-address-lookup-table-program", "solana-program 1.10.3", ] diff --git a/programs/bpf/rust/dep_crate/Cargo.toml b/programs/bpf/rust/dep_crate/Cargo.toml index 92018baf7..bf06019e2 100644 --- a/programs/bpf/rust/dep_crate/Cargo.toml +++ b/programs/bpf/rust/dep_crate/Cargo.toml @@ -12,6 +12,8 @@ edition = "2021" [dependencies] byteorder = { version = "1", default-features = false } solana-program = { path = "../../../../sdk/program", version = "=1.10.3" } +# list of crates which must be buildable for bpf programs +solana-address-lookup-table-program = { path = "../../../../programs/address-lookup-table", version = "=1.10.3" } [lib] crate-type = ["cdylib"] diff --git a/runtime/src/accounts.rs b/runtime/src/accounts.rs index 030d7e677..663ac3ec9 100644 --- a/runtime/src/accounts.rs +++ b/runtime/src/accounts.rs @@ -23,7 +23,7 @@ use { }, log::*, rand::{thread_rng, Rng}, - solana_address_lookup_table_program::state::AddressLookupTable, + solana_address_lookup_table_program::{error::AddressLookupError, state::AddressLookupTable}, solana_sdk::{ account::{Account, AccountSharedData, ReadableAccount, WritableAccount}, account_utils::StateMut, @@ -43,10 +43,7 @@ use { slot_hashes::SlotHashes, system_program, sysvar::{self, instructions::construct_instructions_data}, - transaction::{ - AddressLookupError, Result, SanitizedTransaction, TransactionAccountLocks, - TransactionError, - }, + transaction::{Result, SanitizedTransaction, TransactionAccountLocks, TransactionError}, transaction_context::TransactionAccount, }, std::{ diff --git a/runtime/src/bank/address_lookup_table.rs b/runtime/src/bank/address_lookup_table.rs index 355399f55..3916177af 100644 --- a/runtime/src/bank/address_lookup_table.rs +++ b/runtime/src/bank/address_lookup_table.rs @@ -1,10 +1,9 @@ use { super::Bank, + solana_address_lookup_table_program::error::AddressLookupError, solana_sdk::{ message::v0::{LoadedAddresses, MessageAddressTableLookup}, - transaction::{ - AddressLoader, AddressLookupError, Result as TransactionResult, TransactionError, - }, + transaction::{AddressLoader, Result as TransactionResult, TransactionError}, }, }; diff --git a/sdk/src/transaction/error.rs b/sdk/src/transaction/error.rs index ffd699ddf..25003bb92 100644 --- a/sdk/src/transaction/error.rs +++ b/sdk/src/transaction/error.rs @@ -150,33 +150,3 @@ impl From for TransactionError { Self::SanitizeFailure } } - -#[derive(Debug, Error, PartialEq, Eq, Clone)] -pub enum AddressLookupError { - /// Attempted to lookup addresses from a table that does not exist - #[error("Attempted to lookup addresses from a table that does not exist")] - LookupTableAccountNotFound, - - /// Attempted to lookup addresses from an account owned by the wrong program - #[error("Attempted to lookup addresses from an account owned by the wrong program")] - InvalidAccountOwner, - - /// Attempted to lookup addresses from an invalid account - #[error("Attempted to lookup addresses from an invalid account")] - InvalidAccountData, - - /// Address lookup contains an invalid index - #[error("Address lookup contains an invalid index")] - InvalidLookupIndex, -} - -impl From for TransactionError { - fn from(err: AddressLookupError) -> Self { - match err { - AddressLookupError::LookupTableAccountNotFound => Self::AddressLookupTableNotFound, - AddressLookupError::InvalidAccountOwner => Self::InvalidAddressLookupTableOwner, - AddressLookupError::InvalidAccountData => Self::InvalidAddressLookupTableData, - AddressLookupError::InvalidLookupIndex => Self::InvalidAddressLookupTableIndex, - } - } -}