@@ -9,6 +9,23 @@ use crate::pubkey::Pubkey;
99use core:: { mem, ptr} ;
1010use std:: vec:: Vec ;
1111
12+ /// Matches the pinocchio Account struct.
13+ /// Account fields are private, so this struct allows more readable
14+ /// use of them in tests.
15+ #[ repr( C ) ]
16+ #[ derive( Clone , Copy ) ]
17+ struct AccountLayout {
18+ borrow_state : u8 ,
19+ is_signer : u8 ,
20+ is_writable : u8 ,
21+ executable : u8 ,
22+ resize_delta : i32 ,
23+ key : Pubkey ,
24+ owner : Pubkey ,
25+ lamports : u64 ,
26+ data_len : u64 ,
27+ }
28+
1229/// Strategy that decides how much the slot number is decremented between
1330/// successive entries in `generate_mock_entries`.
1431#[ allow( dead_code) ]
@@ -103,29 +120,15 @@ pub unsafe fn make_account_info(
103120 data : & [ u8 ] ,
104121 borrow_state : u8 ,
105122) -> ( AccountInfo , Vec < u64 > ) {
106- #[ repr( C ) ]
107- #[ derive( Clone , Copy ) ]
108- struct Header {
109- borrow_state : u8 ,
110- is_signer : u8 ,
111- is_writable : u8 ,
112- executable : u8 ,
113- resize_delta : i32 ,
114- key : Pubkey ,
115- owner : Pubkey ,
116- lamports : u64 ,
117- data_len : u64 ,
118- }
119-
120- let hdr_size = mem:: size_of :: < Header > ( ) ;
123+ let hdr_size = mem:: size_of :: < AccountLayout > ( ) ;
121124 let total = hdr_size + data. len ( ) ;
122125 let words = ( total + 7 ) / 8 ;
123126 let mut backing: Vec < u64 > = std:: vec![ 0u64 ; words] ;
124127
125- let hdr_ptr = backing. as_mut_ptr ( ) as * mut Header ;
128+ let hdr_ptr = backing. as_mut_ptr ( ) as * mut AccountLayout ;
126129 ptr:: write (
127130 hdr_ptr,
128- Header {
131+ AccountLayout {
129132 borrow_state,
130133 is_signer : 0 ,
131134 is_writable : 0 ,
@@ -151,3 +154,43 @@ pub unsafe fn make_account_info(
151154 backing,
152155 )
153156}
157+
158+ #[ cfg( test) ]
159+ #[ test]
160+ fn test_account_layout_compatibility ( ) {
161+ assert_eq ! (
162+ mem:: size_of:: <AccountLayout >( ) ,
163+ mem:: size_of:: <Account >( ) ,
164+ "Header size must match Account size"
165+ ) ;
166+ assert_eq ! (
167+ mem:: align_of:: <AccountLayout >( ) ,
168+ mem:: align_of:: <Account >( ) ,
169+ "Header alignment must match Account alignment"
170+ ) ;
171+
172+ unsafe {
173+ let test_header = AccountLayout {
174+ borrow_state : 42 ,
175+ is_signer : 1 ,
176+ is_writable : 1 ,
177+ executable : 0 ,
178+ resize_delta : 100 ,
179+ key : [ 1u8 ; 32 ] ,
180+ owner : [ 2u8 ; 32 ] ,
181+ lamports : 1000 ,
182+ data_len : 256 ,
183+ } ;
184+
185+ let account_ptr = & test_header as * const AccountLayout as * const Account ;
186+ let account_ref = & * account_ptr;
187+ assert_eq ! (
188+ account_ref. borrow_state, 42 ,
189+ "borrow_state field should be accessible and match"
190+ ) ;
191+ assert_eq ! (
192+ account_ref. data_len, 256 ,
193+ "data_len field should be accessible and match"
194+ ) ;
195+ }
196+ }
0 commit comments