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 {
@@ -157,29 +156,52 @@ impl CommitTask {
157156 allow_undelegation,
158157 committed_account,
159158 base_account,
160- force_commit_state : false ,
159+ strategy : TaskStrategy :: Args ,
161160 }
162161 }
163162
164163 pub fn is_commit_diff ( & self ) -> bool {
165- !self . force_commit_state
166- && self . committed_account . account . data . len ( )
167- > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
164+ self . committed_account . account . data . len ( )
165+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
168166 && self . base_account . is_some ( )
169167 }
170168
171- pub fn force_commit_state ( & mut self ) {
172- self . force_commit_state = true ;
169+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
170+ self . strategy = TaskStrategy :: Buffer ;
171+ self
173172 }
174173
175174 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
176- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
177- self . create_commit_diff_ix ( validator, fetched_account)
178- } else {
179- self . create_commit_state_ix ( validator)
175+ match self . strategy {
176+ TaskStrategy :: Args => {
177+ if let Some ( base_account) = self . base_account . as_ref ( ) {
178+ self . create_commit_diff_ix ( validator, base_account)
179+ } else {
180+ self . create_commit_state_ix ( validator)
181+ }
182+ }
183+ TaskStrategy :: Buffer => {
184+ if let Some ( base_account) = self . base_account . as_ref ( ) {
185+ self . create_commit_diff_from_buffer_ix (
186+ validator,
187+ base_account,
188+ )
189+ } else {
190+ self . create_commit_state_from_buffer_ix ( validator)
191+ }
192+ }
180193 }
181194 }
182195
196+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
197+ self . base_account . as_ref ( ) . map ( |base_account| {
198+ compute_diff (
199+ base_account. data ( ) ,
200+ self . committed_account . account . data ( ) ,
201+ )
202+ } )
203+ }
204+
183205 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
184206 let args = CommitStateArgs {
185207 nonce : self . commit_id ,
@@ -198,17 +220,13 @@ impl CommitTask {
198220 fn create_commit_diff_ix (
199221 & self ,
200222 validator : & Pubkey ,
201- fetched_account : & Account ,
223+ base_account : & Account ,
202224 ) -> Instruction {
203- if self . force_commit_state {
204- return self . create_commit_state_ix ( validator) ;
205- }
206-
207225 let args = CommitDiffArgs {
208226 nonce : self . commit_id ,
209227 lamports : self . committed_account . account . lamports ,
210228 diff : compute_diff (
211- fetched_account . data ( ) ,
229+ base_account . data ( ) ,
212230 self . committed_account . account . data ( ) ,
213231 )
214232 . to_vec ( ) ,
@@ -222,6 +240,57 @@ impl CommitTask {
222240 args,
223241 )
224242 }
243+
244+ fn create_commit_state_from_buffer_ix (
245+ & self ,
246+ validator : & Pubkey ,
247+ ) -> Instruction {
248+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
249+ let ( commit_buffer_pubkey, _) =
250+ magicblock_committor_program:: pdas:: buffer_pda (
251+ validator,
252+ & self . committed_account . pubkey ,
253+ & commit_id_slice,
254+ ) ;
255+
256+ dlp:: instruction_builder:: commit_state_from_buffer (
257+ * validator,
258+ self . committed_account . pubkey ,
259+ self . committed_account . account . owner ,
260+ commit_buffer_pubkey,
261+ CommitStateFromBufferArgs {
262+ nonce : self . commit_id ,
263+ lamports : self . committed_account . account . lamports ,
264+ allow_undelegation : self . allow_undelegation ,
265+ } ,
266+ )
267+ }
268+
269+ fn create_commit_diff_from_buffer_ix (
270+ & self ,
271+ validator : & Pubkey ,
272+ _fetched_account : & Account ,
273+ ) -> Instruction {
274+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
275+ let ( commit_buffer_pubkey, _) =
276+ magicblock_committor_program:: pdas:: buffer_pda (
277+ validator,
278+ & self . committed_account . pubkey ,
279+ & commit_id_slice,
280+ ) ;
281+
282+ dlp:: instruction_builder:: commit_diff_from_buffer (
283+ * validator,
284+ self . committed_account . pubkey ,
285+ self . committed_account . account . owner ,
286+ commit_buffer_pubkey,
287+ CommitStateFromBufferArgs {
288+ nonce : self . commit_id ,
289+ lamports : self . committed_account . account . lamports ,
290+ allow_undelegation : self . allow_undelegation ,
291+ } ,
292+ )
293+ }
225294}
226295
227296#[ derive( Clone ) ]
0 commit comments