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. committed_account . account . data ,
70+ & task. base_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
@@ -77,32 +97,58 @@ impl From<ArgsTaskType> for BufferTaskType {
7797
7898impl BaseTask for BufferTask {
7999 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- )
100+ match & self . task_type {
101+ BufferTaskType :: Commit ( task) => {
102+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
103+ let ( commit_buffer_pubkey, _) =
104+ magicblock_committor_program:: pdas:: buffer_pda (
105+ validator,
106+ & task. committed_account . pubkey ,
107+ & commit_id_slice,
108+ ) ;
109+
110+ dlp:: instruction_builder:: commit_state_from_buffer (
111+ * validator,
112+ task. committed_account . pubkey ,
113+ task. committed_account . account . owner ,
114+ commit_buffer_pubkey,
115+ CommitStateFromBufferArgs {
116+ nonce : task. commit_id ,
117+ lamports : task. committed_account . account . lamports ,
118+ allow_undelegation : task. allow_undelegation ,
119+ } ,
120+ )
121+ }
122+ BufferTaskType :: CommitDiff ( task) => {
123+ let commit_id_slice = task. commit_id . to_le_bytes ( ) ;
124+ let ( commit_buffer_pubkey, _) =
125+ magicblock_committor_program:: pdas:: buffer_pda (
126+ validator,
127+ & task. committed_account . pubkey ,
128+ & commit_id_slice,
129+ ) ;
130+
131+ dlp:: instruction_builder:: commit_diff_from_buffer (
132+ * validator,
133+ task. committed_account . pubkey ,
134+ task. committed_account . account . owner ,
135+ commit_buffer_pubkey,
136+ CommitStateFromBufferArgs {
137+ nonce : task. commit_id ,
138+ lamports : task. committed_account . account . lamports ,
139+ allow_undelegation : task. allow_undelegation ,
140+ } ,
141+ )
142+ }
143+ }
100144 }
101145
102146 /// No further optimizations
103147 fn optimize (
104148 self : Box < Self > ,
105149 ) -> Result < Box < dyn BaseTask > , Box < dyn BaseTask > > {
150+ // Since the buffer in BufferTask doesn't contribute to the size of
151+ // transaction, there is nothing we can do here to optimize/reduce the size.
106152 Err ( self )
107153 }
108154
@@ -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