11#[ cfg( feature = "logging" ) ]
22use alloc:: string:: ToString ;
3+ use data_layout:: fixed_layout;
34
4- use core:: { marker :: PhantomData , mem:: MaybeUninit } ;
5+ use core:: mem:: MaybeUninit ;
56use dlp_api:: args:: PostDelegationActions ;
67use 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
14287pub ( 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
0 commit comments