1- use dlp:: args:: CommitStateFromBufferArgs ;
1+ use dlp:: { args:: CommitStateFromBufferArgs , compute_diff } ;
22use magicblock_committor_program:: Chunks ;
33use magicblock_metrics:: metrics:: LabelValue ;
44use solana_instruction:: Instruction ;
@@ -9,15 +9,17 @@ use crate::tasks::TaskStrategy;
99use crate :: {
1010 consts:: MAX_WRITE_CHUNK_SIZE ,
1111 tasks:: {
12- visitor:: Visitor , BaseTask , BaseTaskError , BaseTaskResult , CommitTask ,
13- PreparationState , PreparationTask , TaskType ,
12+ visitor:: Visitor , BaseTask , BaseTaskError , BaseTaskResult ,
13+ CommitDiffTask , CommitTask , PreparationState , PreparationTask ,
14+ TaskType ,
1415 } ,
1516} ;
1617
1718/// Tasks that could be executed using buffers
1819#[ derive( Clone ) ]
1920pub enum BufferTaskType {
2021 Commit ( CommitTask ) ,
22+ CommitDiff ( CommitDiffTask ) ,
2123 // Action in the future
2224}
2325
@@ -46,50 +48,94 @@ impl BufferTask {
4648 }
4749
4850 fn preparation_required ( task_type : & BufferTaskType ) -> PreparationState {
49- let BufferTaskType :: Commit ( ref commit_task) = task_type;
50- let committed_data = commit_task. committed_account . account . data . clone ( ) ;
51- let chunks = Chunks :: from_data_length (
52- committed_data. len ( ) ,
53- MAX_WRITE_CHUNK_SIZE ,
54- ) ;
55-
56- PreparationState :: Required ( PreparationTask {
57- commit_id : commit_task. commit_id ,
58- pubkey : commit_task. committed_account . pubkey ,
59- committed_data,
60- chunks,
61- } )
51+ match task_type {
52+ BufferTaskType :: Commit ( task) => {
53+ let data = task. committed_account . account . data . clone ( ) ;
54+ let chunks =
55+ Chunks :: from_data_length ( data. len ( ) , MAX_WRITE_CHUNK_SIZE ) ;
56+
57+ PreparationState :: Required ( PreparationTask {
58+ commit_id : task. commit_id ,
59+ pubkey : task. committed_account . pubkey ,
60+ committed_data : data,
61+ chunks,
62+ } )
63+ }
64+
65+ BufferTaskType :: CommitDiff ( task) => {
66+ let diff = compute_diff (
67+ & task. committed_account . account . data ,
68+ & task. base_account . data ,
69+ )
70+ . to_vec ( ) ;
71+ let chunks =
72+ Chunks :: from_data_length ( diff. len ( ) , MAX_WRITE_CHUNK_SIZE ) ;
73+
74+ PreparationState :: Required ( PreparationTask {
75+ commit_id : task. commit_id ,
76+ pubkey : task. committed_account . pubkey ,
77+ committed_data : diff,
78+ chunks,
79+ } )
80+ }
81+ }
6282 }
6383}
6484
6585impl BaseTask for BufferTask {
6686 fn instruction ( & self , validator : & Pubkey ) -> Instruction {
67- let BufferTaskType :: Commit ( ref value) = self . task_type ;
68- let commit_id_slice = value. commit_id . to_le_bytes ( ) ;
69- let ( commit_buffer_pubkey, _) =
70- magicblock_committor_program:: pdas:: buffer_pda (
71- validator,
72- & value. committed_account . pubkey ,
73- & commit_id_slice,
74- ) ;
75-
76- dlp:: instruction_builder:: commit_state_from_buffer (
77- * validator,
78- value. committed_account . pubkey ,
79- value. committed_account . account . owner ,
80- commit_buffer_pubkey,
81- CommitStateFromBufferArgs {
82- nonce : value. commit_id ,
83- lamports : value. committed_account . account . lamports ,
84- allow_undelegation : value. allow_undelegation ,
85- } ,
86- )
87+ match & self . task_type {
88+ BufferTaskType :: Commit ( task) => {
89+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
90+ let ( commit_buffer_pubkey, _) =
91+ magicblock_committor_program:: pdas:: buffer_pda (
92+ validator,
93+ & task. committed_account . pubkey ,
94+ & commit_id_slice,
95+ ) ;
96+
97+ dlp:: instruction_builder:: commit_state_from_buffer (
98+ * validator,
99+ task. committed_account . pubkey ,
100+ task. committed_account . account . owner ,
101+ commit_buffer_pubkey,
102+ CommitStateFromBufferArgs {
103+ nonce : task. commit_id ,
104+ lamports : task. committed_account . account . lamports ,
105+ allow_undelegation : task. allow_undelegation ,
106+ } ,
107+ )
108+ }
109+ BufferTaskType :: CommitDiff ( task) => {
110+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
111+ let ( commit_buffer_pubkey, _) =
112+ magicblock_committor_program:: pdas:: buffer_pda (
113+ validator,
114+ & task. committed_account . pubkey ,
115+ & commit_id_slice,
116+ ) ;
117+
118+ dlp:: instruction_builder:: commit_diff_from_buffer (
119+ * validator,
120+ task. committed_account . pubkey ,
121+ task. committed_account . account . owner ,
122+ commit_buffer_pubkey,
123+ CommitStateFromBufferArgs {
124+ nonce : task. commit_id ,
125+ lamports : task. committed_account . account . lamports ,
126+ allow_undelegation : task. allow_undelegation ,
127+ } ,
128+ )
129+ }
130+ }
87131 }
88132
89133 /// No further optimizations
90134 fn optimize (
91135 self : Box < Self > ,
92136 ) -> Result < Box < dyn BaseTask > , Box < dyn BaseTask > > {
137+ // Since the buffer in BufferTask doesn't contribute to the size of
138+ // transaction, there is nothing we can do here to optimize/reduce the size.
93139 Err ( self )
94140 }
95141
@@ -112,6 +158,7 @@ impl BaseTask for BufferTask {
112158 fn compute_units ( & self ) -> u32 {
113159 match self . task_type {
114160 BufferTaskType :: Commit ( _) => 70_000 ,
161+ BufferTaskType :: CommitDiff ( _) => 70_000 ,
115162 }
116163 }
117164
@@ -123,6 +170,7 @@ impl BaseTask for BufferTask {
123170 fn task_type ( & self ) -> TaskType {
124171 match self . task_type {
125172 BufferTaskType :: Commit ( _) => TaskType :: Commit ,
173+ BufferTaskType :: CommitDiff ( _) => TaskType :: Commit ,
126174 }
127175 }
128176
@@ -132,12 +180,21 @@ impl BaseTask for BufferTask {
132180 }
133181
134182 fn reset_commit_id ( & mut self , commit_id : u64 ) {
135- let BufferTaskType :: Commit ( commit_task) = & mut self . task_type ;
136- if commit_id == commit_task. commit_id {
137- return ;
138- }
183+ match & mut self . task_type {
184+ BufferTaskType :: Commit ( commit_task) => {
185+ if commit_id == commit_task. commit_id {
186+ return ;
187+ }
188+ commit_task. commit_id = commit_id;
189+ }
190+ BufferTaskType :: CommitDiff ( task) => {
191+ if commit_id == task. commit_id {
192+ return ;
193+ }
194+ task. commit_id = commit_id;
195+ }
196+ } ;
139197
140- commit_task. commit_id = commit_id;
141198 self . preparation_state = Self :: preparation_required ( & self . task_type )
142199 }
143200}
@@ -146,6 +203,7 @@ impl LabelValue for BufferTask {
146203 fn value ( & self ) -> & str {
147204 match self . task_type {
148205 BufferTaskType :: Commit ( _) => "buffer_commit" ,
206+ BufferTaskType :: CommitDiff ( _) => "buffer_commit_diff" ,
149207 }
150208 }
151209}
0 commit comments