Skip to content

Commit 3de1482

Browse files
committed
redefine DepositAndDelegateShuttleArgs with fixed_layout
1 parent c214269 commit 3de1482

9 files changed

Lines changed: 44 additions & 89 deletions

e-token/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use crate::entrypoint::process_instruction;
99
pub use ephemeral_spl_api::ID;
1010

1111
pub use processor::{
12-
DelegateArgs, DelegateShuttleArgs, DepositAndDelegateShuttleWithPrivateTransferArgs,
13-
DepositAndQueueTransferArgs, ExecuteQueuedTransferArgs, InitializeTransferQueueArgs,
12+
DelegateArgs, DelegateShuttleArgs, DepositAndDelegateShuttleArgs,
13+
DepositAndDelegateShuttleWithPrivateTransferArgs, DepositAndQueueTransferArgs,
14+
ExecuteQueuedTransferArgs, InitializeTransferQueueArgs,
1415
};

e-token/src/processor/deposit_and_delegate_shuttle_ephemeral_ata_with_merge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn process_deposit_and_delegate_shuttle_ephemeral_ata_with_merge(
7070
vault_token_info,
7171
] = require_n_accounts!(accounts, 19);
7272

73-
let args = DepositAndDelegateShuttleArgs::try_from_bytes(instruction_data)?;
73+
let args = DepositAndDelegateShuttleArgs::try_view_from(instruction_data)?;
7474

7575
let accounts = DepositAndDelegateShuttleWithMergeAccounts {
7676
common: DepositAndDelegateShuttleAccounts {

e-token/src/processor/deposit_and_delegate_shuttle_ephemeral_ata_with_merge_and_private_transfer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use alloc::borrow::ToOwned;
21
#[cfg(feature = "logging")]
32
use alloc::string::ToString;
43
use alloc::vec::Vec;
@@ -202,11 +201,11 @@ pub struct DepositAndDelegateShuttleWithPrivateTransferArgs {
202201
}
203202

204203
impl DepositAndDelegateShuttleWithPrivateTransferArgsView<'_> {
205-
fn common_args(&self) -> Result<DepositAndDelegateShuttleCommonArgs, ProgramError> {
204+
fn common_args(&self) -> Result<DepositAndDelegateShuttleCommonArgs<'_>, ProgramError> {
206205
Ok(DepositAndDelegateShuttleCommonArgs {
207206
shuttle_id: self.shuttle_id(),
208207
amount: self.amount(),
209-
validator: self.validator().map(|r| r.to_owned()),
208+
validator: self.validator(),
210209
})
211210
}
212211
}

e-token/src/processor/internal/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ pub(crate) mod lamports_pda;
33
pub(crate) mod shuttle_delegation;
44
pub(crate) mod token_vault;
55
pub(crate) mod transfer_queue_refill;
6+
7+
pub use shuttle_delegation::DepositAndDelegateShuttleArgs;

e-token/src/processor/internal/shuttle_delegation.rs

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#[cfg(feature = "logging")]
22
use alloc::string::ToString;
3+
use data_layout::fixed_layout;
34

4-
use core::{marker::PhantomData, mem::MaybeUninit};
5+
use core::mem::MaybeUninit;
56
use dlp_api::args::PostDelegationActions;
67
use ephemeral_rollups_pinocchio::{
78
consts::{
@@ -59,78 +60,22 @@ pub(crate) struct PreparedShuttleDelegation {
5960
}
6061

6162
#[derive(Clone, Copy)]
62-
pub(crate) struct DepositAndDelegateShuttleCommonArgs {
63+
pub(crate) struct DepositAndDelegateShuttleCommonArgs<'a> {
6364
pub(crate) shuttle_id: u32,
6465
pub(crate) amount: u64,
65-
pub(crate) validator: Option<[u8; 32]>,
66+
pub(crate) validator: Option<&'a [u8; 32]>,
6667
}
6768

68-
//
69-
// DataLayout:
70-
//
71-
// 00..04 : shuttle_id (u32)
72-
// 04..12 : amount (u64)
73-
// 12..44 : validator (optional [u8; 32])
74-
//
75-
// ValidLength:
76-
//
77-
// 12 (without validator)
78-
// 44 (with validator)
79-
//
80-
pub struct DepositAndDelegateShuttleArgs<'a> {
81-
raw: *const u8,
82-
len: usize,
83-
_data: PhantomData<&'a [u8]>,
69+
#[fixed_layout]
70+
pub struct DepositAndDelegateShuttleArgs {
71+
pub shuttle_id: u32,
72+
pub amount: u64,
73+
pub validator: Option<[u8; 32]>,
8474
}
8575

86-
impl DepositAndDelegateShuttleArgs<'_> {
76+
impl DepositAndDelegateShuttleArgsView<'_> {
8777
#[inline]
88-
pub fn try_from_bytes(bytes: &[u8]) -> Result<DepositAndDelegateShuttleArgs<'_>, ProgramError> {
89-
require!(
90-
bytes.len() == 12 || bytes.len() == 44,
91-
ProgramError::InvalidInstructionData
92-
);
93-
94-
Ok(DepositAndDelegateShuttleArgs {
95-
raw: bytes.as_ptr(),
96-
len: bytes.len(),
97-
_data: PhantomData,
98-
})
99-
}
100-
101-
#[inline]
102-
pub fn shuttle_id(&self) -> u32 {
103-
let mut buf = [0u8; 4];
104-
unsafe {
105-
core::ptr::copy_nonoverlapping(self.raw, buf.as_mut_ptr(), 4);
106-
}
107-
u32::from_le_bytes(buf)
108-
}
109-
110-
#[inline]
111-
pub fn amount(&self) -> u64 {
112-
let mut buf = [0u8; 8];
113-
unsafe {
114-
core::ptr::copy_nonoverlapping(self.raw.add(4), buf.as_mut_ptr(), 8);
115-
}
116-
u64::from_le_bytes(buf)
117-
}
118-
119-
#[inline]
120-
pub fn validator(&self) -> Option<[u8; 32]> {
121-
if self.len == 12 {
122-
return None;
123-
}
124-
125-
let mut validator = [0u8; 32];
126-
unsafe {
127-
core::ptr::copy_nonoverlapping(self.raw.add(12), validator.as_mut_ptr(), 32);
128-
}
129-
Some(validator)
130-
}
131-
132-
#[inline]
133-
pub(crate) fn common_args(&self) -> DepositAndDelegateShuttleCommonArgs {
78+
pub(crate) fn common_args(&self) -> DepositAndDelegateShuttleCommonArgs<'_> {
13479
DepositAndDelegateShuttleCommonArgs {
13580
shuttle_id: self.shuttle_id(),
13681
amount: self.amount(),
@@ -141,7 +86,7 @@ impl DepositAndDelegateShuttleArgs<'_> {
14186

14287
pub(crate) fn process_deposit_and_delegate_shuttle_ephemeral_ata_with_post_actions(
14388
accounts: &DepositAndDelegateShuttleAccounts<'_>,
144-
args: DepositAndDelegateShuttleCommonArgs,
89+
args: DepositAndDelegateShuttleCommonArgs<'_>,
14590
extra_setup_lamports: u64,
14691
post_actions: PostDelegationActions,
14792
) -> ProgramResult {
@@ -376,7 +321,7 @@ pub(crate) fn delegate_sponsored_shuttle_with_post_actions(
376321

377322
let seeds: &[&[u8]] = &[shuttle_info.address().as_ref(), mint.as_ref()];
378323
let config = DelegateConfig {
379-
validator: args.validator.map(Address::new_from_array),
324+
validator: args.validator.map(|slice| Address::new_from_array(*slice)),
380325
..DelegateConfig::default()
381326
};
382327

e-token/src/processor/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub(crate) mod utils;
3535
pub mod withdraw_spl_tokens;
3636
pub mod withdraw_through_delegated_shuttle_with_merge;
3737

38+
pub use internal::DepositAndDelegateShuttleArgs;
39+
3840
pub use allocate_transfer_queue::process_allocate_transfer_queue;
3941
pub use close_ephemeral_ata::process_close_ephemeral_ata;
4042
pub use close_lamports_pda_intent::process_close_lamports_pda_intent;

e-token/src/processor/withdraw_through_delegated_shuttle_with_merge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn process_withdraw_through_delegated_shuttle_with_merge(
8383
token_program_info,
8484
] = require_n_accounts!(accounts, 16);
8585

86-
let args = DepositAndDelegateShuttleArgs::try_from_bytes(instruction_data)?;
86+
let args = DepositAndDelegateShuttleArgs::try_view_from(instruction_data)?;
8787

8888
let accounts = WithdrawThroughDelegatedShuttleAccounts {
8989
payer_info,

e-token/tests/deposit_and_delegate_shuttle_ephemeral_ata_with_merge.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ephemeral_spl_api::state::ephemeral_ata::EphemeralAta;
88
use ephemeral_spl_api::state::load_initialized;
99
use ephemeral_spl_api::state::shuttle_ephemeral_ata::ShuttleMetadata;
1010
use ephemeral_spl_api::ID as PROGRAM;
11+
use ephemeral_token_program::DepositAndDelegateShuttleArgs;
1112
use solana_account::Account;
1213
use solana_instruction::{AccountMeta, Instruction};
1314
use solana_program::rent::Rent;
@@ -153,12 +154,6 @@ async fn deposit_and_delegate_shuttle_ephemeral_ata_with_merge_deposits_and_stor
153154
let delegation_record_pda = delegation_record_pda_from_delegated_account(&shuttle_eata);
154155
let delegation_metadata_pda = delegation_metadata_pda_from_delegated_account(&shuttle_eata);
155156

156-
let mut delegate_data =
157-
instruction::ESplInstruction::SetupAndDelegateShuttleEphemeralAtaWithMerge.to_vec();
158-
delegate_data.extend_from_slice(&shuttle_id.to_le_bytes());
159-
delegate_data.extend_from_slice(&DEPOSIT_AMOUNT.to_le_bytes());
160-
delegate_data.extend_from_slice(&validator.to_bytes());
161-
162157
let ix_delegate = Instruction {
163158
program_id: PROGRAM,
164159
accounts: vec![
@@ -182,7 +177,15 @@ async fn deposit_and_delegate_shuttle_ephemeral_ata_with_merge_deposits_and_stor
182177
AccountMeta::new(owner_source_ata, false),
183178
AccountMeta::new(vault_ata, false),
184179
],
185-
data: delegate_data,
180+
data: instruction::ESplInstruction::SetupAndDelegateShuttleEphemeralAtaWithMerge.with_data(
181+
&DepositAndDelegateShuttleArgs {
182+
shuttle_id,
183+
amount: DEPOSIT_AMOUNT,
184+
validator: Some(validator.to_bytes()),
185+
}
186+
.encode()
187+
.unwrap(),
188+
),
186189
};
187190

188191
let tx_delegate = Transaction::new_signed_with_payer(

e-token/tests/withdraw_through_delegated_shuttle_with_merge.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ephemeral_spl_api::{
1212
},
1313
};
1414
use ephemeral_spl_api::{state::load_initialized, ID as PROGRAM};
15+
use ephemeral_token_program::DepositAndDelegateShuttleArgs;
1516
use magicblock_magic_program_api::{
1617
args::{MagicIntentBundleArgs, UndelegateTypeArgs},
1718
instruction::MagicBlockInstruction,
@@ -250,12 +251,6 @@ async fn withdraw_through_delegated_shuttle_with_merge_stores_transfer_and_clean
250251
&ephemeral_spl_api::program::DELEGATION_PROGRAM_ID,
251252
);
252253

253-
let mut withdraw_data =
254-
instruction::ESplInstruction::WithdrawThroughDelegatedShuttleWithMerge.to_vec();
255-
withdraw_data.extend_from_slice(&shuttle_id.to_le_bytes());
256-
withdraw_data.extend_from_slice(&TRANSFER_AMOUNT.to_le_bytes());
257-
withdraw_data.extend_from_slice(&validator.to_bytes());
258-
259254
let ix_withdraw = Instruction {
260255
program_id: PROGRAM,
261256
accounts: vec![
@@ -276,7 +271,15 @@ async fn withdraw_through_delegated_shuttle_with_merge_stores_transfer_and_clean
276271
AccountMeta::new_readonly(mint, false),
277272
AccountMeta::new_readonly(spl_token_interface::ID, false),
278273
],
279-
data: withdraw_data,
274+
data: instruction::ESplInstruction::WithdrawThroughDelegatedShuttleWithMerge.with_data(
275+
&DepositAndDelegateShuttleArgs {
276+
shuttle_id,
277+
amount: TRANSFER_AMOUNT,
278+
validator: Some(validator.to_bytes()),
279+
}
280+
.encode()
281+
.unwrap(),
282+
),
280283
};
281284

282285
let tx_withdraw = Transaction::new_signed_with_payer(

0 commit comments

Comments
 (0)