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 {
@@ -150,29 +149,52 @@ impl CommitTask {
150149 allow_undelegation,
151150 committed_account,
152151 base_account,
153- force_commit_state : false ,
152+ strategy : TaskStrategy :: Args ,
154153 }
155154 }
156155
157156 pub fn is_commit_diff ( & self ) -> bool {
158- !self . force_commit_state
159- && self . committed_account . account . data . len ( )
160- > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
157+ self . committed_account . account . data . len ( )
158+ > CommitTask :: COMMIT_STATE_SIZE_THRESHOLD
161159 && self . base_account . is_some ( )
162160 }
163161
164- pub fn force_commit_state ( & mut self ) {
165- self . force_commit_state = true ;
162+ pub fn switch_to_buffer_strategy ( mut self ) -> Self {
163+ self . strategy = TaskStrategy :: Buffer ;
164+ self
166165 }
167166
168167 pub fn create_commit_ix ( & self , validator : & Pubkey ) -> Instruction {
169- if let Some ( fetched_account) = self . base_account . as_ref ( ) {
170- self . create_commit_diff_ix ( validator, fetched_account)
171- } else {
172- self . create_commit_state_ix ( validator)
168+ match self . strategy {
169+ TaskStrategy :: Args => {
170+ if let Some ( base_account) = self . base_account . as_ref ( ) {
171+ self . create_commit_diff_ix ( validator, base_account)
172+ } else {
173+ self . create_commit_state_ix ( validator)
174+ }
175+ }
176+ TaskStrategy :: Buffer => {
177+ if let Some ( base_account) = self . base_account . as_ref ( ) {
178+ self . create_commit_diff_from_buffer_ix (
179+ validator,
180+ base_account,
181+ )
182+ } else {
183+ self . create_commit_state_from_buffer_ix ( validator)
184+ }
185+ }
173186 }
174187 }
175188
189+ pub fn compute_diff ( & self ) -> Option < dlp:: rkyv:: AlignedVec > {
190+ self . base_account . as_ref ( ) . map ( |base_account| {
191+ compute_diff (
192+ base_account. data ( ) ,
193+ self . committed_account . account . data ( ) ,
194+ )
195+ } )
196+ }
197+
176198 fn create_commit_state_ix ( & self , validator : & Pubkey ) -> Instruction {
177199 let args = CommitStateArgs {
178200 nonce : self . commit_id ,
@@ -191,17 +213,13 @@ impl CommitTask {
191213 fn create_commit_diff_ix (
192214 & self ,
193215 validator : & Pubkey ,
194- fetched_account : & Account ,
216+ base_account : & Account ,
195217 ) -> Instruction {
196- if self . force_commit_state {
197- return self . create_commit_state_ix ( validator) ;
198- }
199-
200218 let args = CommitDiffArgs {
201219 nonce : self . commit_id ,
202220 lamports : self . committed_account . account . lamports ,
203221 diff : compute_diff (
204- fetched_account . data ( ) ,
222+ base_account . data ( ) ,
205223 self . committed_account . account . data ( ) ,
206224 )
207225 . to_vec ( ) ,
@@ -215,6 +233,57 @@ impl CommitTask {
215233 args,
216234 )
217235 }
236+
237+ fn create_commit_state_from_buffer_ix (
238+ & self ,
239+ validator : & Pubkey ,
240+ ) -> Instruction {
241+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
242+ let ( commit_buffer_pubkey, _) =
243+ magicblock_committor_program:: pdas:: buffer_pda (
244+ validator,
245+ & self . committed_account . pubkey ,
246+ & commit_id_slice,
247+ ) ;
248+
249+ dlp:: instruction_builder:: commit_state_from_buffer (
250+ * validator,
251+ self . committed_account . pubkey ,
252+ self . committed_account . account . owner ,
253+ commit_buffer_pubkey,
254+ CommitStateFromBufferArgs {
255+ nonce : self . commit_id ,
256+ lamports : self . committed_account . account . lamports ,
257+ allow_undelegation : self . allow_undelegation ,
258+ } ,
259+ )
260+ }
261+
262+ fn create_commit_diff_from_buffer_ix (
263+ & self ,
264+ validator : & Pubkey ,
265+ _fetched_account : & Account ,
266+ ) -> Instruction {
267+ let commit_id_slice = self . commit_id . to_le_bytes ( ) ;
268+ let ( commit_buffer_pubkey, _) =
269+ magicblock_committor_program:: pdas:: buffer_pda (
270+ validator,
271+ & self . committed_account . pubkey ,
272+ & commit_id_slice,
273+ ) ;
274+
275+ dlp:: instruction_builder:: commit_diff_from_buffer (
276+ * validator,
277+ self . committed_account . pubkey ,
278+ self . committed_account . account . owner ,
279+ commit_buffer_pubkey,
280+ CommitStateFromBufferArgs {
281+ nonce : self . commit_id ,
282+ lamports : self . committed_account . account . lamports ,
283+ allow_undelegation : self . allow_undelegation ,
284+ } ,
285+ )
286+ }
218287}
219288
220289#[ derive( Clone ) ]
0 commit comments