diff --git a/Cargo.lock b/Cargo.lock index c651213e..3723454a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -501,11 +501,11 @@ dependencies = [ [[package]] name = "borsh" -version = "1.5.5" +version = "1.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5430e3be710b68d984d1391c854eb431a9d548640711faa54eecb1df93db91cc" +checksum = "ad8646f98db542e39fc66e68a20b2144f6a732636df7c2354e74645faaa433ce" dependencies = [ - "borsh-derive 1.5.5", + "borsh-derive 1.5.7", "cfg_aliases", ] @@ -524,9 +524,9 @@ dependencies = [ [[package]] name = "borsh-derive" -version = "1.5.5" +version = "1.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8b668d39970baad5356d7c83a86fee3a539e6f93bf6764c97368243e17a0487" +checksum = "fdd1d3c0c2f5833f22386f252fe8ed005c7f59fdcddeef025c01b4c3b9fd9ac3" dependencies = [ "once_cell", "proc-macro-crate 3.1.0", @@ -2223,7 +2223,7 @@ version = "1.1.0" dependencies = [ "base64 0.22.1", "bincode", - "borsh 1.5.5", + "borsh 1.5.7", "bytemuck", "magicblock-delegation-program", "num_enum", @@ -3827,7 +3827,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57b8593f50e34f44ed168dccbac0a9b8e43e138ac75102dac6635fbdfff88d1e" dependencies = [ - "borsh 1.5.5", + "borsh 1.5.7", "futures", "solana-banks-interface", "solana-program", @@ -3928,7 +3928,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "718333bcd0a1a7aed6655aa66bef8d7fb047944922b2d3a18f49cbc13e73d004" dependencies = [ "borsh 0.10.3", - "borsh 1.5.5", + "borsh 1.5.7", ] [[package]] @@ -4183,7 +4183,7 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a5df17b195d312b66dccdde9beec6709766d8230cb4718c4c08854f780d0309" dependencies = [ - "borsh 1.5.5", + "borsh 1.5.7", "serde", "serde_derive", "solana-instruction", @@ -4529,7 +4529,7 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf7bcb14392900fe02e4e34e90234fbf0c673d4e327888410ba99fa2ba0f4e99" dependencies = [ - "borsh 1.5.5", + "borsh 1.5.7", "bs58", "bytemuck", "bytemuck_derive", @@ -4568,7 +4568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ce496a475e5062ba5de97215ab39d9c358f9c9df4bb7f3a45a1f1a8bd9065ed" dependencies = [ "bincode", - "borsh 1.5.5", + "borsh 1.5.7", "getrandom 0.2.15", "js-sys", "num-traits", @@ -4989,7 +4989,7 @@ dependencies = [ "bincode", "blake3", "borsh 0.10.3", - "borsh 1.5.5", + "borsh 1.5.7", "bs58", "bytemuck", "console_error_panic_hook", @@ -5078,7 +5078,7 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8ae2c1a8d0d4ae865882d5770a7ebca92bab9c685e43f0461682c6c05a35bfa" dependencies = [ - "borsh 1.5.5", + "borsh 1.5.7", "num-traits", "serde", "serde_derive", @@ -5198,7 +5198,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40db1ff5a0f8aea2c158d78ab5f2cf897848964251d1df42fef78efd3c85b863" dependencies = [ "borsh 0.10.3", - "borsh 1.5.5", + "borsh 1.5.7", "bs58", "bytemuck", "bytemuck_derive", @@ -5689,7 +5689,7 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baa3120b6cdaa270f39444f5093a90a7b03d296d362878f7a6991d6de3bbe496" dependencies = [ - "borsh 1.5.5", + "borsh 1.5.7", "libsecp256k1", "solana-define-syscall", "thiserror 2.0.11", @@ -5883,7 +5883,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5269e89fde216b4d7e1d1739cf5303f8398a1ff372a81232abbee80e554a838c" dependencies = [ "borsh 0.10.3", - "borsh 1.5.5", + "borsh 1.5.7", "num-traits", "serde", "serde_derive", diff --git a/src/args/call_handler.rs b/src/args/call_handler.rs index 2c9c03ed..98d9cf57 100644 --- a/src/args/call_handler.rs +++ b/src/args/call_handler.rs @@ -1,15 +1,9 @@ use borsh::{BorshDeserialize, BorshSerialize}; -#[derive(BorshSerialize, BorshDeserialize, Clone, Copy)] -pub enum Context { - Commit, - Undelegate, - Standalone, -} - #[derive(BorshSerialize, BorshDeserialize)] pub struct CallHandlerArgs { pub escrow_index: u8, + /// This is raw instruction data, it could include discriminator + args + /// or can be in any other custom format pub data: Vec, - pub context: Context, } diff --git a/src/consts.rs b/src/consts.rs index 8c95732c..72c1abaa 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -10,10 +10,6 @@ pub const PROTOCOL_FEES_PERCENTAGE: u8 = 10; /// The discriminator for the external undelegate instruction. pub const EXTERNAL_UNDELEGATE_DISCRIMINATOR: [u8; 8] = [196, 28, 41, 206, 48, 37, 51, 167]; -/// The discriminator for the external hook after finalization is complete -/// For anchor: corresponds to function/instruction name delegation_program_call_handler -pub const EXTERNAL_CALL_HANDLER_DISCRIMINATOR: [u8; 8] = [157, 197, 228, 30, 0, 80, 121, 135]; - /// The program ID of the delegation program. pub const DELEGATION_PROGRAM_ID: Pubkey = crate::id(); diff --git a/src/processor/call_handler.rs b/src/processor/call_handler.rs index a2beb537..c8c897d7 100644 --- a/src/processor/call_handler.rs +++ b/src/processor/call_handler.rs @@ -1,5 +1,4 @@ use crate::args::CallHandlerArgs; -use crate::consts::EXTERNAL_CALL_HANDLER_DISCRIMINATOR; use crate::ephemeral_balance_seeds_from_payer; use crate::processor::utils::loaders::{ load_initialized_validator_fees_vault, load_owned_pda, load_pda, load_signer, @@ -109,10 +108,9 @@ pub fn process_call_handler( }) .collect(); - let data = [EXTERNAL_CALL_HANDLER_DISCRIMINATOR.to_vec(), data.to_vec()].concat(); let handler_instruction = Instruction { program_id: *destination_program.key, - data, + data: args.data, accounts: accounts_meta, }; let bump_slice = &[escrow_bump]; diff --git a/tests/buffers/test_delegation.so b/tests/buffers/test_delegation.so index 4bbbeb3d..1df10c2e 100755 Binary files a/tests/buffers/test_delegation.so and b/tests/buffers/test_delegation.so differ diff --git a/tests/integration/programs/test-delegation/src/lib.rs b/tests/integration/programs/test-delegation/src/lib.rs index f9dfad37..936e39ab 100644 --- a/tests/integration/programs/test-delegation/src/lib.rs +++ b/tests/integration/programs/test-delegation/src/lib.rs @@ -1,7 +1,6 @@ use anchor_lang::prelude::*; use ephemeral_rollups_sdk::anchor::{delegate, ephemeral}; use ephemeral_rollups_sdk::cpi::DelegateConfig; -use ephemeral_rollups_sdk::pda::ephemeral_balance_pda_from_payer; declare_id!("3vAK9JQiDsKoQNwmcfeEng4Cnv22pYuj1ASfso7U4ukF"); @@ -68,59 +67,42 @@ pub mod test_delegation { } /// Delegation program call handler - pub fn delegation_program_call_handler( - ctx: Context, - hook_args: delegation_program_utils::CallHandlerArgs, + #[instruction(discriminator = [1, 0, 1, 0])] + pub fn commit_base_action_handler( + ctx: Context, + amount: u64, ) -> Result<()> { - let expected = ephemeral_balance_pda_from_payer( - ctx.accounts.escrow_authority.key, - hook_args.escrow_index, + msg!("commit_base_action_handler!"); + let transfer_ctx = CpiContext::new( + ctx.accounts.system_program.to_account_info(), + Transfer { + from: ctx.accounts.escrow_account.to_account_info(), + to: ctx.accounts.destination_account.to_account_info(), + }, ); - if &expected != ctx.accounts.escrow_account.key { - Err(ProgramError::InvalidAccountData) - } else { - Ok(()) - }?; - - if !ctx.accounts.escrow_account.is_signer { - Err(ProgramError::MissingRequiredSignature) - } else { - Ok(()) - }?; - - match hook_args.context { - delegation_program_utils::Context::Commit => { - msg!("commit context"); - let amount = u64::try_from_slice(&hook_args.data)?; - let transfer_ctx = CpiContext::new( - ctx.accounts.system_program.to_account_info(), - Transfer { - from: ctx.accounts.escrow_account.to_account_info(), - to: ctx.accounts.destination_account.to_account_info(), - }, - ); - transfer(transfer_ctx, amount)?; - } - delegation_program_utils::Context::Undelegate => { - msg!("undelegate context"); - let amount = u64::try_from_slice(&hook_args.data)?; - let transfer_ctx = CpiContext::new( - ctx.accounts.system_program.to_account_info(), - Transfer { - from: ctx.accounts.escrow_account.to_account_info(), - to: ctx.accounts.destination_account.to_account_info(), - }, - ); - transfer(transfer_ctx, amount)?; - - let counter_data = &mut ctx.accounts.counter.try_borrow_mut_data()?; - let mut counter = Counter::try_from_slice(&counter_data)?; - counter.count += 1; - - counter_data.copy_from_slice(&counter.try_to_vec()?); - } - delegation_program_utils::Context::Standalone => msg!("standalone context"), - } + + transfer(transfer_ctx, amount) + } + + #[instruction(discriminator = [1, 0, 2, 0])] + pub fn undelegate_base_action_handler( + ctx: Context, + amount: u64, + ) -> Result<()> { + msg!("undelegate_base_action_handler"); + let transfer_ctx = CpiContext::new( + ctx.accounts.system_program.to_account_info(), + Transfer { + from: ctx.accounts.escrow_account.to_account_info(), + to: ctx.accounts.destination_account.to_account_info(), + }, + ); + transfer(transfer_ctx, amount)?; + + let counter_data = &mut ctx.accounts.counter.try_borrow_mut_data()?; + let mut counter = Counter::try_from_slice(&counter_data)?; + counter.count += 1; + counter_data.copy_from_slice(&counter.try_to_vec()?); Ok(()) } @@ -194,16 +176,20 @@ pub struct Increment<'info> { } #[derive(Accounts)] -#[instruction(hook_args: delegation_program_utils::CallHandlerArgs)] -pub struct DelegationProgramCallHandler<'info> { +pub struct CommitBaseActionHandler<'info> { + /// CHECK: The authority that owns the escrow account + pub escrow_authority: UncheckedAccount<'info>, + pub escrow_account: Signer<'info>, + /// CHECK: The destination account to transfer lamports to + #[account(mut)] + pub destination_account: AccountInfo<'info>, + pub system_program: Program<'info, System>, +} + +#[derive(Accounts)] +pub struct UndelegateBaseActionHandler<'info> { /// CHECK: The authority that owns the escrow account pub escrow_authority: UncheckedAccount<'info>, - #[account( - mut, - seeds = [b"balance", &escrow_authority.key().as_ref(), &[hook_args.escrow_index]], - seeds::program = delegation_program_utils::ID, - bump - )] pub escrow_account: Signer<'info>, /// CHECK: The destination account to transfer lamports to #[account(mut)] @@ -222,18 +208,4 @@ mod delegation_program_utils { use anchor_lang::prelude::*; declare_id!("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh"); - - #[derive(AnchorSerialize, AnchorDeserialize)] - pub enum Context { - Commit, - Undelegate, - Standalone, - } - - #[derive(AnchorSerialize, AnchorDeserialize)] - pub struct CallHandlerArgs { - pub escrow_index: u8, - pub data: Vec, - pub context: Context, - } } diff --git a/tests/test_call_handler.rs b/tests/test_call_handler.rs index d0dd33bd..0d2913a4 100644 --- a/tests/test_call_handler.rs +++ b/tests/test_call_handler.rs @@ -24,6 +24,9 @@ use solana_sdk::{ mod fixtures; +const COMMIT_HANDLER_DISCRIMINATOR: [u8; 4] = [1, 0, 1, 0]; +const UNDELEGATE_HANDLER_DISCRIMINATOR: [u8; 4] = [1, 0, 2, 0]; + // Mimic counter from test_delegation program #[derive(BorshSerialize, BorshDeserialize)] pub struct Counter { @@ -296,13 +299,15 @@ async fn test_finalize_call_handler() { payer.pubkey(), // escrow authority vec![ AccountMeta::new(transfer_destination.pubkey(), false), - AccountMeta::new(DELEGATED_PDA_ID, false), AccountMeta::new_readonly(system_program::id(), false), ], CallHandlerArgs { escrow_index: 2, // undelegated escrow index, - data: to_vec(&PRIZE).unwrap(), - context: dlp::args::Context::Commit, + data: [ + COMMIT_HANDLER_DISCRIMINATOR.to_vec(), + to_vec(&PRIZE).unwrap(), + ] + .concat(), }, ); @@ -350,8 +355,11 @@ async fn test_undelegate_call_handler() { ], CallHandlerArgs { escrow_index: 2, // undelegated escrow index, - data: to_vec(&PRIZE).unwrap(), - context: dlp::args::Context::Undelegate, + data: [ + UNDELEGATE_HANDLER_DISCRIMINATOR.to_vec(), + to_vec(&PRIZE).unwrap(), + ] + .concat(), }, ); @@ -399,8 +407,7 @@ async fn test_finalize_invalid_escrow_call_handler() { vec![AccountMeta::new(transfer_destination.pubkey(), false)], CallHandlerArgs { escrow_index: 0, - data: vec![], - context: dlp::args::Context::Commit, + data: COMMIT_HANDLER_DISCRIMINATOR.to_vec(), }, ); let tx = Transaction::new_signed_with_payer( @@ -432,8 +439,7 @@ async fn test_undelegate_invalid_escow_call_handler() { vec![AccountMeta::new(destination.pubkey(), false)], CallHandlerArgs { escrow_index: 0, - data: vec![], - context: dlp::args::Context::Commit, + data: UNDELEGATE_HANDLER_DISCRIMINATOR.to_vec(), }, ); @@ -450,8 +456,11 @@ async fn test_undelegate_invalid_escow_call_handler() { vec![AccountMeta::new(destination.pubkey(), false)], CallHandlerArgs { escrow_index: 0, - data: to_vec(&PRIZE).unwrap(), - context: dlp::args::Context::Undelegate, + data: [ + UNDELEGATE_HANDLER_DISCRIMINATOR.to_vec(), + to_vec(&PRIZE).unwrap(), + ] + .concat(), }, ); let tx = Transaction::new_signed_with_payer(