11use account_fetcher:: * ;
22use dlp:: {
3- args:: { CommitDiffArgs , CommitStateArgs } ,
3+ args:: { CommitDiffArgs , CommitStateArgs , CommitStateFromBufferArgs } ,
44 compute_diff,
55} ;
66use dyn_clone:: DynClone ;
@@ -50,7 +50,6 @@ pub enum PreparationState {
5050 Cleanup ( CleanupTask ) ,
5151}
5252
53- #[ cfg( test) ]
5453#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5554pub enum TaskStrategy {
5655 Args ,
@@ -113,7 +112,7 @@ pub struct CommitTask {
113112 pub allow_undelegation : bool ,
114113 pub committed_account : CommittedAccount ,
115114 base_account : Option < Account > ,
116- force_commit_state : bool ,
115+ strategy : TaskStrategy ,
117116}
118117
119118impl CommitTask {
@@ -152,29 +151,52 @@ impl CommitTask {
152151 allow_undelegation,
153152 committed_account,
154153 base_account,
155- force_commit_state : false ,
154+ strategy : TaskStrategy :: Args ,
156155 }
157156 }
158157
159158 pub fn is_commit_diff ( & self ) -> bool {
160- !self . force_commit_state
161- && self . committed_account . account . data . len ( )
162- > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
159+ self . committed_account . account . data . len ( )
160+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
163161 && self . base_account . is_some ( )
164162 }
165163
166- pub fn force_commit_state ( & mut self ) {
167- self . force_commit_state = true ;
164+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
165+ self . strategy = TaskStrategy :: Buffer ;
166+ self
168167 }
169168
170169 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
171- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
172- self . create_commit_diff_ix ( validator, fetched_account)
173- } else {
174- self . create_commit_state_ix ( validator)
170+ match self . strategy {
171+ TaskStrategy :: Args => {
172+ if let Some ( base_account) = self . base_account . as_ref ( ) {
173+ self . create_commit_diff_ix ( validator, base_account)
174+ } else {
175+ self . create_commit_state_ix ( validator)
176+ }
177+ }
178+ TaskStrategy :: Buffer => {
179+ if let Some ( base_account) = self . base_account . as_ref ( ) {
180+ self . create_commit_diff_from_buffer_ix (
181+ validator,
182+ base_account,
183+ )
184+ } else {
185+ self . create_commit_state_from_buffer_ix ( validator)
186+ }
187+ }
175188 }
176189 }
177190
191+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
192+ self . base_account . as_ref ( ) . map ( |base_account| {
193+ compute_diff (
194+ base_account. data ( ) ,
195+ self . committed_account . account . data ( ) ,
196+ )
197+ } )
198+ }
199+
178200 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
179201 let args = CommitStateArgs {
180202 nonce : self . commit_id ,
@@ -193,17 +215,13 @@ impl CommitTask {
193215 fn create_commit_diff_ix (
194216 & self ,
195217 validator : & Pubkey ,
196- fetched_account : & Account ,
218+ base_account : & Account ,
197219 ) -> Instruction {
198- if self . force_commit_state {
199- return self . create_commit_state_ix ( validator) ;
200- }
201-
202220 let args = CommitDiffArgs {
203221 nonce : self . commit_id ,
204222 lamports : self . committed_account . account . lamports ,
205223 diff : compute_diff (
206- fetched_account . data ( ) ,
224+ base_account . data ( ) ,
207225 self . committed_account . account . data ( ) ,
208226 )
209227 . to_vec ( ) ,
@@ -217,6 +235,57 @@ impl CommitTask {
217235 args,
218236 )
219237 }
238+
239+ fn create_commit_state_from_buffer_ix (
240+ & self ,
241+ validator : & Pubkey ,
242+ ) -> Instruction {
243+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
244+ let ( commit_buffer_pubkey, _) =
245+ magicblock_committor_program:: pdas:: buffer_pda (
246+ validator,
247+ & self . committed_account . pubkey ,
248+ & commit_id_slice,
249+ ) ;
250+
251+ dlp:: instruction_builder:: commit_state_from_buffer (
252+ * validator,
253+ self . committed_account . pubkey ,
254+ self . committed_account . account . owner ,
255+ commit_buffer_pubkey,
256+ CommitStateFromBufferArgs {
257+ nonce : self . commit_id ,
258+ lamports : self . committed_account . account . lamports ,
259+ allow_undelegation : self . allow_undelegation ,
260+ } ,
261+ )
262+ }
263+
264+ fn create_commit_diff_from_buffer_ix (
265+ & self ,
266+ validator : & Pubkey ,
267+ _fetched_account : & Account ,
268+ ) -> Instruction {
269+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
270+ let ( commit_buffer_pubkey, _) =
271+ magicblock_committor_program:: pdas:: buffer_pda (
272+ validator,
273+ & self . committed_account . pubkey ,
274+ & commit_id_slice,
275+ ) ;
276+
277+ dlp:: instruction_builder:: commit_diff_from_buffer (
278+ * validator,
279+ self . committed_account . pubkey ,
280+ self . committed_account . account . owner ,
281+ commit_buffer_pubkey,
282+ CommitStateFromBufferArgs {
283+ nonce : self . commit_id ,
284+ lamports : self . committed_account . account . lamports ,
285+ allow_undelegation : self . allow_undelegation ,
286+ } ,
287+ )
288+ }
220289}
221290
222291#[ derive( Clone ) ]
0 commit comments