11use std:: sync:: Arc ;
22
33use dlp:: {
4- args:: { CommitDiffArgs , CommitStateArgs } ,
4+ args:: { CommitDiffArgs , CommitStateArgs , CommitStateFromBufferArgs } ,
55 compute_diff,
66} ;
77use dyn_clone:: DynClone ;
@@ -53,7 +53,6 @@ pub enum PreparationState {
5353 Cleanup ( CleanupTask ) ,
5454}
5555
56- #[ cfg( test) ]
5756#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5857pub enum TaskStrategy {
5958 Args ,
@@ -153,7 +152,7 @@ impl CommitTaskBuilder {
153152 allow_undelegation,
154153 committed_account,
155154 base_account,
156- force_commit_state : false ,
155+ strategy : TaskStrategy :: Args ,
157156 }
158157 }
159158}
@@ -164,29 +163,46 @@ pub struct CommitTask {
164163 pub allow_undelegation : bool ,
165164 pub committed_account : CommittedAccount ,
166165 base_account : Option < Account > ,
167- force_commit_state : bool ,
166+ strategy : TaskStrategy ,
168167}
169168
170169impl CommitTask {
171- pub fn is_commit_diff ( & self ) -> bool {
172- !self . force_commit_state
173- && self . committed_account . account . data . len ( )
174- > CommitTaskBuilder :: COMMIT_STATE_SIZE_THRESHOLD
175- && self . base_account . is_some ( )
176- }
177-
178- pub fn force_commit_state ( & mut self ) {
179- self . force_commit_state = true ;
170+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
171+ self . strategy = TaskStrategy :: Buffer ;
172+ self
180173 }
181174
182175 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
183- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
184- self . create_commit_diff_ix ( validator, fetched_account)
185- } else {
186- self . create_commit_state_ix ( validator)
176+ match self . strategy {
177+ TaskStrategy :: Args => {
178+ if let Some ( base_account) = self . base_account . as_ref ( ) {
179+ self . create_commit_diff_ix ( validator, base_account)
180+ } else {
181+ self . create_commit_state_ix ( validator)
182+ }
183+ }
184+ TaskStrategy :: Buffer => {
185+ if let Some ( base_account) = self . base_account . as_ref ( ) {
186+ self . create_commit_diff_from_buffer_ix (
187+ validator,
188+ base_account,
189+ )
190+ } else {
191+ self . create_commit_state_from_buffer_ix ( validator)
192+ }
193+ }
187194 }
188195 }
189196
197+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
198+ self . base_account . as_ref ( ) . map ( |base_account| {
199+ compute_diff (
200+ base_account. data ( ) ,
201+ self . committed_account . account . data ( ) ,
202+ )
203+ } )
204+ }
205+
190206 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
191207 let args = CommitStateArgs {
192208 nonce : self . commit_id ,
@@ -205,17 +221,13 @@ impl CommitTask {
205221 fn create_commit_diff_ix (
206222 & self ,
207223 validator : & Pubkey ,
208- fetched_account : & Account ,
224+ base_account : & Account ,
209225 ) -> Instruction {
210- if self . force_commit_state {
211- return self . create_commit_state_ix ( validator) ;
212- }
213-
214226 let args = CommitDiffArgs {
215227 nonce : self . commit_id ,
216228 lamports : self . committed_account . account . lamports ,
217229 diff : compute_diff (
218- fetched_account . data ( ) ,
230+ base_account . data ( ) ,
219231 self . committed_account . account . data ( ) ,
220232 )
221233 . to_vec ( ) ,
@@ -229,6 +241,57 @@ impl CommitTask {
229241 args,
230242 )
231243 }
244+
245+ fn create_commit_state_from_buffer_ix (
246+ & self ,
247+ validator : & Pubkey ,
248+ ) -> Instruction {
249+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
250+ let ( commit_buffer_pubkey, _) =
251+ magicblock_committor_program:: pdas:: buffer_pda (
252+ validator,
253+ & self . committed_account . pubkey ,
254+ & commit_id_slice,
255+ ) ;
256+
257+ dlp:: instruction_builder:: commit_state_from_buffer (
258+ * validator,
259+ self . committed_account . pubkey ,
260+ self . committed_account . account . owner ,
261+ commit_buffer_pubkey,
262+ CommitStateFromBufferArgs {
263+ nonce : self . commit_id ,
264+ lamports : self . committed_account . account . lamports ,
265+ allow_undelegation : self . allow_undelegation ,
266+ } ,
267+ )
268+ }
269+
270+ fn create_commit_diff_from_buffer_ix (
271+ & self ,
272+ validator : & Pubkey ,
273+ _fetched_account : & Account ,
274+ ) -> Instruction {
275+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
276+ let ( commit_buffer_pubkey, _) =
277+ magicblock_committor_program:: pdas:: buffer_pda (
278+ validator,
279+ & self . committed_account . pubkey ,
280+ & commit_id_slice,
281+ ) ;
282+
283+ dlp:: instruction_builder:: commit_diff_from_buffer (
284+ * validator,
285+ self . committed_account . pubkey ,
286+ self . committed_account . account . owner ,
287+ commit_buffer_pubkey,
288+ CommitStateFromBufferArgs {
289+ nonce : self . commit_id ,
290+ lamports : self . committed_account . account . lamports ,
291+ allow_undelegation : self . allow_undelegation ,
292+ } ,
293+ )
294+ }
232295}
233296
234297#[ derive( Clone ) ]
0 commit comments