11//! Program instruction helpers.
22
33use {
4- crate :: {
5- id,
6- state:: { ConfigKeys , ConfigState } ,
7- } ,
4+ crate :: { ConfigKeys , ShortVec , ID } ,
85 bincode:: serialized_size,
96 solana_program:: {
107 instruction:: { AccountMeta , Instruction } ,
@@ -13,10 +10,21 @@ use {
1310 } ,
1411} ;
1512
13+ /// Trait defining config state to be stored at the end of the account data.
14+ pub trait ConfigState : serde:: Serialize + Default {
15+ /// Maximum space that the serialized representation will require
16+ fn max_space ( ) -> u64 ;
17+ }
18+
1619fn initialize_account < T : ConfigState > ( config_pubkey : & Pubkey ) -> Instruction {
1720 let account_metas = vec ! [ AccountMeta :: new( * config_pubkey, true ) ] ;
18- let account_data = ( ConfigKeys { keys : vec ! [ ] } , T :: default ( ) ) ;
19- Instruction :: new_with_bincode ( id ( ) , & account_data, account_metas)
21+ let account_data = (
22+ ConfigKeys {
23+ keys : ShortVec ( vec ! [ ] ) ,
24+ } ,
25+ T :: default ( ) ,
26+ ) ;
27+ Instruction :: new_with_bincode ( ID , & account_data, account_metas)
2028}
2129
2230/// Create a new, empty configuration account
@@ -26,14 +34,19 @@ pub fn create_account<T: ConfigState>(
2634 lamports : u64 ,
2735 keys : Vec < ( Pubkey , bool ) > ,
2836) -> Vec < Instruction > {
29- let space = T :: max_space ( ) . saturating_add ( serialized_size ( & ConfigKeys { keys } ) . unwrap ( ) ) ;
37+ let space = T :: max_space ( ) . saturating_add (
38+ serialized_size ( & ConfigKeys {
39+ keys : ShortVec ( keys) ,
40+ } )
41+ . unwrap ( ) ,
42+ ) ;
3043 vec ! [
3144 system_instruction:: create_account(
3245 from_account_pubkey,
3346 config_account_pubkey,
3447 lamports,
3548 space,
36- & id ( ) ,
49+ & ID ,
3750 ) ,
3851 initialize_account:: <T >( config_account_pubkey) ,
3952 ]
@@ -52,6 +65,11 @@ pub fn store<T: ConfigState>(
5265 account_metas. push ( AccountMeta :: new ( * signer_pubkey, true ) ) ;
5366 }
5467 }
55- let account_data = ( ConfigKeys { keys } , data) ;
56- Instruction :: new_with_bincode ( id ( ) , & account_data, account_metas)
68+ let account_data = (
69+ ConfigKeys {
70+ keys : ShortVec ( keys) ,
71+ } ,
72+ data,
73+ ) ;
74+ Instruction :: new_with_bincode ( ID , & account_data, account_metas)
5775}
0 commit comments