@@ -17,7 +17,7 @@ use magicblock_committor_program::{
1717use magicblock_program:: magic_scheduled_base_intent:: {
1818 BaseAction , CommittedAccount ,
1919} ;
20- use solana_account:: ReadableAccount ;
20+ use solana_account:: { Account , ReadableAccount } ;
2121use solana_pubkey:: Pubkey ;
2222use solana_rpc_client:: rpc_client:: RpcClient ;
2323use solana_sdk:: {
@@ -114,61 +114,93 @@ pub struct CommitTask {
114114 pub commit_id : u64 ,
115115 pub allow_undelegation : bool ,
116116 pub committed_account : CommittedAccount ,
117+ fetched_account : Option < Account > ,
117118}
118119
119120impl CommitTask {
120121 const COMMIT_STATE_SIZE_THRESHOLD : usize = 200 ;
121122
123+ pub fn new (
124+ commit_id : u64 ,
125+ allow_undelegation : bool ,
126+ committed_account : CommittedAccount ,
127+ ) -> Self {
128+ let chain_config =
129+ ChainConfig :: local ( ComputeBudgetConfig :: new ( 1_000_000 ) ) ;
130+
131+ let rpc_client = RpcClient :: new_with_commitment (
132+ chain_config. rpc_uri . to_string ( ) ,
133+ CommitmentConfig {
134+ commitment : chain_config. commitment ,
135+ } ,
136+ ) ;
137+
138+ let fetched_account = if committed_account. account . data . len ( )
139+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
140+ {
141+ rpc_client. get_account ( & committed_account. pubkey ) . ok ( )
142+ } else {
143+ None
144+ } ;
145+
146+ Self {
147+ commit_id,
148+ allow_undelegation,
149+ committed_account,
150+ fetched_account,
151+ }
152+ }
153+
154+ // TODO (snawaz): it is infinitely bad implementation
155+ // as it's making a network call, but we'll fix it soon once
156+ // we start using caching and fetched accounts.
122157 pub fn is_commit_diff ( & self ) -> bool {
123158 self . committed_account . account . data . len ( )
124159 > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
160+ && self . fetched_account . is_some ( )
125161 }
126162
127- pub fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
163+ pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
164+ if let Some ( fetched_account) = self . fetched_account . as_ref ( ) {
165+ self . create_commit_diff_ix ( validator, fetched_account)
166+ } else {
167+ self . create_commit_state_ix ( validator)
168+ }
169+ }
170+
171+ fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
128172 let args = CommitStateArgs {
129173 nonce : self . commit_id ,
130174 lamports : self . committed_account . account . lamports ,
131175 data : self . committed_account . account . data . clone ( ) ,
132176 allow_undelegation : self . allow_undelegation ,
133177 } ;
178+ println ! ( "create_commit_state_ix, data: {}" , args. data. len( ) ) ;
134179 dlp:: instruction_builder:: commit_state (
135180 * validator,
136181 self . committed_account . pubkey ,
137182 self . committed_account . account . owner ,
138183 args,
139184 )
140185 }
141- pub fn create_commit_diff_ix ( & self , validator : & Pubkey ) -> Instruction {
142- let chain_config =
143- ChainConfig :: local ( ComputeBudgetConfig :: new ( 1_000_000 ) ) ;
144-
145- let rpc_client = RpcClient :: new_with_commitment (
146- chain_config. rpc_uri . to_string ( ) ,
147- CommitmentConfig {
148- commitment : chain_config. commitment ,
149- } ,
150- ) ;
151-
152- let account = match rpc_client
153- . get_account ( & self . committed_account . pubkey )
154- {
155- Ok ( account) => account,
156- Err ( e) => {
157- log:: warn!( "Fallback to commit_state and send full-bytes, as rpc failed to fetch the delegated-account from base chain, commmit_id: {} , error: {}" , self . commit_id, e) ;
158- return self . create_commit_state_ix ( validator) ;
159- }
160- } ;
161186
187+ fn create_commit_diff_ix (
188+ & self ,
189+ validator : & Pubkey ,
190+ fetched_account : & Account ,
191+ ) -> Instruction {
162192 let args = CommitDiffArgs {
163193 nonce : self . commit_id ,
164194 lamports : self . committed_account . account . lamports ,
165195 diff : compute_diff (
166- account . data ( ) ,
196+ fetched_account . data ( ) ,
167197 self . committed_account . account . data ( ) ,
168198 )
169199 . to_vec ( ) ,
170200 allow_undelegation : self . allow_undelegation ,
171201 } ;
202+ println ! ( "create_commit_diff_ix, diff: {}" , args. diff. len( ) ) ;
203+
172204 dlp:: instruction_builder:: commit_diff (
173205 * validator,
174206 self . committed_account . pubkey ,
@@ -382,10 +414,10 @@ mod serialization_safety_test {
382414 let validator = Pubkey :: new_unique ( ) ;
383415
384416 // Test Commit variant
385- let commit_task: ArgsTask = ArgsTaskType :: Commit ( CommitTask {
386- commit_id : 123 ,
387- allow_undelegation : true ,
388- committed_account : CommittedAccount {
417+ let commit_task: ArgsTask = ArgsTaskType :: Commit ( CommitTask :: new (
418+ 123 ,
419+ true ,
420+ CommittedAccount {
389421 pubkey : Pubkey :: new_unique ( ) ,
390422 account : Account {
391423 lamports : 1000 ,
@@ -395,7 +427,7 @@ mod serialization_safety_test {
395427 rent_epoch : 0 ,
396428 } ,
397429 } ,
398- } )
430+ ) )
399431 . into ( ) ;
400432 assert_serializable ( & commit_task. instruction ( & validator) ) ;
401433
@@ -442,10 +474,10 @@ mod serialization_safety_test {
442474 let validator = Pubkey :: new_unique ( ) ;
443475
444476 let buffer_task = BufferTask :: new_preparation_required (
445- BufferTaskType :: Commit ( CommitTask {
446- commit_id : 456 ,
447- allow_undelegation : false ,
448- committed_account : CommittedAccount {
477+ BufferTaskType :: Commit ( CommitTask :: new (
478+ 456 ,
479+ false ,
480+ CommittedAccount {
449481 pubkey : Pubkey :: new_unique ( ) ,
450482 account : Account {
451483 lamports : 2000 ,
@@ -455,7 +487,7 @@ mod serialization_safety_test {
455487 rent_epoch : 0 ,
456488 } ,
457489 } ,
458- } ) ,
490+ ) ) ,
459491 ) ;
460492 assert_serializable ( & buffer_task. instruction ( & validator) ) ;
461493 }
@@ -467,10 +499,10 @@ mod serialization_safety_test {
467499
468500 // Test BufferTask preparation
469501 let buffer_task = BufferTask :: new_preparation_required (
470- BufferTaskType :: Commit ( CommitTask {
471- commit_id : 789 ,
472- allow_undelegation : true ,
473- committed_account : CommittedAccount {
502+ BufferTaskType :: Commit ( CommitTask :: new (
503+ 789 ,
504+ true ,
505+ CommittedAccount {
474506 pubkey : Pubkey :: new_unique ( ) ,
475507 account : Account {
476508 lamports : 3000 ,
@@ -480,7 +512,7 @@ mod serialization_safety_test {
480512 rent_epoch : 0 ,
481513 } ,
482514 } ,
483- } ) ,
515+ ) ) ,
484516 ) ;
485517
486518 let PreparationState :: Required ( preparation_task) =
0 commit comments