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 ;
@@ -11,15 +11,17 @@ use crate::tasks::TaskStrategy;
1111use crate :: {
1212 consts:: MAX_WRITE_CHUNK_SIZE ,
1313 tasks:: {
14- visitor:: Visitor , BaseTask , BaseTaskError , BaseTaskResult , CommitTask ,
15- PreparationState , PreparationTask , TaskType ,
14+ visitor:: Visitor , BaseTask , BaseTaskError , BaseTaskResult ,
15+ CommitDiffTask , CommitTask , PreparationState , PreparationTask ,
16+ TaskType ,
1617 } ,
1718} ;
1819
1920/// Tasks that could be executed using buffers
2021#[ derive( Clone ) ]
2122pub enum BufferTaskType {
2223 Commit ( CommitTask ) ,
24+ CommitDiff ( CommitDiffTask ) ,
2325 // Action in the future
2426}
2527
@@ -48,19 +50,37 @@ impl BufferTask {
4850 }
4951
5052 fn preparation_required ( task_type : & BufferTaskType ) -> PreparationState {
51- let BufferTaskType :: Commit ( ref commit_task) = task_type;
52- let committed_data = commit_task. committed_account . account . data . clone ( ) ;
53- let chunks = Chunks :: from_data_length (
54- committed_data. len ( ) ,
55- MAX_WRITE_CHUNK_SIZE ,
56- ) ;
57-
58- PreparationState :: Required ( PreparationTask {
59- commit_id : commit_task. commit_id ,
60- pubkey : commit_task. committed_account . pubkey ,
61- committed_data,
62- chunks,
63- } )
53+ match task_type {
54+ BufferTaskType :: Commit ( task) => {
55+ let data = task. committed_account . account . data . clone ( ) ;
56+ let chunks =
57+ Chunks :: from_data_length ( data. len ( ) , MAX_WRITE_CHUNK_SIZE ) ;
58+
59+ PreparationState :: Required ( PreparationTask {
60+ commit_id : task. commit_id ,
61+ pubkey : task. committed_account . pubkey ,
62+ committed_data : data,
63+ chunks,
64+ } )
65+ }
66+
67+ BufferTaskType :: CommitDiff ( task) => {
68+ let diff = compute_diff (
69+ & task. base_account . data ,
70+ & task. committed_account . account . data ,
71+ )
72+ . to_vec ( ) ;
73+ let chunks =
74+ Chunks :: from_data_length ( diff. len ( ) , MAX_WRITE_CHUNK_SIZE ) ;
75+
76+ PreparationState :: Required ( PreparationTask {
77+ commit_id : task. commit_id ,
78+ pubkey : task. committed_account . pubkey ,
79+ committed_data : diff,
80+ chunks,
81+ } )
82+ }
83+ }
6484 }
6585}
6686
@@ -69,34 +89,60 @@ impl From<ArgsTaskType> for BufferTaskType {
6989 fn from ( value : ArgsTaskType ) -> Self {
7090 match value {
7191 ArgsTaskType :: Commit ( task) => BufferTaskType :: Commit ( task) ,
72- ArgsTaskType :: CommitDiff ( _) => panic ! ( "BufferTask doesn't support CommitDiff yet. Disable your tests (if any) temporarily till the next PR" ) ,
73- _ => unimplemented ! ( "Only commit task can be BufferTask currently. Fix your tests" ) ,
92+ ArgsTaskType :: CommitDiff ( task) => BufferTaskType :: CommitDiff ( task) ,
93+ _ => unimplemented ! (
94+ "Only commit task can be BufferTask currently. Fix your tests"
95+ ) ,
7496 }
7597 }
7698}
7799
78100impl BaseTask for BufferTask {
79101 fn instruction ( & self , validator : & Pubkey ) -> Instruction {
80- let BufferTaskType :: Commit ( ref value) = self . task_type ;
81- let commit_id_slice = value. commit_id . to_le_bytes ( ) ;
82- let ( commit_buffer_pubkey, _) =
83- magicblock_committor_program:: pdas:: buffer_pda (
84- validator,
85- & value. committed_account . pubkey ,
86- & commit_id_slice,
87- ) ;
88-
89- dlp:: instruction_builder:: commit_state_from_buffer (
90- * validator,
91- value. committed_account . pubkey ,
92- value. committed_account . account . owner ,
93- commit_buffer_pubkey,
94- CommitStateFromBufferArgs {
95- nonce : value. commit_id ,
96- lamports : value. committed_account . account . lamports ,
97- allow_undelegation : value. allow_undelegation ,
98- } ,
99- )
102+ match & self . task_type {
103+ BufferTaskType :: Commit ( task) => {
104+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
105+ let ( commit_buffer_pubkey, _) =
106+ magicblock_committor_program:: pdas:: buffer_pda (
107+ validator,
108+ & task. committed_account . pubkey ,
109+ & commit_id_slice,
110+ ) ;
111+
112+ dlp:: instruction_builder:: commit_state_from_buffer (
113+ * validator,
114+ task. committed_account . pubkey ,
115+ task. committed_account . account . owner ,
116+ commit_buffer_pubkey,
117+ CommitStateFromBufferArgs {
118+ nonce : task. commit_id ,
119+ lamports : task. committed_account . account . lamports ,
120+ allow_undelegation : task. allow_undelegation ,
121+ } ,
122+ )
123+ }
124+ BufferTaskType :: CommitDiff ( task) => {
125+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
126+ let ( commit_buffer_pubkey, _) =
127+ magicblock_committor_program:: pdas:: buffer_pda (
128+ validator,
129+ & task. committed_account . pubkey ,
130+ & commit_id_slice,
131+ ) ;
132+
133+ dlp:: instruction_builder:: commit_diff_from_buffer (
134+ * validator,
135+ task. committed_account . pubkey ,
136+ task. committed_account . account . owner ,
137+ commit_buffer_pubkey,
138+ CommitStateFromBufferArgs {
139+ nonce : task. commit_id ,
140+ lamports : task. committed_account . account . lamports ,
141+ allow_undelegation : task. allow_undelegation ,
142+ } ,
143+ )
144+ }
145+ }
100146 }
101147
102148 /// No further optimizations
@@ -125,6 +171,7 @@ impl BaseTask for BufferTask {
125171 fn compute_units ( & self ) -> u32 {
126172 match self . task_type {
127173 BufferTaskType :: Commit ( _) => 70_000 ,
174+ BufferTaskType :: CommitDiff ( _) => 70_000 ,
128175 }
129176 }
130177
@@ -136,6 +183,7 @@ impl BaseTask for BufferTask {
136183 fn task_type ( & self ) -> TaskType {
137184 match self . task_type {
138185 BufferTaskType :: Commit ( _) => TaskType :: Commit ,
186+ BufferTaskType :: CommitDiff ( _) => TaskType :: Commit ,
139187 }
140188 }
141189
@@ -145,12 +193,21 @@ impl BaseTask for BufferTask {
145193 }
146194
147195 fn reset_commit_id ( & mut self , commit_id : u64 ) {
148- let BufferTaskType :: Commit ( commit_task) = & mut self . task_type ;
149- if commit_id == commit_task. commit_id {
150- return ;
151- }
196+ match & mut self . task_type {
197+ BufferTaskType :: Commit ( commit_task) => {
198+ if commit_id == commit_task. commit_id {
199+ return ;
200+ }
201+ commit_task. commit_id = commit_id;
202+ }
203+ BufferTaskType :: CommitDiff ( task) => {
204+ if commit_id == task. commit_id {
205+ return ;
206+ }
207+ task. commit_id = commit_id;
208+ }
209+ } ;
152210
153- commit_task. commit_id = commit_id;
154211 self . preparation_state = Self :: preparation_required ( & self . task_type )
155212 }
156213}
@@ -159,6 +216,7 @@ impl LabelValue for BufferTask {
159216 fn value ( & self ) -> & str {
160217 match self . task_type {
161218 BufferTaskType :: Commit ( _) => "buffer_commit" ,
219+ BufferTaskType :: CommitDiff ( _) => "buffer_commit_diff" ,
162220 }
163221 }
164222}
0 commit comments