11use dlp:: {
2- args:: { CommitDiffArgs , CommitStateArgs } ,
2+ args:: { CommitDiffArgs , CommitStateArgs , CommitStateFromBufferArgs } ,
33 compute_diff,
44} ;
55use dyn_clone:: DynClone ;
@@ -51,7 +51,6 @@ pub enum PreparationState {
5151 Cleanup ( CleanupTask ) ,
5252}
5353
54- #[ cfg( test) ]
5554#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5655pub enum TaskStrategy {
5756 Args ,
@@ -114,7 +113,7 @@ pub struct CommitTask {
114113 pub allow_undelegation : bool ,
115114 pub committed_account : CommittedAccount ,
116115 base_account : Option < Account > ,
117- force_commit_state : bool ,
116+ strategy : TaskStrategy ,
118117}
119118
120119impl CommitTask {
@@ -151,29 +150,52 @@ impl CommitTask {
151150 allow_undelegation,
152151 committed_account,
153152 base_account,
154- force_commit_state : false ,
153+ strategy : TaskStrategy :: Args ,
155154 }
156155 }
157156
158157 pub fn is_commit_diff ( & self ) -> bool {
159- !self . force_commit_state
160- && self . committed_account . account . data . len ( )
161- > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
158+ self . committed_account . account . data . len ( )
159+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
162160 && self . base_account . is_some ( )
163161 }
164162
165- pub fn force_commit_state ( & mut self ) {
166- self . force_commit_state = true ;
163+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
164+ self . strategy = TaskStrategy :: Buffer ;
165+ self
167166 }
168167
169168 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
170- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
171- self . create_commit_diff_ix ( validator, fetched_account)
172- } else {
173- self . create_commit_state_ix ( validator)
169+ match self . strategy {
170+ TaskStrategy :: Args => {
171+ if let Some ( base_account) = self . base_account . as_ref ( ) {
172+ self . create_commit_diff_ix ( validator, base_account)
173+ } else {
174+ self . create_commit_state_ix ( validator)
175+ }
176+ }
177+ TaskStrategy :: Buffer => {
178+ if let Some ( base_account) = self . base_account . as_ref ( ) {
179+ self . create_commit_diff_from_buffer_ix (
180+ validator,
181+ base_account,
182+ )
183+ } else {
184+ self . create_commit_state_from_buffer_ix ( validator)
185+ }
186+ }
174187 }
175188 }
176189
190+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
191+ self . base_account . as_ref ( ) . map ( |base_account| {
192+ compute_diff (
193+ base_account. data ( ) ,
194+ self . committed_account . account . data ( ) ,
195+ )
196+ } )
197+ }
198+
177199 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
178200 let args = CommitStateArgs {
179201 nonce : self . commit_id ,
@@ -192,17 +214,13 @@ impl CommitTask {
192214 fn create_commit_diff_ix (
193215 & self ,
194216 validator : & Pubkey ,
195- fetched_account : & Account ,
217+ base_account : & Account ,
196218 ) -> Instruction {
197- if self . force_commit_state {
198- return self . create_commit_state_ix ( validator) ;
199- }
200-
201219 let args = CommitDiffArgs {
202220 nonce : self . commit_id ,
203221 lamports : self . committed_account . account . lamports ,
204222 diff : compute_diff (
205- fetched_account . data ( ) ,
223+ base_account . data ( ) ,
206224 self . committed_account . account . data ( ) ,
207225 )
208226 . to_vec ( ) ,
@@ -216,6 +234,57 @@ impl CommitTask {
216234 args,
217235 )
218236 }
237+
238+ fn create_commit_state_from_buffer_ix (
239+ & self ,
240+ validator : & Pubkey ,
241+ ) -> Instruction {
242+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
243+ let ( commit_buffer_pubkey, _) =
244+ magicblock_committor_program:: pdas:: buffer_pda (
245+ validator,
246+ & self . committed_account . pubkey ,
247+ & commit_id_slice,
248+ ) ;
249+
250+ dlp:: instruction_builder:: commit_state_from_buffer (
251+ * validator,
252+ self . committed_account . pubkey ,
253+ self . committed_account . account . owner ,
254+ commit_buffer_pubkey,
255+ CommitStateFromBufferArgs {
256+ nonce : self . commit_id ,
257+ lamports : self . committed_account . account . lamports ,
258+ allow_undelegation : self . allow_undelegation ,
259+ } ,
260+ )
261+ }
262+
263+ fn create_commit_diff_from_buffer_ix (
264+ & self ,
265+ validator : & Pubkey ,
266+ _fetched_account : & Account ,
267+ ) -> Instruction {
268+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
269+ let ( commit_buffer_pubkey, _) =
270+ magicblock_committor_program:: pdas:: buffer_pda (
271+ validator,
272+ & self . committed_account . pubkey ,
273+ & commit_id_slice,
274+ ) ;
275+
276+ dlp:: instruction_builder:: commit_diff_from_buffer (
277+ * validator,
278+ self . committed_account . pubkey ,
279+ self . committed_account . account . owner ,
280+ commit_buffer_pubkey,
281+ CommitStateFromBufferArgs {
282+ nonce : self . commit_id ,
283+ lamports : self . committed_account . account . lamports ,
284+ allow_undelegation : self . allow_undelegation ,
285+ } ,
286+ )
287+ }
219288}
220289
221290#[ derive( Clone ) ]
0 commit comments