From bf0ad6c597ece5a821d15a7dfbe8f2f45b2c9303 Mon Sep 17 00:00:00 2001 From: Sarfaraz Nawaz Date: Mon, 26 Jan 2026 00:26:14 +0530 Subject: [PATCH] chore: upgrade pinocchio --- Cargo.lock | 162 +++++++++-- Cargo.toml | 17 +- src/args/types.rs | 2 +- src/consts.rs | 20 +- src/diff/algorithm.rs | 2 +- src/diff/types.rs | 2 +- src/entrypoint.rs | 5 +- src/error.rs | 11 +- .../close_validator_fees_vault.rs | 2 +- .../init_validator_fees_vault.rs | 2 +- .../protocol_claim_fees.rs | 2 +- .../undelegate_confined_account.rs | 2 +- .../whitelist_validator_for_program.rs | 2 +- src/lib.rs | 14 +- src/pod_view.rs | 2 +- src/processor/fast/commit_diff.rs | 8 +- src/processor/fast/commit_diff_from_buffer.rs | 12 +- src/processor/fast/commit_finalize.rs | 6 +- .../fast/commit_finalize_from_buffer.rs | 8 +- src/processor/fast/commit_state.rs | 79 ++--- .../fast/commit_state_from_buffer.rs | 12 +- src/processor/fast/delegate.rs | 71 +++-- src/processor/fast/finalize.rs | 52 ++-- .../fast/internal/commit_finalize_internal.rs | 34 +-- src/processor/fast/mod.rs | 2 +- src/processor/fast/undelegate.rs | 109 +++---- .../fast/undelegate_confined_account.rs | 34 ++- src/processor/fast/utils/pda.rs | 48 ++- src/processor/fast/utils/requires.rs | 274 ++++++++---------- src/processor/utils/curve.rs | 6 +- src/state/delegation_metadata.rs | 9 +- 31 files changed, 560 insertions(+), 451 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee7ead63..ae13c814 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1381,6 +1381,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "five8" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23f76610e969fa1784327ded240f1e28a3fd9520c9cec93b636fcf62dd37f772" +dependencies = [ + "five8_core", +] + [[package]] name = "five8_const" version = "0.1.4" @@ -1390,6 +1399,15 @@ dependencies = [ "five8_core", ] +[[package]] +name = "five8_const" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a0f1728185f277989ca573a402716ae0beaaea3f76a8ff87ef9dd8fb19436c5" +dependencies = [ + "five8_core", +] + [[package]] name = "five8_core" version = "0.1.1" @@ -2316,12 +2334,16 @@ dependencies = [ "const-crypto", "magicblock-delegation-program", "num_enum", - "pinocchio", + "pinocchio 0.10.1", + "pinocchio-associated-token-account", "pinocchio-log", "pinocchio-pubkey", "pinocchio-system", + "pinocchio-token", "rand 0.8.5", "rkyv", + "serde", + "solana-address", "solana-curve25519", "solana-program", "solana-program-test", @@ -2841,11 +2863,36 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b971851087bc3699b001954ad02389d50c41405ece3548cbcafc88b3e20017a" +[[package]] +name = "pinocchio" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfad955b2fe8f736e1ea276ebb31820d778ee69c1161146054e9b31be2e73326" +dependencies = [ + "solana-account-view", + "solana-address", + "solana-define-syscall 4.0.1", + "solana-instruction-view", + "solana-program-error 3.0.0", +] + +[[package]] +name = "pinocchio-associated-token-account" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bd92823a97fb327d7509dfd7cbfa3ead56e9fc0e131972bc0e28ab7036be31a" +dependencies = [ + "solana-account-view", + "solana-address", + "solana-instruction-view", + "solana-program-error 3.0.0", +] + [[package]] name = "pinocchio-log" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f266eb7ddac75ef32c42025d5cc571da4f8c9e6d5c3d70820c204d5fee72e57a" +checksum = "cd11022408f312e6179ece321c1f7dc0d1b2aa7765fddd39b2a7378d65a899e8" dependencies = [ "pinocchio-log-macro", ] @@ -2867,19 +2914,31 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0225638cadcbebae8932cb7f49cb5da7c15c21beb19f048f05a5ca7d93f065" dependencies = [ - "five8_const", - "pinocchio", + "five8_const 0.1.4", + "pinocchio 0.9.2", "sha2-const-stable", ] [[package]] name = "pinocchio-system" -version = "0.3.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "141ed5eafb4ab04568bb0e224e3dc9a9de13c933de4c004e0d1a553498be3a7c" +checksum = "24044a0815753862b558e179e78f03f7344cb755de48617a09d7d23b50883b6c" dependencies = [ - "pinocchio", - "pinocchio-pubkey", + "pinocchio 0.10.1", + "solana-address", +] + +[[package]] +name = "pinocchio-token" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "febf3bbe37f4e2723b9b41a1768c6542a1ae1b1d7bcac27f892f30cabcf70ec4" +dependencies = [ + "solana-account-view", + "solana-address", + "solana-instruction-view", + "solana-program-error 3.0.0", ] [[package]] @@ -3631,10 +3690,11 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.217" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "0dca6411025b24b60bfa7ec1fe1f8e710ac09782dca409ee8237ba74b51295fd" dependencies = [ + "serde_core", "serde_derive", ] @@ -3656,11 +3716,20 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_core" +version = "1.0.226" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba2ba63999edb9dac981fb34b3e5c0d111a69b0924e253ed29d83f7c99e966a4" +dependencies = [ + "serde_derive", +] + [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "8db53ae22f34573731bafa1db20f04027b2d25e02d8205921b569171699cdb33" dependencies = [ "proc-macro2", "quote", @@ -3884,11 +3953,21 @@ checksum = "e0c17d606a298a205fae325489fbed88ee6dc4463c111672172327e741c8905d" dependencies = [ "bincode", "serde", - "solana-program-error", + "solana-program-error 2.2.1", "solana-program-memory", "solana-pubkey", ] +[[package]] +name = "solana-account-view" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37ca34c37f92ee341b73d5ce7c8ef5bb38e9a87955b4bd343c63fa18b149215" +dependencies = [ + "solana-address", + "solana-program-error 3.0.0", +] + [[package]] name = "solana-accounts-db" version = "2.2.0" @@ -3938,6 +4017,24 @@ dependencies = [ "thiserror 2.0.11", ] +[[package]] +name = "solana-address" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37320fd2945c5d654b2c6210624a52d66c3f1f73b653ed211ab91a703b35bdd" +dependencies = [ + "borsh 1.5.7", + "bytemuck", + "bytemuck_derive", + "curve25519-dalek 4.1.3", + "five8", + "five8_const 1.0.0", + "serde", + "solana-define-syscall 4.0.1", + "solana-program-error 3.0.0", + "solana-sha256-hasher 3.1.0", +] + [[package]] name = "solana-address-lookup-table-interface" version = "2.2.2" @@ -4454,7 +4551,7 @@ dependencies = [ "solana-account-info", "solana-define-syscall 2.2.1", "solana-instruction", - "solana-program-error", + "solana-program-error 2.2.1", "solana-pubkey", "solana-stable-layout", ] @@ -4601,7 +4698,7 @@ dependencies = [ "solana-account", "solana-account-info", "solana-instruction", - "solana-program-error", + "solana-program-error 2.2.1", "solana-pubkey", "solana-rent", "solana-sdk-ids", @@ -4759,6 +4856,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "solana-instruction-view" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60147e4d0a4620013df40bf30a86dd299203ff12fcb8b593cd51014fce0875d8" +dependencies = [ + "solana-account-view", + "solana-address", + "solana-define-syscall 4.0.1", + "solana-program-error 3.0.0", +] + [[package]] name = "solana-instructions-sysvar" version = "2.2.1" @@ -4768,7 +4877,7 @@ dependencies = [ "bitflags 2.8.0", "solana-account-info", "solana-instruction", - "solana-program-error", + "solana-program-error 2.2.1", "solana-pubkey", "solana-sanitize", "solana-sdk-ids", @@ -5214,7 +5323,7 @@ dependencies = [ "solana-native-token", "solana-nonce", "solana-program-entrypoint", - "solana-program-error", + "solana-program-error 2.2.1", "solana-program-memory", "solana-program-option", "solana-program-pack", @@ -5248,7 +5357,7 @@ checksum = "473ffe73c68d93e9f2aa726ad2985fe52760052709aaab188100a42c618060ec" dependencies = [ "solana-account-info", "solana-msg", - "solana-program-error", + "solana-program-error 2.2.1", "solana-pubkey", ] @@ -5268,6 +5377,12 @@ dependencies = [ "solana-pubkey", ] +[[package]] +name = "solana-program-error" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1af32c995a7b692a915bb7414d5f8e838450cf7c70414e763d8abcae7b51f28" + [[package]] name = "solana-program-memory" version = "2.2.1" @@ -5290,7 +5405,7 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "319f0ef15e6e12dc37c597faccb7d62525a509fec5f6975ecb9419efddeb277b" dependencies = [ - "solana-program-error", + "solana-program-error 2.2.1", ] [[package]] @@ -5383,7 +5498,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "curve25519-dalek 4.1.3", - "five8_const", + "five8_const 0.1.4", "getrandom 0.2.15", "js-sys", "num-traits", @@ -5980,6 +6095,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db7dc3011ea4c0334aaaa7e7128cb390ecf546b28d412e9bf2064680f57f588f" dependencies = [ + "sha2 0.10.8", "solana-define-syscall 4.0.1", "solana-hash 4.0.1", ] @@ -6081,7 +6197,7 @@ dependencies = [ "solana-cpi", "solana-decode-error", "solana-instruction", - "solana-program-error", + "solana-program-error 2.2.1", "solana-pubkey", "solana-system-interface", "solana-sysvar-id", @@ -6312,7 +6428,7 @@ dependencies = [ "solana-instructions-sysvar", "solana-last-restart-slot", "solana-program-entrypoint", - "solana-program-error", + "solana-program-error 2.2.1", "solana-program-memory", "solana-pubkey", "solana-rent", diff --git a/Cargo.toml b/Cargo.toml index befcdcd7..ce1ed002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,10 +40,6 @@ borsh = { version = "1.5.3", features = [ "derive" ] } bytemuck = { version = ">=1", features = [ "derive" ] } const-crypto = { version = "0.3.0", optional = true } num_enum = "^0.7.2" -pinocchio = { version = "0.9.2", optional = true } -pinocchio-log = { version = "0.5.0", optional = true } -pinocchio-pubkey = { version = "0.3.0", optional = true } -pinocchio-system = { version = "0.3.0", optional = true } rkyv = { version = "0.7.45", optional = true } solana-curve25519 = { version = ">=2.2", optional = true } solana-program = { version = ">=1.16, <3.0.0" } @@ -53,6 +49,19 @@ static_assertions = "1.1.0" strum = { version = ">=0.27", features = ["derive"] } thiserror = { version = ">=1" } +pinocchio = { version = "0.10.1", optional = true } +pinocchio-log = { version = "0.5.1" , optional = true } +pinocchio-pubkey = { version = "0.3.0" , optional = true } +pinocchio-system = { version = "0.5.0" , optional = true } +pinocchio-token = { version = "0.5.0" , optional = true } +pinocchio-associated-token-account = { version = "0.3.0" , optional = true } + +# version must match the one imported by pinocchio +solana-address = { version = "2.0", features = ["bytemuck", "decode", "syscalls", "curve25519", "std"] } + +# manually resolves the conflict with a pinned version of serde +serde = "=1.0.226" + [dev-dependencies] assertables = "9.8.2" magicblock-delegation-program = { path = ".", features = ["unit_test_config"] } diff --git a/src/args/types.rs b/src/args/types.rs index 1ca3d360..d1b3a097 100644 --- a/src/args/types.rs +++ b/src/args/types.rs @@ -1,7 +1,7 @@ use std::ops::Deref; use bytemuck::{Pod, Zeroable}; -use pinocchio::program_error::ProgramError; +use pinocchio::error::ProgramError; use crate::{pod_view::PodView, require_ge}; diff --git a/src/consts.rs b/src/consts.rs index e45a97fb..71f48117 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -1,3 +1,4 @@ +use pinocchio::Address; use solana_program::pubkey; use solana_program::pubkey::Pubkey; @@ -33,13 +34,16 @@ pub const DEFAULT_VALIDATOR_IDENTITY: Pubkey = /// cannot be committed or delegated pub const BROADCAST_IDENTITY: Pubkey = pubkey!("Broadcast1111111111111111111111111111111111"); -pub const BPF_LOADER_UPGRADEABLE_ID: [u8; 32] = - const_crypto::bs58::decode_pubkey("BPFLoaderUpgradeab1e11111111111111111111111"); - -pub const DELEGATION_PROGRAM_DATA_ID: [u8; 32] = const_crypto::ed25519::derive_program_address( - &[&crate::ID.to_bytes()], - &BPF_LOADER_UPGRADEABLE_ID, -) -.0; +pub const BPF_LOADER_UPGRADEABLE_ID: Address = Address::new_from_array( + const_crypto::bs58::decode_pubkey("BPFLoaderUpgradeab1e11111111111111111111111"), +); + +pub const DELEGATION_PROGRAM_DATA_ID: Address = Address::new_from_array( + const_crypto::ed25519::derive_program_address( + &[crate::fast::ID.as_array()], + BPF_LOADER_UPGRADEABLE_ID.as_array(), + ) + .0, +); pub const RENT_EXCEPTION_ZERO_BYTES_LAMPORTS: u64 = 890880; diff --git a/src/diff/algorithm.rs b/src/diff/algorithm.rs index e225659f..2a9bf5be 100644 --- a/src/diff/algorithm.rs +++ b/src/diff/algorithm.rs @@ -1,6 +1,6 @@ use std::cmp::{min, Ordering}; -use pinocchio::program_error::ProgramError; +use pinocchio::error::ProgramError; use rkyv::util::AlignedVec; use crate::error::DlpError; diff --git a/src/diff/types.rs b/src/diff/types.rs index 2d219c3c..2a399ce3 100644 --- a/src/diff/types.rs +++ b/src/diff/types.rs @@ -4,7 +4,7 @@ use core::{ }; use std::{cmp::Ordering, ops::Range}; -use pinocchio::program_error::ProgramError; +use pinocchio::error::ProgramError; use static_assertions::const_assert; use crate::error::DlpError; diff --git a/src/entrypoint.rs b/src/entrypoint.rs index 135a15b5..3a6032ba 100644 --- a/src/entrypoint.rs +++ b/src/entrypoint.rs @@ -1,6 +1,5 @@ use crate::{error::DlpError, fast_process_instruction, slow_process_instruction}; -use pinocchio::program_error::ToStr; use solana_program::entrypoint; entrypoint::custom_heap_default!(); @@ -12,8 +11,8 @@ entrypoint::custom_panic_default!(); /// support fallback branch. #[no_mangle] pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 { - const UNINIT: core::mem::MaybeUninit = - core::mem::MaybeUninit::::uninit(); + const UNINIT: core::mem::MaybeUninit = + core::mem::MaybeUninit::::uninit(); let mut accounts = [UNINIT; { pinocchio::MAX_TX_ACCOUNTS }]; let (program_id, count, data) = diff --git a/src/error.rs b/src/error.rs index 3107abc6..a03eb09a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -151,18 +151,15 @@ impl From for ProgramError { } #[cfg(not(feature = "sdk"))] -impl From for pinocchio::program_error::ProgramError { +impl From for pinocchio::error::ProgramError { fn from(e: DlpError) -> Self { - pinocchio::program_error::ProgramError::Custom(e as u32) + pinocchio::error::ProgramError::Custom(e as u32) } } #[cfg(not(feature = "sdk"))] -impl pinocchio::program_error::ToStr for DlpError { - fn to_str(&self) -> &'static str - where - E: 'static + pinocchio::program_error::ToStr + TryFrom, - { +impl pinocchio::error::ToStr for DlpError { + fn to_str(&self) -> &'static str { self.into() } } diff --git a/src/instruction_builder/close_validator_fees_vault.rs b/src/instruction_builder/close_validator_fees_vault.rs index cba99a87..e6a26370 100644 --- a/src/instruction_builder/close_validator_fees_vault.rs +++ b/src/instruction_builder/close_validator_fees_vault.rs @@ -12,7 +12,7 @@ pub fn close_validator_fees_vault( validator_identity: Pubkey, ) -> Instruction { let validator_fees_vault_pda = validator_fees_vault_pda_from_validator(&validator_identity); - let delegation_program_data = Pubkey::new_from_array(DELEGATION_PROGRAM_DATA_ID); + let delegation_program_data = DELEGATION_PROGRAM_DATA_ID.to_bytes().into(); Instruction { program_id: crate::id(), accounts: vec![ diff --git a/src/instruction_builder/init_validator_fees_vault.rs b/src/instruction_builder/init_validator_fees_vault.rs index db9bc4cd..08eff09f 100644 --- a/src/instruction_builder/init_validator_fees_vault.rs +++ b/src/instruction_builder/init_validator_fees_vault.rs @@ -13,7 +13,7 @@ pub fn init_validator_fees_vault( validator_identity: Pubkey, ) -> Instruction { let validator_fees_vault_pda = validator_fees_vault_pda_from_validator(&validator_identity); - let delegation_program_data = Pubkey::new_from_array(DELEGATION_PROGRAM_DATA_ID); + let delegation_program_data = DELEGATION_PROGRAM_DATA_ID.to_bytes().into(); Instruction { program_id: crate::id(), accounts: vec![ diff --git a/src/instruction_builder/protocol_claim_fees.rs b/src/instruction_builder/protocol_claim_fees.rs index 9ce051a4..be2265e7 100644 --- a/src/instruction_builder/protocol_claim_fees.rs +++ b/src/instruction_builder/protocol_claim_fees.rs @@ -8,7 +8,7 @@ use solana_program::{instruction::AccountMeta, pubkey::Pubkey}; /// See [crate::processor::process_protocol_claim_fees] for docs. pub fn protocol_claim_fees(admin: Pubkey) -> Instruction { let fees_vault_pda = fees_vault_pda(); - let delegation_program_data = Pubkey::new_from_array(DELEGATION_PROGRAM_DATA_ID); + let delegation_program_data = DELEGATION_PROGRAM_DATA_ID.to_bytes().into(); Instruction { program_id: crate::id(), accounts: vec![ diff --git a/src/instruction_builder/undelegate_confined_account.rs b/src/instruction_builder/undelegate_confined_account.rs index 1780e421..2de9871c 100644 --- a/src/instruction_builder/undelegate_confined_account.rs +++ b/src/instruction_builder/undelegate_confined_account.rs @@ -18,7 +18,7 @@ pub fn undelegate_confined_account( let delegation_record_pda = delegation_record_pda_from_delegated_account(&delegated_account); let delegation_metadata_pda = delegation_metadata_pda_from_delegated_account(&delegated_account); - let delegation_program_data = Pubkey::new_from_array(DELEGATION_PROGRAM_DATA_ID); + let delegation_program_data = DELEGATION_PROGRAM_DATA_ID.to_bytes().into(); Instruction { program_id: crate::id(), diff --git a/src/instruction_builder/whitelist_validator_for_program.rs b/src/instruction_builder/whitelist_validator_for_program.rs index 5929a5fe..40d136fe 100644 --- a/src/instruction_builder/whitelist_validator_for_program.rs +++ b/src/instruction_builder/whitelist_validator_for_program.rs @@ -21,7 +21,7 @@ pub fn whitelist_validator_for_program( let args = WhitelistValidatorForProgramArgs { insert }; let program_data = Pubkey::find_program_address(&[program.as_ref()], &bpf_loader_upgradeable::id()).0; - let delegation_program_data = Pubkey::new_from_array(DELEGATION_PROGRAM_DATA_ID); + let delegation_program_data = DELEGATION_PROGRAM_DATA_ID.to_bytes().into(); let program_config_pda = program_config_from_program_id(&program); Instruction { program_id: crate::id(), diff --git a/src/lib.rs b/src/lib.rs index 7e6cf1fd..51481677 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,7 +59,7 @@ declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"); #[cfg(not(feature = "sdk"))] pub mod fast { - pinocchio_pubkey::declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"); + pinocchio::address::declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"); } #[cfg(feature = "solana-security-txt")] @@ -74,14 +74,12 @@ solana_security_txt::security_txt! { #[cfg(not(feature = "sdk"))] pub fn fast_process_instruction( - program_id: &pinocchio::pubkey::Pubkey, - accounts: &[pinocchio::account_info::AccountInfo], + program_id: &pinocchio::Address, + accounts: &[pinocchio::AccountView], data: &[u8], ) -> Option { if data.len() < 8 { - return Some(Err( - pinocchio::program_error::ProgramError::InvalidInstructionData, - )); + return Some(Err(pinocchio::error::ProgramError::InvalidInstructionData)); } let (discriminator_bytes, data) = data.split_at(8); @@ -90,9 +88,7 @@ pub fn fast_process_instruction( Ok(discriminator) => discriminator, Err(_) => { pinocchio_log::log!("Failed to read and parse discriminator"); - return Some(Err( - pinocchio::program_error::ProgramError::InvalidInstructionData, - )); + return Some(Err(pinocchio::error::ProgramError::InvalidInstructionData)); } }; diff --git a/src/pod_view.rs b/src/pod_view.rs index 9026925a..351a7034 100644 --- a/src/pod_view.rs +++ b/src/pod_view.rs @@ -1,4 +1,4 @@ -use pinocchio::program_error::ProgramError; +use pinocchio::error::ProgramError; /// SAFETY /// diff --git a/src/processor/fast/commit_diff.rs b/src/processor/fast/commit_diff.rs index d22fe639..378fb6c4 100644 --- a/src/processor/fast/commit_diff.rs +++ b/src/processor/fast/commit_diff.rs @@ -1,7 +1,5 @@ use borsh::BorshDeserialize; -use pinocchio::{ - account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult, -}; +use pinocchio::{error::ProgramError, AccountView, Address, ProgramResult}; use pinocchio_log::log; use crate::args::{CommitDiffArgsWithoutDiff, SIZE_COMMIT_DIFF_ARGS_WITHOUT_DIFF}; @@ -45,8 +43,8 @@ use crate::{require, require_n_accounts}; /// 3. Copy the new state to the new PDA /// 4. Init a new PDA to store the record of the new state commitment pub fn process_commit_diff( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let [ diff --git a/src/processor/fast/commit_diff_from_buffer.rs b/src/processor/fast/commit_diff_from_buffer.rs index 8c3442ff..bf3995cb 100644 --- a/src/processor/fast/commit_diff_from_buffer.rs +++ b/src/processor/fast/commit_diff_from_buffer.rs @@ -3,17 +3,17 @@ use crate::processor::fast::{process_commit_state_internal, CommitStateInternalA use crate::DiffSet; use borsh::BorshDeserialize; -use pinocchio::account_info::AccountInfo; -use pinocchio::program_error::ProgramError; -use pinocchio::pubkey::Pubkey; +use pinocchio::error::ProgramError; +use pinocchio::AccountView; +use pinocchio::Address; use pinocchio::ProgramResult; use pinocchio_log::log; use super::NewState; pub fn process_commit_diff_from_buffer( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let [validator, delegated_account, commit_state_account, commit_record_account, delegation_record_account, delegation_metadata_account, diff_buffer_account, validator_fees_vault, program_config_account, _system_program] = @@ -29,7 +29,7 @@ pub fn process_commit_diff_from_buffer( let commit_record_nonce = args.nonce; let allow_undelegation = args.allow_undelegation; - let diff = diff_buffer_account.try_borrow_data()?; + let diff = diff_buffer_account.try_borrow()?; let diffset = DiffSet::try_new(diff.as_ref())?; diff --git a/src/processor/fast/commit_finalize.rs b/src/processor/fast/commit_finalize.rs index c4bbfead..ce197d52 100644 --- a/src/processor/fast/commit_finalize.rs +++ b/src/processor/fast/commit_finalize.rs @@ -1,4 +1,4 @@ -use pinocchio::{account_info::AccountInfo, pubkey::Pubkey, ProgramResult}; +use pinocchio::{AccountView, Address, ProgramResult}; use pinocchio_log::log; use crate::args::CommitFinalizeArgsWithBuffer; @@ -24,8 +24,8 @@ use crate::{require_n_accounts, DiffSet}; /// Instruction Data: CommitFinalizeArgsWithBuffer /// pub fn process_commit_finalize( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let [ diff --git a/src/processor/fast/commit_finalize_from_buffer.rs b/src/processor/fast/commit_finalize_from_buffer.rs index 50738c72..e435a617 100644 --- a/src/processor/fast/commit_finalize_from_buffer.rs +++ b/src/processor/fast/commit_finalize_from_buffer.rs @@ -1,4 +1,4 @@ -use pinocchio::{account_info::AccountInfo, pubkey::Pubkey, ProgramResult}; +use pinocchio::{AccountView, Address, ProgramResult}; use pinocchio_log::log; use crate::args::CommitFinalizeArgs; @@ -40,8 +40,8 @@ use crate::{require_n_accounts, DiffSet}; /// 3. Copy the new state to the new PDA /// 4. Init a new PDA to store the record of the new state commitment pub fn process_commit_finalize_from_buffer( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let [ @@ -56,7 +56,7 @@ pub fn process_commit_finalize_from_buffer( let args = CommitFinalizeArgs::try_view_from(data)?; - let data = data_account.try_borrow_data()?; + let data = data_account.try_borrow()?; let commit_args = CommitFinalizeInternalArgs { bumps: &args.bumps, new_state: if args.data_is_diff.is_true() { diff --git a/src/processor/fast/commit_state.rs b/src/processor/fast/commit_state.rs index e060b5a7..8c08cb84 100644 --- a/src/processor/fast/commit_state.rs +++ b/src/processor/fast/commit_state.rs @@ -1,10 +1,8 @@ use borsh::BorshDeserialize; -use pinocchio::instruction::Signer; -use pinocchio::pubkey::{self, pubkey_eq}; -use pinocchio::seeds; -use pinocchio::{ - account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult, -}; +use pinocchio::address::address_eq; +use pinocchio::cpi::Signer; +use pinocchio::instruction::seeds; +use pinocchio::{error::ProgramError, AccountView, Address, ProgramResult}; use pinocchio_log::log; use pinocchio_system::instructions as system; @@ -53,8 +51,8 @@ use super::to_pinocchio_program_error; /// 3. Copy the new state to the new PDA /// 4. Init a new PDA to store the record of the new state commitment pub fn process_commit_state( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let args = CommitStateArgs::try_from_slice(data).map_err(|_| ProgramError::BorshIoError)?; @@ -107,14 +105,14 @@ pub(crate) struct CommitStateInternalArgs<'a> { pub(crate) commit_record_lamports: u64, pub(crate) commit_record_nonce: u64, pub(crate) allow_undelegation: bool, - pub(crate) validator: &'a AccountInfo, - pub(crate) delegated_account: &'a AccountInfo, - pub(crate) commit_state_account: &'a AccountInfo, - pub(crate) commit_record_account: &'a AccountInfo, - pub(crate) delegation_record_account: &'a AccountInfo, - pub(crate) delegation_metadata_account: &'a AccountInfo, - pub(crate) validator_fees_vault: &'a AccountInfo, - pub(crate) program_config_account: &'a AccountInfo, + pub(crate) validator: &'a AccountView, + pub(crate) delegated_account: &'a AccountView, + pub(crate) commit_state_account: &'a AccountView, + pub(crate) commit_record_account: &'a AccountView, + pub(crate) delegation_record_account: &'a AccountView, + pub(crate) delegation_metadata_account: &'a AccountView, + pub(crate) validator_fees_vault: &'a AccountView, + pub(crate) program_config_account: &'a AccountView, } /// Commit a new state of a delegated Pda @@ -141,7 +139,7 @@ pub(crate) fn process_commit_state_internal( require_initialized_validator_fees_vault(args.validator, args.validator_fees_vault, false)?; // Read delegation metadata - let mut delegation_metadata_data = args.delegation_metadata_account.try_borrow_mut_data()?; + let mut delegation_metadata_data = args.delegation_metadata_account.try_borrow_mut()?; let mut delegation_metadata = DelegationMetadata::try_from_bytes_with_discriminator(&delegation_metadata_data) .map_err(to_pinocchio_program_error)?; @@ -159,7 +157,7 @@ pub(crate) fn process_commit_state_internal( // Once the account is marked as undelegatable, any subsequent commit should fail if delegation_metadata.is_undelegatable { log!("delegation metadata is already undelegated: "); - pubkey::log(args.delegation_metadata_account.key()); + args.delegation_metadata_account.address().log(); return Err(DlpError::AlreadyUndelegated.into()); } @@ -170,17 +168,20 @@ pub(crate) fn process_commit_state_internal( .map_err(to_pinocchio_program_error)?; // Load delegation record - let delegation_record_data = args.delegation_record_account.try_borrow_data()?; + let delegation_record_data = args.delegation_record_account.try_borrow()?; let delegation_record = DelegationRecord::try_from_bytes_with_discriminator(&delegation_record_data) .map_err(to_pinocchio_program_error)?; // Check that the authority is allowed to commit - if !pubkey_eq(delegation_record.authority.as_array(), args.validator.key()) { + if !address_eq( + &delegation_record.authority.to_bytes().into(), + args.validator.address(), + ) { log!("validator is not the delegation authority. validator: "); - pubkey::log(args.validator.key()); + args.validator.address().log(); log!("delegation authority: "); - pubkey::log(delegation_record.authority.as_array()); + Address::from(delegation_record.authority.to_bytes()).log(); return Err(DlpError::InvalidAuthority.into()); } @@ -188,7 +189,7 @@ pub(crate) fn process_commit_state_internal( if args.delegated_account.lamports() < delegation_record.lamports { log!( "delegated account has less lamports than the delegation record indicates. delegation account: "); - pubkey::log(args.delegated_account.key()); + args.delegated_account.address().log(); return Err(DlpError::InvalidDelegatedState.into()); } @@ -208,20 +209,20 @@ pub(crate) fn process_commit_state_internal( // Load the program configuration and validate it, if any let has_program_config = require_program_config( args.program_config_account, - delegation_record.owner.as_array(), + &Address::from(delegation_record.owner.to_bytes()), false, )?; if has_program_config { - let program_config_data = args.program_config_account.try_borrow_data()?; + let program_config_data = args.program_config_account.try_borrow()?; let program_config = ProgramConfig::try_from_bytes_with_discriminator(&program_config_data) .map_err(to_pinocchio_program_error)?; if !program_config .approved_validators - .contains(&(*args.validator.key()).into()) + .contains(&args.validator.address().to_bytes().into()) { log!("validator is not whitelisted in the program config: "); - pubkey::log(args.validator.key()); + args.validator.address().log(); return Err(DlpError::InvalidWhitelistProgramConfig.into()); } } @@ -229,14 +230,20 @@ pub(crate) fn process_commit_state_internal( // Load the uninitialized PDAs let commit_state_bump = require_uninitialized_pda( args.commit_state_account, - &[pda::COMMIT_STATE_TAG, args.delegated_account.key()], + &[ + pda::COMMIT_STATE_TAG, + args.delegated_account.address().as_ref(), + ], &crate::fast::ID, true, CommitStateAccountCtx, )?; let commit_record_bump = require_uninitialized_pda( args.commit_record_account, - &[pda::COMMIT_RECORD_TAG, args.delegated_account.key()], + &[ + pda::COMMIT_RECORD_TAG, + args.delegated_account.address().as_ref(), + ], &crate::fast::ID, true, CommitRecordCtx, @@ -249,7 +256,7 @@ pub(crate) fn process_commit_state_internal( args.commit_state_bytes.data_len(), &[Signer::from(&seeds!( pda::COMMIT_STATE_TAG, - args.delegated_account.key(), + args.delegated_account.address().as_ref(), &[commit_state_bump] ))], args.validator, @@ -262,7 +269,7 @@ pub(crate) fn process_commit_state_internal( CommitRecord::size_with_discriminator(), &[Signer::from(&seeds!( pda::COMMIT_RECORD_TAG, - args.delegated_account.key(), + args.delegated_account.address().as_ref(), &[commit_record_bump] ))], args.validator, @@ -270,23 +277,23 @@ pub(crate) fn process_commit_state_internal( // Initialize the commit record let commit_record = CommitRecord { - identity: (*args.validator.key()).into(), - account: (*args.delegated_account.key()).into(), + identity: args.validator.address().to_bytes().into(), + account: args.delegated_account.address().to_bytes().into(), nonce: args.commit_record_nonce, lamports: args.commit_record_lamports, }; - let mut commit_record_data = args.commit_record_account.try_borrow_mut_data()?; + let mut commit_record_data = args.commit_record_account.try_borrow_mut()?; commit_record .to_bytes_with_discriminator(&mut commit_record_data) .map_err(to_pinocchio_program_error)?; // Copy the new state to the initialized PDA - let mut commit_state_data = args.commit_state_account.try_borrow_mut_data()?; + let mut commit_state_data = args.commit_state_account.try_borrow_mut()?; match args.commit_state_bytes { NewState::FullBytes(bytes) => (*commit_state_data).copy_from_slice(bytes), NewState::Diff(diff) => { - let original_data = args.delegated_account.try_borrow_data()?; + let original_data = args.delegated_account.try_borrow()?; merge_diff_copy(&mut commit_state_data, &original_data, &diff)?; } } diff --git a/src/processor/fast/commit_state_from_buffer.rs b/src/processor/fast/commit_state_from_buffer.rs index 167b2227..a14a2c7e 100644 --- a/src/processor/fast/commit_state_from_buffer.rs +++ b/src/processor/fast/commit_state_from_buffer.rs @@ -2,16 +2,16 @@ use crate::args::CommitStateFromBufferArgs; use crate::processor::fast::{process_commit_state_internal, CommitStateInternalArgs}; use borsh::BorshDeserialize; -use pinocchio::account_info::AccountInfo; -use pinocchio::program_error::ProgramError; -use pinocchio::pubkey::Pubkey; +use pinocchio::error::ProgramError; +use pinocchio::AccountView; +use pinocchio::Address; use pinocchio::ProgramResult; use super::NewState; pub fn process_commit_state_from_buffer( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { let [validator, delegated_account, commit_state_account, commit_record_account, delegation_record_account, delegation_metadata_account, state_buffer_account, validator_fees_vault, program_config_account, _system_program] = @@ -27,7 +27,7 @@ pub fn process_commit_state_from_buffer( let commit_record_nonce = args.nonce; let allow_undelegation = args.allow_undelegation; - let state = state_buffer_account.try_borrow_data()?; + let state = state_buffer_account.try_borrow()?; let commit_args = CommitStateInternalArgs { commit_state_bytes: NewState::FullBytes(&state), diff --git a/src/processor/fast/delegate.rs b/src/processor/fast/delegate.rs index 40455f60..b8225d48 100644 --- a/src/processor/fast/delegate.rs +++ b/src/processor/fast/delegate.rs @@ -1,11 +1,9 @@ use borsh::BorshDeserialize; -use pinocchio::instruction::{Seed, Signer}; -use pinocchio::pubkey::{self, pubkey_eq}; +use pinocchio::address::address_eq; +use pinocchio::cpi::{Seed, Signer}; use pinocchio::sysvars::clock::Clock; use pinocchio::sysvars::Sysvar; -use pinocchio::{ - account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey, ProgramResult, -}; +use pinocchio::{error::ProgramError, AccountView, Address, ProgramResult}; use pinocchio_log::log; use pinocchio_system::instructions as system; @@ -51,8 +49,8 @@ use crate::processor::fast::utils::requires::{ /// This instruction is meant to be called via CPI with the owning program signing for the /// delegated account. pub fn process_delegate( - program_id: &Pubkey, - accounts: &[AccountInfo], + program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { process_delegate_inner(program_id, accounts, data, false) @@ -60,16 +58,16 @@ pub fn process_delegate( /// Delegates an account while allowing any validator identity. pub fn process_delegate_with_any_validator( - program_id: &Pubkey, - accounts: &[AccountInfo], + program_id: &Address, + accounts: &[AccountView], data: &[u8], ) -> ProgramResult { process_delegate_inner(program_id, accounts, data, true) } fn process_delegate_inner( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], data: &[u8], allow_system_program_validator: bool, ) -> ProgramResult { @@ -88,8 +86,11 @@ fn process_delegate_inner( // Check that the buffer PDA is initialized and derived correctly from the PDA require_pda( delegate_buffer_account, - &[pda::DELEGATE_BUFFER_TAG, delegated_account.key()], - owner_program.key(), + &[ + pda::DELEGATE_BUFFER_TAG, + delegated_account.address().as_ref(), + ], + owner_program.address(), true, "delegate buffer", )?; @@ -99,7 +100,10 @@ fn process_delegate_inner( // Could save considerable CU, especially in the v2 version where we will pass the bumps let delegation_record_bump = require_uninitialized_pda( delegation_record_account, - &[pda::DELEGATION_RECORD_TAG, delegated_account.key()], + &[ + pda::DELEGATION_RECORD_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, true, DelegationRecordCtx, @@ -110,7 +114,10 @@ fn process_delegate_inner( // Could save considerable CU, especially in the v2 version where we will pass the bumps let delegation_metadata_bump = require_uninitialized_pda( delegation_metadata_account, - &[pda::DELEGATION_METADATA_TAG, delegated_account.key()], + &[ + pda::DELEGATION_METADATA_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, true, DelegationMetadataCtx, @@ -120,7 +127,7 @@ fn process_delegate_inner( DelegateArgs::try_from_slice(data).map_err(|_| ProgramError::InvalidInstructionData)?; if !allow_system_program_validator { if let Some(validator) = args.validator { - if validator.to_bytes() == pinocchio_system::ID { + if validator.to_bytes() == pinocchio_system::ID.to_bytes() { return Err(DlpError::DelegationToSystemProgramNotAllowed.into()); } } @@ -129,11 +136,11 @@ fn process_delegate_inner( // Validate seeds if the delegate account is not on curve, i.e. is a PDA // If the owner is the system program, we check if the account is derived from the delegation program, // allowing delegation of escrow accounts - if !is_on_curve_fast(delegated_account.key()) { - let program_id = if pubkey_eq(owner_program.key(), &pinocchio_system::ID) { + if !is_on_curve_fast(delegated_account.address()) { + let program_id = if address_eq(owner_program.address(), &pinocchio_system::ID) { &crate::fast::ID } else { - owner_program.key() + owner_program.address() }; let seeds_to_validate: &[&[u8]] = match args.seeds.len() { 1 => &[&args.seeds[0]], @@ -181,13 +188,13 @@ fn process_delegate_inner( ], _ => return Err(DlpError::TooManySeeds.into()), }; - let derived_pda = pubkey::find_program_address(seeds_to_validate, program_id).0; + let derived_pda = Address::find_program_address(seeds_to_validate, program_id).0; - if !pubkey_eq(&derived_pda, delegated_account.key()) { + if !address_eq(&derived_pda, delegated_account.address()) { log!("Expected delegated PDA to be: "); - pubkey::log(&derived_pda); + derived_pda.log(); log!("but got: "); - pubkey::log(delegated_account.key()); + delegated_account.address().log(); return Err(ProgramError::InvalidSeeds); } } @@ -198,7 +205,7 @@ fn process_delegate_inner( DelegationRecord::size_with_discriminator(), &[Signer::from(&[ Seed::from(pda::DELEGATION_RECORD_TAG), - Seed::from(delegated_account.key()), + Seed::from(delegated_account.address().as_ref()), Seed::from(&[delegation_record_bump]), ])], payer, @@ -206,14 +213,14 @@ fn process_delegate_inner( // Initialize the delegation record let delegation_record = DelegationRecord { - owner: (*owner_program.key()).into(), + owner: owner_program.address().to_bytes().into(), authority: args.validator.unwrap_or(DEFAULT_VALIDATOR_IDENTITY), commit_frequency_ms: args.commit_frequency_ms as u64, delegation_slot: Clock::get()?.slot, lamports: delegated_account.lamports(), }; - let mut delegation_record_data = delegation_record_account.try_borrow_mut_data()?; + let mut delegation_record_data = delegation_record_account.try_borrow_mut()?; delegation_record .to_bytes_with_discriminator(&mut delegation_record_data) .map_err(to_pinocchio_program_error)?; @@ -222,7 +229,7 @@ fn process_delegate_inner( seeds: args.seeds, last_update_nonce: 0, is_undelegatable: false, - rent_payer: (*payer.key()).into(), + rent_payer: payer.address().to_bytes().into(), }; // Initialize the delegation metadata PDA @@ -232,22 +239,22 @@ fn process_delegate_inner( delegation_metadata.serialized_size(), &[Signer::from(&[ Seed::from(pda::DELEGATION_METADATA_TAG), - Seed::from(delegated_account.key()), + Seed::from(delegated_account.address().as_ref()), Seed::from(&[delegation_metadata_bump]), ])], payer, )?; // Copy the seeds to the delegated metadata PDA - let mut delegation_metadata_data = delegation_metadata_account.try_borrow_mut_data()?; + let mut delegation_metadata_data = delegation_metadata_account.try_borrow_mut()?; delegation_metadata .to_bytes_with_discriminator(&mut delegation_metadata_data.as_mut()) .map_err(to_pinocchio_program_error)?; // Copy the data from the buffer into the original account - if !delegate_buffer_account.data_is_empty() { - let mut delegated_data = delegated_account.try_borrow_mut_data()?; - let delegate_buffer_data = delegate_buffer_account.try_borrow_data()?; + if !delegate_buffer_account.is_data_empty() { + let mut delegated_data = delegated_account.try_borrow_mut()?; + let delegate_buffer_data = delegate_buffer_account.try_borrow()?; (*delegated_data).copy_from_slice(&delegate_buffer_data); } diff --git a/src/processor/fast/finalize.rs b/src/processor/fast/finalize.rs index e3c0cb31..91645a53 100644 --- a/src/processor/fast/finalize.rs +++ b/src/processor/fast/finalize.rs @@ -1,6 +1,6 @@ -use pinocchio::account_info::AccountInfo; -use pinocchio::program_error::ProgramError; -use pinocchio::pubkey::{pubkey_eq, Pubkey}; +use pinocchio::address::{address_eq, Address}; +use pinocchio::error::ProgramError; +use pinocchio::AccountView; use pinocchio::ProgramResult; use pinocchio_log::log; @@ -49,8 +49,8 @@ use super::to_pinocchio_program_error; /// 3. Close the state diff account /// 4. Close the commit state record pub fn process_finalize( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], _data: &[u8], ) -> ProgramResult { let [validator, delegated_account, commit_state_account, commit_record_account, delegation_record_account, delegation_metadata_account, validator_fees_vault, _system_program] = @@ -86,26 +86,32 @@ pub fn process_finalize( require_cr?; // Load delegation metadata - let mut delegation_metadata_data = delegation_metadata_account.try_borrow_mut_data()?; + let mut delegation_metadata_data = delegation_metadata_account.try_borrow_mut()?; let mut delegation_metadata = DelegationMetadata::try_from_bytes_with_discriminator(&delegation_metadata_data) .map_err(to_pinocchio_program_error)?; - let mut delegation_record_data = delegation_record_account.try_borrow_mut_data()?; + let mut delegation_record_data = delegation_record_account.try_borrow_mut()?; let delegation_record = DelegationRecord::try_from_bytes_with_discriminator_mut(&mut delegation_record_data) .map_err(to_pinocchio_program_error)?; // Load commit record - let commit_record_data = commit_record_account.try_borrow_data()?; + let commit_record_data = commit_record_account.try_borrow()?; let commit_record = CommitRecord::try_from_bytes_with_discriminator(&commit_record_data) .map_err(to_pinocchio_program_error)?; // Check that the commit record is the right one - if !pubkey_eq(commit_record.account.as_array(), delegated_account.key()) { + if !address_eq( + &commit_record.account.to_bytes().into(), + delegated_account.address(), + ) { return Err(DlpError::InvalidDelegatedAccount.into()); } - if !pubkey_eq(commit_record.identity.as_array(), validator.key()) { + if !address_eq( + &commit_record.identity.to_bytes().into(), + validator.address(), + ) { return Err(DlpError::InvalidReimbursementAccount.into()); } @@ -128,12 +134,12 @@ pub fn process_finalize( delegation_record.lamports = delegated_account.lamports(); // Load commit state - let commit_state_data = commit_state_account.try_borrow_data()?; + let commit_state_data = commit_state_account.try_borrow()?; // Copying the new commit state to the delegated account delegated_account.resize(commit_state_data.len())?; - let mut delegated_account_data = delegated_account.try_borrow_mut_data()?; + let mut delegated_account_data = delegated_account.try_borrow_mut()?; (*delegated_account_data).copy_from_slice(&commit_state_data); // Drop remaining reference before closing accounts @@ -149,9 +155,9 @@ pub fn process_finalize( /// Settle the committed lamports to the delegated account fn settle_lamports_balance( - delegated_account: &AccountInfo, - commit_state_account: &AccountInfo, - validator_fees_vault: &AccountInfo, + delegated_account: &AccountView, + commit_state_account: &AccountView, + validator_fees_vault: &AccountView, delegation_record_lamports: u64, commit_record_lamports: u64, ) -> Result<(), ProgramError> { @@ -174,14 +180,14 @@ fn settle_lamports_balance( std::cmp::Ordering::Equal => return Ok(()), }; - *transfer_source.try_borrow_mut_lamports()? = transfer_source - .lamports() - .checked_sub(transfer_lamports) - .ok_or(DlpError::Overflow)?; - *transfer_destination.try_borrow_mut_lamports()? = transfer_destination - .lamports() - .checked_add(transfer_lamports) - .ok_or(DlpError::Overflow)?; + transfer_source.set_lamports( + transfer_source + .lamports() + .checked_sub(transfer_lamports) + .ok_or(DlpError::Overflow)?, + ); + + transfer_destination.set_lamports(transfer_destination.lamports() + transfer_lamports); Ok(()) } diff --git a/src/processor/fast/internal/commit_finalize_internal.rs b/src/processor/fast/internal/commit_finalize_internal.rs index 12d9ef76..9acb84fc 100644 --- a/src/processor/fast/internal/commit_finalize_internal.rs +++ b/src/processor/fast/internal/commit_finalize_internal.rs @@ -1,5 +1,5 @@ -use pinocchio::pubkey::{self, pubkey_eq, PDA_MARKER}; -use pinocchio::{account_info::AccountInfo, program_error::ProgramError}; +use pinocchio::address::{address_eq, PDA_MARKER}; +use pinocchio::{error::ProgramError, AccountView, Address}; use pinocchio_log::log; use crate::args::CommitBumps; @@ -18,11 +18,11 @@ pub(crate) struct CommitFinalizeInternalArgs<'a> { pub(crate) new_state: NewState<'a>, pub(crate) commit_id: u64, pub(crate) allow_undelegation: bool, - pub(crate) validator: &'a AccountInfo, - pub(crate) delegated_account: &'a AccountInfo, - pub(crate) delegation_record_account: &'a AccountInfo, - pub(crate) delegation_metadata_account: &'a AccountInfo, - pub(crate) validator_fees_vault: &'a AccountInfo, + pub(crate) validator: &'a AccountView, + pub(crate) delegated_account: &'a AccountView, + pub(crate) delegation_record_account: &'a AccountView, + pub(crate) delegation_metadata_account: &'a AccountView, + pub(crate) validator_fees_vault: &'a AccountView, } /// Commit a new state of a delegated Pda @@ -38,9 +38,9 @@ pub(crate) fn process_commit_finalize_internal( args.delegation_record_account, &[ pda::DELEGATION_RECORD_TAG, - args.delegated_account.key(), + args.delegated_account.address().as_ref(), &[args.bumps.delegation_record], - &crate::fast::ID, + crate::fast::ID.as_ref(), PDA_MARKER ], false @@ -50,9 +50,9 @@ pub(crate) fn process_commit_finalize_internal( args.delegation_metadata_account, &[ pda::DELEGATION_METADATA_TAG, - args.delegated_account.key(), + args.delegated_account.address().as_ref(), &[args.bumps.delegation_metadata], - &crate::fast::ID, + crate::fast::ID.as_ref(), PDA_MARKER ], true @@ -62,9 +62,9 @@ pub(crate) fn process_commit_finalize_internal( args.validator_fees_vault, &[ pda::VALIDATOR_FEES_VAULT_TAG, - args.validator.key(), + args.validator.address().as_ref(), &[args.bumps.validator_fees_vault], - &crate::fast::ID, + crate::fast::ID.as_ref(), PDA_MARKER ], false @@ -84,13 +84,13 @@ pub(crate) fn process_commit_finalize_internal( ); } - let delegation_record_data = args.delegation_record_account.try_borrow_data()?; + let delegation_record_data = args.delegation_record_account.try_borrow()?; let delegation_record = DelegationRecord::try_view_from(&delegation_record_data.as_ref()[8..])?; // Check that the authority is allowed to commit require_eq_keys!( - delegation_record.authority.as_array(), - args.validator.key(), + &Address::from(delegation_record.authority.to_bytes()), + args.validator.address(), DlpError::InvalidAuthority ); @@ -113,7 +113,7 @@ pub(crate) fn process_commit_finalize_internal( args.delegated_account.resize(args.new_state.data_len())?; // copy the new state to the delegated account - let mut delegated_account_data = args.delegated_account.try_borrow_mut_data()?; + let mut delegated_account_data = args.delegated_account.try_borrow_mut()?; match args.new_state { NewState::FullBytes(bytes) => (*delegated_account_data).copy_from_slice(bytes), NewState::Diff(diff) => { diff --git a/src/processor/fast/mod.rs b/src/processor/fast/mod.rs index 02be473c..8413fdcb 100644 --- a/src/processor/fast/mod.rs +++ b/src/processor/fast/mod.rs @@ -25,6 +25,6 @@ pub use undelegate_confined_account::*; pub fn to_pinocchio_program_error( error: solana_program::program_error::ProgramError, -) -> pinocchio::program_error::ProgramError { +) -> pinocchio::error::ProgramError { u64::from(error).into() } diff --git a/src/processor/fast/undelegate.rs b/src/processor/fast/undelegate.rs index f0808b36..536799b9 100644 --- a/src/processor/fast/undelegate.rs +++ b/src/processor/fast/undelegate.rs @@ -9,15 +9,14 @@ use crate::processor::fast::utils::{ }; use crate::state::{DelegationMetadata, DelegationRecord}; use pinocchio::{ - account_info::AccountInfo, + address::{address_eq, Address}, cpi::invoke_signed, - instruction::{AccountMeta, Instruction, Signer}, - program_error::ProgramError, - pubkey::{pubkey_eq, Pubkey}, + cpi::Signer, + error::ProgramError, + instruction::{seeds, InstructionAccount, InstructionView}, sysvars::{rent::Rent, Sysvar}, - ProgramResult, + AccountView, ProgramResult, }; -use pinocchio::{pubkey, seeds}; use pinocchio_log::log; use pinocchio_system::instructions as system; @@ -76,8 +75,8 @@ use super::{ /// - Verify that the new state is the same as the committed state /// - Close the undelegation buffer PDA pub fn process_undelegate( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], _data: &[u8], ) -> ProgramResult { let [validator, delegated_account, owner_program, undelegate_buffer_account, commit_state_account, commit_record_account, delegation_record_account, delegation_metadata_account, rent_reimbursement, fees_vault, validator_fees_vault, system_program] = @@ -97,36 +96,39 @@ pub fn process_undelegate( // Make sure there is no pending commits to be finalized before this call require_uninitialized_pda( commit_state_account, - &[pda::COMMIT_STATE_TAG, delegated_account.key()], + &[pda::COMMIT_STATE_TAG, delegated_account.address().as_ref()], &crate::fast::ID, false, CommitStateAccountCtx, )?; require_uninitialized_pda( commit_record_account, - &[pda::COMMIT_RECORD_TAG, delegated_account.key()], + &[pda::COMMIT_RECORD_TAG, delegated_account.address().as_ref()], &crate::fast::ID, false, CommitRecordCtx, )?; // Load delegation record - let delegation_record_data = delegation_record_account.try_borrow_data()?; + let delegation_record_data = delegation_record_account.try_borrow()?; let delegation_record = DelegationRecord::try_from_bytes_with_discriminator(&delegation_record_data) .map_err(to_pinocchio_program_error)?; // Check passed owner and owner stored in the delegation record match - if !pubkey_eq(delegation_record.owner.as_array(), owner_program.key()) { + if !address_eq( + &delegation_record.owner.to_bytes().into(), + owner_program.address(), + ) { log!("Expected delegation record owner to be : "); - pubkey::log(delegation_record.owner.as_array()); + Address::from(delegation_record.owner.to_bytes()).log(); log!("but got : "); - pubkey::log(owner_program.key()); + owner_program.address().log(); return Err(ProgramError::InvalidAccountOwner); } // Load delegated account metadata - let delegation_metadata_data = delegation_metadata_account.try_borrow_data()?; + let delegation_metadata_data = delegation_metadata_account.try_borrow()?; let delegation_metadata = DelegationMetadata::try_from_bytes_with_discriminator(&delegation_metadata_data) .map_err(to_pinocchio_program_error)?; @@ -135,19 +137,19 @@ pub fn process_undelegate( // Check if the delegated account is undelegatable if !delegation_metadata.is_undelegatable { log!("delegation metadata indicates the account is not undelegatable : "); - pubkey::log(delegation_metadata_account.key()); + delegation_metadata_account.address().log(); return Err(DlpError::NotUndelegatable.into()); } // Check if the rent payer is correct - if !pubkey_eq( - delegation_metadata.rent_payer.as_array(), - rent_reimbursement.key(), + if !address_eq( + &delegation_metadata.rent_payer.to_bytes().into(), + rent_reimbursement.address(), ) { log!("Expected rent payer to be : "); - pubkey::log(delegation_metadata.rent_payer.as_array()); + Address::from(delegation_metadata.rent_payer.to_bytes()).log(); log!("but got : "); - pubkey::log(rent_reimbursement.key()); + rent_reimbursement.address().log(); return Err(DlpError::InvalidReimbursementAddressForDelegationRent.into()); } @@ -156,10 +158,10 @@ pub fn process_undelegate( drop(delegation_metadata_data); // If there is no data, we can just assign the owner back and we're done - if delegated_account.data_is_empty() { + if delegated_account.is_data_empty() { // TODO - we could also do this fast-path if the data was non-empty but zeroed-out unsafe { - delegated_account.assign(owner_program.key()); + delegated_account.assign(owner_program.address()); } process_delegation_cleanup( delegation_record_account, @@ -175,7 +177,10 @@ pub fn process_undelegate( // Initialize the undelegation buffer PDA let undelegate_buffer_bump: u8 = require_uninitialized_pda( undelegate_buffer_account, - &[pda::UNDELEGATE_BUFFER_TAG, delegated_account.key()], + &[ + pda::UNDELEGATE_BUFFER_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, true, UndelegateBufferCtx, @@ -187,15 +192,15 @@ pub fn process_undelegate( delegated_account.data_len(), &[Signer::from(&seeds!( pda::UNDELEGATE_BUFFER_TAG, - delegated_account.key(), + delegated_account.address().as_ref(), &[undelegate_buffer_bump] ))], validator, )?; // Copy data in the undelegation buffer PDA - (*undelegate_buffer_account.try_borrow_mut_data()?) - .copy_from_slice(&delegated_account.try_borrow_data()?); + (*undelegate_buffer_account.try_borrow_mut()?) + .copy_from_slice(&delegated_account.try_borrow()?); // Call a CPI to the owner program to give it back the new state process_undelegation_with_cpi( @@ -205,7 +210,7 @@ pub fn process_undelegate( undelegate_buffer_account, &[Signer::from(&seeds!( pda::UNDELEGATE_BUFFER_TAG, - delegated_account.key(), + delegated_account.address().as_ref(), &[undelegate_buffer_bump] ))], delegation_metadata, @@ -233,13 +238,13 @@ pub fn process_undelegate( /// 4. Settle lamports balance #[allow(clippy::too_many_arguments)] pub(crate) fn process_undelegation_with_cpi( - validator: &AccountInfo, - delegated_account: &AccountInfo, - owner_program: &AccountInfo, - undelegate_buffer_account: &AccountInfo, + validator: &AccountView, + delegated_account: &AccountView, + owner_program: &AccountView, + undelegate_buffer_account: &AccountView, undelegate_buffer_signer_seeds: &[Signer], delegation_metadata: DelegationMetadata, - system_program: &AccountInfo, + system_program: &AccountView, ) -> ProgramResult { let delegated_account_lamports_before_close = delegated_account.lamports(); close_pda(delegated_account, validator)?; @@ -253,14 +258,15 @@ pub(crate) fn process_undelegation_with_cpi( undelegate_buffer_account, undelegate_buffer_signer_seeds, system_program, - owner_program.key(), + owner_program.address(), delegation_metadata, )?; let validator_lamports_after_cpi = validator.lamports(); // Check that the validator lamports are exactly as expected - let delegated_account_min_rent = Rent::get()?.minimum_balance(delegated_account.data_len()); + let delegated_account_min_rent = + Rent::get()?.try_minimum_balance(delegated_account.data_len())?; if validator_lamports_before_cpi != validator_lamports_after_cpi .checked_add(delegated_account_min_rent) @@ -270,8 +276,7 @@ pub(crate) fn process_undelegation_with_cpi( } // Check that the owner program properly moved the state back into the original account during CPI - if delegated_account.try_borrow_data()?.as_ref() - != undelegate_buffer_account.try_borrow_data()?.as_ref() + if delegated_account.try_borrow()?.as_ref() != undelegate_buffer_account.try_borrow()?.as_ref() { return Err(DlpError::InvalidAccountDataAfterCPI.into()); } @@ -292,12 +297,12 @@ pub(crate) fn process_undelegation_with_cpi( /// CPI to the original owner program to re-open the PDA with the new state fn cpi_external_undelegate( - payer: &AccountInfo, - delegated_account: &AccountInfo, - undelegate_buffer_account: &AccountInfo, + payer: &AccountView, + delegated_account: &AccountView, + undelegate_buffer_account: &AccountView, undelegate_buffer_signer_seeds: &[Signer], - system_program: &AccountInfo, - owner_program_id: &Pubkey, + system_program: &AccountView, + owner_program_id: &Address, delegation_metadata: DelegationMetadata, ) -> ProgramResult { let data = { @@ -308,14 +313,14 @@ fn cpi_external_undelegate( data }; - let external_undelegate_instruction = Instruction { + let external_undelegate_instruction = InstructionView { program_id: owner_program_id, data: &data, accounts: &[ - AccountMeta::new(delegated_account.key(), true, false), - AccountMeta::new(undelegate_buffer_account.key(), true, true), - AccountMeta::new(payer.key(), true, true), - AccountMeta::new(system_program.key(), false, false), + InstructionAccount::new(delegated_account.address(), true, false), + InstructionAccount::new(undelegate_buffer_account.address(), true, true), + InstructionAccount::new(payer.address(), true, true), + InstructionAccount::new(system_program.address(), false, false), ], }; @@ -332,11 +337,11 @@ fn cpi_external_undelegate( } fn process_delegation_cleanup( - delegation_record_account: &AccountInfo, - delegation_metadata_account: &AccountInfo, - rent_reimbursement: &AccountInfo, - fees_vault: &AccountInfo, - validator_fees_vault: &AccountInfo, + delegation_record_account: &AccountView, + delegation_metadata_account: &AccountView, + rent_reimbursement: &AccountView, + fees_vault: &AccountView, + validator_fees_vault: &AccountView, delegation_last_update_nonce: u64, ) -> ProgramResult { let commit_count = delegation_last_update_nonce.saturating_sub(1); diff --git a/src/processor/fast/undelegate_confined_account.rs b/src/processor/fast/undelegate_confined_account.rs index 1d1762f2..da758ad2 100644 --- a/src/processor/fast/undelegate_confined_account.rs +++ b/src/processor/fast/undelegate_confined_account.rs @@ -10,8 +10,7 @@ use crate::processor::fast::utils::{ use crate::state::{DelegationMetadata, DelegationRecord}; use crate::{pda, require_eq_keys}; use pinocchio::{ - account_info::AccountInfo, instruction::Signer, program_error::ProgramError, pubkey::Pubkey, - seeds, ProgramResult, + cpi::Signer, error::ProgramError, instruction::seeds, AccountView, Address, ProgramResult, }; use super::{process_undelegation_with_cpi, to_pinocchio_program_error}; @@ -27,8 +26,8 @@ use super::{process_undelegation_with_cpi, to_pinocchio_program_error}; /// 6: [] system program /// 7: [] delegation program data (UpgradeableLoader ProgramData of this program) pub fn process_undelegate_confined_account( - _program_id: &Pubkey, - accounts: &[AccountInfo], + _program_id: &Address, + accounts: &[AccountView], _data: &[u8], ) -> ProgramResult { let [admin, delegated_account, owner_program, undelegate_buffer_account, delegation_record_account, delegation_metadata_account, system_program, delegation_program_data] = @@ -49,34 +48,34 @@ pub fn process_undelegate_confined_account( require_initialized_delegation_metadata(delegated_account, delegation_metadata_account, true)?; // Load delegation record - let delegation_record_data = delegation_record_account.try_borrow_data()?; + let delegation_record_data = delegation_record_account.try_borrow()?; let delegation_record = DelegationRecord::try_from_bytes_with_discriminator(&delegation_record_data) .map_err(to_pinocchio_program_error)?; - let delegation_metadata_data = delegation_metadata_account.try_borrow_data()?; + let delegation_metadata_data = delegation_metadata_account.try_borrow()?; let delegation_metadata = DelegationMetadata::try_from_bytes_with_discriminator(&delegation_metadata_data) .map_err(to_pinocchio_program_error)?; // Confined account: authority must be system program require_eq_keys!( - &delegation_record.authority.to_bytes(), + &Address::from(delegation_record.authority.to_bytes()), &pinocchio_system::ID, DlpError::InvalidAuthority ); // Owner must match the one stored in the record require_eq_keys!( - &delegation_record.owner.to_bytes(), - owner_program.key(), + &Address::from(delegation_record.owner.to_bytes()), + owner_program.address(), ProgramError::InvalidAccountOwner ); // If the delegated account has no data, simply assign back to the original owner and clean up PDAs - if delegated_account.data_is_empty() { + if delegated_account.is_data_empty() { unsafe { - delegated_account.assign(owner_program.key()); + delegated_account.assign(owner_program.address()); } // Close PDAs to the admin close_pda(delegation_record_account, admin)?; @@ -87,7 +86,10 @@ pub fn process_undelegate_confined_account( // Initialize undelegation buffer PDA and copy state let undelegate_buffer_bump: u8 = require_uninitialized_pda( undelegate_buffer_account, - &[pda::UNDELEGATE_BUFFER_TAG, delegated_account.key()], + &[ + pda::UNDELEGATE_BUFFER_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, true, UndelegateBufferCtx, @@ -99,14 +101,14 @@ pub fn process_undelegate_confined_account( delegated_account.data_len(), &[Signer::from(&seeds!( pda::UNDELEGATE_BUFFER_TAG, - delegated_account.key(), + delegated_account.address().as_ref(), &[undelegate_buffer_bump] ))], admin, )?; - (*undelegate_buffer_account.try_borrow_mut_data()?) - .copy_from_slice(&delegated_account.try_borrow_data()?); + (*undelegate_buffer_account.try_borrow_mut()?) + .copy_from_slice(&delegated_account.try_borrow()?); // Reuse the exact same CPI helper as process_undelegate process_undelegation_with_cpi( @@ -116,7 +118,7 @@ pub fn process_undelegate_confined_account( undelegate_buffer_account, &[Signer::from(&seeds!( pda::UNDELEGATE_BUFFER_TAG, - delegated_account.key(), + delegated_account.address().as_ref(), &[undelegate_buffer_bump] ))], delegation_metadata, diff --git a/src/processor/fast/utils/pda.rs b/src/processor/fast/utils/pda.rs index a2e12a3a..ebeee397 100644 --- a/src/processor/fast/utils/pda.rs +++ b/src/processor/fast/utils/pda.rs @@ -1,9 +1,8 @@ -use pinocchio::account_info::AccountInfo; -use pinocchio::instruction::Signer; -use pinocchio::program_error::ProgramError; -use pinocchio::pubkey::Pubkey; +use pinocchio::cpi::Signer; use pinocchio::sysvars::rent::Rent; use pinocchio::sysvars::Sysvar; +use pinocchio::AccountView; +use pinocchio::Address; use pinocchio::ProgramResult; use pinocchio_system::instructions as system; @@ -12,11 +11,11 @@ use crate::consts::PROTOCOL_FEES_PERCENTAGE; /// Creates a new pda #[inline(always)] pub(crate) fn create_pda( - target_account: &AccountInfo, - owner: &Pubkey, + target_account: &AccountView, + owner: &Address, space: usize, pda_signers: &[Signer], - payer: &AccountInfo, + payer: &AccountView, ) -> ProgramResult { // Create the account manually or using the create instruction @@ -26,7 +25,7 @@ pub(crate) fn create_pda( system::CreateAccount { from: payer, to: target_account, - lamports: rent.minimum_balance(space), + lamports: rent.try_minimum_balance(space)?, space: space as u64, owner, } @@ -36,7 +35,7 @@ pub(crate) fn create_pda( // 1) transfer sufficient lamports for rent exemption let rent_exempt_balance = rent - .minimum_balance(space) + .try_minimum_balance(space)? .saturating_sub(target_account.lamports()); if rent_exempt_balance > 0 { system::Transfer { @@ -65,16 +64,13 @@ pub(crate) fn create_pda( /// Close PDA #[inline(always)] -pub(crate) fn close_pda(target_account: &AccountInfo, destination: &AccountInfo) -> ProgramResult { +pub(crate) fn close_pda(target_account: &AccountView, destination: &AccountView) -> ProgramResult { // Transfer tokens from the account to the destination. - unsafe { - *destination.borrow_mut_lamports_unchecked() = destination - .lamports() - .checked_add(target_account.lamports()) - .ok_or(ProgramError::ArithmeticOverflow)?; - *target_account.borrow_mut_lamports_unchecked() = 0; + destination.set_lamports(destination.lamports() + target_account.lamports()); + target_account.set_lamports(0); + unsafe { target_account.assign(&pinocchio_system::ID); } @@ -85,10 +81,10 @@ pub(crate) fn close_pda(target_account: &AccountInfo, destination: &AccountInfo) /// The total fees are calculated as `fee_percentage` of the total lamports in the PDA /// Each fee address receives fee_percentage % of the previous fee address's amount pub(crate) fn close_pda_with_fees( - target_account: &AccountInfo, - rent_reimbursement: &AccountInfo, - fees_vault: &AccountInfo, - validator_fees_vault: &AccountInfo, + target_account: &AccountView, + rent_reimbursement: &AccountView, + fees_vault: &AccountView, + validator_fees_vault: &AccountView, fee_remaining: &mut u64, ) -> ProgramResult { let mut destination_amount = target_account.lamports(); @@ -101,16 +97,16 @@ pub(crate) fn close_pda_with_fees( let protocol_fee = fee_taken * PROTOCOL_FEES_PERCENTAGE as u64 / 100; let validator_fee = fee_taken - protocol_fee; - unsafe { - *fees_vault.borrow_mut_lamports_unchecked() += protocol_fee; - *validator_fees_vault.borrow_mut_lamports_unchecked() += validator_fee; - } + fees_vault.set_lamports(fees_vault.lamports() + protocol_fee); + validator_fees_vault.set_lamports(validator_fees_vault.lamports() + validator_fee); } + rent_reimbursement.set_lamports(rent_reimbursement.lamports() + destination_amount); + target_account.set_lamports(0); + unsafe { - *rent_reimbursement.borrow_mut_lamports_unchecked() += destination_amount; - *target_account.borrow_mut_lamports_unchecked() = 0; target_account.assign(&pinocchio_system::ID); } + target_account.resize(0).map_err(Into::into) } diff --git a/src/processor/fast/utils/requires.rs b/src/processor/fast/utils/requires.rs index 19c66c2a..914eef12 100644 --- a/src/processor/fast/utils/requires.rs +++ b/src/processor/fast/utils/requires.rs @@ -1,32 +1,11 @@ -use pinocchio::account_info::AccountInfo; -use pinocchio::program_error::ProgramError; -use pinocchio::pubkey::{pubkey_eq, Pubkey}; +use pinocchio::address::{address_eq, Address}; +use pinocchio::error::ProgramError; +use pinocchio::AccountView; use pinocchio_log::log; use crate::error::DlpError; use crate::pda::{self, program_config_from_program_id, validator_fees_vault_pda_from_validator}; -#[cfg(not(feature = "log-cost"))] -use pinocchio::pubkey; - -#[cfg(feature = "log-cost")] -mod pubkey { - pub use pinocchio::pubkey::log; - - use pinocchio::pubkey::{self, Pubkey}; - use pinocchio::syscalls::sol_remaining_compute_units; - use pinocchio_log::log; - - #[inline(always)] - pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) { - let prev = unsafe { sol_remaining_compute_units() }; - let rv = pubkey::find_program_address(seeds, program_id); - let curr = unsafe { sol_remaining_compute_units() }; - log!(">> find_program_address => {} CU", prev - curr); - rv - } -} - // require true #[macro_export] macro_rules! require { @@ -39,12 +18,13 @@ macro_rules! require { }}; } +// require (info.is_signer()) #[macro_export] macro_rules! require_signer { ($info: expr) => {{ if !$info.is_signer() { log!("require_signer!({}): ", stringify!($info)); - pubkey::log($info.key()); + $info.address().log(); return Err(ProgramError::MissingRequiredSignature); } }}; @@ -54,14 +34,14 @@ macro_rules! require_signer { #[macro_export] macro_rules! require_eq_keys { ( $key1:expr, $key2:expr, $error:expr) => {{ - if !pinocchio::pubkey::pubkey_eq($key1, $key2) { + if !pinocchio::address::address_eq($key1, $key2) { pinocchio_log::log!( "require_eq_keys!({}, {}) failed: ", stringify!($key1), stringify!($key2) ); - pinocchio::pubkey::log($key1); - pinocchio::pubkey::log($key2); + $key1.log(); + $key2.log(); return Err($error.into()); } }}; @@ -162,7 +142,7 @@ macro_rules! require_n_accounts { $n, $accounts.len() ); - return Err(pinocchio::program_error::ProgramError::NotEnoughAccountKeys); + return Err(pinocchio::error::ProgramError::NotEnoughAccountKeys); } core::cmp::Ordering::Equal => TryInto::<&[_; $n]>::try_into($accounts) .map_err(|_| $crate::error::DlpError::InfallibleError)?, @@ -190,21 +170,21 @@ macro_rules! require_some { /// /// require_owned_by( -/// info: &AccountInfo, -/// owner: &Pubkey +/// info: &AccountView, +/// owner: &Address /// ) -> Result<(), ProgramError> /// #[macro_export] macro_rules! require_owned_by { ($info: expr, $owner: expr) => {{ - if !pubkey_eq($info.owner(), $owner) { + if !address_eq(unsafe { $info.owner() }, $owner) { pinocchio_log::log!( "require_owned_by!({}, {})", stringify!($info), stringify!($owner) ); - pubkey::log($info.key()); - pubkey::log($owner); + $info.address().log(); + $owner.log(); return Err(ProgramError::InvalidAccountOwner); } }}; @@ -212,16 +192,16 @@ macro_rules! require_owned_by { /// /// require_initialized_pda( -/// info: &AccountInfo, +/// info: &AccountView, /// seeds: &[&[u8]], -/// program_id: &Pubkey, +/// program_id: &Address, /// is_writable: bool, /// ) -> Result<(), ProgramError> { /// #[macro_export] macro_rules! require_initialized_pda { ($info:expr, $seeds: expr, $program_id: expr, $is_writable: expr) => {{ - let pda = match pubkey::create_program_address($seeds, $program_id) { + let pda = match pinocchio::Address::create_program_address($seeds, $program_id) { Ok(pda) => pda, Err(_) => { log!( @@ -234,16 +214,16 @@ macro_rules! require_initialized_pda { return Err(ProgramError::InvalidSeeds); } }; - if !pubkey_eq($info.key(), &pda) { + if !address_eq($info.address(), &pda) { log!( - "require_initialized_pda!({}, {}, {}, {}); pubkey_eq failed", + "require_initialized_pda!({}, {}, {}, {}); address_eq failed", stringify!($info), stringify!($seeds), stringify!($program_id), stringify!($is_writable) ); - pubkey::log($info.key()); - pubkey::log($program_id); + $info.address().log(); + $program_id.log(); return Err(ProgramError::InvalidSeeds); } @@ -257,7 +237,7 @@ macro_rules! require_initialized_pda { stringify!($program_id), stringify!($is_writable) ); - pubkey::log($info.key()); + $info.address().log(); return Err(ProgramError::Immutable); } }}; @@ -267,15 +247,14 @@ macro_rules! require_initialized_pda { macro_rules! require_initialized_pda_fast { ($info:expr, $seeds: expr, $is_writable: expr) => {{ let pda = solana_sha256_hasher::hashv($seeds).to_bytes(); - - if !pubkey_eq($info.key(), &pda) { + if !address_eq($info.address(), &pda.into()) { log!( - "require_initialized_pda!({}, {}, {}); pubkey_eq failed", + "require_initialized_pda!({}, {}, {}); address_eq failed", stringify!($info), stringify!($seeds), stringify!($is_writable) ); - pubkey::log($info.key()); + $info.address().log(); return Err(ProgramError::InvalidSeeds); } @@ -288,7 +267,7 @@ macro_rules! require_initialized_pda_fast { stringify!($seeds), stringify!($is_writable) ); - pubkey::log($info.key()); + $info.address().log(); return Err(ProgramError::Immutable); } }}; @@ -297,7 +276,7 @@ macro_rules! require_initialized_pda_fast { #[macro_export] macro_rules! require_pda { ($info:expr, $seeds: expr, $program_id: expr, $is_writable: expr) => {{ - let pda = match pubkey::create_program_address($seeds, $program_id) { + let pda = match pinocchio::Address::create_program_address($seeds, $program_id) { Ok(pda) => pda, Err(_) => { log!( @@ -310,16 +289,16 @@ macro_rules! require_pda { return Err(ProgramError::InvalidSeeds); } }; - if !pubkey_eq($info.key(), &pda) { + if !address_eq($info.address(), &pda) { log!( - "require_pda!({}, {}, {}, {}); pubkey_eq failed", + "require_pda!({}, {}, {}, {}); address_eq failed", stringify!($info), stringify!($seeds), stringify!($program_id), stringify!($is_writable) ); - pubkey::log($info.key()); - pubkey::log($program_id); + $info.address().log(); + $program_id.log(); return Err(ProgramError::InvalidSeeds); } @@ -331,44 +310,25 @@ macro_rules! require_pda { stringify!($program_id), stringify!($is_writable) ); - pubkey::log($info.key()); + $info.address().log(); return Err(ProgramError::Immutable); } }}; } -/// require_program_config( -/// program_config: &AccountInfo, -/// program: &Pubkey, -/// bump: u8, -/// is_writable: bool, -/// ) -> Result { -#[macro_export] -macro_rules! require_program_config { - ($program_config: expr, $program: expr, $bump: expr, $is_writable: expr) => {{ - $crate::require_pda!( - $program_config, - &[pda::PROGRAM_CONFIG_TAG, $program, &[$bump]], - &$crate::fast::ID, - $is_writable - ); - !pubkey_eq($program_config.owner(), &pinocchio_system::ID) - }}; -} - /// Errors if: /// - Account is not owned by expected program. #[inline(always)] pub fn require_owned_pda( - info: &AccountInfo, - owner: &Pubkey, + info: &AccountView, + owner: &Address, label: &str, ) -> Result<(), ProgramError> { - if !pubkey_eq(info.owner(), owner) { + if !address_eq(unsafe { info.owner() }, owner) { log!("Invalid account owner for {}:", label); - pubkey::log(info.key()); - pubkey::log(info.owner()); - pubkey::log(owner); + info.address().log(); + unsafe { info.owner() }.log(); + owner.log(); return Err(ProgramError::InvalidAccountOwner); } Ok(()) @@ -377,10 +337,10 @@ pub fn require_owned_pda( /// Errors if: /// - Account is not a signer. #[inline(always)] -pub fn require_signer(info: &AccountInfo, label: &str) -> Result<(), ProgramError> { +pub fn require_signer(info: &AccountView, label: &str) -> Result<(), ProgramError> { if !info.is_signer() { log!("Account needs to be signer {}: ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::MissingRequiredSignature); } @@ -391,23 +351,23 @@ pub fn require_signer(info: &AccountInfo, label: &str) -> Result<(), ProgramErro /// - Address does not match PDA derived from provided seeds. #[inline(always)] pub fn require_pda( - info: &AccountInfo, + info: &AccountView, seeds: &[&[u8]], - program_id: &Pubkey, + program_id: &Address, is_writable: bool, label: &str, ) -> Result { - let pda = pubkey::find_program_address(seeds, program_id); + let pda = Address::find_program_address(seeds, program_id); - if !pubkey_eq(info.key(), &pda.0) { + if !address_eq(info.address(), &pda.0) { log!("Invalid seeds for {}: ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::InvalidSeeds); } if is_writable && !info.is_writable() { log!("Account needs to be writable. Label: {}", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::Immutable); } @@ -417,8 +377,8 @@ pub fn require_pda( /// Returns true if the account is uninitialized based on the following conditions: /// - Owner is the system program. /// - Data is empty. -pub fn is_uninitialized_account(info: &AccountInfo) -> bool { - pubkey_eq(info.owner(), &pinocchio_system::ID) && info.data_is_empty() +pub fn is_uninitialized_account(info: &AccountView) -> bool { + address_eq(unsafe { info.owner() }, &pinocchio_system::ID) && info.is_data_empty() } /// Errors if: @@ -427,26 +387,26 @@ pub fn is_uninitialized_account(info: &AccountInfo) -> bool { /// - Account is not writable. #[inline(always)] pub fn require_uninitialized_account( - info: &AccountInfo, + info: &AccountView, is_writable: bool, ctx: impl RequireUninitializedAccountCtx, ) -> Result<(), ProgramError> { - if !pubkey_eq(info.owner(), &pinocchio_system::id()) { + if !address_eq(unsafe { info.owner() }, &pinocchio_system::id()) { log!( "Invalid owner for account. Label: {}; account and owner: ", ctx.label() ); - pubkey::log(info.key()); - pubkey::log(info.owner()); + info.address().log(); + unsafe { info.owner() }.log(); return Err(ctx.invalid_account_owner()); } - if !info.data_is_empty() { + if !info.is_data_empty() { log!( "Account needs to be uninitialized. Label: {}, account: ", ctx.label(), ); - pubkey::log(info.key()); + info.address().log(); return Err(ctx.account_already_initialized()); } @@ -455,7 +415,7 @@ pub fn require_uninitialized_account( "Account needs to be writable. label: {}, account: ", ctx.label() ); - pubkey::log(info.key()); + info.address().log(); return Err(ctx.immutable()); } @@ -467,17 +427,17 @@ pub fn require_uninitialized_account( /// - Cannot load as an uninitialized account. #[inline(always)] pub fn require_uninitialized_pda( - info: &AccountInfo, + info: &AccountView, seeds: &[&[u8]], - program_id: &Pubkey, + program_id: &Address, is_writable: bool, ctx: impl RequireUninitializedAccountCtx, ) -> Result { - let pda = pubkey::find_program_address(seeds, program_id); + let pda = Address::find_program_address(seeds, program_id); - if !pubkey_eq(info.key(), &pda.0) { + if !address_eq(info.address(), &pda.0) { log!("Invalid seeds for account {}: ", ctx.label()); - pubkey::log(info.key()); + info.address().log(); return Err(ctx.invalid_seeds()); } @@ -490,16 +450,16 @@ pub fn require_uninitialized_pda( /// - Owner is not the expected program. /// - Account is not writable if set to writable. pub fn require_initialized_pda( - info: &AccountInfo, + info: &AccountView, seeds: &[&[u8]], - program_id: &Pubkey, + program_id: &Address, is_writable: bool, label: &str, ) -> Result { - let pda = pubkey::find_program_address(seeds, program_id); - if !pubkey_eq(info.key(), &pda.0) { + let pda = Address::find_program_address(seeds, program_id); + if !address_eq(info.address(), &pda.0) { log!("Invalid seeds (label: {}) for account ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::InvalidSeeds); } @@ -507,7 +467,7 @@ pub fn require_initialized_pda( if is_writable && !info.is_writable() { log!("Account needs to be writable. label: {}, account: ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::Immutable); } @@ -519,16 +479,16 @@ pub fn require_initialized_pda( /// - Account is not executable. #[inline(always)] #[allow(dead_code)] -pub fn require_program(info: &AccountInfo, key: &Pubkey, label: &str) -> Result<(), ProgramError> { - if !pubkey_eq(info.key(), key) { +pub fn require_program(info: &AccountView, key: &Address, label: &str) -> Result<(), ProgramError> { + if !address_eq(info.address(), key) { log!("Invalid program account {}: ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::IncorrectProgramId); } if !info.executable() { log!("{} program is not executable: ", label); - pubkey::log(info.key()); + info.address().log(); return Err(ProgramError::InvalidAccountData); } @@ -538,7 +498,7 @@ pub fn require_program(info: &AccountInfo, key: &Pubkey, label: &str) -> Result< /// Load fee vault PDA /// - Protocol fees vault PDA pub fn require_initialized_protocol_fees_vault( - fees_vault: &AccountInfo, + fees_vault: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { require_initialized_pda( @@ -555,21 +515,23 @@ pub fn require_initialized_protocol_fees_vault( /// - Validator fees vault PDA must be derived from the validator pubkey /// - Validator fees vault PDA must be initialized with the expected seeds and owner pub fn require_initialized_validator_fees_vault( - validator: &AccountInfo, - validator_fees_vault: &AccountInfo, + validator: &AccountView, + validator_fees_vault: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { - let pda = validator_fees_vault_pda_from_validator(&(*validator.key()).into()); - if !pubkey_eq(validator_fees_vault.key(), pda.as_array()) { + let pda = validator_fees_vault_pda_from_validator(&validator.address().to_bytes().into()) + .to_bytes() + .into(); + if !address_eq(validator_fees_vault.address(), &pda) { log!("Invalid validator fees vault PDA, expected: "); - pubkey::log(pda.as_array()); + pda.log(); log!("but got: "); - pubkey::log(validator_fees_vault.key()); + validator_fees_vault.address().log(); return Err(DlpError::InvalidAuthority.into()); } require_initialized_pda( validator_fees_vault, - &[pda::VALIDATOR_FEES_VAULT_TAG, validator.key()], + &[pda::VALIDATOR_FEES_VAULT_TAG, validator.address().as_ref()], &crate::fast::ID, is_writable, "validator fees vault", @@ -580,38 +542,44 @@ pub fn require_initialized_validator_fees_vault( /// Load program config PDA /// - Program config PDA must be initialized with the expected seeds and owner, or not exists pub fn require_program_config( - program_config: &AccountInfo, - program: &Pubkey, + program_config: &AccountView, + program: &Address, is_writable: bool, ) -> Result { - let pda = program_config_from_program_id(&(*program).into()); - if !pubkey_eq(pda.as_array(), program_config.key()) { + let pda = program_config_from_program_id(&program.to_bytes().into()); + if !address_eq(&pda.to_bytes().into(), program_config.address()) { log!("Invalid program config PDA, expected: "); - pubkey::log(pda.as_array()); + pda.log(); log!("but got: "); - pubkey::log(program_config.key()); + program_config.address().log(); return Err(DlpError::InvalidAuthority.into()); } require_pda( program_config, - &[pda::PROGRAM_CONFIG_TAG, program], + &[pda::PROGRAM_CONFIG_TAG, program.as_ref()], &crate::fast::ID, is_writable, "program config", )?; - Ok(!pubkey_eq(program_config.owner(), &pinocchio_system::ID)) + Ok(!address_eq( + unsafe { program_config.owner() }, + &pinocchio_system::ID, + )) } /// Load initialized delegation record /// - Delegation record must be derived from the delegated account pub fn require_initialized_delegation_record( - delegated_account: &AccountInfo, - delegation_record: &AccountInfo, + delegated_account: &AccountView, + delegation_record: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { require_initialized_pda( delegation_record, - &[pda::DELEGATION_RECORD_TAG, delegated_account.key()], + &[ + pda::DELEGATION_RECORD_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, is_writable, "delegation record", @@ -622,13 +590,16 @@ pub fn require_initialized_delegation_record( /// Load initialized delegation metadata /// - Delegation metadata must be derived from the delegated account pub fn require_initialized_delegation_metadata( - delegated_account: &AccountInfo, - delegation_metadata: &AccountInfo, + delegated_account: &AccountView, + delegation_metadata: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { require_initialized_pda( delegation_metadata, - &[pda::DELEGATION_METADATA_TAG, delegated_account.key()], + &[ + pda::DELEGATION_METADATA_TAG, + delegated_account.address().as_ref(), + ], &crate::fast::ID, is_writable, "delegation metadata", @@ -639,13 +610,13 @@ pub fn require_initialized_delegation_metadata( /// Load initialized commit state account /// - Commit state account must be derived from the delegated account pubkey pub fn require_initialized_commit_state( - delegated_account: &AccountInfo, - commit_state: &AccountInfo, + delegated_account: &AccountView, + commit_state: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { require_initialized_pda( commit_state, - &[pda::COMMIT_STATE_TAG, delegated_account.key()], + &[pda::COMMIT_STATE_TAG, delegated_account.address().as_ref()], &crate::fast::ID, is_writable, "commit state", @@ -656,13 +627,13 @@ pub fn require_initialized_commit_state( /// Load initialized commit state record /// - Commit record account must be derived from the delegated account pubkey pub fn require_initialized_commit_record( - delegated_account: &AccountInfo, - commit_record: &AccountInfo, + delegated_account: &AccountView, + commit_record: &AccountView, is_writable: bool, ) -> Result<(), ProgramError> { require_initialized_pda( commit_record, - &[pda::COMMIT_RECORD_TAG, delegated_account.key()], + &[pda::COMMIT_RECORD_TAG, delegated_account.address().as_ref()], &crate::fast::ID, is_writable, "commit record", @@ -699,19 +670,19 @@ macro_rules! define_uninitialized_ctx { $label } - fn invalid_seeds(&self) -> pinocchio::program_error::ProgramError { + fn invalid_seeds(&self) -> pinocchio::error::ProgramError { $seeds.into() } - fn invalid_account_owner(&self) -> pinocchio::program_error::ProgramError { + fn invalid_account_owner(&self) -> pinocchio::error::ProgramError { $owner.into() } - fn account_already_initialized(&self) -> pinocchio::program_error::ProgramError { + fn account_already_initialized(&self) -> pinocchio::error::ProgramError { $already_init.into() } - fn immutable(&self) -> pinocchio::program_error::ProgramError { + fn immutable(&self) -> pinocchio::error::ProgramError { $immutable.into() } } @@ -764,16 +735,16 @@ define_uninitialized_ctx!( ); pub fn require_authorization( - program_data: &AccountInfo, - admin: &AccountInfo, + program_data: &AccountView, + admin: &AccountView, ) -> Result<(), ProgramError> { #[cfg(feature = "unit_test_config")] { let _ = program_data; require_eq_keys!( - &crate::consts::DEFAULT_VALIDATOR_IDENTITY.to_bytes().into(), - admin.key(), + &Address::from(crate::consts::DEFAULT_VALIDATOR_IDENTITY.to_bytes()), + admin.address(), ProgramError::IncorrectAuthority ); return Ok(()); @@ -784,7 +755,7 @@ pub fn require_authorization( // Derive and validate program data address require_eq_keys!( &crate::consts::DELEGATION_PROGRAM_DATA_ID, - program_data.key(), + program_data.address(), ProgramError::IncorrectAuthority ); @@ -802,7 +773,7 @@ pub fn require_authorization( // // SAFETY: This authorization logic reads raw ProgramData bytes using the current // Upgradeable Loader v3 layout. - let data = program_data.try_borrow_data()?; + let data = program_data.try_borrow()?; if data.len() >= offset_of_upgrade_authority_address + 33 && data[0] == PROGRAM_DATA && data[offset_of_upgrade_authority_address] == OPTION_SOME @@ -810,12 +781,11 @@ pub fn require_authorization( let bytes = &data [offset_of_upgrade_authority_address + 1..offset_of_upgrade_authority_address + 33]; - let upgrade_authority_address = - unsafe { *(bytes.as_ptr() as *const pinocchio::pubkey::Pubkey) }; + let upgrade_authority_address = unsafe { &*(bytes.as_ptr() as *const Address) }; require_eq_keys!( - &upgrade_authority_address, - admin.key(), + upgrade_authority_address, + admin.address(), ProgramError::IncorrectAuthority ); diff --git a/src/processor/utils/curve.rs b/src/processor/utils/curve.rs index 823a864a..897f301b 100644 --- a/src/processor/utils/curve.rs +++ b/src/processor/utils/curve.rs @@ -1,10 +1,10 @@ -pub fn is_on_curve_fast(key: &pinocchio::pubkey::Pubkey) -> bool { +pub fn is_on_curve_fast(key: &pinocchio::Address) -> bool { #[cfg(not(target_os = "solana"))] { use solana_curve25519::edwards::{validate_edwards, PodEdwardsPoint}; // SAFETY: the layout of pinocchio::pubkey::Pubkey and PodEdwardsPoint is identical // so one can be casted to the other without any issue. - validate_edwards(unsafe { &*(key as *const u8 as *const PodEdwardsPoint) }) + validate_edwards(unsafe { &*(key.as_array().as_ptr() as *const PodEdwardsPoint) }) } #[cfg(target_os = "solana")] @@ -16,7 +16,7 @@ pub fn is_on_curve_fast(key: &pinocchio::pubkey::Pubkey) -> bool { let ret = unsafe { pinocchio::syscalls::sol_curve_validate_point( 0, // 0 means Ed25519 - key.as_ptr(), + key.as_array().as_ptr(), &mut result as *mut u8, ) }; diff --git a/src/state/delegation_metadata.rs b/src/state/delegation_metadata.rs index ff012bf8..71f6196f 100644 --- a/src/state/delegation_metadata.rs +++ b/src/state/delegation_metadata.rs @@ -5,10 +5,7 @@ use crate::{ require_ge, }; use borsh::{BorshDeserialize, BorshSerialize}; -use pinocchio::{ - account_info::{AccountInfo, RefMut}, - program_error::ProgramError, -}; +use pinocchio::{account::RefMut, error::ProgramError, AccountView}; use solana_program::pubkey::Pubkey; use super::discriminator::{AccountDiscriminator, AccountWithDiscriminator}; @@ -34,7 +31,7 @@ pub struct DelegationMetadataFast<'a> { } impl<'a> DelegationMetadataFast<'a> { - pub fn from_account(account: &'a AccountInfo) -> Result { + pub fn from_account(account: &'a AccountView) -> Result { require_ge!( account.data_len(), AccountDiscriminator::SPACE @@ -46,7 +43,7 @@ impl<'a> DelegationMetadataFast<'a> { ); Ok(Self { - data: account.try_borrow_mut_data()?, + data: account.try_borrow_mut()?, }) }