11use std:: sync:: Arc ;
22
33use async_trait:: async_trait;
4- use futures_util:: future:: join_all;
54use log:: error;
65use magicblock_program:: magic_scheduled_base_intent:: {
76 CommitType , CommittedAccount , MagicBaseIntent , ScheduledBaseIntent ,
87 UndelegateType ,
98} ;
9+ use solana_account:: Account ;
1010use solana_pubkey:: Pubkey ;
1111use solana_signature:: Signature ;
1212
@@ -51,31 +51,18 @@ pub struct TaskBuilderImpl;
5151pub const COMMIT_STATE_SIZE_THRESHOLD : usize = 256 ;
5252
5353impl TaskBuilderImpl {
54- pub async fn create_commit_task < C : TaskInfoFetcher > (
54+ pub fn create_commit_task (
5555 commit_id : u64 ,
5656 allow_undelegation : bool ,
5757 account : CommittedAccount ,
58- task_info_fetcher : & Arc < C > ,
58+ base_account : Option < Account > ,
5959 ) -> ArgsTask {
60- let base_account = if account. account . data . len ( )
61- > COMMIT_STATE_SIZE_THRESHOLD
62- {
63- match task_info_fetcher. get_base_account ( & account. pubkey ) . await {
64- Ok ( Some ( account) ) => Some ( account) ,
65- Ok ( None ) => {
66- log:: warn!( "AccountNotFound for commit_diff, pubkey: {}, commit_id: {}, Falling back to commit_state." ,
67- account. pubkey, commit_id) ;
68- None
69- }
70- Err ( e) => {
71- log:: warn!( "Failed to fetch base account for commit diff, pubkey: {}, commit_id: {}, error: {}. Falling back to commit_state." ,
72- account. pubkey, commit_id, e) ;
73- None
74- }
75- }
76- } else {
77- None
78- } ;
60+ let base_account =
61+ if account. account . data . len ( ) > COMMIT_STATE_SIZE_THRESHOLD {
62+ base_account
63+ } else {
64+ None
65+ } ;
7966
8067 if let Some ( base_account) = base_account {
8168 ArgsTaskType :: CommitDiff ( CommitDiffTask {
@@ -123,14 +110,37 @@ impl TasksBuilder for TaskBuilderImpl {
123110 }
124111 } ;
125112
126- let committed_pubkeys = accounts
127- . iter ( )
128- . map ( |account| account. pubkey )
129- . collect :: < Vec < _ > > ( ) ;
130- let commit_ids = commit_id_fetcher
131- . fetch_next_commit_ids ( & committed_pubkeys)
132- . await
133- . map_err ( TaskBuilderError :: CommitTasksBuildError ) ?;
113+ let ( commit_ids, base_accounts) = {
114+ let committed_pubkeys = accounts
115+ . iter ( )
116+ . map ( |account| account. pubkey )
117+ . collect :: < Vec < _ > > ( ) ;
118+
119+ let diffable_pubkeys = accounts
120+ . iter ( )
121+ . filter ( |account| {
122+ account. account . data . len ( ) >= COMMIT_STATE_SIZE_THRESHOLD
123+ } )
124+ . map ( |account| account. pubkey )
125+ . collect :: < Vec < _ > > ( ) ;
126+
127+ tokio:: join!(
128+ commit_id_fetcher. fetch_next_commit_ids( & committed_pubkeys) ,
129+ commit_id_fetcher
130+ . get_base_accounts( diffable_pubkeys. as_slice( ) )
131+ )
132+ } ;
133+
134+ let commit_ids =
135+ commit_ids. map_err ( TaskBuilderError :: CommitTasksBuildError ) ?;
136+
137+ let base_accounts = match base_accounts {
138+ Ok ( map) => map,
139+ Err ( err) => {
140+ log:: warn!( "Failed to fetch base accounts for CommitDiff (id={}): {}; falling back to CommitState" , base_intent. id, err) ;
141+ Default :: default ( )
142+ }
143+ } ;
134144
135145 // Persist commit ids for commitees
136146 commit_ids
@@ -141,13 +151,17 @@ impl TasksBuilder for TaskBuilderImpl {
141151 }
142152 } ) ;
143153
144- let tasks = join_all ( accounts
154+ let tasks = accounts
145155 . iter ( )
146- . map ( |account| async {
156+ . map ( |account| {
147157 let commit_id = * commit_ids. get ( & account. pubkey ) . expect ( "CommitIdFetcher provide commit ids for all listed pubkeys, or errors!" ) ;
148- let task = Self :: create_commit_task ( commit_id, allow_undelegation, account. clone ( ) , commit_id_fetcher) . await ;
158+ // TODO (snawaz): if accounts do not have duplicate, then we can use remove
159+ // instead:
160+ // let base_account = base_accounts.remove(&account.pubkey);
161+ let base_account = base_accounts. get ( & account. pubkey ) . cloned ( ) ;
162+ let task = Self :: create_commit_task ( commit_id, allow_undelegation, account. clone ( ) , base_account) ;
149163 Box :: new ( task) as Box < dyn BaseTask >
150- } ) ) . await ;
164+ } ) . collect ( ) ;
151165
152166 Ok ( tasks)
153167 }
0 commit comments