11use dlp:: {
2- args:: { CommitDiffArgs , CommitStateArgs } ,
2+ args:: { CommitDiffArgs , CommitStateArgs , CommitStateFromBufferArgs } ,
33 compute_diff,
44} ;
55use dyn_clone:: DynClone ;
@@ -47,7 +47,6 @@ pub enum PreparationState {
4747 Cleanup ( CleanupTask ) ,
4848}
4949
50- #[ cfg( test) ]
5150#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5251pub enum TaskStrategy {
5352 Args ,
@@ -110,7 +109,7 @@ pub struct CommitTask {
110109 pub allow_undelegation : bool ,
111110 pub committed_account : CommittedAccount ,
112111 base_account : Option < Account > ,
113- force_commit_state : bool ,
112+ strategy : TaskStrategy ,
114113}
115114
116115impl CommitTask {
@@ -154,29 +153,52 @@ impl CommitTask {
154153 allow_undelegation,
155154 committed_account,
156155 base_account : fetched_account,
157- force_commit_state : false ,
156+ strategy : TaskStrategy :: Args ,
158157 }
159158 }
160159
161160 pub fn is_commit_diff ( & self ) -> bool {
162- !self . force_commit_state
163- && self . committed_account . account . data . len ( )
164- > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
161+ self . committed_account . account . data . len ( )
162+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
165163 && self . base_account . is_some ( )
166164 }
167165
168- pub fn force_commit_state ( & mut self ) {
169- self . force_commit_state = true ;
166+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
167+ self . strategy = TaskStrategy :: Buffer ;
168+ self
170169 }
171170
172171 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
173- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
174- self . create_commit_diff_ix ( validator, fetched_account)
175- } else {
176- self . create_commit_state_ix ( validator)
172+ match self . strategy {
173+ TaskStrategy :: Args => {
174+ if let Some ( base_account) = self . base_account . as_ref ( ) {
175+ self . create_commit_diff_ix ( validator, base_account)
176+ } else {
177+ self . create_commit_state_ix ( validator)
178+ }
179+ }
180+ TaskStrategy :: Buffer => {
181+ if let Some ( base_account) = self . base_account . as_ref ( ) {
182+ self . create_commit_diff_from_buffer_ix (
183+ validator,
184+ base_account,
185+ )
186+ } else {
187+ self . create_commit_state_from_buffer_ix ( validator)
188+ }
189+ }
177190 }
178191 }
179192
193+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
194+ self . base_account . as_ref ( ) . map ( |base_account| {
195+ compute_diff (
196+ base_account. data ( ) ,
197+ self . committed_account . account . data ( ) ,
198+ )
199+ } )
200+ }
201+
180202 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
181203 let args = CommitStateArgs {
182204 nonce : self . commit_id ,
@@ -195,17 +217,13 @@ impl CommitTask {
195217 fn create_commit_diff_ix (
196218 & self ,
197219 validator : & Pubkey ,
198- fetched_account : & Account ,
220+ base_account : & Account ,
199221 ) -> Instruction {
200- if self . force_commit_state {
201- return self . create_commit_state_ix ( validator) ;
202- }
203-
204222 let args = CommitDiffArgs {
205223 nonce : self . commit_id ,
206224 lamports : self . committed_account . account . lamports ,
207225 diff : compute_diff (
208- fetched_account . data ( ) ,
226+ base_account . data ( ) ,
209227 self . committed_account . account . data ( ) ,
210228 )
211229 . to_vec ( ) ,
@@ -219,6 +237,57 @@ impl CommitTask {
219237 args,
220238 )
221239 }
240+
241+ fn create_commit_state_from_buffer_ix (
242+ & self ,
243+ validator : & Pubkey ,
244+ ) -> Instruction {
245+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
246+ let ( commit_buffer_pubkey, _) =
247+ magicblock_committor_program:: pdas:: buffer_pda (
248+ validator,
249+ & self . committed_account . pubkey ,
250+ & commit_id_slice,
251+ ) ;
252+
253+ dlp:: instruction_builder:: commit_state_from_buffer (
254+ * validator,
255+ self . committed_account . pubkey ,
256+ self . committed_account . account . owner ,
257+ commit_buffer_pubkey,
258+ CommitStateFromBufferArgs {
259+ nonce : self . commit_id ,
260+ lamports : self . committed_account . account . lamports ,
261+ allow_undelegation : self . allow_undelegation ,
262+ } ,
263+ )
264+ }
265+
266+ fn create_commit_diff_from_buffer_ix (
267+ & self ,
268+ validator : & Pubkey ,
269+ _fetched_account : & Account ,
270+ ) -> Instruction {
271+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
272+ let ( commit_buffer_pubkey, _) =
273+ magicblock_committor_program:: pdas:: buffer_pda (
274+ validator,
275+ & self . committed_account . pubkey ,
276+ & commit_id_slice,
277+ ) ;
278+
279+ dlp:: instruction_builder:: commit_diff_from_buffer (
280+ * validator,
281+ self . committed_account . pubkey ,
282+ self . committed_account . account . owner ,
283+ commit_buffer_pubkey,
284+ CommitStateFromBufferArgs {
285+ nonce : self . commit_id ,
286+ lamports : self . committed_account . account . lamports ,
287+ allow_undelegation : self . allow_undelegation ,
288+ } ,
289+ )
290+ }
222291}
223292
224293#[ derive( Clone ) ]
0 commit comments